blob: 6f1910379b706d457766e70bdcc6c6252f9b58b7 [file] [log] [blame]
Sebastian Jansson30bd4032018-04-13 13:56:17 +02001/*
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
Sebastian Jansson6fae6ec2018-05-08 10:43:18 +020011#ifndef API_UNITS_TIME_DELTA_H_
12#define API_UNITS_TIME_DELTA_H_
Sebastian Jansson30bd4032018-04-13 13:56:17 +020013
Andrey Logvinb95d90b2020-12-09 12:49:39 +000014#ifdef WEBRTC_UNIT_TEST
Sebastian Jansson2afd2812018-08-23 14:44:05 +020015#include <ostream> // no-presubmit-check TODO(webrtc:8982)
Andrey Logvinb95d90b2020-12-09 12:49:39 +000016#endif // WEBRTC_UNIT_TEST
Sebastian Jansson2afd2812018-08-23 14:44:05 +020017
Jonas Olsson941a07c2018-09-13 10:07:07 +020018#include <cstdlib>
Sebastian Jansson30bd4032018-04-13 13:56:17 +020019#include <string>
Yves Gerey988cc082018-10-23 12:03:01 +020020#include <type_traits>
Sebastian Jansson30bd4032018-04-13 13:56:17 +020021
Sebastian Jansson72bba622018-11-19 11:17:12 +010022#include "rtc_base/units/unit_base.h"
Sebastian Jansson30bd4032018-04-13 13:56:17 +020023
24namespace webrtc {
Sebastian Jansson26b5e352019-06-07 11:05:31 +020025
Sebastian Jansson30bd4032018-04-13 13:56:17 +020026// 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).
Sebastian Jansson72bba622018-11-19 11:17:12 +010033class TimeDelta final : public rtc_units_impl::RelativeUnit<TimeDelta> {
Sebastian Jansson30bd4032018-04-13 13:56:17 +020034 public:
Danil Chapovalov8d94dc22020-01-28 17:54:47 +010035 template <typename T>
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>
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
Sebastian Jansson3b69b192018-05-07 13:51:51 +020051 TimeDelta() = delete;
Danil Chapovalov8d94dc22020-01-28 17:54:47 +010052
Sebastian Jansson942b3602018-05-30 15:47:44 +020053 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010054 constexpr T seconds() const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010055 return ToFraction<1000000, T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020056 }
57 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010058 constexpr T ms() const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010059 return ToFraction<1000, T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020060 }
61 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010062 constexpr T us() const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010063 return ToValue<T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020064 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010065 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010066 constexpr T ns() const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010067 return ToMultiple<1000, T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020068 }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020069
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020070 constexpr int64_t seconds_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010071 return ToFractionOr<1000000>(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020072 }
73 constexpr int64_t ms_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010074 return ToFractionOr<1000>(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020075 }
76 constexpr int64_t us_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010077 return ToValueOr(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020078 }
79
Sebastian Janssond7fade52020-01-29 10:44:51 +010080 constexpr TimeDelta Abs() const {
Danil Chapovalov0c626af2020-02-10 11:16:00 +010081 return us() < 0 ? TimeDelta::Micros(-us()) : *this;
Sebastian Janssond7fade52020-01-29 10:44:51 +010082 }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020083
84 private:
Sebastian Jansson72bba622018-11-19 11:17:12 +010085 friend class rtc_units_impl::UnitBase<TimeDelta>;
86 using RelativeUnit::RelativeUnit;
87 static constexpr bool one_sided = false;
Sebastian Jansson30bd4032018-04-13 13:56:17 +020088};
Sebastian Jansson66fa5352018-04-30 16:54:57 +020089
Sebastian Jansson72bba622018-11-19 11:17:12 +010090std::string ToString(TimeDelta value);
Sebastian Janssonb1138622019-04-11 16:48:15 +020091inline std::string ToLogString(TimeDelta value) {
92 return ToString(value);
93}
Sebastian Jansson2afd2812018-08-23 14:44:05 +020094
Andrey Logvinb95d90b2020-12-09 12:49:39 +000095#ifdef WEBRTC_UNIT_TEST
Sebastian Jansson2afd2812018-08-23 14:44:05 +020096inline 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}
Andrey Logvinb95d90b2020-12-09 12:49:39 +0000101#endif // WEBRTC_UNIT_TEST
Sebastian Jansson2afd2812018-08-23 14:44:05 +0200102
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200103} // namespace webrtc
104
Sebastian Jansson6fae6ec2018-05-08 10:43:18 +0200105#endif // API_UNITS_TIME_DELTA_H_