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