blob: 1be368eb5847a95cca75833ee0e4d0e57644afcd [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>
26#endif
27
andrew@webrtc.org6ae5a6d2014-09-16 01:03:29 +000028#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000029#include "webrtc/base/timeutils.h"
30
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031namespace rtc {
32
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070033ClockInterface* g_clock = nullptr;
34
deadbeeff5f03e82016-06-06 11:16:06 -070035ClockInterface* SetClockForTesting(ClockInterface* clock) {
36 ClockInterface* prev = g_clock;
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070037 g_clock = clock;
deadbeeff5f03e82016-06-06 11:16:06 -070038 return prev;
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070039}
40
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -070041uint64_t SystemTimeNanos() {
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070042 int64_t ticks;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043#if defined(WEBRTC_MAC)
44 static mach_timebase_info_data_t timebase;
45 if (timebase.denom == 0) {
46 // Get the timebase if this is the first time we run.
47 // Recommended by Apple's QA1398.
andrew@webrtc.org6ae5a6d2014-09-16 01:03:29 +000048 if (mach_timebase_info(&timebase) != KERN_SUCCESS) {
henrikg91d6ede2015-09-17 00:24:34 -070049 RTC_DCHECK(false);
andrew@webrtc.org6ae5a6d2014-09-16 01:03:29 +000050 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000051 }
52 // Use timebase to convert absolute time tick units into nanoseconds.
53 ticks = mach_absolute_time() * timebase.numer / timebase.denom;
54#elif defined(WEBRTC_POSIX)
55 struct timespec ts;
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070056 // TODO(deadbeef): Do we need to handle the case when CLOCK_MONOTONIC is not
57 // supported?
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000058 clock_gettime(CLOCK_MONOTONIC, &ts);
Peter Boström0c4e06b2015-10-07 12:23:21 +020059 ticks = kNumNanosecsPerSec * static_cast<int64_t>(ts.tv_sec) +
60 static_cast<int64_t>(ts.tv_nsec);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000061#elif defined(WEBRTC_WIN)
62 static volatile LONG last_timegettime = 0;
Peter Boström0c4e06b2015-10-07 12:23:21 +020063 static volatile int64_t num_wrap_timegettime = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000064 volatile LONG* last_timegettime_ptr = &last_timegettime;
65 DWORD now = timeGetTime();
66 // Atomically update the last gotten time
67 DWORD old = InterlockedExchange(last_timegettime_ptr, now);
68 if (now < old) {
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070069 // If now is earlier than old, there may have been a race between threads.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070 // 0x0fffffff ~3.1 days, the code will not take that long to execute
71 // so it must have been a wrap around.
72 if (old > 0xf0000000 && now < 0x0fffffff) {
73 num_wrap_timegettime++;
74 }
75 }
76 ticks = now + (num_wrap_timegettime << 32);
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070077 // TODO(deadbeef): Calculate with nanosecond precision. Otherwise, we're
78 // just wasting a multiply and divide when doing Time() on Windows.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079 ticks = ticks * kNumNanosecsPerMillisec;
Erik Språng1c390982016-01-27 12:55:33 +010080#else
81#error Unsupported platform.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000082#endif
83 return ticks;
84}
85
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -070086int64_t SystemTimeMillis() {
87 return static_cast<int64_t>(SystemTimeNanos() / kNumNanosecsPerMillisec);
88}
89
90uint64_t TimeNanos() {
91 if (g_clock) {
92 return g_clock->TimeNanos();
93 }
94 return SystemTimeNanos();
95}
96
honghaiz34b11eb2016-03-16 08:55:44 -070097uint32_t Time32() {
Peter Boström0c4e06b2015-10-07 12:23:21 +020098 return static_cast<uint32_t>(TimeNanos() / kNumNanosecsPerMillisec);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000099}
100
nisse1bffc1d2016-05-02 08:18:55 -0700101int64_t TimeMillis() {
honghaiz34b11eb2016-03-16 08:55:44 -0700102 return static_cast<int64_t>(TimeNanos() / kNumNanosecsPerMillisec);
103}
104
Peter Boström0c4e06b2015-10-07 12:23:21 +0200105uint64_t TimeMicros() {
106 return static_cast<uint64_t>(TimeNanos() / kNumNanosecsPerMicrosec);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000107}
108
Honghai Zhang82d78622016-05-06 11:29:15 -0700109int64_t TimeAfter(int64_t elapsed) {
henrikg91d6ede2015-09-17 00:24:34 -0700110 RTC_DCHECK_GE(elapsed, 0);
Honghai Zhang82d78622016-05-06 11:29:15 -0700111 return TimeMillis() + elapsed;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000112}
113
Honghai Zhang82d78622016-05-06 11:29:15 -0700114int32_t TimeDiff32(uint32_t later, uint32_t earlier) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000115 return later - earlier;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000116}
117
Honghai Zhang82d78622016-05-06 11:29:15 -0700118int64_t TimeDiff(int64_t later, int64_t earlier) {
honghaiz34b11eb2016-03-16 08:55:44 -0700119 return later - earlier;
120}
121
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000122TimestampWrapAroundHandler::TimestampWrapAroundHandler()
sprang1b3530b2016-03-10 01:32:53 -0800123 : last_ts_(0), num_wrap_(-1) {}
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000124
Peter Boström0c4e06b2015-10-07 12:23:21 +0200125int64_t TimestampWrapAroundHandler::Unwrap(uint32_t ts) {
sprang1b3530b2016-03-10 01:32:53 -0800126 if (num_wrap_ == -1) {
127 last_ts_ = ts;
128 num_wrap_ = 0;
129 return ts;
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000130 }
sprang1b3530b2016-03-10 01:32:53 -0800131
132 if (ts < last_ts_) {
133 if (last_ts_ >= 0xf0000000 && ts < 0x0fffffff)
134 ++num_wrap_;
135 } else if ((ts - last_ts_) > 0xf0000000) {
136 // Backwards wrap. Unwrap with last wrap count and don't update last_ts_.
137 return ts + ((num_wrap_ - 1) << 32);
138 }
139
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000140 last_ts_ = ts;
sprang1b3530b2016-03-10 01:32:53 -0800141 return ts + (num_wrap_ << 32);
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000142}
143
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100144int64_t TmToSeconds(const std::tm& tm) {
145 static short int mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
146 static short int cumul_mdays[12] = {0, 31, 59, 90, 120, 151,
147 181, 212, 243, 273, 304, 334};
148 int year = tm.tm_year + 1900;
149 int month = tm.tm_mon;
150 int day = tm.tm_mday - 1; // Make 0-based like the rest.
151 int hour = tm.tm_hour;
152 int min = tm.tm_min;
153 int sec = tm.tm_sec;
154
155 bool expiry_in_leap_year = (year % 4 == 0 &&
156 (year % 100 != 0 || year % 400 == 0));
157
158 if (year < 1970)
159 return -1;
160 if (month < 0 || month > 11)
161 return -1;
162 if (day < 0 || day >= mdays[month] + (expiry_in_leap_year && month == 2 - 1))
163 return -1;
164 if (hour < 0 || hour > 23)
165 return -1;
166 if (min < 0 || min > 59)
167 return -1;
168 if (sec < 0 || sec > 59)
169 return -1;
170
171 day += cumul_mdays[month];
172
173 // Add number of leap days between 1970 and the expiration year, inclusive.
174 day += ((year / 4 - 1970 / 4) - (year / 100 - 1970 / 100) +
175 (year / 400 - 1970 / 400));
176
177 // We will have added one day too much above if expiration is during a leap
178 // year, and expiration is in January or February.
179 if (expiry_in_leap_year && month <= 2 - 1) // |month| is zero based.
180 day -= 1;
181
182 // Combine all variables into seconds from 1970-01-01 00:00 (except |month|
183 // which was accumulated into |day| above).
184 return (((static_cast<int64_t>
185 (year - 1970) * 365 + day) * 24 + hour) * 60 + min) * 60 + sec;
186}
187
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000188} // namespace rtc