| 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) + |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; | 327 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; |
| 322 return result; | 328 return result; |
| 323 } | 329 } |
| 324 int64 us = us_ - kTimeTToMicrosecondsOffset; | 330 int64 us = us_ - kTimeTToMicrosecondsOffset; |
| 325 result.tv_sec = us / Time::kMicrosecondsPerSecond; | 331 result.tv_sec = us / Time::kMicrosecondsPerSecond; |
| 326 result.tv_usec = us % Time::kMicrosecondsPerSecond; | 332 result.tv_usec = us % Time::kMicrosecondsPerSecond; |
| 327 return result; | 333 return result; |
| 328 } | 334 } |
| 329 | 335 |
| 330 } // namespace base | 336 } // namespace base |
| OLD | NEW |