xref: /aosp_15_r20/external/webrtc/api/units/time_delta.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef API_UNITS_TIME_DELTA_H_
12*d9f75844SAndroid Build Coastguard Worker #define API_UNITS_TIME_DELTA_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #ifdef WEBRTC_UNIT_TEST
15*d9f75844SAndroid Build Coastguard Worker #include <ostream>  // no-presubmit-check TODO(webrtc:8982)
16*d9f75844SAndroid Build Coastguard Worker #endif              // WEBRTC_UNIT_TEST
17*d9f75844SAndroid Build Coastguard Worker 
18*d9f75844SAndroid Build Coastguard Worker #include <cstdlib>
19*d9f75844SAndroid Build Coastguard Worker #include <string>
20*d9f75844SAndroid Build Coastguard Worker #include <type_traits>
21*d9f75844SAndroid Build Coastguard Worker 
22*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/units/unit_base.h"  // IWYU pragma: export
23*d9f75844SAndroid Build Coastguard Worker 
24*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
25*d9f75844SAndroid Build Coastguard Worker 
26*d9f75844SAndroid Build Coastguard Worker // TimeDelta represents the difference between two timestamps. Commonly this can
27*d9f75844SAndroid Build Coastguard Worker // be a duration. However since two Timestamps are not guaranteed to have the
28*d9f75844SAndroid Build Coastguard Worker // same epoch (they might come from different computers, making exact
29*d9f75844SAndroid Build Coastguard Worker // synchronisation infeasible), the duration covered by a TimeDelta can be
30*d9f75844SAndroid Build Coastguard Worker // undefined. To simplify usage, it can be constructed and converted to
31*d9f75844SAndroid Build Coastguard Worker // different units, specifically seconds (s), milliseconds (ms) and
32*d9f75844SAndroid Build Coastguard Worker // microseconds (us).
33*d9f75844SAndroid Build Coastguard Worker class TimeDelta final : public rtc_units_impl::RelativeUnit<TimeDelta> {
34*d9f75844SAndroid Build Coastguard Worker  public:
35*d9f75844SAndroid Build Coastguard Worker   template <typename T>
Minutes(T value)36*d9f75844SAndroid Build Coastguard Worker   static constexpr TimeDelta Minutes(T value) {
37*d9f75844SAndroid Build Coastguard Worker     static_assert(std::is_arithmetic<T>::value, "");
38*d9f75844SAndroid Build Coastguard Worker     return Seconds(value * 60);
39*d9f75844SAndroid Build Coastguard Worker   }
40*d9f75844SAndroid Build Coastguard Worker   template <typename T>
Seconds(T value)41*d9f75844SAndroid Build Coastguard Worker   static constexpr TimeDelta Seconds(T value) {
42*d9f75844SAndroid Build Coastguard Worker     static_assert(std::is_arithmetic<T>::value, "");
43*d9f75844SAndroid Build Coastguard Worker     return FromFraction(1'000'000, value);
44*d9f75844SAndroid Build Coastguard Worker   }
45*d9f75844SAndroid Build Coastguard Worker   template <typename T>
Millis(T value)46*d9f75844SAndroid Build Coastguard Worker   static constexpr TimeDelta Millis(T value) {
47*d9f75844SAndroid Build Coastguard Worker     static_assert(std::is_arithmetic<T>::value, "");
48*d9f75844SAndroid Build Coastguard Worker     return FromFraction(1'000, value);
49*d9f75844SAndroid Build Coastguard Worker   }
50*d9f75844SAndroid Build Coastguard Worker   template <typename T>
51*d9f75844SAndroid Build Coastguard Worker   static constexpr TimeDelta Micros(T value) {
52*d9f75844SAndroid Build Coastguard Worker     static_assert(std::is_arithmetic<T>::value, "");
53*d9f75844SAndroid Build Coastguard Worker     return FromValue(value);
54*d9f75844SAndroid Build Coastguard Worker   }
55*d9f75844SAndroid Build Coastguard Worker 
56*d9f75844SAndroid Build Coastguard Worker   TimeDelta() = delete;
57*d9f75844SAndroid Build Coastguard Worker 
58*d9f75844SAndroid Build Coastguard Worker   template <typename T = int64_t>
59*d9f75844SAndroid Build Coastguard Worker   constexpr T seconds() const {
60*d9f75844SAndroid Build Coastguard Worker     return ToFraction<1000000, T>();
61*d9f75844SAndroid Build Coastguard Worker   }
62*d9f75844SAndroid Build Coastguard Worker   template <typename T = int64_t>
63*d9f75844SAndroid Build Coastguard Worker   constexpr T ms() const {
64*d9f75844SAndroid Build Coastguard Worker     return ToFraction<1000, T>();
65*d9f75844SAndroid Build Coastguard Worker   }
66*d9f75844SAndroid Build Coastguard Worker   template <typename T = int64_t>
67*d9f75844SAndroid Build Coastguard Worker   constexpr T us() const {
68*d9f75844SAndroid Build Coastguard Worker     return ToValue<T>();
69*d9f75844SAndroid Build Coastguard Worker   }
70*d9f75844SAndroid Build Coastguard Worker   template <typename T = int64_t>
71*d9f75844SAndroid Build Coastguard Worker   constexpr T ns() const {
72*d9f75844SAndroid Build Coastguard Worker     return ToMultiple<1000, T>();
73*d9f75844SAndroid Build Coastguard Worker   }
74*d9f75844SAndroid Build Coastguard Worker 
75*d9f75844SAndroid Build Coastguard Worker   constexpr int64_t seconds_or(int64_t fallback_value) const {
76*d9f75844SAndroid Build Coastguard Worker     return ToFractionOr<1000000>(fallback_value);
77*d9f75844SAndroid Build Coastguard Worker   }
78*d9f75844SAndroid Build Coastguard Worker   constexpr int64_t ms_or(int64_t fallback_value) const {
79*d9f75844SAndroid Build Coastguard Worker     return ToFractionOr<1000>(fallback_value);
80*d9f75844SAndroid Build Coastguard Worker   }
81*d9f75844SAndroid Build Coastguard Worker   constexpr int64_t us_or(int64_t fallback_value) const {
82*d9f75844SAndroid Build Coastguard Worker     return ToValueOr(fallback_value);
83*d9f75844SAndroid Build Coastguard Worker   }
84*d9f75844SAndroid Build Coastguard Worker 
85*d9f75844SAndroid Build Coastguard Worker   constexpr TimeDelta Abs() const {
86*d9f75844SAndroid Build Coastguard Worker     return us() < 0 ? TimeDelta::Micros(-us()) : *this;
87*d9f75844SAndroid Build Coastguard Worker   }
88*d9f75844SAndroid Build Coastguard Worker 
89*d9f75844SAndroid Build Coastguard Worker  private:
90*d9f75844SAndroid Build Coastguard Worker   friend class rtc_units_impl::UnitBase<TimeDelta>;
91*d9f75844SAndroid Build Coastguard Worker   using RelativeUnit::RelativeUnit;
92*d9f75844SAndroid Build Coastguard Worker   static constexpr bool one_sided = false;
93*d9f75844SAndroid Build Coastguard Worker };
94*d9f75844SAndroid Build Coastguard Worker 
95*d9f75844SAndroid Build Coastguard Worker std::string ToString(TimeDelta value);
96*d9f75844SAndroid Build Coastguard Worker inline std::string ToLogString(TimeDelta value) {
97*d9f75844SAndroid Build Coastguard Worker   return ToString(value);
98*d9f75844SAndroid Build Coastguard Worker }
99*d9f75844SAndroid Build Coastguard Worker 
100*d9f75844SAndroid Build Coastguard Worker #ifdef WEBRTC_UNIT_TEST
101*d9f75844SAndroid Build Coastguard Worker inline std::ostream& operator<<(  // no-presubmit-check TODO(webrtc:8982)
102*d9f75844SAndroid Build Coastguard Worker     std::ostream& stream,         // no-presubmit-check TODO(webrtc:8982)
103*d9f75844SAndroid Build Coastguard Worker     TimeDelta value) {
104*d9f75844SAndroid Build Coastguard Worker   return stream << ToString(value);
105*d9f75844SAndroid Build Coastguard Worker }
106*d9f75844SAndroid Build Coastguard Worker #endif  // WEBRTC_UNIT_TEST
107*d9f75844SAndroid Build Coastguard Worker 
108*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
109*d9f75844SAndroid Build Coastguard Worker 
110*d9f75844SAndroid Build Coastguard Worker #endif  // API_UNITS_TIME_DELTA_H_
111