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 14 #include <time.h> 15 16 #include "webrtc/base/basictypes.h" 17 18 namespace rtc { 19 20 static const int64 kNumMillisecsPerSec = INT64_C(1000); 21 static const int64 kNumMicrosecsPerSec = INT64_C(1000000); 22 static const int64 kNumNanosecsPerSec = INT64_C(1000000000); 23 24 static const int64 kNumMicrosecsPerMillisec = kNumMicrosecsPerSec / 25 kNumMillisecsPerSec; 26 static const int64 kNumNanosecsPerMillisec = kNumNanosecsPerSec / 27 kNumMillisecsPerSec; 28 static const int64 kNumNanosecsPerMicrosec = kNumNanosecsPerSec / 29 kNumMicrosecsPerSec; 30 31 // January 1970, in NTP milliseconds. 32 static const int64 kJan1970AsNtpMillisecs = INT64_C(2208988800000); 33 34 typedef uint32 TimeStamp; 35 36 // Returns the current time in milliseconds. 37 uint32 Time(); 38 // Returns the current time in microseconds. 39 uint64 TimeMicros(); 40 // Returns the current time in nanoseconds. 41 uint64 TimeNanos(); 42 43 // Stores current time in *tm and microseconds in *microseconds. 44 void CurrentTmTime(struct tm *tm, int *microseconds); 45 46 // Returns a future timestamp, 'elapsed' milliseconds from now. 47 uint32 TimeAfter(int32 elapsed); 48 49 // Comparisons between time values, which can wrap around. 50 bool TimeIsBetween(uint32 earlier, uint32 middle, uint32 later); // Inclusive 51 bool TimeIsLaterOrEqual(uint32 earlier, uint32 later); // Inclusive 52 bool TimeIsLater(uint32 earlier, uint32 later); // Exclusive 53 54 // Returns the later of two timestamps. 55 inline uint32 TimeMax(uint32 ts1, uint32 ts2) { 56 return TimeIsLaterOrEqual(ts1, ts2) ? ts2 : ts1; 57 } 58 59 // Returns the earlier of two timestamps. 60 inline uint32 TimeMin(uint32 ts1, uint32 ts2) { 61 return TimeIsLaterOrEqual(ts1, ts2) ? ts1 : ts2; 62 } 63 64 // Number of milliseconds that would elapse between 'earlier' and 'later' 65 // timestamps. The value is negative if 'later' occurs before 'earlier'. 66 int32 TimeDiff(uint32 later, uint32 earlier); 67 68 // The number of milliseconds that have elapsed since 'earlier'. 69 inline int32 TimeSince(uint32 earlier) { 70 return TimeDiff(Time(), earlier); 71 } 72 73 // The number of milliseconds that will elapse between now and 'later'. 74 inline int32 TimeUntil(uint32 later) { 75 return TimeDiff(later, Time()); 76 } 77 78 // Converts a unix timestamp in nanoseconds to an NTP timestamp in ms. 79 inline int64 UnixTimestampNanosecsToNtpMillisecs(int64 unix_ts_ns) { 80 return unix_ts_ns / kNumNanosecsPerMillisec + kJan1970AsNtpMillisecs; 81 } 82 83 class TimestampWrapAroundHandler { 84 public: 85 TimestampWrapAroundHandler(); 86 87 int64 Unwrap(uint32 ts); 88 89 private: 90 uint32 last_ts_; 91 int64 num_wrap_; 92 }; 93 94 } // namespace rtc 95 96 #endif // WEBRTC_BASE_TIMEUTILS_H_ 97