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 14 matching lines...) Expand all Loading... |
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 | 31 #define EFFICIENT_IMPLEMENTATION 1 |
32 | 32 |
33 namespace rtc { | 33 namespace rtc { |
34 | 34 |
35 const uint32_t HALF = 0x80000000; | |
36 | |
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. |
43 // Recommended by Apple's QA1398. | 41 // Recommended by Apple's QA1398. |
44 if (mach_timebase_info(&timebase) != KERN_SUCCESS) { | 42 if (mach_timebase_info(&timebase) != KERN_SUCCESS) { |
45 RTC_DCHECK(false); | 43 RTC_DCHECK(false); |
46 } | 44 } |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 struct timeval timeval; | 132 struct timeval timeval; |
135 if (gettimeofday(&timeval, NULL) < 0) { | 133 if (gettimeofday(&timeval, NULL) < 0) { |
136 // Incredibly unlikely code path. | 134 // Incredibly unlikely code path. |
137 timeval.tv_sec = timeval.tv_usec = 0; | 135 timeval.tv_sec = timeval.tv_usec = 0; |
138 } | 136 } |
139 time_t secs = timeval.tv_sec; | 137 time_t secs = timeval.tv_sec; |
140 gmtime_r(&secs, tm); | 138 gmtime_r(&secs, tm); |
141 *microseconds = timeval.tv_usec; | 139 *microseconds = timeval.tv_usec; |
142 } | 140 } |
143 | 141 |
144 uint32_t TimeAfter(int32_t elapsed) { | 142 int64_t TimeAfter(int elapsed) { |
145 RTC_DCHECK_GE(elapsed, 0); | 143 RTC_DCHECK_GE(elapsed, 0); |
146 RTC_DCHECK_LT(static_cast<uint32_t>(elapsed), HALF); | |
147 return Time() + elapsed; | 144 return Time() + elapsed; |
148 } | 145 } |
149 | 146 |
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) { | |
159 #if EFFICIENT_IMPLEMENTATION | |
160 int32_t diff = later - earlier; | |
161 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 } | |
167 | |
168 bool TimeIsLater(uint32_t earlier, uint32_t later) { | |
169 #if EFFICIENT_IMPLEMENTATION | |
170 int32_t diff = later - earlier; | |
171 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 } | |
177 | |
178 int32_t TimeDiff(uint32_t later, uint32_t earlier) { | |
179 #if EFFICIENT_IMPLEMENTATION | |
180 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 } | |
198 | |
199 int64_t TimeDiff64(int64_t later, int64_t earlier) { | 147 int64_t TimeDiff64(int64_t later, int64_t earlier) { |
200 return later - earlier; | 148 return later - earlier; |
201 } | 149 } |
202 | 150 |
203 TimestampWrapAroundHandler::TimestampWrapAroundHandler() | 151 TimestampWrapAroundHandler::TimestampWrapAroundHandler() |
204 : last_ts_(0), num_wrap_(-1) {} | 152 : last_ts_(0), num_wrap_(-1) {} |
205 | 153 |
206 int64_t TimestampWrapAroundHandler::Unwrap(uint32_t ts) { | 154 int64_t TimestampWrapAroundHandler::Unwrap(uint32_t ts) { |
207 if (num_wrap_ == -1) { | 155 if (num_wrap_ == -1) { |
208 last_ts_ = ts; | 156 last_ts_ = ts; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 if (expiry_in_leap_year && month <= 2 - 1) // |month| is zero based. | 208 if (expiry_in_leap_year && month <= 2 - 1) // |month| is zero based. |
261 day -= 1; | 209 day -= 1; |
262 | 210 |
263 // Combine all variables into seconds from 1970-01-01 00:00 (except |month| | 211 // Combine all variables into seconds from 1970-01-01 00:00 (except |month| |
264 // which was accumulated into |day| above). | 212 // which was accumulated into |day| above). |
265 return (((static_cast<int64_t> | 213 return (((static_cast<int64_t> |
266 (year - 1970) * 365 + day) * 24 + hour) * 60 + min) * 60 + sec; | 214 (year - 1970) * 365 + day) * 24 + hour) * 60 + min) * 60 + sec; |
267 } | 215 } |
268 | 216 |
269 } // namespace rtc | 217 } // namespace rtc |
OLD | NEW |