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
20 namespace rtc {
21
22 static const int64_t kNumMillisecsPerSec = INT64_C(1000);
23 static const int64_t kNumMicrosecsPerSec = INT64_C(1000000);
24 static const int64_t kNumNanosecsPerSec = INT64_C(1000000000);
25
26 static const int64_t kNumMicrosecsPerMillisec =
27 kNumMicrosecsPerSec / kNumMillisecsPerSec;
28 static const int64_t kNumNanosecsPerMillisec =
29 kNumNanosecsPerSec / kNumMillisecsPerSec;
30 static const int64_t kNumNanosecsPerMicrosec =
31 kNumNanosecsPerSec / kNumMicrosecsPerSec;
32
33 // TODO(honghaiz): Define a type for the time value specifically.
34
35 class ClockInterface {
36 public:
~ClockInterface()37 virtual ~ClockInterface() {}
38 virtual int64_t TimeNanos() const = 0;
39 };
40
41 // Sets the global source of time. This is useful mainly for unit tests.
42 //
43 // Returns the previously set ClockInterface, or nullptr if none is set.
44 //
45 // Does not transfer ownership of the clock. SetClockForTesting(nullptr)
46 // should be called before the ClockInterface is deleted.
47 //
48 // This method is not thread-safe; it should only be used when no other thread
49 // is running (for example, at the start/end of a unit test, or start/end of
50 // main()).
51 //
52 // TODO(deadbeef): Instead of having functions that access this global
53 // ClockInterface, we may want to pass the ClockInterface into everything
54 // that uses it, eliminating the need for a global variable and this function.
55 RTC_EXPORT ClockInterface* SetClockForTesting(ClockInterface* clock);
56
57 // Returns previously set clock, or nullptr if no custom clock is being used.
58 RTC_EXPORT ClockInterface* GetClockForTesting();
59
60 #if defined(WINUWP)
61 // Synchronizes the current clock based upon an NTP server's epoch in
62 // milliseconds.
63 void SyncWithNtp(int64_t time_from_ntp_server_ms);
64 #endif // defined(WINUWP)
65
66 // Returns the actual system time, even if a clock is set for testing.
67 // Useful for timeouts while using a test clock, or for logging.
68 int64_t SystemTimeNanos();
69 int64_t SystemTimeMillis();
70
71 // Returns the current time in milliseconds in 32 bits.
72 uint32_t Time32();
73
74 // Returns the current time in milliseconds in 64 bits.
75 RTC_EXPORT int64_t TimeMillis();
76 // Deprecated. Do not use this in any new code.
Time()77 inline int64_t Time() {
78 return TimeMillis();
79 }
80
81 // Returns the current time in microseconds.
82 RTC_EXPORT int64_t TimeMicros();
83
84 // Returns the current time in nanoseconds.
85 RTC_EXPORT int64_t TimeNanos();
86
87 // Returns a future timestamp, 'elapsed' milliseconds from now.
88 int64_t TimeAfter(int64_t elapsed);
89
90 // Number of milliseconds that would elapse between 'earlier' and 'later'
91 // timestamps. The value is negative if 'later' occurs before 'earlier'.
92 int64_t TimeDiff(int64_t later, int64_t earlier);
93 int32_t TimeDiff32(uint32_t later, uint32_t earlier);
94
95 // The number of milliseconds that have elapsed since 'earlier'.
TimeSince(int64_t earlier)96 inline int64_t TimeSince(int64_t earlier) {
97 return TimeMillis() - earlier;
98 }
99
100 // The number of milliseconds that will elapse between now and 'later'.
TimeUntil(int64_t later)101 inline int64_t TimeUntil(int64_t later) {
102 return later - TimeMillis();
103 }
104
105 class TimestampWrapAroundHandler {
106 public:
107 TimestampWrapAroundHandler();
108
109 int64_t Unwrap(uint32_t ts);
110
111 private:
112 uint32_t last_ts_;
113 int64_t num_wrap_;
114 };
115
116 // Convert from tm, which is relative to 1900-01-01 00:00 to number of
117 // seconds from 1970-01-01 00:00 ("epoch"). Don't return time_t since that
118 // is still 32 bits on many systems.
119 int64_t TmToSeconds(const tm& tm);
120
121 // Return the number of microseconds since January 1, 1970, UTC.
122 // Useful mainly when producing logs to be correlated with other
123 // devices, and when the devices in question all have properly
124 // synchronized clocks.
125 //
126 // Note that this function obeys the system's idea about what the time
127 // is. It is not guaranteed to be monotonic; it will jump in case the
128 // system time is changed, e.g., by some other process calling
129 // settimeofday. Always use rtc::TimeMicros(), not this function, for
130 // measuring time intervals and timeouts.
131 int64_t TimeUTCMicros();
132
133 // Return the number of milliseconds since January 1, 1970, UTC.
134 // See above.
135 int64_t TimeUTCMillis();
136
137 } // namespace rtc
138
139 #endif // RTC_BASE_TIME_UTILS_H_
140