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