OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/time/time.h" | 5 #include "base/time/time.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <sys/time.h> | 8 #include <sys/time.h> |
9 #include <time.h> | 9 #include <time.h> |
10 #if defined(OS_ANDROID) && !defined(__LP64__) | 10 #if defined(OS_ANDROID) && !defined(__LP64__) |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 exploded->month = timestruct.tm_mon + 1; | 204 exploded->month = timestruct.tm_mon + 1; |
205 exploded->day_of_week = timestruct.tm_wday; | 205 exploded->day_of_week = timestruct.tm_wday; |
206 exploded->day_of_month = timestruct.tm_mday; | 206 exploded->day_of_month = timestruct.tm_mday; |
207 exploded->hour = timestruct.tm_hour; | 207 exploded->hour = timestruct.tm_hour; |
208 exploded->minute = timestruct.tm_min; | 208 exploded->minute = timestruct.tm_min; |
209 exploded->second = timestruct.tm_sec; | 209 exploded->second = timestruct.tm_sec; |
210 exploded->millisecond = millisecond; | 210 exploded->millisecond = millisecond; |
211 } | 211 } |
212 | 212 |
213 // static | 213 // static |
214 Time Time::FromExploded(bool is_local, const Exploded& exploded) { | 214 bool Time::FromExploded(bool is_local, const Exploded& exploded, Time* time) { |
215 struct tm timestruct; | 215 struct tm timestruct; |
216 timestruct.tm_sec = exploded.second; | 216 timestruct.tm_sec = exploded.second; |
217 timestruct.tm_min = exploded.minute; | 217 timestruct.tm_min = exploded.minute; |
218 timestruct.tm_hour = exploded.hour; | 218 timestruct.tm_hour = exploded.hour; |
219 timestruct.tm_mday = exploded.day_of_month; | 219 timestruct.tm_mday = exploded.day_of_month; |
220 timestruct.tm_mon = exploded.month - 1; | 220 timestruct.tm_mon = exploded.month - 1; |
221 timestruct.tm_year = exploded.year - 1900; | 221 timestruct.tm_year = exploded.year - 1900; |
222 timestruct.tm_wday = exploded.day_of_week; // mktime/timegm ignore this | 222 timestruct.tm_wday = exploded.day_of_week; // mktime/timegm ignore this |
223 timestruct.tm_yday = 0; // mktime/timegm ignore this | 223 timestruct.tm_yday = 0; // mktime/timegm ignore this |
224 timestruct.tm_isdst = -1; // attempt to figure it out | 224 timestruct.tm_isdst = -1; // attempt to figure it out |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 milliseconds = min_seconds * kMillisecondsPerSecond; | 294 milliseconds = min_seconds * kMillisecondsPerSecond; |
295 } else { | 295 } else { |
296 milliseconds = max_seconds * kMillisecondsPerSecond; | 296 milliseconds = max_seconds * kMillisecondsPerSecond; |
297 milliseconds += (kMillisecondsPerSecond - 1); | 297 milliseconds += (kMillisecondsPerSecond - 1); |
298 } | 298 } |
299 } else { | 299 } else { |
300 milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond; | 300 milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond; |
301 } | 301 } |
302 | 302 |
303 // Adjust from Unix (1970) to Windows (1601) epoch. | 303 // Adjust from Unix (1970) to Windows (1601) epoch. |
304 return Time((milliseconds * kMicrosecondsPerMillisecond) + | 304 base::Time converted_time = |
305 kWindowsEpochDeltaMicroseconds); | 305 Time((milliseconds * kMicrosecondsPerMillisecond) + |
| 306 kWindowsEpochDeltaMicroseconds); |
| 307 |
| 308 // If |exploded.day_of_month| is set to 31 on a 28-30 day month, it will |
| 309 // return the first day of the next month. Thus round-trip the time and |
| 310 // compare the initial |exploded| with |utc_to_exploded| time. |
| 311 base::Time::Exploded to_exploded; |
| 312 if (!is_local) |
| 313 converted_time.UTCExplode(&to_exploded); |
| 314 else |
| 315 converted_time.LocalExplode(&to_exploded); |
| 316 |
| 317 if (ExplodedMostlyEquals(to_exploded, exploded)) { |
| 318 *time = converted_time; |
| 319 return true; |
| 320 } |
| 321 |
| 322 *time = Time(0); |
| 323 return false; |
306 } | 324 } |
307 | 325 |
308 // TimeTicks ------------------------------------------------------------------ | 326 // TimeTicks ------------------------------------------------------------------ |
309 // static | 327 // static |
310 TimeTicks TimeTicks::Now() { | 328 TimeTicks TimeTicks::Now() { |
311 return TimeTicks(ClockNow(CLOCK_MONOTONIC)); | 329 return TimeTicks(ClockNow(CLOCK_MONOTONIC)); |
312 } | 330 } |
313 | 331 |
314 // static | 332 // static |
315 TimeTicks::Clock TimeTicks::GetClock() { | 333 TimeTicks::Clock TimeTicks::GetClock() { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; | 377 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; |
360 return result; | 378 return result; |
361 } | 379 } |
362 int64_t us = us_ - kTimeTToMicrosecondsOffset; | 380 int64_t us = us_ - kTimeTToMicrosecondsOffset; |
363 result.tv_sec = us / Time::kMicrosecondsPerSecond; | 381 result.tv_sec = us / Time::kMicrosecondsPerSecond; |
364 result.tv_usec = us % Time::kMicrosecondsPerSecond; | 382 result.tv_usec = us % Time::kMicrosecondsPerSecond; |
365 return result; | 383 return result; |
366 } | 384 } |
367 | 385 |
368 } // namespace base | 386 } // namespace base |
OLD | NEW |