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 | |
pbos | c7c26a0 | 2017-01-02 08:42:32 -0800 | [diff] [blame^] | 14 | #include <stdint.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | #include <time.h> |
| 16 | |
pbos | c7c26a0 | 2017-01-02 08:42:32 -0800 | [diff] [blame^] | 17 | #include <ctime> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 18 | |
| 19 | namespace rtc { |
| 20 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 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); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 24 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 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; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 31 | |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 32 | // TODO(honghaiz): Define a type for the time value specifically. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 33 | |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 34 | class ClockInterface { |
| 35 | public: |
| 36 | virtual ~ClockInterface() {} |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 37 | virtual int64_t TimeNanos() const = 0; |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | // Sets the global source of time. This is useful mainly for unit tests. |
| 41 | // |
deadbeef | f5f03e8 | 2016-06-06 11:16:06 -0700 | [diff] [blame] | 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. |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 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. |
deadbeef | f5f03e8 | 2016-06-06 11:16:06 -0700 | [diff] [blame] | 54 | ClockInterface* SetClockForTesting(ClockInterface* clock); |
Taylor Brandstetter | b3c6810 | 2016-05-27 14:15:43 -0700 | [diff] [blame] | 55 | |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 56 | // Returns the actual system time, even if a clock is set for testing. |
| 57 | // Useful for timeouts while using a test clock, or for logging. |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 58 | int64_t SystemTimeNanos(); |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 59 | int64_t SystemTimeMillis(); |
| 60 | |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 61 | // Returns the current time in milliseconds in 32 bits. |
| 62 | uint32_t Time32(); |
| 63 | |
| 64 | // Returns the current time in milliseconds in 64 bits. |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 65 | int64_t TimeMillis(); |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 66 | // Deprecated. Do not use this in any new code. |
| 67 | inline int64_t Time() { |
| 68 | return TimeMillis(); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 69 | } |
| 70 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 71 | // Returns the current time in microseconds. |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 72 | int64_t TimeMicros(); |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 73 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 74 | // Returns the current time in nanoseconds. |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 75 | int64_t TimeNanos(); |
| 76 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 77 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 78 | // Returns a future timestamp, 'elapsed' milliseconds from now. |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 79 | int64_t TimeAfter(int64_t elapsed); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 80 | |
| 81 | // Number of milliseconds that would elapse between 'earlier' and 'later' |
| 82 | // timestamps. The value is negative if 'later' occurs before 'earlier'. |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 83 | int64_t TimeDiff(int64_t later, int64_t earlier); |
| 84 | int32_t TimeDiff32(uint32_t later, uint32_t earlier); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 85 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 86 | // The number of milliseconds that have elapsed since 'earlier'. |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 87 | inline int64_t TimeSince(int64_t earlier) { |
| 88 | return TimeMillis() - earlier; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // The number of milliseconds that will elapse between now and 'later'. |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 92 | inline int64_t TimeUntil(int64_t later) { |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 93 | return later - TimeMillis(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 94 | } |
| 95 | |
henrike@webrtc.org | 99b4162 | 2014-05-21 20:42:17 +0000 | [diff] [blame] | 96 | class TimestampWrapAroundHandler { |
| 97 | public: |
| 98 | TimestampWrapAroundHandler(); |
| 99 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 100 | int64_t Unwrap(uint32_t ts); |
henrike@webrtc.org | 99b4162 | 2014-05-21 20:42:17 +0000 | [diff] [blame] | 101 | |
| 102 | private: |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 103 | uint32_t last_ts_; |
| 104 | int64_t num_wrap_; |
henrike@webrtc.org | 99b4162 | 2014-05-21 20:42:17 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 107 | // Convert from std::tm, which is relative to 1900-01-01 00:00 to number of |
| 108 | // seconds from 1970-01-01 00:00 ("epoch"). Don't return time_t since that |
| 109 | // is still 32 bits on many systems. |
| 110 | int64_t TmToSeconds(const std::tm& tm); |
| 111 | |
nisse | cdf37a9 | 2016-09-13 23:41:47 -0700 | [diff] [blame] | 112 | // Return the number of microseconds since January 1, 1970, UTC. |
| 113 | // Useful mainly when producing logs to be correlated with other |
| 114 | // devices, and when the devices in question all have properly |
| 115 | // synchronized clocks. |
| 116 | // |
| 117 | // Note that this function obeys the system's idea about what the time |
| 118 | // is. It is not guaranteed to be monotonic; it will jump in case the |
| 119 | // system time is changed, e.g., by some other process calling |
| 120 | // settimeofday. Always use rtc::TimeMicros(), not this function, for |
| 121 | // measuring time intervals and timeouts. |
| 122 | int64_t TimeUTCMicros(); |
| 123 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 124 | } // namespace rtc |
| 125 | |
| 126 | #endif // WEBRTC_BASE_TIMEUTILS_H_ |