blob: 4af0d9538f058f9d969f954cd9a7fb5620463fd7 [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
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +010014#include <ctime>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015#include <time.h>
16
17#include "webrtc/base/basictypes.h"
18
19namespace rtc {
20
Peter Boström0c4e06b2015-10-07 12:23:21 +020021static const int64_t kNumMillisecsPerSec = INT64_C(1000);
22static const int64_t kNumMicrosecsPerSec = INT64_C(1000000);
23static const int64_t kNumNanosecsPerSec = INT64_C(1000000000);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024
Peter Boström0c4e06b2015-10-07 12:23:21 +020025static const int64_t kNumMicrosecsPerMillisec =
26 kNumMicrosecsPerSec / kNumMillisecsPerSec;
27static const int64_t kNumNanosecsPerMillisec =
28 kNumNanosecsPerSec / kNumMillisecsPerSec;
29static const int64_t kNumNanosecsPerMicrosec =
30 kNumNanosecsPerSec / kNumMicrosecsPerSec;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031
Peter Boström0c4e06b2015-10-07 12:23:21 +020032typedef uint32_t TimeStamp;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033
honghaiz34b11eb2016-03-16 08:55:44 -070034// Returns the current time in milliseconds in 32 bits.
35uint32_t Time32();
36
37// Returns the current time in milliseconds in 64 bits.
38int64_t Time64();
39
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040// Returns the current time in milliseconds.
honghaiz34b11eb2016-03-16 08:55:44 -070041// TODO(honghaiz): Returns Time64 once majority of the webrtc code migrates to
42// 64-bit timestamp.
43inline uint32_t Time() {
44 return Time32();
45}
46
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000047// Returns the current time in microseconds.
Peter Boström0c4e06b2015-10-07 12:23:21 +020048uint64_t TimeMicros();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049// Returns the current time in nanoseconds.
Peter Boström0c4e06b2015-10-07 12:23:21 +020050uint64_t TimeNanos();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000051
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000052// Returns a future timestamp, 'elapsed' milliseconds from now.
Peter Boström0c4e06b2015-10-07 12:23:21 +020053uint32_t TimeAfter(int32_t elapsed);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000054
Peter Boström0c4e06b2015-10-07 12:23:21 +020055bool TimeIsLaterOrEqual(uint32_t earlier, uint32_t later); // Inclusive
56bool TimeIsLater(uint32_t earlier, uint32_t later); // Exclusive
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000057
58// Returns the later of two timestamps.
Peter Boström0c4e06b2015-10-07 12:23:21 +020059inline uint32_t TimeMax(uint32_t ts1, uint32_t ts2) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000060 return TimeIsLaterOrEqual(ts1, ts2) ? ts2 : ts1;
61}
62
63// Returns the earlier of two timestamps.
Peter Boström0c4e06b2015-10-07 12:23:21 +020064inline uint32_t TimeMin(uint32_t ts1, uint32_t ts2) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000065 return TimeIsLaterOrEqual(ts1, ts2) ? ts1 : ts2;
66}
67
68// Number of milliseconds that would elapse between 'earlier' and 'later'
69// timestamps. The value is negative if 'later' occurs before 'earlier'.
Peter Boström0c4e06b2015-10-07 12:23:21 +020070int32_t TimeDiff(uint32_t later, uint32_t earlier);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000071
honghaiz34b11eb2016-03-16 08:55:44 -070072// Number of milliseconds that would elapse between 'earlier' and 'later'
73// timestamps. The value is negative if 'later' occurs before 'earlier'.
74int64_t TimeDiff64(int64_t later, int64_t earlier);
75
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076// The number of milliseconds that have elapsed since 'earlier'.
Peter Boström0c4e06b2015-10-07 12:23:21 +020077inline int32_t TimeSince(uint32_t earlier) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000078 return TimeDiff(Time(), earlier);
79}
80
81// The number of milliseconds that will elapse between now and 'later'.
Peter Boström0c4e06b2015-10-07 12:23:21 +020082inline int32_t TimeUntil(uint32_t later) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083 return TimeDiff(later, Time());
84}
85
henrike@webrtc.org99b41622014-05-21 20:42:17 +000086class TimestampWrapAroundHandler {
87 public:
88 TimestampWrapAroundHandler();
89
Peter Boström0c4e06b2015-10-07 12:23:21 +020090 int64_t Unwrap(uint32_t ts);
henrike@webrtc.org99b41622014-05-21 20:42:17 +000091
92 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +020093 uint32_t last_ts_;
94 int64_t num_wrap_;
henrike@webrtc.org99b41622014-05-21 20:42:17 +000095};
96
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +010097// 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
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000102} // namespace rtc
103
104#endif // WEBRTC_BASE_TIMEUTILS_H_