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 | } |
| 64 | |
| 65 | double SecondsAsDouble() const; |
| 66 | |
| 67 | TimeDelta Abs() const { return TimeDelta::us(std::abs(us())); } |
| 68 | bool IsZero() const { return microseconds_ == 0; } |
Sebastian Jansson | 3b69b19 | 2018-05-07 13:51:51 +0200 | [diff] [blame] | 69 | bool IsFinite() const { return !IsInfinite(); } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 70 | bool IsInfinite() const { |
| 71 | return microseconds_ == timedelta_impl::kPlusInfinityVal || |
| 72 | microseconds_ == timedelta_impl::kMinusInfinityVal; |
| 73 | } |
| 74 | bool IsPlusInfinity() const { |
| 75 | return microseconds_ == timedelta_impl::kPlusInfinityVal; |
| 76 | } |
| 77 | bool IsMinusInfinity() const { |
| 78 | return microseconds_ == timedelta_impl::kMinusInfinityVal; |
| 79 | } |
| 80 | TimeDelta operator+(const TimeDelta& other) const { |
| 81 | return TimeDelta::us(us() + other.us()); |
| 82 | } |
| 83 | TimeDelta operator-(const TimeDelta& other) const { |
| 84 | return TimeDelta::us(us() - other.us()); |
| 85 | } |
| 86 | TimeDelta& operator-=(const TimeDelta& other) { |
| 87 | microseconds_ -= other.us(); |
| 88 | return *this; |
| 89 | } |
| 90 | TimeDelta& operator+=(const TimeDelta& other) { |
| 91 | microseconds_ += other.us(); |
| 92 | return *this; |
| 93 | } |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 94 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 95 | bool operator==(const TimeDelta& other) const { |
| 96 | return microseconds_ == other.microseconds_; |
| 97 | } |
| 98 | bool operator!=(const TimeDelta& other) const { |
| 99 | return microseconds_ != other.microseconds_; |
| 100 | } |
| 101 | bool operator<=(const TimeDelta& other) const { |
| 102 | return microseconds_ <= other.microseconds_; |
| 103 | } |
| 104 | bool operator>=(const TimeDelta& other) const { |
| 105 | return microseconds_ >= other.microseconds_; |
| 106 | } |
| 107 | bool operator>(const TimeDelta& other) const { |
| 108 | return microseconds_ > other.microseconds_; |
| 109 | } |
| 110 | bool operator<(const TimeDelta& other) const { |
| 111 | return microseconds_ < other.microseconds_; |
| 112 | } |
| 113 | |
| 114 | private: |
| 115 | explicit TimeDelta(int64_t us) : microseconds_(us) {} |
| 116 | int64_t microseconds_; |
| 117 | }; |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 118 | |
| 119 | inline TimeDelta operator*(const TimeDelta& delta, const double& scalar) { |
| 120 | return TimeDelta::us(std::round(delta.us() * scalar)); |
| 121 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 122 | inline TimeDelta operator*(const double& scalar, const TimeDelta& delta) { |
| 123 | return delta * scalar; |
| 124 | } |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 125 | inline TimeDelta operator*(const TimeDelta& delta, const int64_t& scalar) { |
| 126 | return TimeDelta::us(delta.us() * scalar); |
| 127 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 128 | inline TimeDelta operator*(const int64_t& scalar, const TimeDelta& delta) { |
| 129 | return delta * scalar; |
| 130 | } |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 131 | inline TimeDelta operator*(const TimeDelta& delta, const int32_t& scalar) { |
| 132 | return TimeDelta::us(delta.us() * scalar); |
| 133 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 134 | inline TimeDelta operator*(const int32_t& scalar, const TimeDelta& delta) { |
| 135 | return delta * scalar; |
| 136 | } |
| 137 | |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 138 | inline TimeDelta operator/(const TimeDelta& delta, const int64_t& scalar) { |
| 139 | return TimeDelta::us(delta.us() / scalar); |
| 140 | } |
| 141 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 142 | std::string ToString(const TimeDelta& value); |
| 143 | } // namespace webrtc |
| 144 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame^] | 145 | #endif // API_UNITS_TIME_DELTA_H_ |