timestamp.h (4393B)
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_TIMESTAMP_H_ 12 #define API_UNITS_TIMESTAMP_H_ 13 14 #include <cstdint> 15 #include <string> 16 #include <type_traits> 17 18 #include "api/units/time_delta.h" 19 #include "rtc_base/checks.h" 20 #include "rtc_base/system/rtc_export.h" 21 #include "rtc_base/units/unit_base.h" // IWYU pragma: export 22 23 namespace webrtc { 24 // Timestamp represents the time that has passed since some unspecified epoch. 25 // The epoch is assumed to be before any represented timestamps, this means that 26 // negative values are not valid. The most notable feature is that the 27 // difference of two Timestamps results in a TimeDelta. 28 class Timestamp final : public rtc_units_impl::UnitBase<Timestamp> { 29 public: 30 template <typename T> 31 static constexpr Timestamp Seconds(T value) { 32 static_assert(std::is_arithmetic<T>::value, ""); 33 return FromFraction(1'000'000, value); 34 } 35 template <typename T> 36 static constexpr Timestamp Millis(T value) { 37 static_assert(std::is_arithmetic<T>::value, ""); 38 return FromFraction(1'000, value); 39 } 40 template <typename T> 41 static constexpr Timestamp Micros(T value) { 42 static_assert(std::is_arithmetic<T>::value, ""); 43 return FromValue(value); 44 } 45 46 Timestamp() = delete; 47 48 template <typename Sink> 49 friend void AbslStringify(Sink& sink, Timestamp value); 50 51 template <typename T = int64_t> 52 constexpr T seconds() const { 53 return ToFraction<1000000, T>(); 54 } 55 template <typename T = int64_t> 56 constexpr T ms() const { 57 return ToFraction<1000, T>(); 58 } 59 template <typename T = int64_t> 60 constexpr T us() const { 61 return ToValue<T>(); 62 } 63 template <typename T = int64_t> 64 constexpr T ns() const { 65 return ToMultiple<1000, T>(); 66 } 67 68 constexpr int64_t seconds_or(int64_t fallback_value) const { 69 return ToFractionOr<1000000>(fallback_value); 70 } 71 constexpr int64_t ms_or(int64_t fallback_value) const { 72 return ToFractionOr<1000>(fallback_value); 73 } 74 constexpr int64_t us_or(int64_t fallback_value) const { 75 return ToValueOr(fallback_value); 76 } 77 78 constexpr Timestamp operator+(const TimeDelta delta) const { 79 if (IsPlusInfinity() || delta.IsPlusInfinity()) { 80 RTC_DCHECK(!IsMinusInfinity()); 81 RTC_DCHECK(!delta.IsMinusInfinity()); 82 return PlusInfinity(); 83 } else if (IsMinusInfinity() || delta.IsMinusInfinity()) { 84 RTC_DCHECK(!IsPlusInfinity()); 85 RTC_DCHECK(!delta.IsPlusInfinity()); 86 return MinusInfinity(); 87 } 88 return Timestamp::Micros(us() + delta.us()); 89 } 90 constexpr Timestamp operator-(const TimeDelta delta) const { 91 if (IsPlusInfinity() || delta.IsMinusInfinity()) { 92 RTC_DCHECK(!IsMinusInfinity()); 93 RTC_DCHECK(!delta.IsPlusInfinity()); 94 return PlusInfinity(); 95 } else if (IsMinusInfinity() || delta.IsPlusInfinity()) { 96 RTC_DCHECK(!IsPlusInfinity()); 97 RTC_DCHECK(!delta.IsMinusInfinity()); 98 return MinusInfinity(); 99 } 100 return Timestamp::Micros(us() - delta.us()); 101 } 102 constexpr TimeDelta operator-(const Timestamp other) const { 103 if (IsPlusInfinity() || other.IsMinusInfinity()) { 104 RTC_DCHECK(!IsMinusInfinity()); 105 RTC_DCHECK(!other.IsPlusInfinity()); 106 return TimeDelta::PlusInfinity(); 107 } else if (IsMinusInfinity() || other.IsPlusInfinity()) { 108 RTC_DCHECK(!IsPlusInfinity()); 109 RTC_DCHECK(!other.IsMinusInfinity()); 110 return TimeDelta::MinusInfinity(); 111 } 112 return TimeDelta::Micros(us() - other.us()); 113 } 114 constexpr Timestamp& operator-=(const TimeDelta delta) { 115 *this = *this - delta; 116 return *this; 117 } 118 constexpr Timestamp& operator+=(const TimeDelta delta) { 119 *this = *this + delta; 120 return *this; 121 } 122 123 private: 124 friend class rtc_units_impl::UnitBase<Timestamp>; 125 using UnitBase::UnitBase; 126 static constexpr bool one_sided = true; 127 }; 128 129 RTC_EXPORT std::string ToString(Timestamp value); 130 131 template <typename Sink> 132 void AbslStringify(Sink& sink, Timestamp value) { 133 sink.Append(ToString(value)); 134 } 135 136 } // namespace webrtc 137 138 #endif // API_UNITS_TIMESTAMP_H_