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__) |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 } | 103 } |
104 #else // _POSIX_MONOTONIC_CLOCK | 104 #else // _POSIX_MONOTONIC_CLOCK |
105 #error No usable tick clock function on this platform. | 105 #error No usable tick clock function on this platform. |
106 #endif // _POSIX_MONOTONIC_CLOCK | 106 #endif // _POSIX_MONOTONIC_CLOCK |
107 #endif // !defined(OS_MACOSX) | 107 #endif // !defined(OS_MACOSX) |
108 | 108 |
109 } // namespace | 109 } // namespace |
110 | 110 |
111 namespace base { | 111 namespace base { |
112 | 112 |
| 113 // static |
| 114 TimeDelta TimeDelta::FromTimeSpec(const timespec& ts) { |
| 115 return TimeDelta(ts.tv_sec * Time::kMicrosecondsPerSecond + |
| 116 ts.tv_nsec / Time::kNanosecondsPerMicrosecond); |
| 117 } |
| 118 |
113 struct timespec TimeDelta::ToTimeSpec() const { | 119 struct timespec TimeDelta::ToTimeSpec() const { |
114 int64_t microseconds = InMicroseconds(); | 120 int64_t microseconds = InMicroseconds(); |
115 time_t seconds = 0; | 121 time_t seconds = 0; |
116 if (microseconds >= Time::kMicrosecondsPerSecond) { | 122 if (microseconds >= Time::kMicrosecondsPerSecond) { |
117 seconds = InSeconds(); | 123 seconds = InSeconds(); |
118 microseconds -= seconds * Time::kMicrosecondsPerSecond; | 124 microseconds -= seconds * Time::kMicrosecondsPerSecond; |
119 } | 125 } |
120 struct timespec result = | 126 struct timespec result = |
121 {seconds, | 127 {seconds, |
122 static_cast<long>(microseconds * Time::kNanosecondsPerMicrosecond)}; | 128 static_cast<long>(microseconds * Time::kNanosecondsPerMicrosecond)}; |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; | 388 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; |
383 return result; | 389 return result; |
384 } | 390 } |
385 int64_t us = us_ - kTimeTToMicrosecondsOffset; | 391 int64_t us = us_ - kTimeTToMicrosecondsOffset; |
386 result.tv_sec = us / Time::kMicrosecondsPerSecond; | 392 result.tv_sec = us / Time::kMicrosecondsPerSecond; |
387 result.tv_usec = us % Time::kMicrosecondsPerSecond; | 393 result.tv_usec = us % Time::kMicrosecondsPerSecond; |
388 return result; | 394 return result; |
389 } | 395 } |
390 | 396 |
391 } // namespace base | 397 } // namespace base |
OLD | NEW |