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.h" | 5 #include "base/time.h" |
6 | 6 |
7 #include <sys/time.h> | 7 #include <sys/time.h> |
8 #include <time.h> | 8 #include <time.h> |
9 #if defined(OS_ANDROID) | 9 #if defined(OS_ANDROID) |
10 #include <time64.h> | 10 #include <time64.h> |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 const int64 Time::kWindowsEpochDeltaMicroseconds = | 104 const int64 Time::kWindowsEpochDeltaMicroseconds = |
105 kWindowsEpochDeltaSeconds * Time::kMicrosecondsPerSecond; | 105 kWindowsEpochDeltaSeconds * Time::kMicrosecondsPerSecond; |
106 | 106 |
107 // Some functions in time.cc use time_t directly, so we provide an offset | 107 // Some functions in time.cc use time_t directly, so we provide an offset |
108 // to convert from time_t (Unix epoch) and internal (Windows epoch). | 108 // to convert from time_t (Unix epoch) and internal (Windows epoch). |
109 // static | 109 // static |
110 const int64 Time::kTimeTToMicrosecondsOffset = kWindowsEpochDeltaMicroseconds; | 110 const int64 Time::kTimeTToMicrosecondsOffset = kWindowsEpochDeltaMicroseconds; |
111 | 111 |
112 // static | 112 // static |
113 Time Time::Now() { | 113 Time Time::Now() { |
| 114 if (TimeFactory::instance()) |
| 115 return TimeFactory::instance()->TimeNow(); |
| 116 |
114 struct timeval tv; | 117 struct timeval tv; |
115 struct timezone tz = { 0, 0 }; // UTC | 118 struct timezone tz = { 0, 0 }; // UTC |
116 if (gettimeofday(&tv, &tz) != 0) { | 119 if (gettimeofday(&tv, &tz) != 0) { |
117 DCHECK(0) << "Could not determine time of day"; | 120 DCHECK(0) << "Could not determine time of day"; |
118 LOG_ERRNO(ERROR) << "Call to gettimeofday failed."; | 121 LOG_ERRNO(ERROR) << "Call to gettimeofday failed."; |
119 // Return null instead of uninitialized |tv| value, which contains random | 122 // Return null instead of uninitialized |tv| value, which contains random |
120 // garbage data. This may result in the crash seen in crbug.com/147570. | 123 // garbage data. This may result in the crash seen in crbug.com/147570. |
121 return Time(); | 124 return Time(); |
122 } | 125 } |
123 // Combine seconds and microseconds in a 64-bit field containing microseconds | 126 // Combine seconds and microseconds in a 64-bit field containing microseconds |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 } | 233 } |
231 | 234 |
232 // TimeTicks ------------------------------------------------------------------ | 235 // TimeTicks ------------------------------------------------------------------ |
233 // FreeBSD 6 has CLOCK_MONOLITHIC but defines _POSIX_MONOTONIC_CLOCK to -1. | 236 // FreeBSD 6 has CLOCK_MONOLITHIC but defines _POSIX_MONOTONIC_CLOCK to -1. |
234 #if (defined(OS_POSIX) && \ | 237 #if (defined(OS_POSIX) && \ |
235 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0) || \ | 238 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0) || \ |
236 defined(OS_BSD) || defined(OS_ANDROID) | 239 defined(OS_BSD) || defined(OS_ANDROID) |
237 | 240 |
238 // static | 241 // static |
239 TimeTicks TimeTicks::Now() { | 242 TimeTicks TimeTicks::Now() { |
| 243 if (TimeFactory::instance()) |
| 244 return TimeFactory::instance()->TimeTicksNow(); |
| 245 |
240 uint64_t absolute_micro; | 246 uint64_t absolute_micro; |
241 | 247 |
242 struct timespec ts; | 248 struct timespec ts; |
243 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { | 249 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { |
244 NOTREACHED() << "clock_gettime(CLOCK_MONOTONIC) failed."; | 250 NOTREACHED() << "clock_gettime(CLOCK_MONOTONIC) failed."; |
245 return TimeTicks(); | 251 return TimeTicks(); |
246 } | 252 } |
247 | 253 |
248 absolute_micro = | 254 absolute_micro = |
249 (static_cast<int64>(ts.tv_sec) * Time::kMicrosecondsPerSecond) + | 255 (static_cast<int64>(ts.tv_sec) * Time::kMicrosecondsPerSecond) + |
250 (static_cast<int64>(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond); | 256 (static_cast<int64>(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond); |
251 | 257 |
252 return TimeTicks(absolute_micro); | 258 return TimeTicks(absolute_micro); |
253 } | 259 } |
254 #else // _POSIX_MONOTONIC_CLOCK | 260 #else // _POSIX_MONOTONIC_CLOCK |
255 #error No usable tick clock function on this platform. | 261 #error No usable tick clock function on this platform. |
256 #endif // _POSIX_MONOTONIC_CLOCK | 262 #endif // _POSIX_MONOTONIC_CLOCK |
257 | 263 |
258 // static | 264 // static |
259 TimeTicks TimeTicks::HighResNow() { | 265 TimeTicks TimeTicks::HighResNow() { |
| 266 if (TimeFactory::instance()) |
| 267 return TimeFactory::instance()->TimeTicksHighResNow(); |
260 return Now(); | 268 return Now(); |
261 } | 269 } |
262 | 270 |
263 #if defined(OS_CHROMEOS) | 271 #if defined(OS_CHROMEOS) |
264 // Force definition of the system trace clock; it is a chromeos-only api | 272 // Force definition of the system trace clock; it is a chromeos-only api |
265 // at the moment and surfacing it in the right place requires mucking | 273 // at the moment and surfacing it in the right place requires mucking |
266 // with glibc et al. | 274 // with glibc et al. |
267 #define CLOCK_SYSTEM_TRACE 11 | 275 #define CLOCK_SYSTEM_TRACE 11 |
268 | 276 |
269 // static | 277 // static |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; | 329 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; |
322 return result; | 330 return result; |
323 } | 331 } |
324 int64 us = us_ - kTimeTToMicrosecondsOffset; | 332 int64 us = us_ - kTimeTToMicrosecondsOffset; |
325 result.tv_sec = us / Time::kMicrosecondsPerSecond; | 333 result.tv_sec = us / Time::kMicrosecondsPerSecond; |
326 result.tv_usec = us % Time::kMicrosecondsPerSecond; | 334 result.tv_usec = us % Time::kMicrosecondsPerSecond; |
327 return result; | 335 return result; |
328 } | 336 } |
329 | 337 |
330 } // namespace base | 338 } // namespace base |
OLD | NEW |