blob: ff22a6149a65efd5aa8624298f02e2daa1ea9e41 [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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_TIME_UTILS_H_
12#define RTC_BASE_TIME_UTILS_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>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020018#include "rtc_base/system/rtc_export.h"
Johannes Kronb73c9f02021-02-15 13:29:45 +010019#include "rtc_base/system_time.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.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020056RTC_EXPORT ClockInterface* SetClockForTesting(ClockInterface* clock);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020057
58// Returns previously set clock, or nullptr if no custom clock is being used.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020059RTC_EXPORT ClockInterface* GetClockForTesting();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020060
Robin Raymondce1b1402018-11-22 20:10:11 -050061#if defined(WINUWP)
62// Synchronizes the current clock based upon an NTP server's epoch in
63// milliseconds.
64void SyncWithNtp(int64_t time_from_ntp_server_ms);
65#endif // defined(WINUWP)
66
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020067// Returns the actual system time, even if a clock is set for testing.
68// Useful for timeouts while using a test clock, or for logging.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020069int64_t SystemTimeMillis();
70
71// Returns the current time in milliseconds in 32 bits.
72uint32_t Time32();
73
74// Returns the current time in milliseconds in 64 bits.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020075RTC_EXPORT int64_t TimeMillis();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020076// Deprecated. Do not use this in any new code.
77inline int64_t Time() {
78 return TimeMillis();
79}
80
81// Returns the current time in microseconds.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020082RTC_EXPORT int64_t TimeMicros();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020083
84// Returns the current time in nanoseconds.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020085RTC_EXPORT int64_t TimeNanos();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020086
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020087// Returns a future timestamp, 'elapsed' milliseconds from now.
88int64_t TimeAfter(int64_t elapsed);
89
90// Number of milliseconds that would elapse between 'earlier' and 'later'
91// timestamps. The value is negative if 'later' occurs before 'earlier'.
92int64_t TimeDiff(int64_t later, int64_t earlier);
93int32_t TimeDiff32(uint32_t later, uint32_t earlier);
94
95// The number of milliseconds that have elapsed since 'earlier'.
96inline int64_t TimeSince(int64_t earlier) {
97 return TimeMillis() - earlier;
98}
99
100// The number of milliseconds that will elapse between now and 'later'.
101inline int64_t TimeUntil(int64_t later) {
102 return later - TimeMillis();
103}
104
105class TimestampWrapAroundHandler {
106 public:
107 TimestampWrapAroundHandler();
108
109 int64_t Unwrap(uint32_t ts);
110
111 private:
112 uint32_t last_ts_;
113 int64_t num_wrap_;
114};
115
Yves Gerey988cc082018-10-23 12:03:01 +0200116// Convert from tm, which is relative to 1900-01-01 00:00 to number of
117// seconds from 1970-01-01 00:00 ("epoch"). Don't return time_t since that
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200118// is still 32 bits on many systems.
Yves Gerey988cc082018-10-23 12:03:01 +0200119int64_t TmToSeconds(const tm& tm);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200120
121// Return the number of microseconds since January 1, 1970, UTC.
122// Useful mainly when producing logs to be correlated with other
123// devices, and when the devices in question all have properly
124// synchronized clocks.
125//
126// Note that this function obeys the system's idea about what the time
127// is. It is not guaranteed to be monotonic; it will jump in case the
128// system time is changed, e.g., by some other process calling
129// settimeofday. Always use rtc::TimeMicros(), not this function, for
130// measuring time intervals and timeouts.
131int64_t TimeUTCMicros();
132
Minyue Li656d6092018-08-10 15:38:52 +0200133// Return the number of milliseconds since January 1, 1970, UTC.
134// See above.
135int64_t TimeUTCMillis();
136
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200137} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000138
Steve Anton10542f22019-01-11 09:11:00 -0800139#endif // RTC_BASE_TIME_UTILS_H_