OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 10 matching lines...) Expand all Loading... |
21 #ifndef WIN32_LEAN_AND_MEAN | 21 #ifndef WIN32_LEAN_AND_MEAN |
22 #define WIN32_LEAN_AND_MEAN | 22 #define WIN32_LEAN_AND_MEAN |
23 #endif | 23 #endif |
24 #include <windows.h> | 24 #include <windows.h> |
25 #include <mmsystem.h> | 25 #include <mmsystem.h> |
26 #endif | 26 #endif |
27 | 27 |
28 #include "webrtc/base/checks.h" | 28 #include "webrtc/base/checks.h" |
29 #include "webrtc/base/timeutils.h" | 29 #include "webrtc/base/timeutils.h" |
30 | 30 |
31 #define EFFICIENT_IMPLEMENTATION 1 | |
32 | |
33 namespace rtc { | 31 namespace rtc { |
34 | 32 |
35 const uint32_t HALF = 0x80000000; | 33 const uint32_t HALF = 0x80000000; |
36 | 34 |
37 uint64_t TimeNanos() { | 35 uint64_t TimeNanos() { |
38 int64_t ticks = 0; | 36 int64_t ticks = 0; |
39 #if defined(WEBRTC_MAC) | 37 #if defined(WEBRTC_MAC) |
40 static mach_timebase_info_data_t timebase; | 38 static mach_timebase_info_data_t timebase; |
41 if (timebase.denom == 0) { | 39 if (timebase.denom == 0) { |
42 // Get the timebase if this is the first time we run. | 40 // Get the timebase if this is the first time we run. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 } | 83 } |
86 | 84 |
87 int64_t Time64() { | 85 int64_t Time64() { |
88 return static_cast<int64_t>(TimeNanos() / kNumNanosecsPerMillisec); | 86 return static_cast<int64_t>(TimeNanos() / kNumNanosecsPerMillisec); |
89 } | 87 } |
90 | 88 |
91 uint64_t TimeMicros() { | 89 uint64_t TimeMicros() { |
92 return static_cast<uint64_t>(TimeNanos() / kNumNanosecsPerMicrosec); | 90 return static_cast<uint64_t>(TimeNanos() / kNumNanosecsPerMicrosec); |
93 } | 91 } |
94 | 92 |
95 #if defined(WEBRTC_WIN) | |
96 static const uint64_t kFileTimeToUnixTimeEpochOffset = 116444736000000000ULL; | |
97 | |
98 struct timeval { | |
99 long tv_sec, tv_usec; // NOLINT | |
100 }; | |
101 | |
102 // Emulate POSIX gettimeofday(). | |
103 // Based on breakpad/src/third_party/glog/src/utilities.cc | |
104 static int gettimeofday(struct timeval *tv, void *tz) { | |
105 // FILETIME is measured in tens of microseconds since 1601-01-01 UTC. | |
106 FILETIME ft; | |
107 GetSystemTimeAsFileTime(&ft); | |
108 | |
109 LARGE_INTEGER li; | |
110 li.LowPart = ft.dwLowDateTime; | |
111 li.HighPart = ft.dwHighDateTime; | |
112 | |
113 // Convert to seconds and microseconds since Unix time Epoch. | |
114 int64_t micros = (li.QuadPart - kFileTimeToUnixTimeEpochOffset) / 10; | |
115 tv->tv_sec = static_cast<long>(micros / kNumMicrosecsPerSec); // NOLINT | |
116 tv->tv_usec = static_cast<long>(micros % kNumMicrosecsPerSec); // NOLINT | |
117 | |
118 return 0; | |
119 } | |
120 | |
121 // Emulate POSIX gmtime_r(). | |
122 static struct tm *gmtime_r(const time_t *timep, struct tm *result) { | |
123 // On Windows, gmtime is thread safe. | |
124 struct tm *tm = gmtime(timep); // NOLINT | |
125 if (tm == NULL) { | |
126 return NULL; | |
127 } | |
128 *result = *tm; | |
129 return result; | |
130 } | |
131 #endif // WEBRTC_WIN | |
132 | |
133 void CurrentTmTime(struct tm *tm, int *microseconds) { | |
134 struct timeval timeval; | |
135 if (gettimeofday(&timeval, NULL) < 0) { | |
136 // Incredibly unlikely code path. | |
137 timeval.tv_sec = timeval.tv_usec = 0; | |
138 } | |
139 time_t secs = timeval.tv_sec; | |
140 gmtime_r(&secs, tm); | |
141 *microseconds = timeval.tv_usec; | |
142 } | |
143 | |
144 uint32_t TimeAfter(int32_t elapsed) { | 93 uint32_t TimeAfter(int32_t elapsed) { |
145 RTC_DCHECK_GE(elapsed, 0); | 94 RTC_DCHECK_GE(elapsed, 0); |
146 RTC_DCHECK_LT(static_cast<uint32_t>(elapsed), HALF); | 95 RTC_DCHECK_LT(static_cast<uint32_t>(elapsed), HALF); |
147 return Time() + elapsed; | 96 return Time() + elapsed; |
148 } | 97 } |
149 | 98 |
150 bool TimeIsBetween(uint32_t earlier, uint32_t middle, uint32_t later) { | |
151 if (earlier <= later) { | |
152 return ((earlier <= middle) && (middle <= later)); | |
153 } else { | |
154 return !((later < middle) && (middle < earlier)); | |
155 } | |
156 } | |
157 | |
158 bool TimeIsLaterOrEqual(uint32_t earlier, uint32_t later) { | 99 bool TimeIsLaterOrEqual(uint32_t earlier, uint32_t later) { |
159 #if EFFICIENT_IMPLEMENTATION | |
160 int32_t diff = later - earlier; | 100 int32_t diff = later - earlier; |
161 return (diff >= 0 && static_cast<uint32_t>(diff) < HALF); | 101 return (diff >= 0 && static_cast<uint32_t>(diff) < HALF); |
162 #else | |
163 const bool later_or_equal = TimeIsBetween(earlier, later, earlier + HALF); | |
164 return later_or_equal; | |
165 #endif | |
166 } | 102 } |
167 | 103 |
168 bool TimeIsLater(uint32_t earlier, uint32_t later) { | 104 bool TimeIsLater(uint32_t earlier, uint32_t later) { |
169 #if EFFICIENT_IMPLEMENTATION | |
170 int32_t diff = later - earlier; | 105 int32_t diff = later - earlier; |
171 return (diff > 0 && static_cast<uint32_t>(diff) < HALF); | 106 return (diff > 0 && static_cast<uint32_t>(diff) < HALF); |
172 #else | |
173 const bool earlier_or_equal = TimeIsBetween(later, earlier, later + HALF); | |
174 return !earlier_or_equal; | |
175 #endif | |
176 } | 107 } |
177 | 108 |
178 int32_t TimeDiff(uint32_t later, uint32_t earlier) { | 109 int32_t TimeDiff(uint32_t later, uint32_t earlier) { |
179 #if EFFICIENT_IMPLEMENTATION | |
180 return later - earlier; | 110 return later - earlier; |
181 #else | |
182 const bool later_or_equal = TimeIsBetween(earlier, later, earlier + HALF); | |
183 if (later_or_equal) { | |
184 if (earlier <= later) { | |
185 return static_cast<long>(later - earlier); | |
186 } else { | |
187 return static_cast<long>(later + (UINT32_MAX - earlier) + 1); | |
188 } | |
189 } else { | |
190 if (later <= earlier) { | |
191 return -static_cast<long>(earlier - later); | |
192 } else { | |
193 return -static_cast<long>(earlier + (UINT32_MAX - later) + 1); | |
194 } | |
195 } | |
196 #endif | |
197 } | 111 } |
198 | 112 |
199 int64_t TimeDiff64(int64_t later, int64_t earlier) { | 113 int64_t TimeDiff64(int64_t later, int64_t earlier) { |
200 return later - earlier; | 114 return later - earlier; |
201 } | 115 } |
202 | 116 |
203 TimestampWrapAroundHandler::TimestampWrapAroundHandler() | 117 TimestampWrapAroundHandler::TimestampWrapAroundHandler() |
204 : last_ts_(0), num_wrap_(-1) {} | 118 : last_ts_(0), num_wrap_(-1) {} |
205 | 119 |
206 int64_t TimestampWrapAroundHandler::Unwrap(uint32_t ts) { | 120 int64_t TimestampWrapAroundHandler::Unwrap(uint32_t ts) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 if (expiry_in_leap_year && month <= 2 - 1) // |month| is zero based. | 174 if (expiry_in_leap_year && month <= 2 - 1) // |month| is zero based. |
261 day -= 1; | 175 day -= 1; |
262 | 176 |
263 // Combine all variables into seconds from 1970-01-01 00:00 (except |month| | 177 // Combine all variables into seconds from 1970-01-01 00:00 (except |month| |
264 // which was accumulated into |day| above). | 178 // which was accumulated into |day| above). |
265 return (((static_cast<int64_t> | 179 return (((static_cast<int64_t> |
266 (year - 1970) * 365 + day) * 24 + hour) * 60 + min) * 60 + sec; | 180 (year - 1970) * 365 + day) * 24 + hour) * 60 + min) * 60 + sec; |
267 } | 181 } |
268 | 182 |
269 } // namespace rtc | 183 } // namespace rtc |
OLD | NEW |