blob: f924d5fddc46e17ba274a3213c67a355f5fd13bc [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 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#include <stdint.h>
12
13#if defined(WEBRTC_POSIX)
14#include <sys/time.h>
15#if defined(WEBRTC_MAC)
16#include <mach/mach_time.h>
17#endif
18#endif
19
20#if defined(WEBRTC_WIN)
andrew@webrtc.org6ae5a6d2014-09-16 01:03:29 +000021#ifndef WIN32_LEAN_AND_MEAN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022#define WIN32_LEAN_AND_MEAN
andrew@webrtc.org6ae5a6d2014-09-16 01:03:29 +000023#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024#include <windows.h>
25#include <mmsystem.h>
nissecdf37a92016-09-13 23:41:47 -070026#include <sys/timeb.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027#endif
28
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/checks.h"
30#include "rtc_base/timeutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032namespace rtc {
33
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070034ClockInterface* g_clock = nullptr;
35
deadbeeff5f03e82016-06-06 11:16:06 -070036ClockInterface* SetClockForTesting(ClockInterface* clock) {
37 ClockInterface* prev = g_clock;
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070038 g_clock = clock;
deadbeeff5f03e82016-06-06 11:16:06 -070039 return prev;
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070040}
41
deadbeef22e08142017-06-12 14:30:28 -070042ClockInterface* GetClockForTesting() {
43 return g_clock;
44}
45
nissedeb95f32016-11-28 01:54:54 -080046int64_t SystemTimeNanos() {
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070047 int64_t ticks;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048#if defined(WEBRTC_MAC)
49 static mach_timebase_info_data_t timebase;
50 if (timebase.denom == 0) {
51 // Get the timebase if this is the first time we run.
52 // Recommended by Apple's QA1398.
andrew@webrtc.org6ae5a6d2014-09-16 01:03:29 +000053 if (mach_timebase_info(&timebase) != KERN_SUCCESS) {
nisseeb4ca4e2017-01-12 02:24:27 -080054 RTC_NOTREACHED();
andrew@webrtc.org6ae5a6d2014-09-16 01:03:29 +000055 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000056 }
57 // Use timebase to convert absolute time tick units into nanoseconds.
58 ticks = mach_absolute_time() * timebase.numer / timebase.denom;
59#elif defined(WEBRTC_POSIX)
60 struct timespec ts;
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070061 // TODO(deadbeef): Do we need to handle the case when CLOCK_MONOTONIC is not
62 // supported?
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063 clock_gettime(CLOCK_MONOTONIC, &ts);
Peter Boström0c4e06b2015-10-07 12:23:21 +020064 ticks = kNumNanosecsPerSec * static_cast<int64_t>(ts.tv_sec) +
65 static_cast<int64_t>(ts.tv_nsec);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066#elif defined(WEBRTC_WIN)
67 static volatile LONG last_timegettime = 0;
Peter Boström0c4e06b2015-10-07 12:23:21 +020068 static volatile int64_t num_wrap_timegettime = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069 volatile LONG* last_timegettime_ptr = &last_timegettime;
70 DWORD now = timeGetTime();
71 // Atomically update the last gotten time
72 DWORD old = InterlockedExchange(last_timegettime_ptr, now);
73 if (now < old) {
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070074 // If now is earlier than old, there may have been a race between threads.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075 // 0x0fffffff ~3.1 days, the code will not take that long to execute
76 // so it must have been a wrap around.
77 if (old > 0xf0000000 && now < 0x0fffffff) {
78 num_wrap_timegettime++;
79 }
80 }
81 ticks = now + (num_wrap_timegettime << 32);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070082 // TODO(deadbeef): Calculate with nanosecond precision. Otherwise, we're
83 // just wasting a multiply and divide when doing Time() on Windows.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000084 ticks = ticks * kNumNanosecsPerMillisec;
Erik Språng1c390982016-01-27 12:55:33 +010085#else
86#error Unsupported platform.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000087#endif
88 return ticks;
89}
90
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -070091int64_t SystemTimeMillis() {
92 return static_cast<int64_t>(SystemTimeNanos() / kNumNanosecsPerMillisec);
93}
94
nissedeb95f32016-11-28 01:54:54 -080095int64_t TimeNanos() {
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -070096 if (g_clock) {
97 return g_clock->TimeNanos();
98 }
99 return SystemTimeNanos();
100}
101
honghaiz34b11eb2016-03-16 08:55:44 -0700102uint32_t Time32() {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200103 return static_cast<uint32_t>(TimeNanos() / kNumNanosecsPerMillisec);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000104}
105
nisse1bffc1d2016-05-02 08:18:55 -0700106int64_t TimeMillis() {
nissedeb95f32016-11-28 01:54:54 -0800107 return TimeNanos() / kNumNanosecsPerMillisec;
honghaiz34b11eb2016-03-16 08:55:44 -0700108}
109
nissedeb95f32016-11-28 01:54:54 -0800110int64_t TimeMicros() {
111 return TimeNanos() / kNumNanosecsPerMicrosec;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000112}
113
Honghai Zhang82d78622016-05-06 11:29:15 -0700114int64_t TimeAfter(int64_t elapsed) {
henrikg91d6ede2015-09-17 00:24:34 -0700115 RTC_DCHECK_GE(elapsed, 0);
Honghai Zhang82d78622016-05-06 11:29:15 -0700116 return TimeMillis() + elapsed;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000117}
118
Honghai Zhang82d78622016-05-06 11:29:15 -0700119int32_t TimeDiff32(uint32_t later, uint32_t earlier) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000120 return later - earlier;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000121}
122
Honghai Zhang82d78622016-05-06 11:29:15 -0700123int64_t TimeDiff(int64_t later, int64_t earlier) {
honghaiz34b11eb2016-03-16 08:55:44 -0700124 return later - earlier;
125}
126
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000127TimestampWrapAroundHandler::TimestampWrapAroundHandler()
sprang1b3530b2016-03-10 01:32:53 -0800128 : last_ts_(0), num_wrap_(-1) {}
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000129
Peter Boström0c4e06b2015-10-07 12:23:21 +0200130int64_t TimestampWrapAroundHandler::Unwrap(uint32_t ts) {
sprang1b3530b2016-03-10 01:32:53 -0800131 if (num_wrap_ == -1) {
132 last_ts_ = ts;
133 num_wrap_ = 0;
134 return ts;
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000135 }
sprang1b3530b2016-03-10 01:32:53 -0800136
137 if (ts < last_ts_) {
138 if (last_ts_ >= 0xf0000000 && ts < 0x0fffffff)
139 ++num_wrap_;
140 } else if ((ts - last_ts_) > 0xf0000000) {
141 // Backwards wrap. Unwrap with last wrap count and don't update last_ts_.
142 return ts + ((num_wrap_ - 1) << 32);
143 }
144
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000145 last_ts_ = ts;
sprang1b3530b2016-03-10 01:32:53 -0800146 return ts + (num_wrap_ << 32);
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000147}
148
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100149int64_t TmToSeconds(const std::tm& tm) {
150 static short int mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
151 static short int cumul_mdays[12] = {0, 31, 59, 90, 120, 151,
152 181, 212, 243, 273, 304, 334};
153 int year = tm.tm_year + 1900;
154 int month = tm.tm_mon;
155 int day = tm.tm_mday - 1; // Make 0-based like the rest.
156 int hour = tm.tm_hour;
157 int min = tm.tm_min;
158 int sec = tm.tm_sec;
159
160 bool expiry_in_leap_year = (year % 4 == 0 &&
161 (year % 100 != 0 || year % 400 == 0));
162
163 if (year < 1970)
164 return -1;
165 if (month < 0 || month > 11)
166 return -1;
167 if (day < 0 || day >= mdays[month] + (expiry_in_leap_year && month == 2 - 1))
168 return -1;
169 if (hour < 0 || hour > 23)
170 return -1;
171 if (min < 0 || min > 59)
172 return -1;
173 if (sec < 0 || sec > 59)
174 return -1;
175
176 day += cumul_mdays[month];
177
178 // Add number of leap days between 1970 and the expiration year, inclusive.
179 day += ((year / 4 - 1970 / 4) - (year / 100 - 1970 / 100) +
180 (year / 400 - 1970 / 400));
181
182 // We will have added one day too much above if expiration is during a leap
183 // year, and expiration is in January or February.
184 if (expiry_in_leap_year && month <= 2 - 1) // |month| is zero based.
185 day -= 1;
186
187 // Combine all variables into seconds from 1970-01-01 00:00 (except |month|
188 // which was accumulated into |day| above).
189 return (((static_cast<int64_t>
190 (year - 1970) * 365 + day) * 24 + hour) * 60 + min) * 60 + sec;
191}
192
nissecdf37a92016-09-13 23:41:47 -0700193int64_t TimeUTCMicros() {
194#if defined(WEBRTC_POSIX)
195 struct timeval time;
deadbeef37f5ecf2017-02-27 14:06:41 -0800196 gettimeofday(&time, nullptr);
nissecdf37a92016-09-13 23:41:47 -0700197 // Convert from second (1.0) and microsecond (1e-6).
198 return (static_cast<int64_t>(time.tv_sec) * rtc::kNumMicrosecsPerSec +
199 time.tv_usec);
200
201#elif defined(WEBRTC_WIN)
202 struct _timeb time;
203 _ftime(&time);
204 // Convert from second (1.0) and milliseconds (1e-3).
205 return (static_cast<int64_t>(time.time) * rtc::kNumMicrosecsPerSec +
206 static_cast<int64_t>(time.millitm) * rtc::kNumMicrosecsPerMillisec);
207#endif
208}
209
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000210} // namespace rtc