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