blob: 4e38a031409f0f05ab5838774bc7eef10a6907b3 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_TIMEUTILS_H_
12#define RTC_BASE_TIMEUTILS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <stdint.h>
15#include <time.h>
Steve Anton300bf8e2017-07-14 10:13:10 -070016#include <string>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020019#include "rtc_base/strings/string_builder.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020020
21namespace rtc {
22
23static const int64_t kNumMillisecsPerSec = INT64_C(1000);
24static const int64_t kNumMicrosecsPerSec = INT64_C(1000000);
25static const int64_t kNumNanosecsPerSec = INT64_C(1000000000);
26
27static const int64_t kNumMicrosecsPerMillisec =
28 kNumMicrosecsPerSec / kNumMillisecsPerSec;
29static const int64_t kNumNanosecsPerMillisec =
30 kNumNanosecsPerSec / kNumMillisecsPerSec;
31static const int64_t kNumNanosecsPerMicrosec =
32 kNumNanosecsPerSec / kNumMicrosecsPerSec;
33
34// TODO(honghaiz): Define a type for the time value specifically.
35
36class ClockInterface {
37 public:
38 virtual ~ClockInterface() {}
39 virtual int64_t TimeNanos() const = 0;
40};
41
42// Sets the global source of time. This is useful mainly for unit tests.
43//
44// Returns the previously set ClockInterface, or nullptr if none is set.
45//
46// Does not transfer ownership of the clock. SetClockForTesting(nullptr)
47// should be called before the ClockInterface is deleted.
48//
49// This method is not thread-safe; it should only be used when no other thread
50// is running (for example, at the start/end of a unit test, or start/end of
51// main()).
52//
53// TODO(deadbeef): Instead of having functions that access this global
54// ClockInterface, we may want to pass the ClockInterface into everything
55// that uses it, eliminating the need for a global variable and this function.
56ClockInterface* SetClockForTesting(ClockInterface* clock);
57
58// Returns previously set clock, or nullptr if no custom clock is being used.
59ClockInterface* GetClockForTesting();
60
61// Returns the actual system time, even if a clock is set for testing.
62// Useful for timeouts while using a test clock, or for logging.
63int64_t SystemTimeNanos();
64int64_t SystemTimeMillis();
65
66// Returns the current time in milliseconds in 32 bits.
67uint32_t Time32();
68
69// Returns the current time in milliseconds in 64 bits.
70int64_t TimeMillis();
71// Deprecated. Do not use this in any new code.
72inline int64_t Time() {
73 return TimeMillis();
74}
75
76// Returns the current time in microseconds.
77int64_t TimeMicros();
78
79// Returns the current time in nanoseconds.
80int64_t TimeNanos();
81
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020082// Returns a future timestamp, 'elapsed' milliseconds from now.
83int64_t TimeAfter(int64_t elapsed);
84
85// Number of milliseconds that would elapse between 'earlier' and 'later'
86// timestamps. The value is negative if 'later' occurs before 'earlier'.
87int64_t TimeDiff(int64_t later, int64_t earlier);
88int32_t TimeDiff32(uint32_t later, uint32_t earlier);
89
90// The number of milliseconds that have elapsed since 'earlier'.
91inline int64_t TimeSince(int64_t earlier) {
92 return TimeMillis() - earlier;
93}
94
95// The number of milliseconds that will elapse between now and 'later'.
96inline int64_t TimeUntil(int64_t later) {
97 return later - TimeMillis();
98}
99
100class TimestampWrapAroundHandler {
101 public:
102 TimestampWrapAroundHandler();
103
104 int64_t Unwrap(uint32_t ts);
105
106 private:
107 uint32_t last_ts_;
108 int64_t num_wrap_;
109};
110
Yves Gerey988cc082018-10-23 12:03:01 +0200111// Convert from tm, which is relative to 1900-01-01 00:00 to number of
112// seconds from 1970-01-01 00:00 ("epoch"). Don't return time_t since that
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200113// is still 32 bits on many systems.
Yves Gerey988cc082018-10-23 12:03:01 +0200114int64_t TmToSeconds(const tm& tm);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200115
116// Return the number of microseconds since January 1, 1970, UTC.
117// Useful mainly when producing logs to be correlated with other
118// devices, and when the devices in question all have properly
119// synchronized clocks.
120//
121// Note that this function obeys the system's idea about what the time
122// is. It is not guaranteed to be monotonic; it will jump in case the
123// system time is changed, e.g., by some other process calling
124// settimeofday. Always use rtc::TimeMicros(), not this function, for
125// measuring time intervals and timeouts.
126int64_t TimeUTCMicros();
127
Minyue Li656d6092018-08-10 15:38:52 +0200128// Return the number of milliseconds since January 1, 1970, UTC.
129// See above.
130int64_t TimeUTCMillis();
131
Steve Anton300bf8e2017-07-14 10:13:10 -0700132// Interval of time from the range [min, max] inclusive.
133class IntervalRange {
134 public:
135 IntervalRange() : min_(0), max_(0) {}
136 IntervalRange(int min, int max) : min_(min), max_(max) {
137 RTC_DCHECK_LE(min, max);
138 }
139
140 int min() const { return min_; }
141 int max() const { return max_; }
142
143 std::string ToString() const {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200144 rtc::StringBuilder ss;
Steve Anton300bf8e2017-07-14 10:13:10 -0700145 ss << "[" << min_ << "," << max_ << "]";
Jonas Olsson84df1c72018-09-14 16:59:32 +0200146 return ss.Release();
Steve Anton300bf8e2017-07-14 10:13:10 -0700147 }
148
149 bool operator==(const IntervalRange& o) const {
150 return min_ == o.min_ && max_ == o.max_;
151 }
152
Qingsi Wang866e08d2018-03-22 17:54:23 -0700153 bool operator!=(const IntervalRange& o) const { return !operator==(o); }
154
Steve Anton300bf8e2017-07-14 10:13:10 -0700155 private:
156 int min_;
157 int max_;
158};
159
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200160} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000161
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200162#endif // RTC_BASE_TIMEUTILS_H_