blob: dc5b611f647bec9452206de052d7e5fb69f4593c [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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>
Yves Gerey988cc082018-10-23 10:03:0117#include "rtc_base/numerics/safe_conversions.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2618#endif
19#endif
20
21#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 13:03:0522// clang-format off
23// clang formatting would put <windows.h> last,
24// which leads to compilation failure.
henrike@webrtc.orgf0488722014-05-13 18:00:2625#include <windows.h>
26#include <mmsystem.h>
nissecdf37a92016-09-14 06:41:4727#include <sys/timeb.h>
Yves Gerey665174f2018-06-19 13:03:0528// clang-format on
henrike@webrtc.orgf0488722014-05-13 18:00:2629#endif
30
Mirko Bonadei92ea95e2017-09-15 04:47:3131#include "rtc_base/checks.h"
32#include "rtc_base/timeutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2633
henrike@webrtc.orgf0488722014-05-13 18:00:2634namespace rtc {
35
Taylor Brandstetterb3c68102016-05-27 21:15:4336ClockInterface* g_clock = nullptr;
37
deadbeeff5f03e82016-06-06 18:16:0638ClockInterface* SetClockForTesting(ClockInterface* clock) {
39 ClockInterface* prev = g_clock;
Taylor Brandstetterb3c68102016-05-27 21:15:4340 g_clock = clock;
deadbeeff5f03e82016-06-06 18:16:0641 return prev;
Taylor Brandstetterb3c68102016-05-27 21:15:4342}
43
deadbeef22e08142017-06-12 21:30:2844ClockInterface* GetClockForTesting() {
45 return g_clock;
46}
47
nissedeb95f32016-11-28 09:54:5448int64_t SystemTimeNanos() {
Taylor Brandstetterb3c68102016-05-27 21:15:4349 int64_t ticks;
henrike@webrtc.orgf0488722014-05-13 18:00:2650#if defined(WEBRTC_MAC)
51 static mach_timebase_info_data_t timebase;
52 if (timebase.denom == 0) {
53 // Get the timebase if this is the first time we run.
54 // Recommended by Apple's QA1398.
andrew@webrtc.org6ae5a6d2014-09-16 01:03:2955 if (mach_timebase_info(&timebase) != KERN_SUCCESS) {
nisseeb4ca4e2017-01-12 10:24:2756 RTC_NOTREACHED();
andrew@webrtc.org6ae5a6d2014-09-16 01:03:2957 }
henrike@webrtc.orgf0488722014-05-13 18:00:2658 }
59 // Use timebase to convert absolute time tick units into nanoseconds.
Karl Wiberge0269cd2018-02-26 22:44:1960 const auto mul = [](uint64_t a, uint32_t b) -> int64_t {
61 RTC_DCHECK_NE(b, 0);
62 RTC_DCHECK_LE(a, std::numeric_limits<int64_t>::max() / b)
63 << "The multiplication " << a << " * " << b << " overflows";
64 return rtc::dchecked_cast<int64_t>(a * b);
65 };
66 ticks = mul(mach_absolute_time(), timebase.numer) / timebase.denom;
henrike@webrtc.orgf0488722014-05-13 18:00:2667#elif defined(WEBRTC_POSIX)
68 struct timespec ts;
Taylor Brandstetterb3c68102016-05-27 21:15:4369 // TODO(deadbeef): Do we need to handle the case when CLOCK_MONOTONIC is not
70 // supported?
henrike@webrtc.orgf0488722014-05-13 18:00:2671 clock_gettime(CLOCK_MONOTONIC, &ts);
Peter Boström0c4e06b2015-10-07 10:23:2172 ticks = kNumNanosecsPerSec * static_cast<int64_t>(ts.tv_sec) +
73 static_cast<int64_t>(ts.tv_nsec);
henrike@webrtc.orgf0488722014-05-13 18:00:2674#elif defined(WEBRTC_WIN)
75 static volatile LONG last_timegettime = 0;
Peter Boström0c4e06b2015-10-07 10:23:2176 static volatile int64_t num_wrap_timegettime = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:2677 volatile LONG* last_timegettime_ptr = &last_timegettime;
78 DWORD now = timeGetTime();
79 // Atomically update the last gotten time
80 DWORD old = InterlockedExchange(last_timegettime_ptr, now);
81 if (now < old) {
Taylor Brandstetterb3c68102016-05-27 21:15:4382 // If now is earlier than old, there may have been a race between threads.
henrike@webrtc.orgf0488722014-05-13 18:00:2683 // 0x0fffffff ~3.1 days, the code will not take that long to execute
84 // so it must have been a wrap around.
85 if (old > 0xf0000000 && now < 0x0fffffff) {
86 num_wrap_timegettime++;
87 }
88 }
89 ticks = now + (num_wrap_timegettime << 32);
Taylor Brandstetterb3c68102016-05-27 21:15:4390 // TODO(deadbeef): Calculate with nanosecond precision. Otherwise, we're
91 // just wasting a multiply and divide when doing Time() on Windows.
henrike@webrtc.orgf0488722014-05-13 18:00:2692 ticks = ticks * kNumNanosecsPerMillisec;
Erik Språng1c390982016-01-27 11:55:3393#else
94#error Unsupported platform.
henrike@webrtc.orgf0488722014-05-13 18:00:2695#endif
96 return ticks;
97}
98
Taylor Brandstetter4f0dfbd2016-06-16 00:15:2399int64_t SystemTimeMillis() {
100 return static_cast<int64_t>(SystemTimeNanos() / kNumNanosecsPerMillisec);
101}
102
nissedeb95f32016-11-28 09:54:54103int64_t TimeNanos() {
Taylor Brandstetter4f0dfbd2016-06-16 00:15:23104 if (g_clock) {
105 return g_clock->TimeNanos();
106 }
107 return SystemTimeNanos();
108}
109
honghaiz34b11eb2016-03-16 15:55:44110uint32_t Time32() {
Peter Boström0c4e06b2015-10-07 10:23:21111 return static_cast<uint32_t>(TimeNanos() / kNumNanosecsPerMillisec);
henrike@webrtc.orgf0488722014-05-13 18:00:26112}
113
nisse1bffc1d2016-05-02 15:18:55114int64_t TimeMillis() {
nissedeb95f32016-11-28 09:54:54115 return TimeNanos() / kNumNanosecsPerMillisec;
honghaiz34b11eb2016-03-16 15:55:44116}
117
nissedeb95f32016-11-28 09:54:54118int64_t TimeMicros() {
119 return TimeNanos() / kNumNanosecsPerMicrosec;
henrike@webrtc.orgf0488722014-05-13 18:00:26120}
121
Honghai Zhang82d78622016-05-06 18:29:15122int64_t TimeAfter(int64_t elapsed) {
henrikg91d6ede2015-09-17 07:24:34123 RTC_DCHECK_GE(elapsed, 0);
Honghai Zhang82d78622016-05-06 18:29:15124 return TimeMillis() + elapsed;
henrike@webrtc.orgf0488722014-05-13 18:00:26125}
126
Honghai Zhang82d78622016-05-06 18:29:15127int32_t TimeDiff32(uint32_t later, uint32_t earlier) {
henrike@webrtc.orgf0488722014-05-13 18:00:26128 return later - earlier;
henrike@webrtc.orgf0488722014-05-13 18:00:26129}
130
Honghai Zhang82d78622016-05-06 18:29:15131int64_t TimeDiff(int64_t later, int64_t earlier) {
honghaiz34b11eb2016-03-16 15:55:44132 return later - earlier;
133}
134
henrike@webrtc.org99b41622014-05-21 20:42:17135TimestampWrapAroundHandler::TimestampWrapAroundHandler()
sprang1b3530b2016-03-10 09:32:53136 : last_ts_(0), num_wrap_(-1) {}
henrike@webrtc.org99b41622014-05-21 20:42:17137
Peter Boström0c4e06b2015-10-07 10:23:21138int64_t TimestampWrapAroundHandler::Unwrap(uint32_t ts) {
sprang1b3530b2016-03-10 09:32:53139 if (num_wrap_ == -1) {
140 last_ts_ = ts;
141 num_wrap_ = 0;
142 return ts;
henrike@webrtc.org99b41622014-05-21 20:42:17143 }
sprang1b3530b2016-03-10 09:32:53144
145 if (ts < last_ts_) {
146 if (last_ts_ >= 0xf0000000 && ts < 0x0fffffff)
147 ++num_wrap_;
148 } else if ((ts - last_ts_) > 0xf0000000) {
149 // Backwards wrap. Unwrap with last wrap count and don't update last_ts_.
150 return ts + ((num_wrap_ - 1) << 32);
151 }
152
henrike@webrtc.org99b41622014-05-21 20:42:17153 last_ts_ = ts;
sprang1b3530b2016-03-10 09:32:53154 return ts + (num_wrap_ << 32);
henrike@webrtc.org99b41622014-05-21 20:42:17155}
156
Yves Gerey988cc082018-10-23 10:03:01157int64_t TmToSeconds(const tm& tm) {
Torbjorn Granlund46c9cc02015-12-01 12:06:34158 static short int mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
159 static short int cumul_mdays[12] = {0, 31, 59, 90, 120, 151,
160 181, 212, 243, 273, 304, 334};
161 int year = tm.tm_year + 1900;
162 int month = tm.tm_mon;
163 int day = tm.tm_mday - 1; // Make 0-based like the rest.
164 int hour = tm.tm_hour;
165 int min = tm.tm_min;
166 int sec = tm.tm_sec;
167
Yves Gerey665174f2018-06-19 13:03:05168 bool expiry_in_leap_year =
169 (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
Torbjorn Granlund46c9cc02015-12-01 12:06:34170
171 if (year < 1970)
172 return -1;
173 if (month < 0 || month > 11)
174 return -1;
175 if (day < 0 || day >= mdays[month] + (expiry_in_leap_year && month == 2 - 1))
176 return -1;
177 if (hour < 0 || hour > 23)
178 return -1;
179 if (min < 0 || min > 59)
180 return -1;
181 if (sec < 0 || sec > 59)
182 return -1;
183
184 day += cumul_mdays[month];
185
186 // Add number of leap days between 1970 and the expiration year, inclusive.
187 day += ((year / 4 - 1970 / 4) - (year / 100 - 1970 / 100) +
188 (year / 400 - 1970 / 400));
189
190 // We will have added one day too much above if expiration is during a leap
191 // year, and expiration is in January or February.
Yves Gerey665174f2018-06-19 13:03:05192 if (expiry_in_leap_year && month <= 2 - 1) // |month| is zero based.
Torbjorn Granlund46c9cc02015-12-01 12:06:34193 day -= 1;
194
195 // Combine all variables into seconds from 1970-01-01 00:00 (except |month|
196 // which was accumulated into |day| above).
Yves Gerey665174f2018-06-19 13:03:05197 return (((static_cast<int64_t>(year - 1970) * 365 + day) * 24 + hour) * 60 +
198 min) *
199 60 +
200 sec;
Torbjorn Granlund46c9cc02015-12-01 12:06:34201}
202
nissecdf37a92016-09-14 06:41:47203int64_t TimeUTCMicros() {
Minyue Li656d6092018-08-10 13:38:52204 if (g_clock) {
205 return g_clock->TimeNanos() / kNumNanosecsPerMicrosec;
206 }
nissecdf37a92016-09-14 06:41:47207#if defined(WEBRTC_POSIX)
208 struct timeval time;
deadbeef37f5ecf2017-02-27 22:06:41209 gettimeofday(&time, nullptr);
nissecdf37a92016-09-14 06:41:47210 // Convert from second (1.0) and microsecond (1e-6).
211 return (static_cast<int64_t>(time.tv_sec) * rtc::kNumMicrosecsPerSec +
212 time.tv_usec);
213
214#elif defined(WEBRTC_WIN)
215 struct _timeb time;
216 _ftime(&time);
217 // Convert from second (1.0) and milliseconds (1e-3).
218 return (static_cast<int64_t>(time.time) * rtc::kNumMicrosecsPerSec +
219 static_cast<int64_t>(time.millitm) * rtc::kNumMicrosecsPerMillisec);
220#endif
221}
222
Minyue Li656d6092018-08-10 13:38:52223int64_t TimeUTCMillis() {
224 return TimeUTCMicros() / kNumMicrosecsPerMillisec;
225}
226
Yves Gerey665174f2018-06-19 13:03:05227} // namespace rtc