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__) |
11 #include <time64.h> | 11 #include <time64.h> |
12 #endif | 12 #endif |
13 #include <unistd.h> | 13 #include <unistd.h> |
14 | 14 |
15 #include <limits> | 15 #include <limits> |
16 #include <ostream> | 16 #include <ostream> |
17 | 17 |
18 #include "base/basictypes.h" | |
19 #include "base/logging.h" | 18 #include "base/logging.h" |
20 #include "build/build_config.h" | 19 #include "build/build_config.h" |
21 | 20 |
22 #if defined(OS_ANDROID) | 21 #if defined(OS_ANDROID) |
23 #include "base/os_compat_android.h" | 22 #include "base/os_compat_android.h" |
24 #elif defined(OS_NACL) | 23 #elif defined(OS_NACL) |
25 #include "base/os_compat_nacl.h" | 24 #include "base/os_compat_nacl.h" |
26 #endif | 25 #endif |
27 | 26 |
28 #if !defined(OS_MACOSX) | 27 #if !defined(OS_MACOSX) |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 | 72 |
74 void SysTimeToTimeStruct(SysTime t, struct tm* timestruct, bool is_local) { | 73 void SysTimeToTimeStruct(SysTime t, struct tm* timestruct, bool is_local) { |
75 base::AutoLock locked(g_sys_time_to_time_struct_lock.Get()); | 74 base::AutoLock locked(g_sys_time_to_time_struct_lock.Get()); |
76 if (is_local) | 75 if (is_local) |
77 localtime_r(&t, timestruct); | 76 localtime_r(&t, timestruct); |
78 else | 77 else |
79 gmtime_r(&t, timestruct); | 78 gmtime_r(&t, timestruct); |
80 } | 79 } |
81 #endif // OS_ANDROID | 80 #endif // OS_ANDROID |
82 | 81 |
83 int64 ConvertTimespecToMicros(const struct timespec& ts) { | 82 int64_t ConvertTimespecToMicros(const struct timespec& ts) { |
84 base::CheckedNumeric<int64> result(ts.tv_sec); | 83 base::CheckedNumeric<int64_t> result(ts.tv_sec); |
85 result *= base::Time::kMicrosecondsPerSecond; | 84 result *= base::Time::kMicrosecondsPerSecond; |
86 result += (ts.tv_nsec / base::Time::kNanosecondsPerMicrosecond); | 85 result += (ts.tv_nsec / base::Time::kNanosecondsPerMicrosecond); |
87 return result.ValueOrDie(); | 86 return result.ValueOrDie(); |
88 } | 87 } |
89 | 88 |
90 // Helper function to get results from clock_gettime() and convert to a | 89 // Helper function to get results from clock_gettime() and convert to a |
91 // microsecond timebase. Minimum requirement is MONOTONIC_CLOCK to be supported | 90 // microsecond timebase. Minimum requirement is MONOTONIC_CLOCK to be supported |
92 // on the system. FreeBSD 6 has CLOCK_MONOTONIC but defines | 91 // on the system. FreeBSD 6 has CLOCK_MONOTONIC but defines |
93 // _POSIX_MONOTONIC_CLOCK to -1. | 92 // _POSIX_MONOTONIC_CLOCK to -1. |
94 #if (defined(OS_POSIX) && \ | 93 #if (defined(OS_POSIX) && \ |
95 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0) || \ | 94 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0) || \ |
96 defined(OS_BSD) || defined(OS_ANDROID) | 95 defined(OS_BSD) || defined(OS_ANDROID) |
97 int64 ClockNow(clockid_t clk_id) { | 96 int64_t ClockNow(clockid_t clk_id) { |
98 struct timespec ts; | 97 struct timespec ts; |
99 if (clock_gettime(clk_id, &ts) != 0) { | 98 if (clock_gettime(clk_id, &ts) != 0) { |
100 NOTREACHED() << "clock_gettime(" << clk_id << ") failed."; | 99 NOTREACHED() << "clock_gettime(" << clk_id << ") failed."; |
101 return 0; | 100 return 0; |
102 } | 101 } |
103 return ConvertTimespecToMicros(ts); | 102 return ConvertTimespecToMicros(ts); |
104 } | 103 } |
105 #else // _POSIX_MONOTONIC_CLOCK | 104 #else // _POSIX_MONOTONIC_CLOCK |
106 #error No usable tick clock function on this platform. | 105 #error No usable tick clock function on this platform. |
107 #endif // _POSIX_MONOTONIC_CLOCK | 106 #endif // _POSIX_MONOTONIC_CLOCK |
108 #endif // !defined(OS_MACOSX) | 107 #endif // !defined(OS_MACOSX) |
109 | 108 |
110 } // namespace | 109 } // namespace |
111 | 110 |
112 namespace base { | 111 namespace base { |
113 | 112 |
114 struct timespec TimeDelta::ToTimeSpec() const { | 113 struct timespec TimeDelta::ToTimeSpec() const { |
115 int64 microseconds = InMicroseconds(); | 114 int64_t microseconds = InMicroseconds(); |
116 time_t seconds = 0; | 115 time_t seconds = 0; |
117 if (microseconds >= Time::kMicrosecondsPerSecond) { | 116 if (microseconds >= Time::kMicrosecondsPerSecond) { |
118 seconds = InSeconds(); | 117 seconds = InSeconds(); |
119 microseconds -= seconds * Time::kMicrosecondsPerSecond; | 118 microseconds -= seconds * Time::kMicrosecondsPerSecond; |
120 } | 119 } |
121 struct timespec result = | 120 struct timespec result = |
122 {seconds, | 121 {seconds, |
123 static_cast<long>(microseconds * Time::kNanosecondsPerMicrosecond)}; | 122 static_cast<long>(microseconds * Time::kNanosecondsPerMicrosecond)}; |
124 return result; | 123 return result; |
125 } | 124 } |
126 | 125 |
127 #if !defined(OS_MACOSX) | 126 #if !defined(OS_MACOSX) |
128 // The Time routines in this file use standard POSIX routines, or almost- | 127 // The Time routines in this file use standard POSIX routines, or almost- |
129 // standard routines in the case of timegm. We need to use a Mach-specific | 128 // standard routines in the case of timegm. We need to use a Mach-specific |
130 // function for TimeTicks::Now() on Mac OS X. | 129 // function for TimeTicks::Now() on Mac OS X. |
131 | 130 |
132 // Time ----------------------------------------------------------------------- | 131 // Time ----------------------------------------------------------------------- |
133 | 132 |
134 // Windows uses a Gregorian epoch of 1601. We need to match this internally | 133 // Windows uses a Gregorian epoch of 1601. We need to match this internally |
135 // so that our time representations match across all platforms. See bug 14734. | 134 // so that our time representations match across all platforms. See bug 14734. |
136 // irb(main):010:0> Time.at(0).getutc() | 135 // irb(main):010:0> Time.at(0).getutc() |
137 // => Thu Jan 01 00:00:00 UTC 1970 | 136 // => Thu Jan 01 00:00:00 UTC 1970 |
138 // irb(main):011:0> Time.at(-11644473600).getutc() | 137 // irb(main):011:0> Time.at(-11644473600).getutc() |
139 // => Mon Jan 01 00:00:00 UTC 1601 | 138 // => Mon Jan 01 00:00:00 UTC 1601 |
140 static const int64 kWindowsEpochDeltaSeconds = INT64_C(11644473600); | 139 static const int64_t kWindowsEpochDeltaSeconds = INT64_C(11644473600); |
141 | 140 |
142 // static | 141 // static |
143 const int64 Time::kWindowsEpochDeltaMicroseconds = | 142 const int64_t Time::kWindowsEpochDeltaMicroseconds = |
144 kWindowsEpochDeltaSeconds * Time::kMicrosecondsPerSecond; | 143 kWindowsEpochDeltaSeconds * Time::kMicrosecondsPerSecond; |
145 | 144 |
146 // Some functions in time.cc use time_t directly, so we provide an offset | 145 // Some functions in time.cc use time_t directly, so we provide an offset |
147 // to convert from time_t (Unix epoch) and internal (Windows epoch). | 146 // to convert from time_t (Unix epoch) and internal (Windows epoch). |
148 // static | 147 // static |
149 const int64 Time::kTimeTToMicrosecondsOffset = kWindowsEpochDeltaMicroseconds; | 148 const int64_t Time::kTimeTToMicrosecondsOffset = kWindowsEpochDeltaMicroseconds; |
150 | 149 |
151 // static | 150 // static |
152 Time Time::Now() { | 151 Time Time::Now() { |
153 struct timeval tv; | 152 struct timeval tv; |
154 struct timezone tz = { 0, 0 }; // UTC | 153 struct timezone tz = { 0, 0 }; // UTC |
155 if (gettimeofday(&tv, &tz) != 0) { | 154 if (gettimeofday(&tv, &tz) != 0) { |
156 DCHECK(0) << "Could not determine time of day"; | 155 DCHECK(0) << "Could not determine time of day"; |
157 PLOG(ERROR) << "Call to gettimeofday failed."; | 156 PLOG(ERROR) << "Call to gettimeofday failed."; |
158 // Return null instead of uninitialized |tv| value, which contains random | 157 // Return null instead of uninitialized |tv| value, which contains random |
159 // garbage data. This may result in the crash seen in crbug.com/147570. | 158 // garbage data. This may result in the crash seen in crbug.com/147570. |
160 return Time(); | 159 return Time(); |
161 } | 160 } |
162 // Combine seconds and microseconds in a 64-bit field containing microseconds | 161 // Combine seconds and microseconds in a 64-bit field containing microseconds |
163 // since the epoch. That's enough for nearly 600 centuries. Adjust from | 162 // since the epoch. That's enough for nearly 600 centuries. Adjust from |
164 // Unix (1970) to Windows (1601) epoch. | 163 // Unix (1970) to Windows (1601) epoch. |
165 return Time((tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec) + | 164 return Time((tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec) + |
166 kWindowsEpochDeltaMicroseconds); | 165 kWindowsEpochDeltaMicroseconds); |
167 } | 166 } |
168 | 167 |
169 // static | 168 // static |
170 Time Time::NowFromSystemTime() { | 169 Time Time::NowFromSystemTime() { |
171 // Just use Now() because Now() returns the system time. | 170 // Just use Now() because Now() returns the system time. |
172 return Now(); | 171 return Now(); |
173 } | 172 } |
174 | 173 |
175 void Time::Explode(bool is_local, Exploded* exploded) const { | 174 void Time::Explode(bool is_local, Exploded* exploded) const { |
176 // Time stores times with microsecond resolution, but Exploded only carries | 175 // Time stores times with microsecond resolution, but Exploded only carries |
177 // millisecond resolution, so begin by being lossy. Adjust from Windows | 176 // millisecond resolution, so begin by being lossy. Adjust from Windows |
178 // epoch (1601) to Unix epoch (1970); | 177 // epoch (1601) to Unix epoch (1970); |
179 int64 microseconds = us_ - kWindowsEpochDeltaMicroseconds; | 178 int64_t microseconds = us_ - kWindowsEpochDeltaMicroseconds; |
180 // The following values are all rounded towards -infinity. | 179 // The following values are all rounded towards -infinity. |
181 int64 milliseconds; // Milliseconds since epoch. | 180 int64_t milliseconds; // Milliseconds since epoch. |
182 SysTime seconds; // Seconds since epoch. | 181 SysTime seconds; // Seconds since epoch. |
183 int millisecond; // Exploded millisecond value (0-999). | 182 int millisecond; // Exploded millisecond value (0-999). |
184 if (microseconds >= 0) { | 183 if (microseconds >= 0) { |
185 // Rounding towards -infinity <=> rounding towards 0, in this case. | 184 // Rounding towards -infinity <=> rounding towards 0, in this case. |
186 milliseconds = microseconds / kMicrosecondsPerMillisecond; | 185 milliseconds = microseconds / kMicrosecondsPerMillisecond; |
187 seconds = milliseconds / kMillisecondsPerSecond; | 186 seconds = milliseconds / kMillisecondsPerSecond; |
188 millisecond = milliseconds % kMillisecondsPerSecond; | 187 millisecond = milliseconds % kMillisecondsPerSecond; |
189 } else { | 188 } else { |
190 // Round these *down* (towards -infinity). | 189 // Round these *down* (towards -infinity). |
191 milliseconds = (microseconds - kMicrosecondsPerMillisecond + 1) / | 190 milliseconds = (microseconds - kMicrosecondsPerMillisecond + 1) / |
(...skipping 29 matching lines...) Expand all Loading... |
221 timestruct.tm_mon = exploded.month - 1; | 220 timestruct.tm_mon = exploded.month - 1; |
222 timestruct.tm_year = exploded.year - 1900; | 221 timestruct.tm_year = exploded.year - 1900; |
223 timestruct.tm_wday = exploded.day_of_week; // mktime/timegm ignore this | 222 timestruct.tm_wday = exploded.day_of_week; // mktime/timegm ignore this |
224 timestruct.tm_yday = 0; // mktime/timegm ignore this | 223 timestruct.tm_yday = 0; // mktime/timegm ignore this |
225 timestruct.tm_isdst = -1; // attempt to figure it out | 224 timestruct.tm_isdst = -1; // attempt to figure it out |
226 #if !defined(OS_NACL) && !defined(OS_SOLARIS) | 225 #if !defined(OS_NACL) && !defined(OS_SOLARIS) |
227 timestruct.tm_gmtoff = 0; // not a POSIX field, so mktime/timegm ignore | 226 timestruct.tm_gmtoff = 0; // not a POSIX field, so mktime/timegm ignore |
228 timestruct.tm_zone = NULL; // not a POSIX field, so mktime/timegm ignore | 227 timestruct.tm_zone = NULL; // not a POSIX field, so mktime/timegm ignore |
229 #endif | 228 #endif |
230 | 229 |
231 | 230 int64_t milliseconds; |
232 int64 milliseconds; | |
233 SysTime seconds; | 231 SysTime seconds; |
234 | 232 |
235 // Certain exploded dates do not really exist due to daylight saving times, | 233 // Certain exploded dates do not really exist due to daylight saving times, |
236 // and this causes mktime() to return implementation-defined values when | 234 // and this causes mktime() to return implementation-defined values when |
237 // tm_isdst is set to -1. On Android, the function will return -1, while the | 235 // tm_isdst is set to -1. On Android, the function will return -1, while the |
238 // C libraries of other platforms typically return a liberally-chosen value. | 236 // C libraries of other platforms typically return a liberally-chosen value. |
239 // Handling this requires the special code below. | 237 // Handling this requires the special code below. |
240 | 238 |
241 // SysTimeFromTimeStruct() modifies the input structure, save current value. | 239 // SysTimeFromTimeStruct() modifies the input structure, save current value. |
242 struct tm timestruct0 = timestruct; | 240 struct tm timestruct0 = timestruct; |
243 | 241 |
244 seconds = SysTimeFromTimeStruct(×truct, is_local); | 242 seconds = SysTimeFromTimeStruct(×truct, is_local); |
245 if (seconds == -1) { | 243 if (seconds == -1) { |
246 // Get the time values with tm_isdst == 0 and 1, then select the closest one | 244 // Get the time values with tm_isdst == 0 and 1, then select the closest one |
247 // to UTC 00:00:00 that isn't -1. | 245 // to UTC 00:00:00 that isn't -1. |
248 timestruct = timestruct0; | 246 timestruct = timestruct0; |
249 timestruct.tm_isdst = 0; | 247 timestruct.tm_isdst = 0; |
250 int64 seconds_isdst0 = SysTimeFromTimeStruct(×truct, is_local); | 248 int64_t seconds_isdst0 = SysTimeFromTimeStruct(×truct, is_local); |
251 | 249 |
252 timestruct = timestruct0; | 250 timestruct = timestruct0; |
253 timestruct.tm_isdst = 1; | 251 timestruct.tm_isdst = 1; |
254 int64 seconds_isdst1 = SysTimeFromTimeStruct(×truct, is_local); | 252 int64_t seconds_isdst1 = SysTimeFromTimeStruct(×truct, is_local); |
255 | 253 |
256 // seconds_isdst0 or seconds_isdst1 can be -1 for some timezones. | 254 // seconds_isdst0 or seconds_isdst1 can be -1 for some timezones. |
257 // E.g. "CLST" (Chile Summer Time) returns -1 for 'tm_isdt == 1'. | 255 // E.g. "CLST" (Chile Summer Time) returns -1 for 'tm_isdt == 1'. |
258 if (seconds_isdst0 < 0) | 256 if (seconds_isdst0 < 0) |
259 seconds = seconds_isdst1; | 257 seconds = seconds_isdst1; |
260 else if (seconds_isdst1 < 0) | 258 else if (seconds_isdst1 < 0) |
261 seconds = seconds_isdst0; | 259 seconds = seconds_isdst0; |
262 else | 260 else |
263 seconds = std::min(seconds_isdst0, seconds_isdst1); | 261 seconds = std::min(seconds_isdst0, seconds_isdst1); |
264 } | 262 } |
(...skipping 12 matching lines...) Expand all Loading... |
277 // The minimum and maximum representible times that mktime and timegm could | 275 // The minimum and maximum representible times that mktime and timegm could |
278 // return are used here instead of values outside that range to allow for | 276 // return are used here instead of values outside that range to allow for |
279 // proper round-tripping between exploded and counter-type time | 277 // proper round-tripping between exploded and counter-type time |
280 // representations in the presence of possible truncation to time_t by | 278 // representations in the presence of possible truncation to time_t by |
281 // division and use with other functions that accept time_t. | 279 // division and use with other functions that accept time_t. |
282 // | 280 // |
283 // When representing the most distant time in the future, add in an extra | 281 // When representing the most distant time in the future, add in an extra |
284 // 999ms to avoid the time being less than any other possible value that | 282 // 999ms to avoid the time being less than any other possible value that |
285 // this function can return. | 283 // this function can return. |
286 | 284 |
287 // On Android, SysTime is int64, special care must be taken to avoid | 285 // On Android, SysTime is int64_t, special care must be taken to avoid |
288 // overflows. | 286 // overflows. |
289 const int64 min_seconds = (sizeof(SysTime) < sizeof(int64)) | 287 const int64_t min_seconds = (sizeof(SysTime) < sizeof(int64_t)) |
290 ? std::numeric_limits<SysTime>::min() | 288 ? std::numeric_limits<SysTime>::min() |
291 : std::numeric_limits<int32_t>::min(); | 289 : std::numeric_limits<int32_t>::min(); |
292 const int64 max_seconds = (sizeof(SysTime) < sizeof(int64)) | 290 const int64_t max_seconds = (sizeof(SysTime) < sizeof(int64_t)) |
293 ? std::numeric_limits<SysTime>::max() | 291 ? std::numeric_limits<SysTime>::max() |
294 : std::numeric_limits<int32_t>::max(); | 292 : std::numeric_limits<int32_t>::max(); |
295 if (exploded.year < 1969) { | 293 if (exploded.year < 1969) { |
296 milliseconds = min_seconds * kMillisecondsPerSecond; | 294 milliseconds = min_seconds * kMillisecondsPerSecond; |
297 } else { | 295 } else { |
298 milliseconds = max_seconds * kMillisecondsPerSecond; | 296 milliseconds = max_seconds * kMillisecondsPerSecond; |
299 milliseconds += (kMillisecondsPerSecond - 1); | 297 milliseconds += (kMillisecondsPerSecond - 1); |
300 } | 298 } |
301 } else { | 299 } else { |
302 milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond; | 300 milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond; |
303 } | 301 } |
304 | 302 |
(...skipping 28 matching lines...) Expand all Loading... |
333 | 331 |
334 // static | 332 // static |
335 Time Time::FromTimeVal(struct timeval t) { | 333 Time Time::FromTimeVal(struct timeval t) { |
336 DCHECK_LT(t.tv_usec, static_cast<int>(Time::kMicrosecondsPerSecond)); | 334 DCHECK_LT(t.tv_usec, static_cast<int>(Time::kMicrosecondsPerSecond)); |
337 DCHECK_GE(t.tv_usec, 0); | 335 DCHECK_GE(t.tv_usec, 0); |
338 if (t.tv_usec == 0 && t.tv_sec == 0) | 336 if (t.tv_usec == 0 && t.tv_sec == 0) |
339 return Time(); | 337 return Time(); |
340 if (t.tv_usec == static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1 && | 338 if (t.tv_usec == static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1 && |
341 t.tv_sec == std::numeric_limits<time_t>::max()) | 339 t.tv_sec == std::numeric_limits<time_t>::max()) |
342 return Max(); | 340 return Max(); |
343 return Time( | 341 return Time((static_cast<int64_t>(t.tv_sec) * Time::kMicrosecondsPerSecond) + |
344 (static_cast<int64>(t.tv_sec) * Time::kMicrosecondsPerSecond) + | 342 t.tv_usec + kTimeTToMicrosecondsOffset); |
345 t.tv_usec + | |
346 kTimeTToMicrosecondsOffset); | |
347 } | 343 } |
348 | 344 |
349 struct timeval Time::ToTimeVal() const { | 345 struct timeval Time::ToTimeVal() const { |
350 struct timeval result; | 346 struct timeval result; |
351 if (is_null()) { | 347 if (is_null()) { |
352 result.tv_sec = 0; | 348 result.tv_sec = 0; |
353 result.tv_usec = 0; | 349 result.tv_usec = 0; |
354 return result; | 350 return result; |
355 } | 351 } |
356 if (is_max()) { | 352 if (is_max()) { |
357 result.tv_sec = std::numeric_limits<time_t>::max(); | 353 result.tv_sec = std::numeric_limits<time_t>::max(); |
358 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; | 354 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; |
359 return result; | 355 return result; |
360 } | 356 } |
361 int64 us = us_ - kTimeTToMicrosecondsOffset; | 357 int64_t us = us_ - kTimeTToMicrosecondsOffset; |
362 result.tv_sec = us / Time::kMicrosecondsPerSecond; | 358 result.tv_sec = us / Time::kMicrosecondsPerSecond; |
363 result.tv_usec = us % Time::kMicrosecondsPerSecond; | 359 result.tv_usec = us % Time::kMicrosecondsPerSecond; |
364 return result; | 360 return result; |
365 } | 361 } |
366 | 362 |
367 } // namespace base | 363 } // namespace base |
OLD | NEW |