blob: 24b04ee2ee46342c1551901309042f8f4438955a [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
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.
9 */
10
11#include <stdint.h>
12
13#if defined(WEBRTC_POSIX)
14#include <sys/time.h>
15#if defined(WEBRTC_MAC)
16#include <mach/mach_time.h>
17#endif
18#endif
19
20#if defined(WEBRTC_WIN)
andrew@webrtc.org6ae5a6d2014-09-16 01:03:29 +000021#ifndef WIN32_LEAN_AND_MEAN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022#define WIN32_LEAN_AND_MEAN
andrew@webrtc.org6ae5a6d2014-09-16 01:03:29 +000023#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024#include <windows.h>
25#include <mmsystem.h>
26#endif
27
andrew@webrtc.org6ae5a6d2014-09-16 01:03:29 +000028#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000029#include "webrtc/base/timeutils.h"
30
31#define EFFICIENT_IMPLEMENTATION 1
32
33namespace rtc {
34
Peter Boström0c4e06b2015-10-07 12:23:21 +020035const uint32_t HALF = 0x80000000;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000036
Peter Boström0c4e06b2015-10-07 12:23:21 +020037uint64_t TimeNanos() {
38 int64_t ticks = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000039#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.
andrew@webrtc.org6ae5a6d2014-09-16 01:03:29 +000044 if (mach_timebase_info(&timebase) != KERN_SUCCESS) {
henrikg91d6ede2015-09-17 00:24:34 -070045 RTC_DCHECK(false);
andrew@webrtc.org6ae5a6d2014-09-16 01:03:29 +000046 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000047 }
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)
51 struct timespec ts;
52 // TODO: Do we need to handle the case when CLOCK_MONOTONIC
53 // is not supported?
54 clock_gettime(CLOCK_MONOTONIC, &ts);
Peter Boström0c4e06b2015-10-07 12:23:21 +020055 ticks = kNumNanosecsPerSec * static_cast<int64_t>(ts.tv_sec) +
56 static_cast<int64_t>(ts.tv_nsec);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000057#elif defined(WEBRTC_WIN)
58 static volatile LONG last_timegettime = 0;
Peter Boström0c4e06b2015-10-07 12:23:21 +020059 static volatile int64_t num_wrap_timegettime = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000060 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);
64 if (now < old) {
65 // If now is earlier than old, there may have been a race between
66 // threads.
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++;
71 }
72 }
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;
Erik Språng1c390982016-01-27 12:55:33 +010077#else
78#error Unsupported platform.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079#endif
80 return ticks;
81}
82
Peter Boström0c4e06b2015-10-07 12:23:21 +020083uint32_t Time() {
84 return static_cast<uint32_t>(TimeNanos() / kNumNanosecsPerMillisec);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000085}
86
Peter Boström0c4e06b2015-10-07 12:23:21 +020087uint64_t TimeMicros() {
88 return static_cast<uint64_t>(TimeNanos() / kNumNanosecsPerMicrosec);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000089}
90
91#if defined(WEBRTC_WIN)
Peter Boström0c4e06b2015-10-07 12:23:21 +020092static const uint64_t kFileTimeToUnixTimeEpochOffset = 116444736000000000ULL;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093
94struct timeval {
95 long tv_sec, tv_usec; // NOLINT
96};
97
98// Emulate POSIX gettimeofday().
99// Based on breakpad/src/third_party/glog/src/utilities.cc
100static int gettimeofday(struct timeval *tv, void *tz) {
101 // FILETIME is measured in tens of microseconds since 1601-01-01 UTC.
102 FILETIME ft;
103 GetSystemTimeAsFileTime(&ft);
104
105 LARGE_INTEGER li;
106 li.LowPart = ft.dwLowDateTime;
107 li.HighPart = ft.dwHighDateTime;
108
109 // Convert to seconds and microseconds since Unix time Epoch.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200110 int64_t micros = (li.QuadPart - kFileTimeToUnixTimeEpochOffset) / 10;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000111 tv->tv_sec = static_cast<long>(micros / kNumMicrosecsPerSec); // NOLINT
112 tv->tv_usec = static_cast<long>(micros % kNumMicrosecsPerSec); // NOLINT
113
114 return 0;
115}
116
117// Emulate POSIX gmtime_r().
118static struct tm *gmtime_r(const time_t *timep, struct tm *result) {
119 // On Windows, gmtime is thread safe.
120 struct tm *tm = gmtime(timep); // NOLINT
121 if (tm == NULL) {
122 return NULL;
123 }
124 *result = *tm;
125 return result;
126}
andrew@webrtc.org6ae5a6d2014-09-16 01:03:29 +0000127#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000128
129void CurrentTmTime(struct tm *tm, int *microseconds) {
130 struct timeval timeval;
131 if (gettimeofday(&timeval, NULL) < 0) {
132 // Incredibly unlikely code path.
133 timeval.tv_sec = timeval.tv_usec = 0;
134 }
135 time_t secs = timeval.tv_sec;
136 gmtime_r(&secs, tm);
137 *microseconds = timeval.tv_usec;
138}
139
Peter Boström0c4e06b2015-10-07 12:23:21 +0200140uint32_t TimeAfter(int32_t elapsed) {
henrikg91d6ede2015-09-17 00:24:34 -0700141 RTC_DCHECK_GE(elapsed, 0);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200142 RTC_DCHECK_LT(static_cast<uint32_t>(elapsed), HALF);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000143 return Time() + elapsed;
144}
145
Peter Boström0c4e06b2015-10-07 12:23:21 +0200146bool TimeIsBetween(uint32_t earlier, uint32_t middle, uint32_t later) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000147 if (earlier <= later) {
148 return ((earlier <= middle) && (middle <= later));
149 } else {
150 return !((later < middle) && (middle < earlier));
151 }
152}
153
Peter Boström0c4e06b2015-10-07 12:23:21 +0200154bool TimeIsLaterOrEqual(uint32_t earlier, uint32_t later) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000155#if EFFICIENT_IMPLEMENTATION
Peter Boström0c4e06b2015-10-07 12:23:21 +0200156 int32_t diff = later - earlier;
157 return (diff >= 0 && static_cast<uint32_t>(diff) < HALF);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000158#else
159 const bool later_or_equal = TimeIsBetween(earlier, later, earlier + HALF);
160 return later_or_equal;
161#endif
162}
163
Peter Boström0c4e06b2015-10-07 12:23:21 +0200164bool TimeIsLater(uint32_t earlier, uint32_t later) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000165#if EFFICIENT_IMPLEMENTATION
Peter Boström0c4e06b2015-10-07 12:23:21 +0200166 int32_t diff = later - earlier;
167 return (diff > 0 && static_cast<uint32_t>(diff) < HALF);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000168#else
169 const bool earlier_or_equal = TimeIsBetween(later, earlier, later + HALF);
170 return !earlier_or_equal;
171#endif
172}
173
Peter Boström0c4e06b2015-10-07 12:23:21 +0200174int32_t TimeDiff(uint32_t later, uint32_t earlier) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000175#if EFFICIENT_IMPLEMENTATION
176 return later - earlier;
177#else
178 const bool later_or_equal = TimeIsBetween(earlier, later, earlier + HALF);
179 if (later_or_equal) {
180 if (earlier <= later) {
181 return static_cast<long>(later - earlier);
182 } else {
183 return static_cast<long>(later + (UINT32_MAX - earlier) + 1);
184 }
185 } else {
186 if (later <= earlier) {
187 return -static_cast<long>(earlier - later);
188 } else {
189 return -static_cast<long>(earlier + (UINT32_MAX - later) + 1);
190 }
191 }
192#endif
193}
194
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000195TimestampWrapAroundHandler::TimestampWrapAroundHandler()
196 : last_ts_(0), num_wrap_(0) {}
197
Peter Boström0c4e06b2015-10-07 12:23:21 +0200198int64_t TimestampWrapAroundHandler::Unwrap(uint32_t ts) {
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000199 if (ts < last_ts_) {
200 if (last_ts_ > 0xf0000000 && ts < 0x0fffffff) {
201 ++num_wrap_;
202 }
203 }
204 last_ts_ = ts;
205 int64_t unwrapped_ts = ts + (num_wrap_ << 32);
206 return unwrapped_ts;
207}
208
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100209int64_t TmToSeconds(const std::tm& tm) {
210 static short int mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
211 static short int cumul_mdays[12] = {0, 31, 59, 90, 120, 151,
212 181, 212, 243, 273, 304, 334};
213 int year = tm.tm_year + 1900;
214 int month = tm.tm_mon;
215 int day = tm.tm_mday - 1; // Make 0-based like the rest.
216 int hour = tm.tm_hour;
217 int min = tm.tm_min;
218 int sec = tm.tm_sec;
219
220 bool expiry_in_leap_year = (year % 4 == 0 &&
221 (year % 100 != 0 || year % 400 == 0));
222
223 if (year < 1970)
224 return -1;
225 if (month < 0 || month > 11)
226 return -1;
227 if (day < 0 || day >= mdays[month] + (expiry_in_leap_year && month == 2 - 1))
228 return -1;
229 if (hour < 0 || hour > 23)
230 return -1;
231 if (min < 0 || min > 59)
232 return -1;
233 if (sec < 0 || sec > 59)
234 return -1;
235
236 day += cumul_mdays[month];
237
238 // Add number of leap days between 1970 and the expiration year, inclusive.
239 day += ((year / 4 - 1970 / 4) - (year / 100 - 1970 / 100) +
240 (year / 400 - 1970 / 400));
241
242 // We will have added one day too much above if expiration is during a leap
243 // year, and expiration is in January or February.
244 if (expiry_in_leap_year && month <= 2 - 1) // |month| is zero based.
245 day -= 1;
246
247 // Combine all variables into seconds from 1970-01-01 00:00 (except |month|
248 // which was accumulated into |day| above).
249 return (((static_cast<int64_t>
250 (year - 1970) * 365 + day) * 24 + hour) * 60 + min) * 60 + sec;
251}
252
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000253} // namespace rtc