blob: de3e6afb28d824ecb9dfc26f1029dd0d0958fd24 [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
Peter Boström0c4e06b2015-10-07 12:23:21 +020033const uint32_t HALF = 0x80000000;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000034
Peter Boström0c4e06b2015-10-07 12:23:21 +020035uint64_t TimeNanos() {
36 int64_t ticks = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037#if defined(WEBRTC_MAC)
38 static mach_timebase_info_data_t timebase;
39 if (timebase.denom == 0) {
40 // Get the timebase if this is the first time we run.
41 // Recommended by Apple's QA1398.
andrew@webrtc.org6ae5a6d2014-09-16 01:03:29 +000042 if (mach_timebase_info(&timebase) != KERN_SUCCESS) {
henrikg91d6ede2015-09-17 00:24:34 -070043 RTC_DCHECK(false);
andrew@webrtc.org6ae5a6d2014-09-16 01:03:29 +000044 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000045 }
46 // Use timebase to convert absolute time tick units into nanoseconds.
47 ticks = mach_absolute_time() * timebase.numer / timebase.denom;
48#elif defined(WEBRTC_POSIX)
49 struct timespec ts;
50 // TODO: Do we need to handle the case when CLOCK_MONOTONIC
51 // is not supported?
52 clock_gettime(CLOCK_MONOTONIC, &ts);
Peter Boström0c4e06b2015-10-07 12:23:21 +020053 ticks = kNumNanosecsPerSec * static_cast<int64_t>(ts.tv_sec) +
54 static_cast<int64_t>(ts.tv_nsec);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000055#elif defined(WEBRTC_WIN)
56 static volatile LONG last_timegettime = 0;
Peter Boström0c4e06b2015-10-07 12:23:21 +020057 static volatile int64_t num_wrap_timegettime = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000058 volatile LONG* last_timegettime_ptr = &last_timegettime;
59 DWORD now = timeGetTime();
60 // Atomically update the last gotten time
61 DWORD old = InterlockedExchange(last_timegettime_ptr, now);
62 if (now < old) {
63 // If now is earlier than old, there may have been a race between
64 // threads.
65 // 0x0fffffff ~3.1 days, the code will not take that long to execute
66 // so it must have been a wrap around.
67 if (old > 0xf0000000 && now < 0x0fffffff) {
68 num_wrap_timegettime++;
69 }
70 }
71 ticks = now + (num_wrap_timegettime << 32);
72 // TODO: Calculate with nanosecond precision. Otherwise, we're just
73 // wasting a multiply and divide when doing Time() on Windows.
74 ticks = ticks * kNumNanosecsPerMillisec;
Erik Språng1c390982016-01-27 12:55:33 +010075#else
76#error Unsupported platform.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000077#endif
78 return ticks;
79}
80
honghaiz34b11eb2016-03-16 08:55:44 -070081uint32_t Time32() {
Peter Boström0c4e06b2015-10-07 12:23:21 +020082 return static_cast<uint32_t>(TimeNanos() / kNumNanosecsPerMillisec);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083}
84
honghaiz34b11eb2016-03-16 08:55:44 -070085int64_t Time64() {
86 return static_cast<int64_t>(TimeNanos() / kNumNanosecsPerMillisec);
87}
88
Peter Boström0c4e06b2015-10-07 12:23:21 +020089uint64_t TimeMicros() {
90 return static_cast<uint64_t>(TimeNanos() / kNumNanosecsPerMicrosec);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000091}
92
Peter Boström0c4e06b2015-10-07 12:23:21 +020093uint32_t TimeAfter(int32_t elapsed) {
henrikg91d6ede2015-09-17 00:24:34 -070094 RTC_DCHECK_GE(elapsed, 0);
Peter Boström0c4e06b2015-10-07 12:23:21 +020095 RTC_DCHECK_LT(static_cast<uint32_t>(elapsed), HALF);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096 return Time() + elapsed;
97}
98
Peter Boström0c4e06b2015-10-07 12:23:21 +020099bool TimeIsLaterOrEqual(uint32_t earlier, uint32_t later) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200100 int32_t diff = later - earlier;
101 return (diff >= 0 && static_cast<uint32_t>(diff) < HALF);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000102}
103
Peter Boström0c4e06b2015-10-07 12:23:21 +0200104bool TimeIsLater(uint32_t earlier, uint32_t later) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200105 int32_t diff = later - earlier;
106 return (diff > 0 && static_cast<uint32_t>(diff) < HALF);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000107}
108
Peter Boström0c4e06b2015-10-07 12:23:21 +0200109int32_t TimeDiff(uint32_t later, uint32_t earlier) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000110 return later - earlier;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000111}
112
honghaiz34b11eb2016-03-16 08:55:44 -0700113int64_t TimeDiff64(int64_t later, int64_t earlier) {
114 return later - earlier;
115}
116
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000117TimestampWrapAroundHandler::TimestampWrapAroundHandler()
sprang1b3530b2016-03-10 01:32:53 -0800118 : last_ts_(0), num_wrap_(-1) {}
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000119
Peter Boström0c4e06b2015-10-07 12:23:21 +0200120int64_t TimestampWrapAroundHandler::Unwrap(uint32_t ts) {
sprang1b3530b2016-03-10 01:32:53 -0800121 if (num_wrap_ == -1) {
122 last_ts_ = ts;
123 num_wrap_ = 0;
124 return ts;
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000125 }
sprang1b3530b2016-03-10 01:32:53 -0800126
127 if (ts < last_ts_) {
128 if (last_ts_ >= 0xf0000000 && ts < 0x0fffffff)
129 ++num_wrap_;
130 } else if ((ts - last_ts_) > 0xf0000000) {
131 // Backwards wrap. Unwrap with last wrap count and don't update last_ts_.
132 return ts + ((num_wrap_ - 1) << 32);
133 }
134
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000135 last_ts_ = ts;
sprang1b3530b2016-03-10 01:32:53 -0800136 return ts + (num_wrap_ << 32);
henrike@webrtc.org99b41622014-05-21 20:42:17 +0000137}
138
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100139int64_t TmToSeconds(const std::tm& tm) {
140 static short int mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
141 static short int cumul_mdays[12] = {0, 31, 59, 90, 120, 151,
142 181, 212, 243, 273, 304, 334};
143 int year = tm.tm_year + 1900;
144 int month = tm.tm_mon;
145 int day = tm.tm_mday - 1; // Make 0-based like the rest.
146 int hour = tm.tm_hour;
147 int min = tm.tm_min;
148 int sec = tm.tm_sec;
149
150 bool expiry_in_leap_year = (year % 4 == 0 &&
151 (year % 100 != 0 || year % 400 == 0));
152
153 if (year < 1970)
154 return -1;
155 if (month < 0 || month > 11)
156 return -1;
157 if (day < 0 || day >= mdays[month] + (expiry_in_leap_year && month == 2 - 1))
158 return -1;
159 if (hour < 0 || hour > 23)
160 return -1;
161 if (min < 0 || min > 59)
162 return -1;
163 if (sec < 0 || sec > 59)
164 return -1;
165
166 day += cumul_mdays[month];
167
168 // Add number of leap days between 1970 and the expiration year, inclusive.
169 day += ((year / 4 - 1970 / 4) - (year / 100 - 1970 / 100) +
170 (year / 400 - 1970 / 400));
171
172 // We will have added one day too much above if expiration is during a leap
173 // year, and expiration is in January or February.
174 if (expiry_in_leap_year && month <= 2 - 1) // |month| is zero based.
175 day -= 1;
176
177 // Combine all variables into seconds from 1970-01-01 00:00 (except |month|
178 // which was accumulated into |day| above).
179 return (((static_cast<int64_t>
180 (year - 1970) * 365 + day) * 24 + hour) * 60 + min) * 60 + sec;
181}
182
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000183} // namespace rtc