[email protected] | cc6fe33 | 2012-01-10 00:22:35 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/time.h" |
| 6 | |
| 7 | #include <CoreFoundation/CFDate.h> |
| 8 | #include <CoreFoundation/CFTimeZone.h> |
| 9 | #include <mach/mach_time.h> |
| 10 | #include <sys/time.h> |
| 11 | #include <time.h> |
| 12 | |
| 13 | #include "base/basictypes.h" |
| 14 | #include "base/logging.h" |
[email protected] | df0ca6c8 | 2010-10-17 04:09:06 | [diff] [blame] | 15 | #include "base/mac/scoped_cftyperef.h" |
[email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 16 | |
| 17 | namespace base { |
| 18 | |
| 19 | // The Time routines in this file use Mach and CoreFoundation APIs, since the |
| 20 | // POSIX definition of time_t in Mac OS X wraps around after 2038--and |
| 21 | // there are already cookie expiration dates, etc., past that time out in |
| 22 | // the field. Using CFDate prevents that problem, and using mach_absolute_time |
| 23 | // for TimeTicks gives us nice high-resolution interval timing. |
| 24 | |
| 25 | // Time ----------------------------------------------------------------------- |
| 26 | |
[email protected] | a4a3292e | 2009-08-26 02:53:36 | [diff] [blame] | 27 | // Core Foundation uses a double second count since 2001-01-01 00:00:00 UTC. |
| 28 | // The UNIX epoch is 1970-01-01 00:00:00 UTC. |
| 29 | // Windows uses a Gregorian epoch of 1601. We need to match this internally |
| 30 | // so that our time representations match across all platforms. See bug 14734. |
| 31 | // irb(main):010:0> Time.at(0).getutc() |
| 32 | // => Thu Jan 01 00:00:00 UTC 1970 |
| 33 | // irb(main):011:0> Time.at(-11644473600).getutc() |
| 34 | // => Mon Jan 01 00:00:00 UTC 1601 |
| 35 | static const int64 kWindowsEpochDeltaSeconds = GG_INT64_C(11644473600); |
| 36 | static const int64 kWindowsEpochDeltaMilliseconds = |
| 37 | kWindowsEpochDeltaSeconds * Time::kMillisecondsPerSecond; |
[email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 38 | |
[email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 39 | // static |
[email protected] | a4a3292e | 2009-08-26 02:53:36 | [diff] [blame] | 40 | const int64 Time::kWindowsEpochDeltaMicroseconds = |
| 41 | kWindowsEpochDeltaSeconds * Time::kMicrosecondsPerSecond; |
| 42 | |
| 43 | // Some functions in time.cc use time_t directly, so we provide an offset |
| 44 | // to convert from time_t (Unix epoch) and internal (Windows epoch). |
| 45 | // static |
| 46 | const int64 Time::kTimeTToMicrosecondsOffset = kWindowsEpochDeltaMicroseconds; |
[email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 47 | |
| 48 | // static |
| 49 | Time Time::Now() { |
| 50 | CFAbsoluteTime now = |
| 51 | CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970; |
[email protected] | a4a3292e | 2009-08-26 02:53:36 | [diff] [blame] | 52 | return Time(static_cast<int64>(now * kMicrosecondsPerSecond) + |
| 53 | kWindowsEpochDeltaMicroseconds); |
[email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // static |
[email protected] | 85786f9 | 2009-04-18 00:42:48 | [diff] [blame] | 57 | Time Time::NowFromSystemTime() { |
| 58 | // Just use Now() because Now() returns the system time. |
| 59 | return Now(); |
| 60 | } |
| 61 | |
| 62 | // static |
[email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 63 | Time Time::FromExploded(bool is_local, const Exploded& exploded) { |
| 64 | CFGregorianDate date; |
| 65 | date.second = exploded.second + |
| 66 | exploded.millisecond / static_cast<double>(kMillisecondsPerSecond); |
| 67 | date.minute = exploded.minute; |
| 68 | date.hour = exploded.hour; |
| 69 | date.day = exploded.day_of_month; |
| 70 | date.month = exploded.month; |
| 71 | date.year = exploded.year; |
| 72 | |
[email protected] | df0ca6c8 | 2010-10-17 04:09:06 | [diff] [blame] | 73 | base::mac::ScopedCFTypeRef<CFTimeZoneRef> |
[email protected] | 1b5753a | 2008-11-04 20:36:11 | [diff] [blame] | 74 | time_zone(is_local ? CFTimeZoneCopySystem() : NULL); |
[email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 75 | CFAbsoluteTime seconds = CFGregorianDateGetAbsoluteTime(date, time_zone) + |
| 76 | kCFAbsoluteTimeIntervalSince1970; |
[email protected] | a4a3292e | 2009-08-26 02:53:36 | [diff] [blame] | 77 | return Time(static_cast<int64>(seconds * kMicrosecondsPerSecond) + |
| 78 | kWindowsEpochDeltaMicroseconds); |
[email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void Time::Explode(bool is_local, Exploded* exploded) const { |
[email protected] | cc6fe33 | 2012-01-10 00:22:35 | [diff] [blame] | 82 | // Avoid rounding issues, by only putting the integral number of seconds |
| 83 | // (rounded towards -infinity) into a |CFAbsoluteTime| (which is a |double|). |
| 84 | int64 microsecond = us_ % kMicrosecondsPerSecond; |
| 85 | if (microsecond < 0) |
| 86 | microsecond += kMicrosecondsPerSecond; |
| 87 | CFAbsoluteTime seconds = ((us_ - microsecond) / kMicrosecondsPerSecond) - |
| 88 | kWindowsEpochDeltaSeconds - |
| 89 | kCFAbsoluteTimeIntervalSince1970; |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 90 | |
[email protected] | df0ca6c8 | 2010-10-17 04:09:06 | [diff] [blame] | 91 | base::mac::ScopedCFTypeRef<CFTimeZoneRef> |
[email protected] | 1b5753a | 2008-11-04 20:36:11 | [diff] [blame] | 92 | time_zone(is_local ? CFTimeZoneCopySystem() : NULL); |
[email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 93 | CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(seconds, time_zone); |
[email protected] | cc6fe33 | 2012-01-10 00:22:35 | [diff] [blame] | 94 | // 1 = Monday, ..., 7 = Sunday. |
| 95 | int cf_day_of_week = CFAbsoluteTimeGetDayOfWeek(seconds, time_zone); |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 96 | |
[email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 97 | exploded->year = date.year; |
| 98 | exploded->month = date.month; |
[email protected] | cc6fe33 | 2012-01-10 00:22:35 | [diff] [blame] | 99 | exploded->day_of_week = (cf_day_of_week == 7) ? 0 : cf_day_of_week - 1; |
[email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 100 | exploded->day_of_month = date.day; |
| 101 | exploded->hour = date.hour; |
| 102 | exploded->minute = date.minute; |
[email protected] | cc6fe33 | 2012-01-10 00:22:35 | [diff] [blame] | 103 | // Make sure seconds are rounded down towards -infinity. |
| 104 | exploded->second = floor(date.second); |
| 105 | // Calculate milliseconds ourselves, since we rounded the |seconds|, making |
| 106 | // sure to round towards -infinity. |
| 107 | exploded->millisecond = |
| 108 | (microsecond >= 0) ? microsecond / kMicrosecondsPerMillisecond : |
| 109 | (microsecond - kMicrosecondsPerMillisecond + 1) / |
| 110 | kMicrosecondsPerMillisecond; |
[email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // TimeTicks ------------------------------------------------------------------ |
| 114 | |
| 115 | // static |
| 116 | TimeTicks TimeTicks::Now() { |
| 117 | uint64_t absolute_micro; |
| 118 | |
| 119 | static mach_timebase_info_data_t timebase_info; |
| 120 | if (timebase_info.denom == 0) { |
| 121 | // Zero-initialization of statics guarantees that denom will be 0 before |
| 122 | // calling mach_timebase_info. mach_timebase_info will never set denom to |
| 123 | // 0 as that would be invalid, so the zero-check can be used to determine |
| 124 | // whether mach_timebase_info has already been called. This is |
| 125 | // recommended by Apple's QA1398. |
| 126 | kern_return_t kr = mach_timebase_info(&timebase_info); |
[email protected] | e1be56d | 2011-05-04 01:29:38 | [diff] [blame] | 127 | DCHECK_EQ(KERN_SUCCESS, kr); |
[email protected] | 8822d97 | 2008-11-03 19:36:59 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // mach_absolute_time is it when it comes to ticks on the Mac. Other calls |
| 131 | // with less precision (such as TickCount) just call through to |
| 132 | // mach_absolute_time. |
| 133 | |
| 134 | // timebase_info converts absolute time tick units into nanoseconds. Convert |
| 135 | // to microseconds up front to stave off overflows. |
| 136 | absolute_micro = mach_absolute_time() / Time::kNanosecondsPerMicrosecond * |
| 137 | timebase_info.numer / timebase_info.denom; |
| 138 | |
| 139 | // Don't bother with the rollover handling that the Windows version does. |
| 140 | // With numer and denom = 1 (the expected case), the 64-bit absolute time |
| 141 | // reported in nanoseconds is enough to last nearly 585 years. |
| 142 | |
| 143 | return TimeTicks(absolute_micro); |
| 144 | } |
| 145 | |
| 146 | // static |
| 147 | TimeTicks TimeTicks::HighResNow() { |
| 148 | return Now(); |
| 149 | } |
| 150 | |
| 151 | } // namespace base |