xref: /aosp_15_r20/external/webrtc/api/units/time_delta.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 WEBRTC_UNIT_TEST
15 #include <ostream>  // no-presubmit-check TODO(webrtc:8982)
16 #endif              // WEBRTC_UNIT_TEST
17 
18 #include <cstdlib>
19 #include <string>
20 #include <type_traits>
21 
22 #include "rtc_base/units/unit_base.h"  // IWYU pragma: export
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>
Minutes(T value)36   static constexpr TimeDelta Minutes(T value) {
37     static_assert(std::is_arithmetic<T>::value, "");
38     return Seconds(value * 60);
39   }
40   template <typename T>
Seconds(T value)41   static constexpr TimeDelta Seconds(T value) {
42     static_assert(std::is_arithmetic<T>::value, "");
43     return FromFraction(1'000'000, value);
44   }
45   template <typename T>
Millis(T value)46   static constexpr TimeDelta Millis(T value) {
47     static_assert(std::is_arithmetic<T>::value, "");
48     return FromFraction(1'000, value);
49   }
50   template <typename T>
51   static constexpr TimeDelta Micros(T value) {
52     static_assert(std::is_arithmetic<T>::value, "");
53     return FromValue(value);
54   }
55 
56   TimeDelta() = delete;
57 
58   template <typename T = int64_t>
59   constexpr T seconds() const {
60     return ToFraction<1000000, T>();
61   }
62   template <typename T = int64_t>
63   constexpr T ms() const {
64     return ToFraction<1000, T>();
65   }
66   template <typename T = int64_t>
67   constexpr T us() const {
68     return ToValue<T>();
69   }
70   template <typename T = int64_t>
71   constexpr T ns() const {
72     return ToMultiple<1000, T>();
73   }
74 
75   constexpr int64_t seconds_or(int64_t fallback_value) const {
76     return ToFractionOr<1000000>(fallback_value);
77   }
78   constexpr int64_t ms_or(int64_t fallback_value) const {
79     return ToFractionOr<1000>(fallback_value);
80   }
81   constexpr int64_t us_or(int64_t fallback_value) const {
82     return ToValueOr(fallback_value);
83   }
84 
85   constexpr TimeDelta Abs() const {
86     return us() < 0 ? TimeDelta::Micros(-us()) : *this;
87   }
88 
89  private:
90   friend class rtc_units_impl::UnitBase<TimeDelta>;
91   using RelativeUnit::RelativeUnit;
92   static constexpr bool one_sided = false;
93 };
94 
95 std::string ToString(TimeDelta value);
96 inline std::string ToLogString(TimeDelta value) {
97   return ToString(value);
98 }
99 
100 #ifdef WEBRTC_UNIT_TEST
101 inline std::ostream& operator<<(  // no-presubmit-check TODO(webrtc:8982)
102     std::ostream& stream,         // no-presubmit-check TODO(webrtc:8982)
103     TimeDelta value) {
104   return stream << ToString(value);
105 }
106 #endif  // WEBRTC_UNIT_TEST
107 
108 }  // namespace webrtc
109 
110 #endif  // API_UNITS_TIME_DELTA_H_
111