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