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