blob: f54aef420bab0c695417b2e0941ec1e416b27ad9 [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>
henrike@webrtc.orgf0488722014-05-13 18:00:2615#endif
16
17#if defined(WEBRTC_WIN)
nissecdf37a92016-09-14 06:41:4718#include <sys/timeb.h>
henrike@webrtc.orgf0488722014-05-13 18:00:2619#endif
20
Mirko Bonadei92ea95e2017-09-15 04:47:3121#include "rtc_base/checks.h"
Jerome Humbert081f7a32019-12-11 21:10:0522#include "rtc_base/numerics/safe_conversions.h"
Johannes Kronb73c9f02021-02-15 12:29:4523#include "rtc_base/system_time.h"
Steve Anton10542f22019-01-11 17:11:0024#include "rtc_base/time_utils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2625
henrike@webrtc.orgf0488722014-05-13 18:00:2626namespace rtc {
27
Taylor Brandstetterb3c68102016-05-27 21:15:4328ClockInterface* g_clock = nullptr;
29
deadbeeff5f03e82016-06-06 18:16:0630ClockInterface* SetClockForTesting(ClockInterface* clock) {
31 ClockInterface* prev = g_clock;
Taylor Brandstetterb3c68102016-05-27 21:15:4332 g_clock = clock;
deadbeeff5f03e82016-06-06 18:16:0633 return prev;
Taylor Brandstetterb3c68102016-05-27 21:15:4334}
35
deadbeef22e08142017-06-12 21:30:2836ClockInterface* GetClockForTesting() {
37 return g_clock;
38}
39
Robin Raymondce1b1402018-11-23 01:10:1140#if defined(WINUWP)
41
42namespace {
43
44class TimeHelper final {
45 public:
46 TimeHelper(const TimeHelper&) = delete;
47
48 // Resets the clock based upon an NTP server. This routine must be called
49 // prior to the main system start-up to ensure all clocks are based upon
50 // an NTP server time if NTP synchronization is required. No critical
51 // section is used thus this method must be called prior to any clock
52 // routines being used.
53 static void SyncWithNtp(int64_t ntp_server_time_ms) {
54 auto& singleton = Singleton();
55 TIME_ZONE_INFORMATION time_zone;
56 GetTimeZoneInformation(&time_zone);
57 int64_t time_zone_bias_ns =
58 rtc::dchecked_cast<int64_t>(time_zone.Bias) * 60 * 1000 * 1000 * 1000;
59 singleton.app_start_time_ns_ =
60 (ntp_server_time_ms - kNTPTimeToUnixTimeEpochOffset) * 1000000 -
61 time_zone_bias_ns;
62 singleton.UpdateReferenceTime();
63 }
64
65 // Returns the number of nanoseconds that have passed since unix epoch.
66 static int64_t TicksNs() {
67 auto& singleton = Singleton();
68 int64_t result = 0;
69 LARGE_INTEGER qpcnt;
70 QueryPerformanceCounter(&qpcnt);
71 result = rtc::dchecked_cast<int64_t>(
72 (rtc::dchecked_cast<uint64_t>(qpcnt.QuadPart) * 100000 /
73 rtc::dchecked_cast<uint64_t>(singleton.os_ticks_per_second_)) *
74 10000);
75 result = singleton.app_start_time_ns_ + result -
76 singleton.time_since_os_start_ns_;
77 return result;
78 }
79
80 private:
81 TimeHelper() {
82 TIME_ZONE_INFORMATION time_zone;
83 GetTimeZoneInformation(&time_zone);
84 int64_t time_zone_bias_ns =
85 rtc::dchecked_cast<int64_t>(time_zone.Bias) * 60 * 1000 * 1000 * 1000;
86 FILETIME ft;
87 // This will give us system file in UTC format.
88 GetSystemTimeAsFileTime(&ft);
89 LARGE_INTEGER li;
90 li.HighPart = ft.dwHighDateTime;
91 li.LowPart = ft.dwLowDateTime;
92
93 app_start_time_ns_ = (li.QuadPart - kFileTimeToUnixTimeEpochOffset) * 100 -
94 time_zone_bias_ns;
95
96 UpdateReferenceTime();
97 }
98
99 static TimeHelper& Singleton() {
100 static TimeHelper singleton;
101 return singleton;
102 }
103
104 void UpdateReferenceTime() {
105 LARGE_INTEGER qpfreq;
106 QueryPerformanceFrequency(&qpfreq);
107 os_ticks_per_second_ = rtc::dchecked_cast<int64_t>(qpfreq.QuadPart);
108
109 LARGE_INTEGER qpcnt;
110 QueryPerformanceCounter(&qpcnt);
111 time_since_os_start_ns_ = rtc::dchecked_cast<int64_t>(
112 (rtc::dchecked_cast<uint64_t>(qpcnt.QuadPart) * 100000 /
113 rtc::dchecked_cast<uint64_t>(os_ticks_per_second_)) *
114 10000);
115 }
116
117 private:
118 static constexpr uint64_t kFileTimeToUnixTimeEpochOffset =
119 116444736000000000ULL;
120 static constexpr uint64_t kNTPTimeToUnixTimeEpochOffset = 2208988800000L;
121
122 // The number of nanoseconds since unix system epoch
123 int64_t app_start_time_ns_;
124 // The number of nanoseconds since the OS started
125 int64_t time_since_os_start_ns_;
126 // The OS calculated ticks per second
127 int64_t os_ticks_per_second_;
128};
129
130} // namespace
131
132void SyncWithNtp(int64_t time_from_ntp_server_ms) {
133 TimeHelper::SyncWithNtp(time_from_ntp_server_ms);
134}
135
136#endif // defined(WINUWP)
137
Taylor Brandstetter4f0dfbd2016-06-16 00:15:23138int64_t SystemTimeMillis() {
139 return static_cast<int64_t>(SystemTimeNanos() / kNumNanosecsPerMillisec);
140}
141
nissedeb95f32016-11-28 09:54:54142int64_t TimeNanos() {
Taylor Brandstetter4f0dfbd2016-06-16 00:15:23143 if (g_clock) {
144 return g_clock->TimeNanos();
145 }
146 return SystemTimeNanos();
147}
148
honghaiz34b11eb2016-03-16 15:55:44149uint32_t Time32() {
Peter Boström0c4e06b2015-10-07 10:23:21150 return static_cast<uint32_t>(TimeNanos() / kNumNanosecsPerMillisec);
henrike@webrtc.orgf0488722014-05-13 18:00:26151}
152
nisse1bffc1d2016-05-02 15:18:55153int64_t TimeMillis() {
nissedeb95f32016-11-28 09:54:54154 return TimeNanos() / kNumNanosecsPerMillisec;
honghaiz34b11eb2016-03-16 15:55:44155}
156
nissedeb95f32016-11-28 09:54:54157int64_t TimeMicros() {
158 return TimeNanos() / kNumNanosecsPerMicrosec;
henrike@webrtc.orgf0488722014-05-13 18:00:26159}
160
Honghai Zhang82d78622016-05-06 18:29:15161int64_t TimeAfter(int64_t elapsed) {
henrikg91d6ede2015-09-17 07:24:34162 RTC_DCHECK_GE(elapsed, 0);
Honghai Zhang82d78622016-05-06 18:29:15163 return TimeMillis() + elapsed;
henrike@webrtc.orgf0488722014-05-13 18:00:26164}
165
Honghai Zhang82d78622016-05-06 18:29:15166int32_t TimeDiff32(uint32_t later, uint32_t earlier) {
henrike@webrtc.orgf0488722014-05-13 18:00:26167 return later - earlier;
henrike@webrtc.orgf0488722014-05-13 18:00:26168}
169
Honghai Zhang82d78622016-05-06 18:29:15170int64_t TimeDiff(int64_t later, int64_t earlier) {
honghaiz34b11eb2016-03-16 15:55:44171 return later - earlier;
172}
173
henrike@webrtc.org99b41622014-05-21 20:42:17174TimestampWrapAroundHandler::TimestampWrapAroundHandler()
sprang1b3530b2016-03-10 09:32:53175 : last_ts_(0), num_wrap_(-1) {}
henrike@webrtc.org99b41622014-05-21 20:42:17176
Peter Boström0c4e06b2015-10-07 10:23:21177int64_t TimestampWrapAroundHandler::Unwrap(uint32_t ts) {
sprang1b3530b2016-03-10 09:32:53178 if (num_wrap_ == -1) {
179 last_ts_ = ts;
180 num_wrap_ = 0;
181 return ts;
henrike@webrtc.org99b41622014-05-21 20:42:17182 }
sprang1b3530b2016-03-10 09:32:53183
184 if (ts < last_ts_) {
185 if (last_ts_ >= 0xf0000000 && ts < 0x0fffffff)
186 ++num_wrap_;
187 } else if ((ts - last_ts_) > 0xf0000000) {
188 // Backwards wrap. Unwrap with last wrap count and don't update last_ts_.
Dan Minor27398d62020-07-01 16:16:16189 return ts + (num_wrap_ - 1) * (int64_t{1} << 32);
sprang1b3530b2016-03-10 09:32:53190 }
191
henrike@webrtc.org99b41622014-05-21 20:42:17192 last_ts_ = ts;
sprang1b3530b2016-03-10 09:32:53193 return ts + (num_wrap_ << 32);
henrike@webrtc.org99b41622014-05-21 20:42:17194}
195
Yves Gerey988cc082018-10-23 10:03:01196int64_t TmToSeconds(const tm& tm) {
Torbjorn Granlund46c9cc02015-12-01 12:06:34197 static short int mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
198 static short int cumul_mdays[12] = {0, 31, 59, 90, 120, 151,
199 181, 212, 243, 273, 304, 334};
200 int year = tm.tm_year + 1900;
201 int month = tm.tm_mon;
202 int day = tm.tm_mday - 1; // Make 0-based like the rest.
203 int hour = tm.tm_hour;
204 int min = tm.tm_min;
205 int sec = tm.tm_sec;
206
Yves Gerey665174f2018-06-19 13:03:05207 bool expiry_in_leap_year =
208 (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
Torbjorn Granlund46c9cc02015-12-01 12:06:34209
210 if (year < 1970)
211 return -1;
212 if (month < 0 || month > 11)
213 return -1;
214 if (day < 0 || day >= mdays[month] + (expiry_in_leap_year && month == 2 - 1))
215 return -1;
216 if (hour < 0 || hour > 23)
217 return -1;
218 if (min < 0 || min > 59)
219 return -1;
220 if (sec < 0 || sec > 59)
221 return -1;
222
223 day += cumul_mdays[month];
224
225 // Add number of leap days between 1970 and the expiration year, inclusive.
226 day += ((year / 4 - 1970 / 4) - (year / 100 - 1970 / 100) +
227 (year / 400 - 1970 / 400));
228
229 // We will have added one day too much above if expiration is during a leap
230 // year, and expiration is in January or February.
Yves Gerey665174f2018-06-19 13:03:05231 if (expiry_in_leap_year && month <= 2 - 1) // |month| is zero based.
Torbjorn Granlund46c9cc02015-12-01 12:06:34232 day -= 1;
233
234 // Combine all variables into seconds from 1970-01-01 00:00 (except |month|
235 // which was accumulated into |day| above).
Yves Gerey665174f2018-06-19 13:03:05236 return (((static_cast<int64_t>(year - 1970) * 365 + day) * 24 + hour) * 60 +
237 min) *
238 60 +
239 sec;
Torbjorn Granlund46c9cc02015-12-01 12:06:34240}
241
nissecdf37a92016-09-14 06:41:47242int64_t TimeUTCMicros() {
Minyue Li656d6092018-08-10 13:38:52243 if (g_clock) {
244 return g_clock->TimeNanos() / kNumNanosecsPerMicrosec;
245 }
nissecdf37a92016-09-14 06:41:47246#if defined(WEBRTC_POSIX)
247 struct timeval time;
deadbeef37f5ecf2017-02-27 22:06:41248 gettimeofday(&time, nullptr);
nissecdf37a92016-09-14 06:41:47249 // Convert from second (1.0) and microsecond (1e-6).
250 return (static_cast<int64_t>(time.tv_sec) * rtc::kNumMicrosecsPerSec +
251 time.tv_usec);
252
253#elif defined(WEBRTC_WIN)
254 struct _timeb time;
255 _ftime(&time);
256 // Convert from second (1.0) and milliseconds (1e-3).
257 return (static_cast<int64_t>(time.time) * rtc::kNumMicrosecsPerSec +
258 static_cast<int64_t>(time.millitm) * rtc::kNumMicrosecsPerMillisec);
259#endif
260}
261
Minyue Li656d6092018-08-10 13:38:52262int64_t TimeUTCMillis() {
263 return TimeUTCMicros() / kNumMicrosecsPerMillisec;
264}
265
Yves Gerey665174f2018-06-19 13:03:05266} // namespace rtc