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 | |
| 14 | #include <stdint.h> |
| 15 | #include <cmath> |
| 16 | #include <limits> |
| 17 | #include <string> |
| 18 | |
| 19 | #include "rtc_base/checks.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | namespace timedelta_impl { |
| 23 | constexpr int64_t kPlusInfinityVal = std::numeric_limits<int64_t>::max(); |
| 24 | constexpr int64_t kMinusInfinityVal = std::numeric_limits<int64_t>::min(); |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 25 | } // namespace timedelta_impl |
| 26 | |
| 27 | // TimeDelta represents the difference between two timestamps. Commonly this can |
| 28 | // be a duration. However since two Timestamps are not guaranteed to have the |
| 29 | // same epoch (they might come from different computers, making exact |
| 30 | // synchronisation infeasible), the duration covered by a TimeDelta can be |
| 31 | // undefined. To simplify usage, it can be constructed and converted to |
| 32 | // different units, specifically seconds (s), milliseconds (ms) and |
| 33 | // microseconds (us). |
| 34 | class TimeDelta { |
| 35 | public: |
Sebastian Jansson | 3b69b19 | 2018-05-07 13:51:51 +0200 | [diff] [blame] | 36 | TimeDelta() = delete; |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 37 | static TimeDelta Zero() { return TimeDelta(0); } |
| 38 | static TimeDelta PlusInfinity() { |
| 39 | return TimeDelta(timedelta_impl::kPlusInfinityVal); |
| 40 | } |
| 41 | static TimeDelta MinusInfinity() { |
| 42 | return TimeDelta(timedelta_impl::kMinusInfinityVal); |
| 43 | } |
Sebastian Jansson | f7ffd94 | 2018-04-16 11:46:42 +0200 | [diff] [blame] | 44 | static TimeDelta seconds(int64_t seconds) { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 45 | return TimeDelta::us(seconds * 1000000); |
| 46 | } |
| 47 | static TimeDelta ms(int64_t milliseconds) { |
| 48 | return TimeDelta::us(milliseconds * 1000); |
| 49 | } |
| 50 | static TimeDelta us(int64_t microseconds) { |
| 51 | // Infinities only allowed via use of explicit constants. |
| 52 | RTC_DCHECK(microseconds > std::numeric_limits<int64_t>::min()); |
| 53 | RTC_DCHECK(microseconds < std::numeric_limits<int64_t>::max()); |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 54 | return TimeDelta(microseconds); |
| 55 | } |
Sebastian Jansson | f7ffd94 | 2018-04-16 11:46:42 +0200 | [diff] [blame] | 56 | int64_t seconds() const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 57 | return (us() + (us() >= 0 ? 500000 : -500000)) / 1000000; |
| 58 | } |
| 59 | int64_t ms() const { return (us() + (us() >= 0 ? 500 : -500)) / 1000; } |
| 60 | int64_t us() const { |
| 61 | RTC_DCHECK(IsFinite()); |
| 62 | return microseconds_; |
| 63 | } |
Sebastian Jansson | 5f83cf0 | 2018-05-08 14:52:22 +0200 | [diff] [blame] | 64 | int64_t ns() const { |
| 65 | RTC_DCHECK(us() > std::numeric_limits<int64_t>::min() / 1000); |
| 66 | RTC_DCHECK(us() < std::numeric_limits<int64_t>::max() / 1000); |
| 67 | return us() * 1000; |
| 68 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 69 | |
| 70 | double SecondsAsDouble() const; |
| 71 | |
| 72 | TimeDelta Abs() const { return TimeDelta::us(std::abs(us())); } |
| 73 | bool IsZero() const { return microseconds_ == 0; } |
Sebastian Jansson | 3b69b19 | 2018-05-07 13:51:51 +0200 | [diff] [blame] | 74 | bool IsFinite() const { return !IsInfinite(); } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 75 | bool IsInfinite() const { |
| 76 | return microseconds_ == timedelta_impl::kPlusInfinityVal || |
| 77 | microseconds_ == timedelta_impl::kMinusInfinityVal; |
| 78 | } |
| 79 | bool IsPlusInfinity() const { |
| 80 | return microseconds_ == timedelta_impl::kPlusInfinityVal; |
| 81 | } |
| 82 | bool IsMinusInfinity() const { |
| 83 | return microseconds_ == timedelta_impl::kMinusInfinityVal; |
| 84 | } |
| 85 | TimeDelta operator+(const TimeDelta& other) const { |
| 86 | return TimeDelta::us(us() + other.us()); |
| 87 | } |
| 88 | TimeDelta operator-(const TimeDelta& other) const { |
| 89 | return TimeDelta::us(us() - other.us()); |
| 90 | } |
| 91 | TimeDelta& operator-=(const TimeDelta& other) { |
| 92 | microseconds_ -= other.us(); |
| 93 | return *this; |
| 94 | } |
| 95 | TimeDelta& operator+=(const TimeDelta& other) { |
| 96 | microseconds_ += other.us(); |
| 97 | return *this; |
| 98 | } |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 99 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 100 | bool operator==(const TimeDelta& other) const { |
| 101 | return microseconds_ == other.microseconds_; |
| 102 | } |
| 103 | bool operator!=(const TimeDelta& other) const { |
| 104 | return microseconds_ != other.microseconds_; |
| 105 | } |
| 106 | bool operator<=(const TimeDelta& other) const { |
| 107 | return microseconds_ <= other.microseconds_; |
| 108 | } |
| 109 | bool operator>=(const TimeDelta& other) const { |
| 110 | return microseconds_ >= other.microseconds_; |
| 111 | } |
| 112 | bool operator>(const TimeDelta& other) const { |
| 113 | return microseconds_ > other.microseconds_; |
| 114 | } |
| 115 | bool operator<(const TimeDelta& other) const { |
| 116 | return microseconds_ < other.microseconds_; |
| 117 | } |
| 118 | |
| 119 | private: |
| 120 | explicit TimeDelta(int64_t us) : microseconds_(us) {} |
| 121 | int64_t microseconds_; |
| 122 | }; |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 123 | |
| 124 | inline TimeDelta operator*(const TimeDelta& delta, const double& scalar) { |
| 125 | return TimeDelta::us(std::round(delta.us() * scalar)); |
| 126 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 127 | inline TimeDelta operator*(const double& scalar, const TimeDelta& delta) { |
| 128 | return delta * scalar; |
| 129 | } |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 130 | inline TimeDelta operator*(const TimeDelta& delta, const int64_t& scalar) { |
| 131 | return TimeDelta::us(delta.us() * scalar); |
| 132 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 133 | inline TimeDelta operator*(const int64_t& scalar, const TimeDelta& delta) { |
| 134 | return delta * scalar; |
| 135 | } |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 136 | inline TimeDelta operator*(const TimeDelta& delta, const int32_t& scalar) { |
| 137 | return TimeDelta::us(delta.us() * scalar); |
| 138 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 139 | inline TimeDelta operator*(const int32_t& scalar, const TimeDelta& delta) { |
| 140 | return delta * scalar; |
| 141 | } |
| 142 | |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 143 | inline TimeDelta operator/(const TimeDelta& delta, const int64_t& scalar) { |
| 144 | return TimeDelta::us(delta.us() / scalar); |
| 145 | } |
| 146 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 147 | std::string ToString(const TimeDelta& value); |
| 148 | } // namespace webrtc |
| 149 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 150 | #endif // API_UNITS_TIME_DELTA_H_ |