xref: /aosp_15_r20/external/webrtc/rtc_base/time_utils.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2005 The WebRTC Project Authors. All rights reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef RTC_BASE_TIME_UTILS_H_
12*d9f75844SAndroid Build Coastguard Worker #define RTC_BASE_TIME_UTILS_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <stdint.h>
15*d9f75844SAndroid Build Coastguard Worker #include <time.h>
16*d9f75844SAndroid Build Coastguard Worker 
17*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h"
18*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/system/rtc_export.h"
19*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/system_time.h"
20*d9f75844SAndroid Build Coastguard Worker 
21*d9f75844SAndroid Build Coastguard Worker namespace rtc {
22*d9f75844SAndroid Build Coastguard Worker 
23*d9f75844SAndroid Build Coastguard Worker static const int64_t kNumMillisecsPerSec = INT64_C(1000);
24*d9f75844SAndroid Build Coastguard Worker static const int64_t kNumMicrosecsPerSec = INT64_C(1000000);
25*d9f75844SAndroid Build Coastguard Worker static const int64_t kNumNanosecsPerSec = INT64_C(1000000000);
26*d9f75844SAndroid Build Coastguard Worker 
27*d9f75844SAndroid Build Coastguard Worker static const int64_t kNumMicrosecsPerMillisec =
28*d9f75844SAndroid Build Coastguard Worker     kNumMicrosecsPerSec / kNumMillisecsPerSec;
29*d9f75844SAndroid Build Coastguard Worker static const int64_t kNumNanosecsPerMillisec =
30*d9f75844SAndroid Build Coastguard Worker     kNumNanosecsPerSec / kNumMillisecsPerSec;
31*d9f75844SAndroid Build Coastguard Worker static const int64_t kNumNanosecsPerMicrosec =
32*d9f75844SAndroid Build Coastguard Worker     kNumNanosecsPerSec / kNumMicrosecsPerSec;
33*d9f75844SAndroid Build Coastguard Worker 
34*d9f75844SAndroid Build Coastguard Worker // Elapsed milliseconds between NTP base, 1900 January 1 00:00 GMT
35*d9f75844SAndroid Build Coastguard Worker // (see https://tools.ietf.org/html/rfc868), and January 1 00:00 GMT 1970
36*d9f75844SAndroid Build Coastguard Worker // epoch. This is useful when converting between the NTP time base and the
37*d9f75844SAndroid Build Coastguard Worker // time base used in RTCP reports.
38*d9f75844SAndroid Build Coastguard Worker constexpr int64_t kNtpJan1970Millisecs = 2'208'988'800 * kNumMillisecsPerSec;
39*d9f75844SAndroid Build Coastguard Worker 
40*d9f75844SAndroid Build Coastguard Worker // TODO(honghaiz): Define a type for the time value specifically.
41*d9f75844SAndroid Build Coastguard Worker 
42*d9f75844SAndroid Build Coastguard Worker class ClockInterface {
43*d9f75844SAndroid Build Coastguard Worker  public:
~ClockInterface()44*d9f75844SAndroid Build Coastguard Worker   virtual ~ClockInterface() {}
45*d9f75844SAndroid Build Coastguard Worker   virtual int64_t TimeNanos() const = 0;
46*d9f75844SAndroid Build Coastguard Worker };
47*d9f75844SAndroid Build Coastguard Worker 
48*d9f75844SAndroid Build Coastguard Worker // Sets the global source of time. This is useful mainly for unit tests.
49*d9f75844SAndroid Build Coastguard Worker //
50*d9f75844SAndroid Build Coastguard Worker // Returns the previously set ClockInterface, or nullptr if none is set.
51*d9f75844SAndroid Build Coastguard Worker //
52*d9f75844SAndroid Build Coastguard Worker // Does not transfer ownership of the clock. SetClockForTesting(nullptr)
53*d9f75844SAndroid Build Coastguard Worker // should be called before the ClockInterface is deleted.
54*d9f75844SAndroid Build Coastguard Worker //
55*d9f75844SAndroid Build Coastguard Worker // This method is not thread-safe; it should only be used when no other thread
56*d9f75844SAndroid Build Coastguard Worker // is running (for example, at the start/end of a unit test, or start/end of
57*d9f75844SAndroid Build Coastguard Worker // main()).
58*d9f75844SAndroid Build Coastguard Worker //
59*d9f75844SAndroid Build Coastguard Worker // TODO(deadbeef): Instead of having functions that access this global
60*d9f75844SAndroid Build Coastguard Worker // ClockInterface, we may want to pass the ClockInterface into everything
61*d9f75844SAndroid Build Coastguard Worker // that uses it, eliminating the need for a global variable and this function.
62*d9f75844SAndroid Build Coastguard Worker RTC_EXPORT ClockInterface* SetClockForTesting(ClockInterface* clock);
63*d9f75844SAndroid Build Coastguard Worker 
64*d9f75844SAndroid Build Coastguard Worker // Returns previously set clock, or nullptr if no custom clock is being used.
65*d9f75844SAndroid Build Coastguard Worker RTC_EXPORT ClockInterface* GetClockForTesting();
66*d9f75844SAndroid Build Coastguard Worker 
67*d9f75844SAndroid Build Coastguard Worker #if defined(WINUWP)
68*d9f75844SAndroid Build Coastguard Worker // Synchronizes the current clock based upon an NTP server's epoch in
69*d9f75844SAndroid Build Coastguard Worker // milliseconds.
70*d9f75844SAndroid Build Coastguard Worker void SyncWithNtp(int64_t time_from_ntp_server_ms);
71*d9f75844SAndroid Build Coastguard Worker 
72*d9f75844SAndroid Build Coastguard Worker // Returns the current time in nanoseconds. The clock is synchonized with the
73*d9f75844SAndroid Build Coastguard Worker // system wall clock time upon instatiation. It may also be synchronized using
74*d9f75844SAndroid Build Coastguard Worker // the SyncWithNtp() function above. Please note that the clock will most likely
75*d9f75844SAndroid Build Coastguard Worker // drift away from the system wall clock time as time goes by.
76*d9f75844SAndroid Build Coastguard Worker int64_t WinUwpSystemTimeNanos();
77*d9f75844SAndroid Build Coastguard Worker #endif  // defined(WINUWP)
78*d9f75844SAndroid Build Coastguard Worker 
79*d9f75844SAndroid Build Coastguard Worker // Returns the actual system time, even if a clock is set for testing.
80*d9f75844SAndroid Build Coastguard Worker // Useful for timeouts while using a test clock, or for logging.
81*d9f75844SAndroid Build Coastguard Worker int64_t SystemTimeMillis();
82*d9f75844SAndroid Build Coastguard Worker 
83*d9f75844SAndroid Build Coastguard Worker // Returns the current time in milliseconds in 32 bits.
84*d9f75844SAndroid Build Coastguard Worker uint32_t Time32();
85*d9f75844SAndroid Build Coastguard Worker 
86*d9f75844SAndroid Build Coastguard Worker // Returns the current time in milliseconds in 64 bits.
87*d9f75844SAndroid Build Coastguard Worker RTC_EXPORT int64_t TimeMillis();
88*d9f75844SAndroid Build Coastguard Worker // Deprecated. Do not use this in any new code.
Time()89*d9f75844SAndroid Build Coastguard Worker inline int64_t Time() {
90*d9f75844SAndroid Build Coastguard Worker   return TimeMillis();
91*d9f75844SAndroid Build Coastguard Worker }
92*d9f75844SAndroid Build Coastguard Worker 
93*d9f75844SAndroid Build Coastguard Worker // Returns the current time in microseconds.
94*d9f75844SAndroid Build Coastguard Worker RTC_EXPORT int64_t TimeMicros();
95*d9f75844SAndroid Build Coastguard Worker 
96*d9f75844SAndroid Build Coastguard Worker // Returns the current time in nanoseconds.
97*d9f75844SAndroid Build Coastguard Worker RTC_EXPORT int64_t TimeNanos();
98*d9f75844SAndroid Build Coastguard Worker 
99*d9f75844SAndroid Build Coastguard Worker // Returns a future timestamp, 'elapsed' milliseconds from now.
100*d9f75844SAndroid Build Coastguard Worker int64_t TimeAfter(int64_t elapsed);
101*d9f75844SAndroid Build Coastguard Worker 
102*d9f75844SAndroid Build Coastguard Worker // Number of milliseconds that would elapse between 'earlier' and 'later'
103*d9f75844SAndroid Build Coastguard Worker // timestamps.  The value is negative if 'later' occurs before 'earlier'.
104*d9f75844SAndroid Build Coastguard Worker int64_t TimeDiff(int64_t later, int64_t earlier);
105*d9f75844SAndroid Build Coastguard Worker int32_t TimeDiff32(uint32_t later, uint32_t earlier);
106*d9f75844SAndroid Build Coastguard Worker 
107*d9f75844SAndroid Build Coastguard Worker // The number of milliseconds that have elapsed since 'earlier'.
TimeSince(int64_t earlier)108*d9f75844SAndroid Build Coastguard Worker inline int64_t TimeSince(int64_t earlier) {
109*d9f75844SAndroid Build Coastguard Worker   return TimeMillis() - earlier;
110*d9f75844SAndroid Build Coastguard Worker }
111*d9f75844SAndroid Build Coastguard Worker 
112*d9f75844SAndroid Build Coastguard Worker // The number of milliseconds that will elapse between now and 'later'.
TimeUntil(int64_t later)113*d9f75844SAndroid Build Coastguard Worker inline int64_t TimeUntil(int64_t later) {
114*d9f75844SAndroid Build Coastguard Worker   return later - TimeMillis();
115*d9f75844SAndroid Build Coastguard Worker }
116*d9f75844SAndroid Build Coastguard Worker 
117*d9f75844SAndroid Build Coastguard Worker class TimestampWrapAroundHandler {
118*d9f75844SAndroid Build Coastguard Worker  public:
119*d9f75844SAndroid Build Coastguard Worker   TimestampWrapAroundHandler();
120*d9f75844SAndroid Build Coastguard Worker 
121*d9f75844SAndroid Build Coastguard Worker   int64_t Unwrap(uint32_t ts);
122*d9f75844SAndroid Build Coastguard Worker 
123*d9f75844SAndroid Build Coastguard Worker  private:
124*d9f75844SAndroid Build Coastguard Worker   uint32_t last_ts_;
125*d9f75844SAndroid Build Coastguard Worker   int64_t num_wrap_;
126*d9f75844SAndroid Build Coastguard Worker };
127*d9f75844SAndroid Build Coastguard Worker 
128*d9f75844SAndroid Build Coastguard Worker // Convert from tm, which is relative to 1900-01-01 00:00 to number of
129*d9f75844SAndroid Build Coastguard Worker // seconds from 1970-01-01 00:00 ("epoch"). Don't return time_t since that
130*d9f75844SAndroid Build Coastguard Worker // is still 32 bits on many systems.
131*d9f75844SAndroid Build Coastguard Worker int64_t TmToSeconds(const tm& tm);
132*d9f75844SAndroid Build Coastguard Worker 
133*d9f75844SAndroid Build Coastguard Worker // Return the number of microseconds since January 1, 1970, UTC.
134*d9f75844SAndroid Build Coastguard Worker // Useful mainly when producing logs to be correlated with other
135*d9f75844SAndroid Build Coastguard Worker // devices, and when the devices in question all have properly
136*d9f75844SAndroid Build Coastguard Worker // synchronized clocks.
137*d9f75844SAndroid Build Coastguard Worker //
138*d9f75844SAndroid Build Coastguard Worker // Note that this function obeys the system's idea about what the time
139*d9f75844SAndroid Build Coastguard Worker // is. It is not guaranteed to be monotonic; it will jump in case the
140*d9f75844SAndroid Build Coastguard Worker // system time is changed, e.g., by some other process calling
141*d9f75844SAndroid Build Coastguard Worker // settimeofday. Always use rtc::TimeMicros(), not this function, for
142*d9f75844SAndroid Build Coastguard Worker // measuring time intervals and timeouts.
143*d9f75844SAndroid Build Coastguard Worker int64_t TimeUTCMicros();
144*d9f75844SAndroid Build Coastguard Worker 
145*d9f75844SAndroid Build Coastguard Worker // Return the number of milliseconds since January 1, 1970, UTC.
146*d9f75844SAndroid Build Coastguard Worker // See above.
147*d9f75844SAndroid Build Coastguard Worker int64_t TimeUTCMillis();
148*d9f75844SAndroid Build Coastguard Worker 
149*d9f75844SAndroid Build Coastguard Worker }  // namespace rtc
150*d9f75844SAndroid Build Coastguard Worker 
151*d9f75844SAndroid Build Coastguard Worker #endif  // RTC_BASE_TIME_UTILS_H_
152