blob: 9f112e49c189e32a988eb5683e3cf461041aeba1 [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
Mirko Bonadei92ea95e2017-09-15 04:47:3117#include "rtc_base/checks.h"
Jerome Humbert081f7a32019-12-11 21:10:0518#include "rtc_base/numerics/safe_conversions.h"
Johannes Kronb73c9f02021-02-15 12:29:4519#include "rtc_base/system_time.h"
Steve Anton10542f22019-01-11 17:11:0020#include "rtc_base/time_utils.h"
Philipp Hancke3adaeef2023-06-27 11:08:4421#if defined(WEBRTC_WIN)
22#include "rtc_base/win32.h"
23#endif
24#if defined(WEBRTC_WIN)
25#include <minwinbase.h>
26#endif
henrike@webrtc.orgf0488722014-05-13 18:00:2627
henrike@webrtc.orgf0488722014-05-13 18:00:2628namespace rtc {
29
Philipp Hancke3adaeef2023-06-27 11:08:4430#if defined(WEBRTC_WIN) || defined(WINUWP)
31// FileTime (January 1st 1601) to Unix time (January 1st 1970)
32// offset in units of 100ns.
33static constexpr uint64_t kFileTimeToUnixTimeEpochOffset =
34 116444736000000000ULL;
35static constexpr uint64_t kFileTimeToMicroSeconds = 10LL;
36#endif
37
Taylor Brandstetterb3c68102016-05-27 21:15:4338ClockInterface* g_clock = nullptr;
39
deadbeeff5f03e82016-06-06 18:16:0640ClockInterface* SetClockForTesting(ClockInterface* clock) {
41 ClockInterface* prev = g_clock;
Taylor Brandstetterb3c68102016-05-27 21:15:4342 g_clock = clock;
deadbeeff5f03e82016-06-06 18:16:0643 return prev;
Taylor Brandstetterb3c68102016-05-27 21:15:4344}
45
deadbeef22e08142017-06-12 21:30:2846ClockInterface* GetClockForTesting() {
47 return g_clock;
48}
49
Robin Raymondce1b1402018-11-23 01:10:1150#if defined(WINUWP)
51
52namespace {
53
54class TimeHelper final {
55 public:
56 TimeHelper(const TimeHelper&) = delete;
57
58 // Resets the clock based upon an NTP server. This routine must be called
59 // prior to the main system start-up to ensure all clocks are based upon
60 // an NTP server time if NTP synchronization is required. No critical
61 // section is used thus this method must be called prior to any clock
62 // routines being used.
63 static void SyncWithNtp(int64_t ntp_server_time_ms) {
64 auto& singleton = Singleton();
65 TIME_ZONE_INFORMATION time_zone;
66 GetTimeZoneInformation(&time_zone);
67 int64_t time_zone_bias_ns =
68 rtc::dchecked_cast<int64_t>(time_zone.Bias) * 60 * 1000 * 1000 * 1000;
69 singleton.app_start_time_ns_ =
70 (ntp_server_time_ms - kNTPTimeToUnixTimeEpochOffset) * 1000000 -
71 time_zone_bias_ns;
72 singleton.UpdateReferenceTime();
73 }
74
75 // Returns the number of nanoseconds that have passed since unix epoch.
76 static int64_t TicksNs() {
77 auto& singleton = Singleton();
78 int64_t result = 0;
79 LARGE_INTEGER qpcnt;
80 QueryPerformanceCounter(&qpcnt);
81 result = rtc::dchecked_cast<int64_t>(
82 (rtc::dchecked_cast<uint64_t>(qpcnt.QuadPart) * 100000 /
83 rtc::dchecked_cast<uint64_t>(singleton.os_ticks_per_second_)) *
84 10000);
85 result = singleton.app_start_time_ns_ + result -
86 singleton.time_since_os_start_ns_;
87 return result;
88 }
89
90 private:
91 TimeHelper() {
92 TIME_ZONE_INFORMATION time_zone;
93 GetTimeZoneInformation(&time_zone);
94 int64_t time_zone_bias_ns =
95 rtc::dchecked_cast<int64_t>(time_zone.Bias) * 60 * 1000 * 1000 * 1000;
96 FILETIME ft;
97 // This will give us system file in UTC format.
98 GetSystemTimeAsFileTime(&ft);
99 LARGE_INTEGER li;
100 li.HighPart = ft.dwHighDateTime;
101 li.LowPart = ft.dwLowDateTime;
102
103 app_start_time_ns_ = (li.QuadPart - kFileTimeToUnixTimeEpochOffset) * 100 -
104 time_zone_bias_ns;
105
106 UpdateReferenceTime();
107 }
108
109 static TimeHelper& Singleton() {
110 static TimeHelper singleton;
111 return singleton;
112 }
113
114 void UpdateReferenceTime() {
115 LARGE_INTEGER qpfreq;
116 QueryPerformanceFrequency(&qpfreq);
117 os_ticks_per_second_ = rtc::dchecked_cast<int64_t>(qpfreq.QuadPart);
118
119 LARGE_INTEGER qpcnt;
120 QueryPerformanceCounter(&qpcnt);
121 time_since_os_start_ns_ = rtc::dchecked_cast<int64_t>(
122 (rtc::dchecked_cast<uint64_t>(qpcnt.QuadPart) * 100000 /
123 rtc::dchecked_cast<uint64_t>(os_ticks_per_second_)) *
124 10000);
125 }
126
127 private:
Robin Raymondce1b1402018-11-23 01:10:11128 static constexpr uint64_t kNTPTimeToUnixTimeEpochOffset = 2208988800000L;
129
130 // The number of nanoseconds since unix system epoch
131 int64_t app_start_time_ns_;
132 // The number of nanoseconds since the OS started
133 int64_t time_since_os_start_ns_;
134 // The OS calculated ticks per second
135 int64_t os_ticks_per_second_;
136};
137
138} // namespace
139
140void SyncWithNtp(int64_t time_from_ntp_server_ms) {
141 TimeHelper::SyncWithNtp(time_from_ntp_server_ms);
142}
143
Johannes Kron373bb7b2021-02-23 13:23:47144int64_t WinUwpSystemTimeNanos() {
145 return TimeHelper::TicksNs();
146}
147
Robin Raymondce1b1402018-11-23 01:10:11148#endif // defined(WINUWP)
149
Taylor Brandstetter4f0dfbd2016-06-16 00:15:23150int64_t SystemTimeMillis() {
151 return static_cast<int64_t>(SystemTimeNanos() / kNumNanosecsPerMillisec);
152}
153
nissedeb95f32016-11-28 09:54:54154int64_t TimeNanos() {
Taylor Brandstetter4f0dfbd2016-06-16 00:15:23155 if (g_clock) {
156 return g_clock->TimeNanos();
157 }
158 return SystemTimeNanos();
159}
160
honghaiz34b11eb2016-03-16 15:55:44161uint32_t Time32() {
Peter Boström0c4e06b2015-10-07 10:23:21162 return static_cast<uint32_t>(TimeNanos() / kNumNanosecsPerMillisec);
henrike@webrtc.orgf0488722014-05-13 18:00:26163}
164
nisse1bffc1d2016-05-02 15:18:55165int64_t TimeMillis() {
nissedeb95f32016-11-28 09:54:54166 return TimeNanos() / kNumNanosecsPerMillisec;
honghaiz34b11eb2016-03-16 15:55:44167}
168
nissedeb95f32016-11-28 09:54:54169int64_t TimeMicros() {
170 return TimeNanos() / kNumNanosecsPerMicrosec;
henrike@webrtc.orgf0488722014-05-13 18:00:26171}
172
Honghai Zhang82d78622016-05-06 18:29:15173int64_t TimeAfter(int64_t elapsed) {
henrikg91d6ede2015-09-17 07:24:34174 RTC_DCHECK_GE(elapsed, 0);
Honghai Zhang82d78622016-05-06 18:29:15175 return TimeMillis() + elapsed;
henrike@webrtc.orgf0488722014-05-13 18:00:26176}
177
Honghai Zhang82d78622016-05-06 18:29:15178int32_t TimeDiff32(uint32_t later, uint32_t earlier) {
henrike@webrtc.orgf0488722014-05-13 18:00:26179 return later - earlier;
henrike@webrtc.orgf0488722014-05-13 18:00:26180}
181
Honghai Zhang82d78622016-05-06 18:29:15182int64_t TimeDiff(int64_t later, int64_t earlier) {
honghaiz34b11eb2016-03-16 15:55:44183 return later - earlier;
184}
185
Yves Gerey988cc082018-10-23 10:03:01186int64_t TmToSeconds(const tm& tm) {
Torbjorn Granlund46c9cc02015-12-01 12:06:34187 static short int mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
188 static short int cumul_mdays[12] = {0, 31, 59, 90, 120, 151,
189 181, 212, 243, 273, 304, 334};
190 int year = tm.tm_year + 1900;
191 int month = tm.tm_mon;
192 int day = tm.tm_mday - 1; // Make 0-based like the rest.
193 int hour = tm.tm_hour;
194 int min = tm.tm_min;
195 int sec = tm.tm_sec;
196
Yves Gerey665174f2018-06-19 13:03:05197 bool expiry_in_leap_year =
198 (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
Torbjorn Granlund46c9cc02015-12-01 12:06:34199
200 if (year < 1970)
201 return -1;
202 if (month < 0 || month > 11)
203 return -1;
204 if (day < 0 || day >= mdays[month] + (expiry_in_leap_year && month == 2 - 1))
205 return -1;
206 if (hour < 0 || hour > 23)
207 return -1;
208 if (min < 0 || min > 59)
209 return -1;
210 if (sec < 0 || sec > 59)
211 return -1;
212
213 day += cumul_mdays[month];
214
215 // Add number of leap days between 1970 and the expiration year, inclusive.
216 day += ((year / 4 - 1970 / 4) - (year / 100 - 1970 / 100) +
217 (year / 400 - 1970 / 400));
218
219 // We will have added one day too much above if expiration is during a leap
220 // year, and expiration is in January or February.
Artem Titov96e3b992021-07-26 14:03:14221 if (expiry_in_leap_year && month <= 2 - 1) // `month` is zero based.
Torbjorn Granlund46c9cc02015-12-01 12:06:34222 day -= 1;
223
Artem Titov96e3b992021-07-26 14:03:14224 // Combine all variables into seconds from 1970-01-01 00:00 (except `month`
225 // which was accumulated into `day` above).
Yves Gerey665174f2018-06-19 13:03:05226 return (((static_cast<int64_t>(year - 1970) * 365 + day) * 24 + hour) * 60 +
227 min) *
228 60 +
229 sec;
Torbjorn Granlund46c9cc02015-12-01 12:06:34230}
231
nissecdf37a92016-09-14 06:41:47232int64_t TimeUTCMicros() {
Minyue Li656d6092018-08-10 13:38:52233 if (g_clock) {
234 return g_clock->TimeNanos() / kNumNanosecsPerMicrosec;
235 }
nissecdf37a92016-09-14 06:41:47236#if defined(WEBRTC_POSIX)
237 struct timeval time;
deadbeef37f5ecf2017-02-27 22:06:41238 gettimeofday(&time, nullptr);
nissecdf37a92016-09-14 06:41:47239 // Convert from second (1.0) and microsecond (1e-6).
240 return (static_cast<int64_t>(time.tv_sec) * rtc::kNumMicrosecsPerSec +
241 time.tv_usec);
nissecdf37a92016-09-14 06:41:47242#elif defined(WEBRTC_WIN)
Philipp Hancke3adaeef2023-06-27 11:08:44243 FILETIME ft;
244 // This will give us system file in UTC format in multiples of 100ns.
245 GetSystemTimeAsFileTime(&ft);
246 LARGE_INTEGER li;
247 li.HighPart = ft.dwHighDateTime;
248 li.LowPart = ft.dwLowDateTime;
249 return (li.QuadPart - kFileTimeToUnixTimeEpochOffset) /
250 kFileTimeToMicroSeconds;
nissecdf37a92016-09-14 06:41:47251#endif
252}
253
Minyue Li656d6092018-08-10 13:38:52254int64_t TimeUTCMillis() {
255 return TimeUTCMicros() / kNumMicrosecsPerMillisec;
256}
257
Yves Gerey665174f2018-06-19 13:03:05258} // namespace rtc