Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 11 | #ifndef API_UNITS_TIME_DELTA_H_ |
| 12 | #define API_UNITS_TIME_DELTA_H_ |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 13 | |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 14 | #ifdef UNIT_TEST |
| 15 | #include <ostream> // no-presubmit-check TODO(webrtc:8982) |
| 16 | #endif // UNIT_TEST |
| 17 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 18 | #include <stdint.h> |
| 19 | #include <cmath> |
Jonas Olsson | 941a07c | 2018-09-13 10:07:07 +0200 | [diff] [blame] | 20 | #include <cstdlib> |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 21 | #include <limits> |
| 22 | #include <string> |
| 23 | |
| 24 | #include "rtc_base/checks.h" |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 25 | #include "rtc_base/numerics/safe_conversions.h" |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | namespace timedelta_impl { |
| 29 | constexpr int64_t kPlusInfinityVal = std::numeric_limits<int64_t>::max(); |
| 30 | constexpr int64_t kMinusInfinityVal = std::numeric_limits<int64_t>::min(); |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 31 | } // namespace timedelta_impl |
| 32 | |
| 33 | // TimeDelta represents the difference between two timestamps. Commonly this can |
| 34 | // be a duration. However since two Timestamps are not guaranteed to have the |
| 35 | // same epoch (they might come from different computers, making exact |
| 36 | // synchronisation infeasible), the duration covered by a TimeDelta can be |
| 37 | // undefined. To simplify usage, it can be constructed and converted to |
| 38 | // different units, specifically seconds (s), milliseconds (ms) and |
| 39 | // microseconds (us). |
| 40 | class TimeDelta { |
| 41 | public: |
Sebastian Jansson | 3b69b19 | 2018-05-07 13:51:51 +0200 | [diff] [blame] | 42 | TimeDelta() = delete; |
Sebastian Jansson | 8e06419 | 2018-08-07 12:34:33 +0200 | [diff] [blame] | 43 | static constexpr TimeDelta Zero() { return TimeDelta(0); } |
| 44 | static constexpr TimeDelta PlusInfinity() { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 45 | return TimeDelta(timedelta_impl::kPlusInfinityVal); |
| 46 | } |
Sebastian Jansson | 8e06419 | 2018-08-07 12:34:33 +0200 | [diff] [blame] | 47 | static constexpr TimeDelta MinusInfinity() { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 48 | return TimeDelta(timedelta_impl::kMinusInfinityVal); |
| 49 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 50 | template <int64_t seconds> |
| 51 | static constexpr TimeDelta Seconds() { |
| 52 | static_assert(seconds > timedelta_impl::kMinusInfinityVal / 1000000, ""); |
| 53 | static_assert(seconds < timedelta_impl::kPlusInfinityVal / 1000000, ""); |
| 54 | return TimeDelta(seconds * 1000000); |
| 55 | } |
| 56 | template <int64_t ms> |
| 57 | static constexpr TimeDelta Millis() { |
| 58 | static_assert(ms > timedelta_impl::kMinusInfinityVal / 1000, ""); |
| 59 | static_assert(ms < timedelta_impl::kPlusInfinityVal / 1000, ""); |
| 60 | return TimeDelta(ms * 1000); |
| 61 | } |
| 62 | template <int64_t us> |
| 63 | static constexpr TimeDelta Micros() { |
| 64 | static_assert(us > timedelta_impl::kMinusInfinityVal, ""); |
| 65 | static_assert(us < timedelta_impl::kPlusInfinityVal, ""); |
| 66 | return TimeDelta(us); |
| 67 | } |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 68 | |
| 69 | template < |
| 70 | typename T, |
| 71 | typename std::enable_if<std::is_integral<T>::value>::type* = nullptr> |
| 72 | static TimeDelta seconds(T seconds) { |
| 73 | RTC_DCHECK_GT(seconds, timedelta_impl::kMinusInfinityVal / 1000000); |
| 74 | RTC_DCHECK_LT(seconds, timedelta_impl::kPlusInfinityVal / 1000000); |
| 75 | return TimeDelta(rtc::dchecked_cast<int64_t>(seconds) * 1000000); |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 76 | } |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 77 | template < |
| 78 | typename T, |
| 79 | typename std::enable_if<std::is_integral<T>::value>::type* = nullptr> |
| 80 | static TimeDelta ms(T milliseconds) { |
| 81 | RTC_DCHECK_GT(milliseconds, timedelta_impl::kMinusInfinityVal / 1000); |
| 82 | RTC_DCHECK_LT(milliseconds, timedelta_impl::kPlusInfinityVal / 1000); |
| 83 | return TimeDelta(rtc::dchecked_cast<int64_t>(milliseconds) * 1000); |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 84 | } |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 85 | template < |
| 86 | typename T, |
| 87 | typename std::enable_if<std::is_integral<T>::value>::type* = nullptr> |
| 88 | static TimeDelta us(T microseconds) { |
| 89 | RTC_DCHECK_GT(microseconds, timedelta_impl::kMinusInfinityVal); |
| 90 | RTC_DCHECK_LT(microseconds, timedelta_impl::kPlusInfinityVal); |
| 91 | return TimeDelta(rtc::dchecked_cast<int64_t>(microseconds)); |
Sebastian Jansson | 5f83cf0 | 2018-05-08 14:52:22 +0200 | [diff] [blame] | 92 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 93 | |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 94 | template <typename T, |
| 95 | typename std::enable_if<std::is_floating_point<T>::value>::type* = |
| 96 | nullptr> |
| 97 | static TimeDelta seconds(T seconds) { |
| 98 | return TimeDelta::us(seconds * 1e6); |
| 99 | } |
| 100 | template <typename T, |
| 101 | typename std::enable_if<std::is_floating_point<T>::value>::type* = |
| 102 | nullptr> |
| 103 | static TimeDelta ms(T milliseconds) { |
| 104 | return TimeDelta::us(milliseconds * 1e3); |
| 105 | } |
| 106 | template <typename T, |
| 107 | typename std::enable_if<std::is_floating_point<T>::value>::type* = |
| 108 | nullptr> |
| 109 | static TimeDelta us(T microseconds) { |
| 110 | if (microseconds == std::numeric_limits<T>::infinity()) { |
| 111 | return PlusInfinity(); |
| 112 | } else if (microseconds == -std::numeric_limits<T>::infinity()) { |
| 113 | return MinusInfinity(); |
| 114 | } else { |
| 115 | RTC_DCHECK(!std::isnan(microseconds)); |
| 116 | RTC_DCHECK_GT(microseconds, timedelta_impl::kMinusInfinityVal); |
| 117 | RTC_DCHECK_LT(microseconds, timedelta_impl::kPlusInfinityVal); |
| 118 | return TimeDelta(rtc::dchecked_cast<int64_t>(microseconds)); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | template <typename T = int64_t> |
| 123 | typename std::enable_if<std::is_integral<T>::value, T>::type seconds() const { |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 124 | RTC_DCHECK(IsFinite()); |
| 125 | return rtc::dchecked_cast<T>(UnsafeSeconds()); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 126 | } |
| 127 | template <typename T = int64_t> |
| 128 | typename std::enable_if<std::is_integral<T>::value, T>::type ms() const { |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 129 | RTC_DCHECK(IsFinite()); |
| 130 | return rtc::dchecked_cast<T>(UnsafeMillis()); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 131 | } |
| 132 | template <typename T = int64_t> |
| 133 | typename std::enable_if<std::is_integral<T>::value, T>::type us() const { |
| 134 | RTC_DCHECK(IsFinite()); |
| 135 | return rtc::dchecked_cast<T>(microseconds_); |
| 136 | } |
| 137 | template <typename T = int64_t> |
| 138 | typename std::enable_if<std::is_integral<T>::value, T>::type ns() const { |
| 139 | RTC_DCHECK_GE(us(), std::numeric_limits<T>::min() / 1000); |
| 140 | RTC_DCHECK_LE(us(), std::numeric_limits<T>::max() / 1000); |
| 141 | return rtc::dchecked_cast<T>(us() * 1000); |
| 142 | } |
| 143 | |
| 144 | template <typename T> |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 145 | constexpr typename std::enable_if<std::is_floating_point<T>::value, T>::type |
| 146 | seconds() const { |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 147 | return us<T>() * 1e-6; |
| 148 | } |
| 149 | template <typename T> |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 150 | constexpr typename std::enable_if<std::is_floating_point<T>::value, T>::type |
| 151 | ms() const { |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 152 | return us<T>() * 1e-3; |
| 153 | } |
| 154 | template <typename T> |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 155 | constexpr typename std::enable_if<std::is_floating_point<T>::value, T>::type |
| 156 | us() const { |
| 157 | return IsPlusInfinity() |
| 158 | ? std::numeric_limits<T>::infinity() |
| 159 | : IsMinusInfinity() ? -std::numeric_limits<T>::infinity() |
| 160 | : microseconds_; |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 161 | } |
| 162 | template <typename T> |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 163 | constexpr typename std::enable_if<std::is_floating_point<T>::value, T>::type |
| 164 | ns() const { |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 165 | return us<T>() * 1e3; |
| 166 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 167 | |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 168 | constexpr int64_t seconds_or(int64_t fallback_value) const { |
| 169 | return IsFinite() ? UnsafeSeconds() : fallback_value; |
| 170 | } |
| 171 | constexpr int64_t ms_or(int64_t fallback_value) const { |
| 172 | return IsFinite() ? UnsafeMillis() : fallback_value; |
| 173 | } |
| 174 | constexpr int64_t us_or(int64_t fallback_value) const { |
| 175 | return IsFinite() ? microseconds_ : fallback_value; |
| 176 | } |
| 177 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 178 | TimeDelta Abs() const { return TimeDelta::us(std::abs(us())); } |
Sebastian Jansson | 8e06419 | 2018-08-07 12:34:33 +0200 | [diff] [blame] | 179 | constexpr bool IsZero() const { return microseconds_ == 0; } |
| 180 | constexpr bool IsFinite() const { return !IsInfinite(); } |
| 181 | constexpr bool IsInfinite() const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 182 | return microseconds_ == timedelta_impl::kPlusInfinityVal || |
| 183 | microseconds_ == timedelta_impl::kMinusInfinityVal; |
| 184 | } |
Sebastian Jansson | 8e06419 | 2018-08-07 12:34:33 +0200 | [diff] [blame] | 185 | constexpr bool IsPlusInfinity() const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 186 | return microseconds_ == timedelta_impl::kPlusInfinityVal; |
| 187 | } |
Sebastian Jansson | 8e06419 | 2018-08-07 12:34:33 +0200 | [diff] [blame] | 188 | constexpr bool IsMinusInfinity() const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 189 | return microseconds_ == timedelta_impl::kMinusInfinityVal; |
| 190 | } |
| 191 | TimeDelta operator+(const TimeDelta& other) const { |
Sebastian Jansson | 88c1a9e | 2018-08-30 13:58:38 +0200 | [diff] [blame] | 192 | if (IsPlusInfinity() || other.IsPlusInfinity()) { |
| 193 | RTC_DCHECK(!IsMinusInfinity()); |
| 194 | RTC_DCHECK(!other.IsMinusInfinity()); |
| 195 | return PlusInfinity(); |
| 196 | } else if (IsMinusInfinity() || other.IsMinusInfinity()) { |
| 197 | RTC_DCHECK(!IsPlusInfinity()); |
| 198 | RTC_DCHECK(!other.IsPlusInfinity()); |
| 199 | return MinusInfinity(); |
| 200 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 201 | return TimeDelta::us(us() + other.us()); |
| 202 | } |
| 203 | TimeDelta operator-(const TimeDelta& other) const { |
Sebastian Jansson | 88c1a9e | 2018-08-30 13:58:38 +0200 | [diff] [blame] | 204 | if (IsPlusInfinity() || other.IsMinusInfinity()) { |
| 205 | RTC_DCHECK(!IsMinusInfinity()); |
| 206 | RTC_DCHECK(!other.IsPlusInfinity()); |
| 207 | return PlusInfinity(); |
| 208 | } else if (IsMinusInfinity() || other.IsPlusInfinity()) { |
| 209 | RTC_DCHECK(!IsPlusInfinity()); |
| 210 | RTC_DCHECK(!other.IsMinusInfinity()); |
| 211 | return MinusInfinity(); |
| 212 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 213 | return TimeDelta::us(us() - other.us()); |
| 214 | } |
| 215 | TimeDelta& operator-=(const TimeDelta& other) { |
Sebastian Jansson | 88c1a9e | 2018-08-30 13:58:38 +0200 | [diff] [blame] | 216 | *this = *this - other; |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 217 | return *this; |
| 218 | } |
| 219 | TimeDelta& operator+=(const TimeDelta& other) { |
Sebastian Jansson | 88c1a9e | 2018-08-30 13:58:38 +0200 | [diff] [blame] | 220 | *this = *this + other; |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 221 | return *this; |
| 222 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 223 | constexpr double operator/(const TimeDelta& other) const { |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 224 | return us<double>() / other.us<double>(); |
| 225 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 226 | constexpr bool operator==(const TimeDelta& other) const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 227 | return microseconds_ == other.microseconds_; |
| 228 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 229 | constexpr bool operator!=(const TimeDelta& other) const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 230 | return microseconds_ != other.microseconds_; |
| 231 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 232 | constexpr bool operator<=(const TimeDelta& other) const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 233 | return microseconds_ <= other.microseconds_; |
| 234 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 235 | constexpr bool operator>=(const TimeDelta& other) const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 236 | return microseconds_ >= other.microseconds_; |
| 237 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 238 | constexpr bool operator>(const TimeDelta& other) const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 239 | return microseconds_ > other.microseconds_; |
| 240 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 241 | constexpr bool operator<(const TimeDelta& other) const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 242 | return microseconds_ < other.microseconds_; |
| 243 | } |
| 244 | |
| 245 | private: |
Sebastian Jansson | 8e06419 | 2018-08-07 12:34:33 +0200 | [diff] [blame] | 246 | explicit constexpr TimeDelta(int64_t us) : microseconds_(us) {} |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 247 | constexpr int64_t UnsafeSeconds() const { |
| 248 | return (microseconds_ + (microseconds_ >= 0 ? 500000 : -500000)) / 1000000; |
| 249 | } |
| 250 | constexpr int64_t UnsafeMillis() const { |
| 251 | return (microseconds_ + (microseconds_ >= 0 ? 500 : -500)) / 1000; |
| 252 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 253 | int64_t microseconds_; |
| 254 | }; |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 255 | |
| 256 | inline TimeDelta operator*(const TimeDelta& delta, const double& scalar) { |
| 257 | return TimeDelta::us(std::round(delta.us() * scalar)); |
| 258 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 259 | inline TimeDelta operator*(const double& scalar, const TimeDelta& delta) { |
| 260 | return delta * scalar; |
| 261 | } |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 262 | inline TimeDelta operator*(const TimeDelta& delta, const int64_t& scalar) { |
| 263 | return TimeDelta::us(delta.us() * scalar); |
| 264 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 265 | inline TimeDelta operator*(const int64_t& scalar, const TimeDelta& delta) { |
| 266 | return delta * scalar; |
| 267 | } |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 268 | inline TimeDelta operator*(const TimeDelta& delta, const int32_t& scalar) { |
| 269 | return TimeDelta::us(delta.us() * scalar); |
| 270 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 271 | inline TimeDelta operator*(const int32_t& scalar, const TimeDelta& delta) { |
| 272 | return delta * scalar; |
| 273 | } |
| 274 | |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 275 | inline TimeDelta operator/(const TimeDelta& delta, const int64_t& scalar) { |
| 276 | return TimeDelta::us(delta.us() / scalar); |
| 277 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 278 | std::string ToString(const TimeDelta& value); |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 279 | |
| 280 | #ifdef UNIT_TEST |
| 281 | inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) |
| 282 | std::ostream& stream, // no-presubmit-check TODO(webrtc:8982) |
| 283 | TimeDelta value) { |
| 284 | return stream << ToString(value); |
| 285 | } |
| 286 | #endif // UNIT_TEST |
| 287 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 288 | } // namespace webrtc |
| 289 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 290 | #endif // API_UNITS_TIME_DELTA_H_ |