1 /*
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
11 #ifndef RTC_BASE_TIME_UTILS_H_
12 #define RTC_BASE_TIME_UTILS_H_
13
14 #include <stdint.h>
15 #include <time.h>
16
17 #include "rtc_base/checks.h"
18 #include "rtc_base/system/rtc_export.h"
19 #include "rtc_base/system_time.h"
20
21 namespace rtc {
22
23 static const int64_t kNumMillisecsPerSec = INT64_C(1000);
24 static const int64_t kNumMicrosecsPerSec = INT64_C(1000000);
25 static const int64_t kNumNanosecsPerSec = INT64_C(1000000000);
26
27 static const int64_t kNumMicrosecsPerMillisec =
28 kNumMicrosecsPerSec / kNumMillisecsPerSec;
29 static const int64_t kNumNanosecsPerMillisec =
30 kNumNanosecsPerSec / kNumMillisecsPerSec;
31 static const int64_t kNumNanosecsPerMicrosec =
32 kNumNanosecsPerSec / kNumMicrosecsPerSec;
33
34 // 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.
38 constexpr int64_t kNtpJan1970Millisecs = 2'208'988'800 * kNumMillisecsPerSec;
39
40 // TODO(honghaiz): Define a type for the time value specifically.
41
42 class ClockInterface {
43 public:
~ClockInterface()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.
62 RTC_EXPORT ClockInterface* SetClockForTesting(ClockInterface* clock);
63
64 // Returns previously set clock, or nullptr if no custom clock is being used.
65 RTC_EXPORT ClockInterface* GetClockForTesting();
66
67 #if defined(WINUWP)
68 // Synchronizes the current clock based upon an NTP server's epoch in
69 // milliseconds.
70 void SyncWithNtp(int64_t time_from_ntp_server_ms);
71
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.
76 int64_t WinUwpSystemTimeNanos();
77 #endif // defined(WINUWP)
78
79 // 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.
81 int64_t SystemTimeMillis();
82
83 // Returns the current time in milliseconds in 32 bits.
84 uint32_t Time32();
85
86 // Returns the current time in milliseconds in 64 bits.
87 RTC_EXPORT int64_t TimeMillis();
88 // Deprecated. Do not use this in any new code.
Time()89 inline int64_t Time() {
90 return TimeMillis();
91 }
92
93 // Returns the current time in microseconds.
94 RTC_EXPORT int64_t TimeMicros();
95
96 // Returns the current time in nanoseconds.
97 RTC_EXPORT int64_t TimeNanos();
98
99 // Returns a future timestamp, 'elapsed' milliseconds from now.
100 int64_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'.
104 int64_t TimeDiff(int64_t later, int64_t earlier);
105 int32_t TimeDiff32(uint32_t later, uint32_t earlier);
106
107 // The number of milliseconds that have elapsed since 'earlier'.
TimeSince(int64_t earlier)108 inline int64_t TimeSince(int64_t earlier) {
109 return TimeMillis() - earlier;
110 }
111
112 // The number of milliseconds that will elapse between now and 'later'.
TimeUntil(int64_t later)113 inline int64_t TimeUntil(int64_t later) {
114 return later - TimeMillis();
115 }
116
117 class TimestampWrapAroundHandler {
118 public:
119 TimestampWrapAroundHandler();
120
121 int64_t Unwrap(uint32_t ts);
122
123 private:
124 uint32_t last_ts_;
125 int64_t num_wrap_;
126 };
127
128 // Convert from tm, which is relative to 1900-01-01 00:00 to number of
129 // seconds from 1970-01-01 00:00 ("epoch"). Don't return time_t since that
130 // is still 32 bits on many systems.
131 int64_t TmToSeconds(const tm& tm);
132
133 // Return the number of microseconds since January 1, 1970, UTC.
134 // Useful mainly when producing logs to be correlated with other
135 // devices, and when the devices in question all have properly
136 // synchronized clocks.
137 //
138 // Note that this function obeys the system's idea about what the time
139 // is. It is not guaranteed to be monotonic; it will jump in case the
140 // system time is changed, e.g., by some other process calling
141 // settimeofday. Always use rtc::TimeMicros(), not this function, for
142 // measuring time intervals and timeouts.
143 int64_t TimeUTCMicros();
144
145 // Return the number of milliseconds since January 1, 1970, UTC.
146 // See above.
147 int64_t TimeUTCMillis();
148
149 } // namespace rtc
150
151 #endif // RTC_BASE_TIME_UTILS_H_
152