blob: 271c1d6e938e623ec1296b11f792c62ceea69669 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2005 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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_TIME_UTILS_H_
12#define RTC_BASE_TIME_UTILS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <stdint.h>
15#include <time.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020018#include "rtc_base/system/rtc_export.h"
Johannes Kronb73c9f02021-02-15 13:29:45 +010019#include "rtc_base/system_time.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020020
21namespace rtc {
22
23static const int64_t kNumMillisecsPerSec = INT64_C(1000);
24static const int64_t kNumMicrosecsPerSec = INT64_C(1000000);
25static const int64_t kNumNanosecsPerSec = INT64_C(1000000000);
26
27static const int64_t kNumMicrosecsPerMillisec =
28 kNumMicrosecsPerSec / kNumMillisecsPerSec;
29static const int64_t kNumNanosecsPerMillisec =
30 kNumNanosecsPerSec / kNumMillisecsPerSec;
31static const int64_t kNumNanosecsPerMicrosec =
32 kNumNanosecsPerSec / kNumMicrosecsPerSec;
33
Nico Grunbaum7eac6ca2022-01-24 13:49:58 -080034// Elapsed milliseconds between NTP base, 1900 January 1 00:00 GMT
35// (see https://tools.ietf.org/html/rfc868), and January 1 00:00 GMT 1970
36// epoch. This is useful when converting between the NTP time base and the
37// time base used in RTCP reports.
38constexpr int64_t kNtpJan1970Millisecs = 2'208'988'800 * kNumMillisecsPerSec;
39
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020040// TODO(honghaiz): Define a type for the time value specifically.
41
42class ClockInterface {
43 public:
44 virtual ~ClockInterface() {}
45 virtual int64_t TimeNanos() const = 0;
46};
47
48// Sets the global source of time. This is useful mainly for unit tests.
49//
50// Returns the previously set ClockInterface, or nullptr if none is set.
51//
52// Does not transfer ownership of the clock. SetClockForTesting(nullptr)
53// should be called before the ClockInterface is deleted.
54//
55// This method is not thread-safe; it should only be used when no other thread
56// is running (for example, at the start/end of a unit test, or start/end of
57// main()).
58//
59// TODO(deadbeef): Instead of having functions that access this global
60// ClockInterface, we may want to pass the ClockInterface into everything
61// that uses it, eliminating the need for a global variable and this function.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020062RTC_EXPORT ClockInterface* SetClockForTesting(ClockInterface* clock);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020063
64// Returns previously set clock, or nullptr if no custom clock is being used.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020065RTC_EXPORT ClockInterface* GetClockForTesting();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020066
Robin Raymondce1b1402018-11-22 20:10:11 -050067#if defined(WINUWP)
68// Synchronizes the current clock based upon an NTP server's epoch in
69// milliseconds.
70void SyncWithNtp(int64_t time_from_ntp_server_ms);
Johannes Kron373bb7b2021-02-23 14:23:47 +010071
72// Returns the current time in nanoseconds. The clock is synchonized with the
73// system wall clock time upon instatiation. It may also be synchronized using
74// the SyncWithNtp() function above. Please note that the clock will most likely
75// drift away from the system wall clock time as time goes by.
76int64_t WinUwpSystemTimeNanos();
Robin Raymondce1b1402018-11-22 20:10:11 -050077#endif // defined(WINUWP)
78
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020079// Returns the actual system time, even if a clock is set for testing.
80// Useful for timeouts while using a test clock, or for logging.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020081int64_t SystemTimeMillis();
82
83// Returns the current time in milliseconds in 32 bits.
84uint32_t Time32();
85
86// Returns the current time in milliseconds in 64 bits.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020087RTC_EXPORT int64_t TimeMillis();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020088// Deprecated. Do not use this in any new code.
89inline int64_t Time() {
90 return TimeMillis();
91}
92
93// Returns the current time in microseconds.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020094RTC_EXPORT int64_t TimeMicros();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020095
96// Returns the current time in nanoseconds.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020097RTC_EXPORT int64_t TimeNanos();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020098
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020099// Returns a future timestamp, 'elapsed' milliseconds from now.
100int64_t TimeAfter(int64_t elapsed);
101
102// Number of milliseconds that would elapse between 'earlier' and 'later'
103// timestamps. The value is negative if 'later' occurs before 'earlier'.
104int64_t TimeDiff(int64_t later, int64_t earlier);
105int32_t TimeDiff32(uint32_t later, uint32_t earlier);
106
107// The number of milliseconds that have elapsed since 'earlier'.
108inline int64_t TimeSince(int64_t earlier) {
109 return TimeMillis() - earlier;
110}
111
112// The number of milliseconds that will elapse between now and 'later'.
113inline int64_t TimeUntil(int64_t later) {
114 return later - TimeMillis();
115}
116
Yves Gerey988cc082018-10-23 12:03:01 +0200117// Convert from tm, which is relative to 1900-01-01 00:00 to number of
118// seconds from 1970-01-01 00:00 ("epoch"). Don't return time_t since that
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200119// is still 32 bits on many systems.
Yves Gerey988cc082018-10-23 12:03:01 +0200120int64_t TmToSeconds(const tm& tm);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200121
122// Return the number of microseconds since January 1, 1970, UTC.
123// Useful mainly when producing logs to be correlated with other
124// devices, and when the devices in question all have properly
125// synchronized clocks.
126//
127// Note that this function obeys the system's idea about what the time
128// is. It is not guaranteed to be monotonic; it will jump in case the
129// system time is changed, e.g., by some other process calling
130// settimeofday. Always use rtc::TimeMicros(), not this function, for
131// measuring time intervals and timeouts.
132int64_t TimeUTCMicros();
133
Minyue Li656d6092018-08-10 15:38:52 +0200134// Return the number of milliseconds since January 1, 1970, UTC.
135// See above.
136int64_t TimeUTCMillis();
137
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200138} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000139
Steve Anton10542f22019-01-11 09:11:00 -0800140#endif // RTC_BASE_TIME_UTILS_H_