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