Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2005 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2005 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 static const int64_t kNumMicrosecsPerMillisec = | 25 static const int64_t kNumMicrosecsPerMillisec = |
| 26 kNumMicrosecsPerSec / kNumMillisecsPerSec; | 26 kNumMicrosecsPerSec / kNumMillisecsPerSec; |
| 27 static const int64_t kNumNanosecsPerMillisec = | 27 static const int64_t kNumNanosecsPerMillisec = |
| 28 kNumNanosecsPerSec / kNumMillisecsPerSec; | 28 kNumNanosecsPerSec / kNumMillisecsPerSec; |
| 29 static const int64_t kNumNanosecsPerMicrosec = | 29 static const int64_t kNumNanosecsPerMicrosec = |
| 30 kNumNanosecsPerSec / kNumMicrosecsPerSec; | 30 kNumNanosecsPerSec / kNumMicrosecsPerSec; |
| 31 | 31 |
| 32 // January 1970, in NTP milliseconds. | 32 // January 1970, in NTP milliseconds. |
| 33 static const int64_t kJan1970AsNtpMillisecs = INT64_C(2208988800000); | 33 static const int64_t kJan1970AsNtpMillisecs = INT64_C(2208988800000); |
| 34 | 34 |
| 35 typedef uint32_t TimeStamp; | |
| 36 | |
| 37 // Returns the current time in milliseconds in 32 bits. | 35 // Returns the current time in milliseconds in 32 bits. |
| 38 uint32_t Time32(); | 36 uint32_t Time32(); |
| 39 | 37 |
| 40 // Returns the current time in milliseconds in 64 bits. | 38 // Returns the current time in milliseconds in 64 bits. |
| 39 // TODO(honghaiz): remove this and replace the call with Time(). | |
| 41 int64_t Time64(); | 40 int64_t Time64(); |
| 42 | 41 |
| 43 // Returns the current time in milliseconds. | 42 // Returns the current time in milliseconds. |
| 44 // TODO(honghaiz): Returns Time64 once majority of the webrtc code migrates to | 43 inline int64_t Time() { |
| 45 // 64-bit timestamp. | 44 return Time64(); |
| 46 inline uint32_t Time() { | |
| 47 return Time32(); | |
| 48 } | 45 } |
| 49 | 46 |
| 50 // Returns the current time in microseconds. | 47 // Returns the current time in microseconds. |
| 51 uint64_t TimeMicros(); | 48 uint64_t TimeMicros(); |
| 52 // Returns the current time in nanoseconds. | 49 // Returns the current time in nanoseconds. |
| 53 uint64_t TimeNanos(); | 50 uint64_t TimeNanos(); |
| 54 | 51 |
| 55 // Stores current time in *tm and microseconds in *microseconds. | 52 // Stores current time in *tm and microseconds in *microseconds. |
| 56 void CurrentTmTime(struct tm *tm, int *microseconds); | 53 void CurrentTmTime(struct tm *tm, int *microseconds); |
| 57 | 54 |
| 58 // Returns a future timestamp, 'elapsed' milliseconds from now. | 55 // Returns a future timestamp, 'elapsed' milliseconds from now. |
| 59 uint32_t TimeAfter(int32_t elapsed); | 56 int64_t TimeAfter(int elapsed); |
|
Taylor Brandstetter
2016/04/05 01:08:14
Why take an int parameter instead of int64_t? Some
pthatcher1
2016/04/11 20:56:59
I agree. Using int64_t consistently seems like a
honghaiz3
2016/04/18 23:39:04
Done.
| |
| 60 | |
| 61 // Comparisons between time values, which can wrap around. | |
| 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 | |
| 67 | |
| 68 // Returns the later of two timestamps. | |
| 69 inline uint32_t TimeMax(uint32_t ts1, uint32_t ts2) { | |
| 70 return TimeIsLaterOrEqual(ts1, ts2) ? ts2 : ts1; | |
| 71 } | |
| 72 | |
| 73 // Returns the earlier of two timestamps. | |
| 74 inline uint32_t TimeMin(uint32_t ts1, uint32_t ts2) { | |
| 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'. | |
| 80 int32_t TimeDiff(uint32_t later, uint32_t earlier); | |
| 81 | 57 |
| 82 // Number of milliseconds that would elapse between 'earlier' and 'later' | 58 // Number of milliseconds that would elapse between 'earlier' and 'later' |
| 83 // timestamps. The value is negative if 'later' occurs before 'earlier'. | 59 // timestamps. The value is negative if 'later' occurs before 'earlier'. |
| 84 int64_t TimeDiff64(int64_t later, int64_t earlier); | 60 int64_t TimeDiff64(int64_t later, int64_t earlier); |
| 61 inline int TimeDiff(int64_t later, int64_t earlier) { | |
| 62 return static_cast<int>(TimeDiff64(later, earlier)); | |
| 63 } | |
| 85 | 64 |
| 86 // The number of milliseconds that have elapsed since 'earlier'. | 65 // The number of milliseconds that have elapsed since 'earlier'. |
| 87 inline int32_t TimeSince(uint32_t earlier) { | 66 inline int TimeSince(int64_t earlier) { |
|
Taylor Brandstetter
2016/04/05 01:08:14
Similarly, why do these return ints?
honghaiz3
2016/04/18 23:39:04
For all delta-time (e.g. elapsed time, or time del
Taylor Brandstetter
2016/04/19 20:35:27
See my other comment.
honghaiz3
2016/04/22 23:45:20
Done.
| |
| 88 return TimeDiff(Time(), earlier); | 67 return static_cast<int>(Time() - earlier); |
| 89 } | 68 } |
| 90 | 69 |
| 91 // The number of milliseconds that will elapse between now and 'later'. | 70 // The number of milliseconds that will elapse between now and 'later'. |
| 92 inline int32_t TimeUntil(uint32_t later) { | 71 inline int TimeUntil(uint64_t later) { |
| 93 return TimeDiff(later, Time()); | 72 return static_cast<int>(later - Time()); |
| 94 } | 73 } |
| 95 | 74 |
| 96 // Converts a unix timestamp in nanoseconds to an NTP timestamp in ms. | 75 // Converts a unix timestamp in nanoseconds to an NTP timestamp in ms. |
| 97 inline int64_t UnixTimestampNanosecsToNtpMillisecs(int64_t unix_ts_ns) { | 76 inline int64_t UnixTimestampNanosecsToNtpMillisecs(int64_t unix_ts_ns) { |
| 98 return unix_ts_ns / kNumNanosecsPerMillisec + kJan1970AsNtpMillisecs; | 77 return unix_ts_ns / kNumNanosecsPerMillisec + kJan1970AsNtpMillisecs; |
| 99 } | 78 } |
| 100 | 79 |
| 101 class TimestampWrapAroundHandler { | 80 class TimestampWrapAroundHandler { |
| 102 public: | 81 public: |
| 103 TimestampWrapAroundHandler(); | 82 TimestampWrapAroundHandler(); |
| 104 | 83 |
| 105 int64_t Unwrap(uint32_t ts); | 84 int64_t Unwrap(uint32_t ts); |
| 106 | 85 |
| 107 private: | 86 private: |
| 108 uint32_t last_ts_; | 87 uint32_t last_ts_; |
| 109 int64_t num_wrap_; | 88 int64_t num_wrap_; |
| 110 }; | 89 }; |
| 111 | 90 |
| 112 // Convert from std::tm, which is relative to 1900-01-01 00:00 to number of | 91 // 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 | 92 // seconds from 1970-01-01 00:00 ("epoch"). Don't return time_t since that |
| 114 // is still 32 bits on many systems. | 93 // is still 32 bits on many systems. |
| 115 int64_t TmToSeconds(const std::tm& tm); | 94 int64_t TmToSeconds(const std::tm& tm); |
| 116 | 95 |
| 117 } // namespace rtc | 96 } // namespace rtc |
| 118 | 97 |
| 119 #endif // WEBRTC_BASE_TIMEUTILS_H_ | 98 #endif // WEBRTC_BASE_TIMEUTILS_H_ |
| OLD | NEW |