| 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 #include <unistd.h> | 9 #include <unistd.h> |
| 10 | 10 |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond; | 186 milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond; |
| 187 } | 187 } |
| 188 | 188 |
| 189 // Adjust from Unix (1970) to Windows (1601) epoch. | 189 // Adjust from Unix (1970) to Windows (1601) epoch. |
| 190 return Time((milliseconds * kMicrosecondsPerMillisecond) + | 190 return Time((milliseconds * kMicrosecondsPerMillisecond) + |
| 191 kWindowsEpochDeltaMicroseconds); | 191 kWindowsEpochDeltaMicroseconds); |
| 192 } | 192 } |
| 193 | 193 |
| 194 // TimeTicks ------------------------------------------------------------------ | 194 // TimeTicks ------------------------------------------------------------------ |
| 195 // FreeBSD 6 has CLOCK_MONOLITHIC but defines _POSIX_MONOTONIC_CLOCK to -1. | 195 // FreeBSD 6 has CLOCK_MONOLITHIC but defines _POSIX_MONOTONIC_CLOCK to -1. |
| 196 #if (defined(OS_POSIX) && !defined(OS_NACL) && \ | 196 #if (defined(OS_POSIX) && \ |
| 197 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0) || \ | 197 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0) || \ |
| 198 defined(OS_BSD) || defined(OS_ANDROID) | 198 defined(OS_BSD) || defined(OS_ANDROID) |
| 199 | 199 |
| 200 // static | 200 // static |
| 201 TimeTicks TimeTicks::Now() { | 201 TimeTicks TimeTicks::Now() { |
| 202 uint64_t absolute_micro; | 202 uint64_t absolute_micro; |
| 203 | 203 |
| 204 struct timespec ts; | 204 struct timespec ts; |
| 205 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { | 205 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { |
| 206 NOTREACHED() << "clock_gettime(CLOCK_MONOTONIC) failed."; | 206 NOTREACHED() << "clock_gettime(CLOCK_MONOTONIC) failed."; |
| 207 return TimeTicks(); | 207 return TimeTicks(); |
| 208 } | 208 } |
| 209 | 209 |
| 210 absolute_micro = | 210 absolute_micro = |
| 211 (static_cast<int64>(ts.tv_sec) * Time::kMicrosecondsPerSecond) + | 211 (static_cast<int64>(ts.tv_sec) * Time::kMicrosecondsPerSecond) + |
| 212 (static_cast<int64>(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond); | 212 (static_cast<int64>(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond); |
| 213 | 213 |
| 214 return TimeTicks(absolute_micro); | 214 return TimeTicks(absolute_micro); |
| 215 } | 215 } |
| 216 | |
| 217 #elif defined(OS_NACL) | |
| 218 | |
| 219 TimeTicks TimeTicks::Now() { | |
| 220 // Sadly, Native Client does not have _POSIX_TIMERS enabled in sys/features.h | |
| 221 // Apparently NaCl only has CLOCK_REALTIME: | |
| 222 // http://code.google.com/p/nativeclient/issues/detail?id=1159 | |
| 223 return TimeTicks(clock()); | |
| 224 } | |
| 225 | |
| 226 #else // _POSIX_MONOTONIC_CLOCK | 216 #else // _POSIX_MONOTONIC_CLOCK |
| 227 #error No usable tick clock function on this platform. | 217 #error No usable tick clock function on this platform. |
| 228 #endif // _POSIX_MONOTONIC_CLOCK | 218 #endif // _POSIX_MONOTONIC_CLOCK |
| 229 | 219 |
| 230 // static | 220 // static |
| 231 TimeTicks TimeTicks::HighResNow() { | 221 TimeTicks TimeTicks::HighResNow() { |
| 232 return Now(); | 222 return Now(); |
| 233 } | 223 } |
| 234 | 224 |
| 235 #if defined(OS_CHROMEOS) | 225 #if defined(OS_CHROMEOS) |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; | 283 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; |
| 294 return result; | 284 return result; |
| 295 } | 285 } |
| 296 int64 us = us_ - kTimeTToMicrosecondsOffset; | 286 int64 us = us_ - kTimeTToMicrosecondsOffset; |
| 297 result.tv_sec = us / Time::kMicrosecondsPerSecond; | 287 result.tv_sec = us / Time::kMicrosecondsPerSecond; |
| 298 result.tv_usec = us % Time::kMicrosecondsPerSecond; | 288 result.tv_usec = us % Time::kMicrosecondsPerSecond; |
| 299 return result; | 289 return result; |
| 300 } | 290 } |
| 301 | 291 |
| 302 } // namespace base | 292 } // namespace base |
| OLD | NEW |