henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 6ae5a6d | 2014-09-16 01:03:29 +0000 | [diff] [blame] | 21 | #ifndef WIN32_LEAN_AND_MEAN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | #define WIN32_LEAN_AND_MEAN |
andrew@webrtc.org | 6ae5a6d | 2014-09-16 01:03:29 +0000 | [diff] [blame] | 23 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 24 | #include <windows.h> |
| 25 | #include <mmsystem.h> |
| 26 | #endif |
| 27 | |
andrew@webrtc.org | 6ae5a6d | 2014-09-16 01:03:29 +0000 | [diff] [blame] | 28 | #include "webrtc/base/checks.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 29 | #include "webrtc/base/timeutils.h" |
| 30 | |
| 31 | #define EFFICIENT_IMPLEMENTATION 1 |
| 32 | |
| 33 | namespace rtc { |
| 34 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 35 | const uint32_t HALF = 0x80000000; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 36 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 37 | uint64_t TimeNanos() { |
| 38 | int64_t ticks = 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 39 | #if defined(WEBRTC_MAC) |
| 40 | static mach_timebase_info_data_t timebase; |
| 41 | if (timebase.denom == 0) { |
| 42 | // Get the timebase if this is the first time we run. |
| 43 | // Recommended by Apple's QA1398. |
andrew@webrtc.org | 6ae5a6d | 2014-09-16 01:03:29 +0000 | [diff] [blame] | 44 | if (mach_timebase_info(&timebase) != KERN_SUCCESS) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 45 | RTC_DCHECK(false); |
andrew@webrtc.org | 6ae5a6d | 2014-09-16 01:03:29 +0000 | [diff] [blame] | 46 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 47 | } |
| 48 | // Use timebase to convert absolute time tick units into nanoseconds. |
| 49 | ticks = mach_absolute_time() * timebase.numer / timebase.denom; |
| 50 | #elif defined(WEBRTC_POSIX) |
| 51 | struct timespec ts; |
| 52 | // TODO: Do we need to handle the case when CLOCK_MONOTONIC |
| 53 | // is not supported? |
| 54 | clock_gettime(CLOCK_MONOTONIC, &ts); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 55 | ticks = kNumNanosecsPerSec * static_cast<int64_t>(ts.tv_sec) + |
| 56 | static_cast<int64_t>(ts.tv_nsec); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 57 | #elif defined(WEBRTC_WIN) |
| 58 | static volatile LONG last_timegettime = 0; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 59 | static volatile int64_t num_wrap_timegettime = 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 60 | volatile LONG* last_timegettime_ptr = &last_timegettime; |
| 61 | DWORD now = timeGetTime(); |
| 62 | // Atomically update the last gotten time |
| 63 | DWORD old = InterlockedExchange(last_timegettime_ptr, now); |
| 64 | if (now < old) { |
| 65 | // If now is earlier than old, there may have been a race between |
| 66 | // threads. |
| 67 | // 0x0fffffff ~3.1 days, the code will not take that long to execute |
| 68 | // so it must have been a wrap around. |
| 69 | if (old > 0xf0000000 && now < 0x0fffffff) { |
| 70 | num_wrap_timegettime++; |
| 71 | } |
| 72 | } |
| 73 | ticks = now + (num_wrap_timegettime << 32); |
| 74 | // TODO: Calculate with nanosecond precision. Otherwise, we're just |
| 75 | // wasting a multiply and divide when doing Time() on Windows. |
| 76 | ticks = ticks * kNumNanosecsPerMillisec; |
Erik Språng | 1c39098 | 2016-01-27 12:55:33 +0100 | [diff] [blame] | 77 | #else |
| 78 | #error Unsupported platform. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 79 | #endif |
| 80 | return ticks; |
| 81 | } |
| 82 | |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame^] | 83 | uint32_t Time32() { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 84 | return static_cast<uint32_t>(TimeNanos() / kNumNanosecsPerMillisec); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 85 | } |
| 86 | |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame^] | 87 | int64_t Time64() { |
| 88 | return static_cast<int64_t>(TimeNanos() / kNumNanosecsPerMillisec); |
| 89 | } |
| 90 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 91 | uint64_t TimeMicros() { |
| 92 | return static_cast<uint64_t>(TimeNanos() / kNumNanosecsPerMicrosec); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | #if defined(WEBRTC_WIN) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 96 | static const uint64_t kFileTimeToUnixTimeEpochOffset = 116444736000000000ULL; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 97 | |
| 98 | struct timeval { |
| 99 | long tv_sec, tv_usec; // NOLINT |
| 100 | }; |
| 101 | |
| 102 | // Emulate POSIX gettimeofday(). |
| 103 | // Based on breakpad/src/third_party/glog/src/utilities.cc |
| 104 | static int gettimeofday(struct timeval *tv, void *tz) { |
| 105 | // FILETIME is measured in tens of microseconds since 1601-01-01 UTC. |
| 106 | FILETIME ft; |
| 107 | GetSystemTimeAsFileTime(&ft); |
| 108 | |
| 109 | LARGE_INTEGER li; |
| 110 | li.LowPart = ft.dwLowDateTime; |
| 111 | li.HighPart = ft.dwHighDateTime; |
| 112 | |
| 113 | // Convert to seconds and microseconds since Unix time Epoch. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 114 | int64_t micros = (li.QuadPart - kFileTimeToUnixTimeEpochOffset) / 10; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 115 | tv->tv_sec = static_cast<long>(micros / kNumMicrosecsPerSec); // NOLINT |
| 116 | tv->tv_usec = static_cast<long>(micros % kNumMicrosecsPerSec); // NOLINT |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | // Emulate POSIX gmtime_r(). |
| 122 | static struct tm *gmtime_r(const time_t *timep, struct tm *result) { |
| 123 | // On Windows, gmtime is thread safe. |
| 124 | struct tm *tm = gmtime(timep); // NOLINT |
| 125 | if (tm == NULL) { |
| 126 | return NULL; |
| 127 | } |
| 128 | *result = *tm; |
| 129 | return result; |
| 130 | } |
andrew@webrtc.org | 6ae5a6d | 2014-09-16 01:03:29 +0000 | [diff] [blame] | 131 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 132 | |
| 133 | void CurrentTmTime(struct tm *tm, int *microseconds) { |
| 134 | struct timeval timeval; |
| 135 | if (gettimeofday(&timeval, NULL) < 0) { |
| 136 | // Incredibly unlikely code path. |
| 137 | timeval.tv_sec = timeval.tv_usec = 0; |
| 138 | } |
| 139 | time_t secs = timeval.tv_sec; |
| 140 | gmtime_r(&secs, tm); |
| 141 | *microseconds = timeval.tv_usec; |
| 142 | } |
| 143 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 144 | uint32_t TimeAfter(int32_t elapsed) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 145 | RTC_DCHECK_GE(elapsed, 0); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 146 | RTC_DCHECK_LT(static_cast<uint32_t>(elapsed), HALF); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 147 | return Time() + elapsed; |
| 148 | } |
| 149 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 150 | bool TimeIsBetween(uint32_t earlier, uint32_t middle, uint32_t later) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 151 | if (earlier <= later) { |
| 152 | return ((earlier <= middle) && (middle <= later)); |
| 153 | } else { |
| 154 | return !((later < middle) && (middle < earlier)); |
| 155 | } |
| 156 | } |
| 157 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 158 | bool TimeIsLaterOrEqual(uint32_t earlier, uint32_t later) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 159 | #if EFFICIENT_IMPLEMENTATION |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 160 | int32_t diff = later - earlier; |
| 161 | return (diff >= 0 && static_cast<uint32_t>(diff) < HALF); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 162 | #else |
| 163 | const bool later_or_equal = TimeIsBetween(earlier, later, earlier + HALF); |
| 164 | return later_or_equal; |
| 165 | #endif |
| 166 | } |
| 167 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 168 | bool TimeIsLater(uint32_t earlier, uint32_t later) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 169 | #if EFFICIENT_IMPLEMENTATION |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 170 | int32_t diff = later - earlier; |
| 171 | return (diff > 0 && static_cast<uint32_t>(diff) < HALF); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 172 | #else |
| 173 | const bool earlier_or_equal = TimeIsBetween(later, earlier, later + HALF); |
| 174 | return !earlier_or_equal; |
| 175 | #endif |
| 176 | } |
| 177 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 178 | int32_t TimeDiff(uint32_t later, uint32_t earlier) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 179 | #if EFFICIENT_IMPLEMENTATION |
| 180 | return later - earlier; |
| 181 | #else |
| 182 | const bool later_or_equal = TimeIsBetween(earlier, later, earlier + HALF); |
| 183 | if (later_or_equal) { |
| 184 | if (earlier <= later) { |
| 185 | return static_cast<long>(later - earlier); |
| 186 | } else { |
| 187 | return static_cast<long>(later + (UINT32_MAX - earlier) + 1); |
| 188 | } |
| 189 | } else { |
| 190 | if (later <= earlier) { |
| 191 | return -static_cast<long>(earlier - later); |
| 192 | } else { |
| 193 | return -static_cast<long>(earlier + (UINT32_MAX - later) + 1); |
| 194 | } |
| 195 | } |
| 196 | #endif |
| 197 | } |
| 198 | |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame^] | 199 | int64_t TimeDiff64(int64_t later, int64_t earlier) { |
| 200 | return later - earlier; |
| 201 | } |
| 202 | |
henrike@webrtc.org | 99b4162 | 2014-05-21 20:42:17 +0000 | [diff] [blame] | 203 | TimestampWrapAroundHandler::TimestampWrapAroundHandler() |
sprang | 1b3530b | 2016-03-10 01:32:53 -0800 | [diff] [blame] | 204 | : last_ts_(0), num_wrap_(-1) {} |
henrike@webrtc.org | 99b4162 | 2014-05-21 20:42:17 +0000 | [diff] [blame] | 205 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 206 | int64_t TimestampWrapAroundHandler::Unwrap(uint32_t ts) { |
sprang | 1b3530b | 2016-03-10 01:32:53 -0800 | [diff] [blame] | 207 | if (num_wrap_ == -1) { |
| 208 | last_ts_ = ts; |
| 209 | num_wrap_ = 0; |
| 210 | return ts; |
henrike@webrtc.org | 99b4162 | 2014-05-21 20:42:17 +0000 | [diff] [blame] | 211 | } |
sprang | 1b3530b | 2016-03-10 01:32:53 -0800 | [diff] [blame] | 212 | |
| 213 | if (ts < last_ts_) { |
| 214 | if (last_ts_ >= 0xf0000000 && ts < 0x0fffffff) |
| 215 | ++num_wrap_; |
| 216 | } else if ((ts - last_ts_) > 0xf0000000) { |
| 217 | // Backwards wrap. Unwrap with last wrap count and don't update last_ts_. |
| 218 | return ts + ((num_wrap_ - 1) << 32); |
| 219 | } |
| 220 | |
henrike@webrtc.org | 99b4162 | 2014-05-21 20:42:17 +0000 | [diff] [blame] | 221 | last_ts_ = ts; |
sprang | 1b3530b | 2016-03-10 01:32:53 -0800 | [diff] [blame] | 222 | return ts + (num_wrap_ << 32); |
henrike@webrtc.org | 99b4162 | 2014-05-21 20:42:17 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 225 | int64_t TmToSeconds(const std::tm& tm) { |
| 226 | static short int mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; |
| 227 | static short int cumul_mdays[12] = {0, 31, 59, 90, 120, 151, |
| 228 | 181, 212, 243, 273, 304, 334}; |
| 229 | int year = tm.tm_year + 1900; |
| 230 | int month = tm.tm_mon; |
| 231 | int day = tm.tm_mday - 1; // Make 0-based like the rest. |
| 232 | int hour = tm.tm_hour; |
| 233 | int min = tm.tm_min; |
| 234 | int sec = tm.tm_sec; |
| 235 | |
| 236 | bool expiry_in_leap_year = (year % 4 == 0 && |
| 237 | (year % 100 != 0 || year % 400 == 0)); |
| 238 | |
| 239 | if (year < 1970) |
| 240 | return -1; |
| 241 | if (month < 0 || month > 11) |
| 242 | return -1; |
| 243 | if (day < 0 || day >= mdays[month] + (expiry_in_leap_year && month == 2 - 1)) |
| 244 | return -1; |
| 245 | if (hour < 0 || hour > 23) |
| 246 | return -1; |
| 247 | if (min < 0 || min > 59) |
| 248 | return -1; |
| 249 | if (sec < 0 || sec > 59) |
| 250 | return -1; |
| 251 | |
| 252 | day += cumul_mdays[month]; |
| 253 | |
| 254 | // Add number of leap days between 1970 and the expiration year, inclusive. |
| 255 | day += ((year / 4 - 1970 / 4) - (year / 100 - 1970 / 100) + |
| 256 | (year / 400 - 1970 / 400)); |
| 257 | |
| 258 | // We will have added one day too much above if expiration is during a leap |
| 259 | // year, and expiration is in January or February. |
| 260 | if (expiry_in_leap_year && month <= 2 - 1) // |month| is zero based. |
| 261 | day -= 1; |
| 262 | |
| 263 | // Combine all variables into seconds from 1970-01-01 00:00 (except |month| |
| 264 | // which was accumulated into |day| above). |
| 265 | return (((static_cast<int64_t> |
| 266 | (year - 1970) * 365 + day) * 24 + hour) * 60 + min) * 60 + sec; |
| 267 | } |
| 268 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 269 | } // namespace rtc |