blob: bdeccc3739263721ed33d9635e035d0b9f9cdfb8 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
18namespace rtc {
19
Peter Boström0c4e06b2015-10-07 12:23:21 +020020static const int64_t kNumMillisecsPerSec = INT64_C(1000);
21static const int64_t kNumMicrosecsPerSec = INT64_C(1000000);
22static const int64_t kNumNanosecsPerSec = INT64_C(1000000000);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023
Peter Boström0c4e06b2015-10-07 12:23:21 +020024static const int64_t kNumMicrosecsPerMillisec =
25 kNumMicrosecsPerSec / kNumMillisecsPerSec;
26static const int64_t kNumNanosecsPerMillisec =
27 kNumNanosecsPerSec / kNumMillisecsPerSec;
28static const int64_t kNumNanosecsPerMicrosec =
29 kNumNanosecsPerSec / kNumMicrosecsPerSec;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030
31// January 1970, in NTP milliseconds.
Peter Boström0c4e06b2015-10-07 12:23:21 +020032static const int64_t kJan1970AsNtpMillisecs = INT64_C(2208988800000);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033
Peter Boström0c4e06b2015-10-07 12:23:21 +020034typedef uint32_t TimeStamp;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035
36// Returns the current time in milliseconds.
Peter Boström0c4e06b2015-10-07 12:23:21 +020037uint32_t Time();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038// Returns the current time in microseconds.
Peter Boström0c4e06b2015-10-07 12:23:21 +020039uint64_t TimeMicros();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040// Returns the current time in nanoseconds.
Peter Boström0c4e06b2015-10-07 12:23:21 +020041uint64_t TimeNanos();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042
43// Stores current time in *tm and microseconds in *microseconds.
44void CurrentTmTime(struct tm *tm, int *microseconds);
45
46// Returns a future timestamp, 'elapsed' milliseconds from now.
Peter Boström0c4e06b2015-10-07 12:23:21 +020047uint32_t TimeAfter(int32_t elapsed);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048
49// Comparisons between time values, which can wrap around.
Peter Boström0c4e06b2015-10-07 12:23:21 +020050bool TimeIsBetween(uint32_t earlier,
51 uint32_t middle,
52 uint32_t later); // Inclusive
53bool TimeIsLaterOrEqual(uint32_t earlier, uint32_t later); // Inclusive
54bool TimeIsLater(uint32_t earlier, uint32_t later); // Exclusive
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000055
56// Returns the later of two timestamps.
Peter Boström0c4e06b2015-10-07 12:23:21 +020057inline uint32_t TimeMax(uint32_t ts1, uint32_t ts2) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000058 return TimeIsLaterOrEqual(ts1, ts2) ? ts2 : ts1;
59}
60
61// Returns the earlier of two timestamps.
Peter Boström0c4e06b2015-10-07 12:23:21 +020062inline uint32_t TimeMin(uint32_t ts1, uint32_t ts2) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063 return TimeIsLaterOrEqual(ts1, ts2) ? ts1 : ts2;
64}
65
66// Number of milliseconds that would elapse between 'earlier' and 'later'
67// timestamps. The value is negative if 'later' occurs before 'earlier'.
Peter Boström0c4e06b2015-10-07 12:23:21 +020068int32_t TimeDiff(uint32_t later, uint32_t earlier);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069
70// The number of milliseconds that have elapsed since 'earlier'.
Peter Boström0c4e06b2015-10-07 12:23:21 +020071inline int32_t TimeSince(uint32_t earlier) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000072 return TimeDiff(Time(), earlier);
73}
74
75// The number of milliseconds that will elapse between now and 'later'.
Peter Boström0c4e06b2015-10-07 12:23:21 +020076inline int32_t TimeUntil(uint32_t later) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000077 return TimeDiff(later, Time());
78}
79
80// Converts a unix timestamp in nanoseconds to an NTP timestamp in ms.
Peter Boström0c4e06b2015-10-07 12:23:21 +020081inline int64_t UnixTimestampNanosecsToNtpMillisecs(int64_t unix_ts_ns) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000082 return unix_ts_ns / kNumNanosecsPerMillisec + kJan1970AsNtpMillisecs;
83}
84
henrike@webrtc.org99b41622014-05-21 20:42:17 +000085class TimestampWrapAroundHandler {
86 public:
87 TimestampWrapAroundHandler();
88
Peter Boström0c4e06b2015-10-07 12:23:21 +020089 int64_t Unwrap(uint32_t ts);
henrike@webrtc.org99b41622014-05-21 20:42:17 +000090
91 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +020092 uint32_t last_ts_;
93 int64_t num_wrap_;
henrike@webrtc.org99b41622014-05-21 20:42:17 +000094};
95
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096} // namespace rtc
97
98#endif // WEBRTC_BASE_TIMEUTILS_H_