2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
13 #if defined(WEBRTC_POSIX)
15 #if defined(WEBRTC_MAC)
16 #include <mach/mach_time.h>
20 #if defined(WEBRTC_WIN)
21 #ifndef WIN32_LEAN_AND_MEAN
22 #define WIN32_LEAN_AND_MEAN
28 #include "webrtc/base/checks.h"
29 #include "webrtc/base/timeutils.h"
31 #define EFFICIENT_IMPLEMENTATION 1
35 const uint32 HALF = 0x80000000;
39 #if defined(WEBRTC_MAC)
40 static mach_timebase_info_data_t timebase;
41 if (timebase.denom == 0) {
42 // Get the timebase if this is the first time we run.
43 // Recommended by Apple's QA1398.
44 if (mach_timebase_info(&timebase) != KERN_SUCCESS) {
48 // Use timebase to convert absolute time tick units into nanoseconds.
49 ticks = mach_absolute_time() * timebase.numer / timebase.denom;
50 #elif defined(WEBRTC_POSIX)
52 // TODO: Do we need to handle the case when CLOCK_MONOTONIC
54 clock_gettime(CLOCK_MONOTONIC, &ts);
55 ticks = kNumNanosecsPerSec * static_cast<int64>(ts.tv_sec) +
56 static_cast<int64>(ts.tv_nsec);
57 #elif defined(WEBRTC_WIN)
58 static volatile LONG last_timegettime = 0;
59 static volatile int64 num_wrap_timegettime = 0;
60 volatile LONG* last_timegettime_ptr = &last_timegettime;
61 DWORD now = timeGetTime();
62 // Atomically update the last gotten time
63 DWORD old = InterlockedExchange(last_timegettime_ptr, now);
65 // If now is earlier than old, there may have been a race between
67 // 0x0fffffff ~3.1 days, the code will not take that long to execute
68 // so it must have been a wrap around.
69 if (old > 0xf0000000 && now < 0x0fffffff) {
70 num_wrap_timegettime++;
73 ticks = now + (num_wrap_timegettime << 32);
74 // TODO: Calculate with nanosecond precision. Otherwise, we're just
75 // wasting a multiply and divide when doing Time() on Windows.
76 ticks = ticks * kNumNanosecsPerMillisec;
82 return static_cast<uint32>(TimeNanos() / kNumNanosecsPerMillisec);
86 return static_cast<uint64>(TimeNanos() / kNumNanosecsPerMicrosec);
89 #if defined(WEBRTC_WIN)
90 static const uint64 kFileTimeToUnixTimeEpochOffset = 116444736000000000ULL;
93 long tv_sec, tv_usec; // NOLINT
96 // Emulate POSIX gettimeofday().
97 // Based on breakpad/src/third_party/glog/src/utilities.cc
98 static int gettimeofday(struct timeval *tv, void *tz) {
99 // FILETIME is measured in tens of microseconds since 1601-01-01 UTC.
101 GetSystemTimeAsFileTime(&ft);
104 li.LowPart = ft.dwLowDateTime;
105 li.HighPart = ft.dwHighDateTime;
107 // Convert to seconds and microseconds since Unix time Epoch.
108 int64 micros = (li.QuadPart - kFileTimeToUnixTimeEpochOffset) / 10;
109 tv->tv_sec = static_cast<long>(micros / kNumMicrosecsPerSec); // NOLINT
110 tv->tv_usec = static_cast<long>(micros % kNumMicrosecsPerSec); // NOLINT
115 // Emulate POSIX gmtime_r().
116 static struct tm *gmtime_r(const time_t *timep, struct tm *result) {
117 // On Windows, gmtime is thread safe.
118 struct tm *tm = gmtime(timep); // NOLINT
127 void CurrentTmTime(struct tm *tm, int *microseconds) {
128 struct timeval timeval;
129 if (gettimeofday(&timeval, NULL) < 0) {
130 // Incredibly unlikely code path.
131 timeval.tv_sec = timeval.tv_usec = 0;
133 time_t secs = timeval.tv_sec;
135 *microseconds = timeval.tv_usec;
138 uint32 TimeAfter(int32 elapsed) {
139 DCHECK_GE(elapsed, 0);
140 DCHECK_LT(static_cast<uint32>(elapsed), HALF);
141 return Time() + elapsed;
144 bool TimeIsBetween(uint32 earlier, uint32 middle, uint32 later) {
145 if (earlier <= later) {
146 return ((earlier <= middle) && (middle <= later));
148 return !((later < middle) && (middle < earlier));
152 bool TimeIsLaterOrEqual(uint32 earlier, uint32 later) {
153 #if EFFICIENT_IMPLEMENTATION
154 int32 diff = later - earlier;
155 return (diff >= 0 && static_cast<uint32>(diff) < HALF);
157 const bool later_or_equal = TimeIsBetween(earlier, later, earlier + HALF);
158 return later_or_equal;
162 bool TimeIsLater(uint32 earlier, uint32 later) {
163 #if EFFICIENT_IMPLEMENTATION
164 int32 diff = later - earlier;
165 return (diff > 0 && static_cast<uint32>(diff) < HALF);
167 const bool earlier_or_equal = TimeIsBetween(later, earlier, later + HALF);
168 return !earlier_or_equal;
172 int32 TimeDiff(uint32 later, uint32 earlier) {
173 #if EFFICIENT_IMPLEMENTATION
174 return later - earlier;
176 const bool later_or_equal = TimeIsBetween(earlier, later, earlier + HALF);
177 if (later_or_equal) {
178 if (earlier <= later) {
179 return static_cast<long>(later - earlier);
181 return static_cast<long>(later + (UINT32_MAX - earlier) + 1);
184 if (later <= earlier) {
185 return -static_cast<long>(earlier - later);
187 return -static_cast<long>(earlier + (UINT32_MAX - later) + 1);
193 TimestampWrapAroundHandler::TimestampWrapAroundHandler()
194 : last_ts_(0), num_wrap_(0) {}
196 int64 TimestampWrapAroundHandler::Unwrap(uint32 ts) {
198 if (last_ts_ > 0xf0000000 && ts < 0x0fffffff) {
203 int64_t unwrapped_ts = ts + (num_wrap_ << 32);