OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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.h" | 5 #include "base/time.h" |
6 | 6 |
7 #ifdef OS_MACOSX | |
8 #include <mach/mach_time.h> | |
9 #endif | |
10 #include <sys/time.h> | 7 #include <sys/time.h> |
11 #include <time.h> | 8 #include <time.h> |
12 | 9 |
13 #include <limits> | 10 #include <limits> |
14 | 11 |
15 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
16 #include "base/logging.h" | 13 #include "base/logging.h" |
17 | 14 |
18 namespace base { | 15 namespace base { |
19 | 16 |
20 // The Time routines in this file use standard POSIX routines, or almost- | 17 // The Time routines in this file use standard POSIX routines, or almost- |
21 // standard routines in the case of timegm. We need to use a Mach-specific | 18 // standard routines in the case of timegm. We need to use a Mach-specific |
22 // function for TimeTicks::Now() on Mac OS X. | 19 // function for TimeTicks::Now() on Mac OS X. |
23 | 20 |
24 // Time ----------------------------------------------------------------------- | 21 // Time ----------------------------------------------------------------------- |
25 | 22 |
26 // Some functions in time.cc use time_t directly, so we provide a zero offset | 23 // Windows uses a Gregorian epoch of 1601. We need to match this internally |
27 // for them. The epoch is 1970-01-01 00:00:00 UTC. | 24 // so that our time representations match across all platforms. See bug 14734. |
| 25 // irb(main):010:0> Time.at(0).getutc() |
| 26 // => Thu Jan 01 00:00:00 UTC 1970 |
| 27 // irb(main):011:0> Time.at(-11644473600).getutc() |
| 28 // => Mon Jan 01 00:00:00 UTC 1601 |
| 29 static const int64 kWindowsEpochDeltaSeconds = GG_INT64_C(11644473600); |
| 30 static const int64 kWindowsEpochDeltaMilliseconds = |
| 31 kWindowsEpochDeltaSeconds * Time::kMillisecondsPerSecond; |
| 32 static const int64 kWindowsEpochDeltaMicroseconds = |
| 33 kWindowsEpochDeltaSeconds * Time::kMicrosecondsPerSecond; |
| 34 |
| 35 // Some functions in time.cc use time_t directly, so we provide an offset |
| 36 // to convert from time_t (Unix epoch) and internal (Windows epoch). |
28 // static | 37 // static |
29 const int64 Time::kTimeTToMicrosecondsOffset = GG_INT64_C(0); | 38 const int64 Time::kTimeTToMicrosecondsOffset = kWindowsEpochDeltaMicroseconds; |
30 | 39 |
31 // static | 40 // static |
32 Time Time::Now() { | 41 Time Time::Now() { |
33 struct timeval tv; | 42 struct timeval tv; |
34 struct timezone tz = { 0, 0 }; // UTC | 43 struct timezone tz = { 0, 0 }; // UTC |
35 if (gettimeofday(&tv, &tz) != 0) { | 44 if (gettimeofday(&tv, &tz) != 0) { |
36 DCHECK(0) << "Could not determine time of day"; | 45 DCHECK(0) << "Could not determine time of day"; |
37 } | 46 } |
38 // Combine seconds and microseconds in a 64-bit field containing microseconds | 47 // Combine seconds and microseconds in a 64-bit field containing microseconds |
39 // since the epoch. That's enough for nearly 600 centuries. | 48 // since the epoch. That's enough for nearly 600 centuries. Adjust from |
40 return tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec; | 49 // Unix (1970) to Windows (1601) epoch. |
| 50 return Time((tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec) + |
| 51 kWindowsEpochDeltaMicroseconds); |
41 } | 52 } |
42 | 53 |
43 // static | 54 // static |
44 Time Time::NowFromSystemTime() { | 55 Time Time::NowFromSystemTime() { |
45 // Just use Now() because Now() returns the system time. | 56 // Just use Now() because Now() returns the system time. |
46 return Now(); | 57 return Now(); |
47 } | 58 } |
48 | 59 |
49 // static | 60 // static |
50 Time Time::FromExploded(bool is_local, const Exploded& exploded) { | 61 Time Time::FromExploded(bool is_local, const Exploded& exploded) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 kMillisecondsPerSecond; | 104 kMillisecondsPerSecond; |
94 } else { | 105 } else { |
95 milliseconds = (std::numeric_limits<time_t>::max() * | 106 milliseconds = (std::numeric_limits<time_t>::max() * |
96 kMillisecondsPerSecond) + | 107 kMillisecondsPerSecond) + |
97 kMillisecondsPerSecond - 1; | 108 kMillisecondsPerSecond - 1; |
98 } | 109 } |
99 } else { | 110 } else { |
100 milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond; | 111 milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond; |
101 } | 112 } |
102 | 113 |
103 return Time(milliseconds * kMicrosecondsPerMillisecond); | 114 // Adjust from Unix (1970) to Windows (1601) epoch. |
| 115 return Time((milliseconds * kMicrosecondsPerMillisecond) + |
| 116 kWindowsEpochDeltaMicroseconds); |
104 } | 117 } |
105 | 118 |
106 void Time::Explode(bool is_local, Exploded* exploded) const { | 119 void Time::Explode(bool is_local, Exploded* exploded) const { |
107 // Time stores times with microsecond resolution, but Exploded only carries | 120 // Time stores times with microsecond resolution, but Exploded only carries |
108 // millisecond resolution, so begin by being lossy. | 121 // millisecond resolution, so begin by being lossy. Adjust from Windows |
109 int64 milliseconds = us_ / kMicrosecondsPerMillisecond; | 122 // epoch (1601) to Unix epoch (1970); |
| 123 int64 milliseconds = (us_ - kWindowsEpochDeltaMicroseconds) / |
| 124 kMicrosecondsPerMillisecond; |
110 time_t seconds = milliseconds / kMillisecondsPerSecond; | 125 time_t seconds = milliseconds / kMillisecondsPerSecond; |
111 | 126 |
112 struct tm timestruct; | 127 struct tm timestruct; |
113 if (is_local) | 128 if (is_local) |
114 localtime_r(&seconds, ×truct); | 129 localtime_r(&seconds, ×truct); |
115 else | 130 else |
116 gmtime_r(&seconds, ×truct); | 131 gmtime_r(&seconds, ×truct); |
117 | 132 |
118 exploded->year = timestruct.tm_year + 1900; | 133 exploded->year = timestruct.tm_year + 1900; |
119 exploded->month = timestruct.tm_mon + 1; | 134 exploded->month = timestruct.tm_mon + 1; |
120 exploded->day_of_week = timestruct.tm_wday; | 135 exploded->day_of_week = timestruct.tm_wday; |
121 exploded->day_of_month = timestruct.tm_mday; | 136 exploded->day_of_month = timestruct.tm_mday; |
122 exploded->hour = timestruct.tm_hour; | 137 exploded->hour = timestruct.tm_hour; |
123 exploded->minute = timestruct.tm_min; | 138 exploded->minute = timestruct.tm_min; |
124 exploded->second = timestruct.tm_sec; | 139 exploded->second = timestruct.tm_sec; |
125 exploded->millisecond = milliseconds % kMillisecondsPerSecond; | 140 exploded->millisecond = milliseconds % kMillisecondsPerSecond; |
126 } | 141 } |
127 | 142 |
128 // TimeTicks ------------------------------------------------------------------ | 143 // TimeTicks ------------------------------------------------------------------ |
129 | 144 |
| 145 #if defined(OS_POSIX) && \ |
| 146 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 |
| 147 |
130 // static | 148 // static |
131 TimeTicks TimeTicks::Now() { | 149 TimeTicks TimeTicks::Now() { |
132 uint64_t absolute_micro; | 150 uint64_t absolute_micro; |
133 | 151 |
134 #if defined(OS_MACOSX) | |
135 static mach_timebase_info_data_t timebase_info; | |
136 if (timebase_info.denom == 0) { | |
137 // Zero-initialization of statics guarantees that denom will be 0 before | |
138 // calling mach_timebase_info. mach_timebase_info will never set denom to | |
139 // 0 as that would be invalid, so the zero-check can be used to determine | |
140 // whether mach_timebase_info has already been called. This is | |
141 // recommended by Apple's QA1398. | |
142 kern_return_t kr = mach_timebase_info(&timebase_info); | |
143 DCHECK(kr == KERN_SUCCESS); | |
144 } | |
145 | |
146 // mach_absolute_time is it when it comes to ticks on the Mac. Other calls | |
147 // with less precision (such as TickCount) just call through to | |
148 // mach_absolute_time. | |
149 | |
150 // timebase_info converts absolute time tick units into nanoseconds. Convert | |
151 // to microseconds up front to stave off overflows. | |
152 absolute_micro = mach_absolute_time() / Time::kNanosecondsPerMicrosecond * | |
153 timebase_info.numer / timebase_info.denom; | |
154 | |
155 // Don't bother with the rollover handling that the Windows version does. | |
156 // With numer and denom = 1 (the expected case), the 64-bit absolute time | |
157 // reported in nanoseconds is enough to last nearly 585 years. | |
158 | |
159 #elif defined(OS_POSIX) && \ | |
160 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 | |
161 | |
162 struct timespec ts; | 152 struct timespec ts; |
163 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { | 153 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { |
164 NOTREACHED() << "clock_gettime(CLOCK_MONOTONIC) failed."; | 154 NOTREACHED() << "clock_gettime(CLOCK_MONOTONIC) failed."; |
165 return TimeTicks(); | 155 return TimeTicks(); |
166 } | 156 } |
167 | 157 |
168 absolute_micro = | 158 absolute_micro = |
169 (static_cast<int64>(ts.tv_sec) * Time::kMicrosecondsPerSecond) + | 159 (static_cast<int64>(ts.tv_sec) * Time::kMicrosecondsPerSecond) + |
170 (static_cast<int64>(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond); | 160 (static_cast<int64>(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond); |
171 | 161 |
| 162 return TimeTicks(absolute_micro); |
| 163 } |
| 164 |
172 #else // _POSIX_MONOTONIC_CLOCK | 165 #else // _POSIX_MONOTONIC_CLOCK |
173 #error No usable tick clock function on this platform. | 166 #error No usable tick clock function on this platform. |
174 #endif // _POSIX_MONOTONIC_CLOCK | 167 #endif // _POSIX_MONOTONIC_CLOCK |
175 | 168 |
176 return TimeTicks(absolute_micro); | |
177 } | |
178 | |
179 // static | 169 // static |
180 TimeTicks TimeTicks::HighResNow() { | 170 TimeTicks TimeTicks::HighResNow() { |
181 return Now(); | 171 return Now(); |
182 } | 172 } |
183 | 173 |
184 } // namespace base | 174 } // namespace base |
OLD | NEW |