time_delta.h (3195B)
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 11 #ifndef API_UNITS_TIME_DELTA_H_ 12 #define API_UNITS_TIME_DELTA_H_ 13 14 #include <cstdint> 15 #include <cstdlib> 16 #include <string> 17 #include <type_traits> 18 19 #include "rtc_base/system/rtc_export.h" 20 #include "rtc_base/units/unit_base.h" // IWYU pragma: export 21 22 namespace webrtc { 23 24 // TimeDelta represents the difference between two timestamps. Commonly this can 25 // be a duration. However since two Timestamps are not guaranteed to have the 26 // same epoch (they might come from different computers, making exact 27 // synchronisation infeasible), the duration covered by a TimeDelta can be 28 // undefined. To simplify usage, it can be constructed and converted to 29 // different units, specifically seconds (s), milliseconds (ms) and 30 // microseconds (us). 31 class TimeDelta final : public rtc_units_impl::RelativeUnit<TimeDelta> { 32 public: 33 template <typename T> 34 static constexpr TimeDelta Minutes(T value) { 35 static_assert(std::is_arithmetic<T>::value, ""); 36 return Seconds(value * 60); 37 } 38 template <typename T> 39 static constexpr TimeDelta Seconds(T value) { 40 static_assert(std::is_arithmetic<T>::value, ""); 41 return FromFraction(1'000'000, value); 42 } 43 template <typename T> 44 static constexpr TimeDelta Millis(T value) { 45 static_assert(std::is_arithmetic<T>::value, ""); 46 return FromFraction(1'000, value); 47 } 48 template <typename T> 49 static constexpr TimeDelta Micros(T value) { 50 static_assert(std::is_arithmetic<T>::value, ""); 51 return FromValue(value); 52 } 53 54 constexpr TimeDelta() = default; 55 56 template <typename Sink> 57 friend void AbslStringify(Sink& sink, TimeDelta value); 58 59 template <typename T = int64_t> 60 constexpr T seconds() const { 61 return ToFraction<1000000, T>(); 62 } 63 template <typename T = int64_t> 64 constexpr T ms() const { 65 return ToFraction<1000, T>(); 66 } 67 template <typename T = int64_t> 68 constexpr T us() const { 69 return ToValue<T>(); 70 } 71 template <typename T = int64_t> 72 constexpr T ns() const { 73 return ToMultiple<1000, T>(); 74 } 75 76 constexpr int64_t seconds_or(int64_t fallback_value) const { 77 return ToFractionOr<1000000>(fallback_value); 78 } 79 constexpr int64_t ms_or(int64_t fallback_value) const { 80 return ToFractionOr<1000>(fallback_value); 81 } 82 constexpr int64_t us_or(int64_t fallback_value) const { 83 return ToValueOr(fallback_value); 84 } 85 86 constexpr TimeDelta Abs() const { 87 return us() < 0 ? TimeDelta::Micros(-us()) : *this; 88 } 89 90 private: 91 friend class rtc_units_impl::UnitBase<TimeDelta>; 92 using RelativeUnit::RelativeUnit; 93 static constexpr bool one_sided = false; 94 }; 95 96 RTC_EXPORT std::string ToString(TimeDelta value); 97 98 template <typename Sink> 99 void AbslStringify(Sink& sink, TimeDelta value) { 100 sink.Append(ToString(value)); 101 } 102 103 } // namespace webrtc 104 105 #endif // API_UNITS_TIME_DELTA_H_