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 | |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 14 | #include <ctime> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | #include <time.h> |
| 16 | |
| 17 | #include "webrtc/base/basictypes.h" |
| 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 | |
| 32 | // January 1970, in NTP milliseconds. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 33 | static const int64_t kJan1970AsNtpMillisecs = INT64_C(2208988800000); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 34 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 35 | typedef uint32_t TimeStamp; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 36 | |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame^] | 37 | // Returns the current time in milliseconds in 32 bits. |
| 38 | uint32_t Time32(); |
| 39 | |
| 40 | // Returns the current time in milliseconds in 64 bits. |
| 41 | int64_t Time64(); |
| 42 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 43 | // Returns the current time in milliseconds. |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame^] | 44 | // TODO(honghaiz): Returns Time64 once majority of the webrtc code migrates to |
| 45 | // 64-bit timestamp. |
| 46 | inline uint32_t Time() { |
| 47 | return Time32(); |
| 48 | } |
| 49 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 50 | // Returns the current time in microseconds. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 51 | uint64_t TimeMicros(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 52 | // Returns the current time in nanoseconds. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 53 | uint64_t TimeNanos(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 54 | |
| 55 | // Stores current time in *tm and microseconds in *microseconds. |
| 56 | void CurrentTmTime(struct tm *tm, int *microseconds); |
| 57 | |
| 58 | // Returns a future timestamp, 'elapsed' milliseconds from now. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 59 | uint32_t TimeAfter(int32_t elapsed); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 60 | |
| 61 | // Comparisons between time values, which can wrap around. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 62 | bool TimeIsBetween(uint32_t earlier, |
| 63 | uint32_t middle, |
| 64 | uint32_t later); // Inclusive |
| 65 | bool TimeIsLaterOrEqual(uint32_t earlier, uint32_t later); // Inclusive |
| 66 | bool TimeIsLater(uint32_t earlier, uint32_t later); // Exclusive |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 67 | |
| 68 | // Returns the later of two timestamps. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 69 | inline uint32_t TimeMax(uint32_t ts1, uint32_t ts2) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 70 | return TimeIsLaterOrEqual(ts1, ts2) ? ts2 : ts1; |
| 71 | } |
| 72 | |
| 73 | // Returns the earlier of two timestamps. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 74 | inline uint32_t TimeMin(uint32_t ts1, uint32_t ts2) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 75 | return TimeIsLaterOrEqual(ts1, ts2) ? ts1 : ts2; |
| 76 | } |
| 77 | |
| 78 | // Number of milliseconds that would elapse between 'earlier' and 'later' |
| 79 | // timestamps. The value is negative if 'later' occurs before 'earlier'. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 80 | int32_t TimeDiff(uint32_t later, uint32_t earlier); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 81 | |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame^] | 82 | // Number of milliseconds that would elapse between 'earlier' and 'later' |
| 83 | // timestamps. The value is negative if 'later' occurs before 'earlier'. |
| 84 | int64_t TimeDiff64(int64_t later, int64_t earlier); |
| 85 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 86 | // The number of milliseconds that have elapsed since 'earlier'. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 87 | inline int32_t TimeSince(uint32_t earlier) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 88 | return TimeDiff(Time(), earlier); |
| 89 | } |
| 90 | |
| 91 | // The number of milliseconds that will elapse between now and 'later'. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 92 | inline int32_t TimeUntil(uint32_t later) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 93 | return TimeDiff(later, Time()); |
| 94 | } |
| 95 | |
| 96 | // Converts a unix timestamp in nanoseconds to an NTP timestamp in ms. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 97 | inline int64_t UnixTimestampNanosecsToNtpMillisecs(int64_t unix_ts_ns) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 98 | return unix_ts_ns / kNumNanosecsPerMillisec + kJan1970AsNtpMillisecs; |
| 99 | } |
| 100 | |
henrike@webrtc.org | 99b4162 | 2014-05-21 20:42:17 +0000 | [diff] [blame] | 101 | class TimestampWrapAroundHandler { |
| 102 | public: |
| 103 | TimestampWrapAroundHandler(); |
| 104 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 105 | int64_t Unwrap(uint32_t ts); |
henrike@webrtc.org | 99b4162 | 2014-05-21 20:42:17 +0000 | [diff] [blame] | 106 | |
| 107 | private: |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 108 | uint32_t last_ts_; |
| 109 | int64_t num_wrap_; |
henrike@webrtc.org | 99b4162 | 2014-05-21 20:42:17 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 112 | // Convert from std::tm, which is relative to 1900-01-01 00:00 to number of |
| 113 | // seconds from 1970-01-01 00:00 ("epoch"). Don't return time_t since that |
| 114 | // is still 32 bits on many systems. |
| 115 | int64_t TmToSeconds(const std::tm& tm); |
| 116 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 117 | } // namespace rtc |
| 118 | |
| 119 | #endif // WEBRTC_BASE_TIMEUTILS_H_ |