blob: d5951005e316813b2d0203fb41372c2dc494041c [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>
Evan Shrubsoled425f502022-04-28 10:18:12 +020036 static constexpr TimeDelta Minutes(T value) {
37 static_assert(std::is_arithmetic<T>::value, "");
38 return Seconds(value * 60);
39 }
40 template <typename T>
Danil Chapovalov8d94dc22020-01-28 17:54:47 +010041 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>
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
Sebastian Jansson3b69b192018-05-07 13:51:51 +020056 TimeDelta() = delete;
Danil Chapovalov8d94dc22020-01-28 17:54:47 +010057
Sebastian Jansson942b3602018-05-30 15:47:44 +020058 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010059 constexpr T seconds() const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010060 return ToFraction<1000000, T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020061 }
62 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010063 constexpr T ms() const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010064 return ToFraction<1000, T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020065 }
66 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010067 constexpr T us() const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010068 return ToValue<T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020069 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010070 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010071 constexpr T ns() const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010072 return ToMultiple<1000, T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020073 }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020074
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020075 constexpr int64_t seconds_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010076 return ToFractionOr<1000000>(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020077 }
78 constexpr int64_t ms_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010079 return ToFractionOr<1000>(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020080 }
81 constexpr int64_t us_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010082 return ToValueOr(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020083 }
84
Sebastian Janssond7fade52020-01-29 10:44:51 +010085 constexpr TimeDelta Abs() const {
Danil Chapovalov0c626af2020-02-10 11:16:00 +010086 return us() < 0 ? TimeDelta::Micros(-us()) : *this;
Sebastian Janssond7fade52020-01-29 10:44:51 +010087 }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020088
89 private:
Sebastian Jansson72bba622018-11-19 11:17:12 +010090 friend class rtc_units_impl::UnitBase<TimeDelta>;
91 using RelativeUnit::RelativeUnit;
92 static constexpr bool one_sided = false;
Sebastian Jansson30bd4032018-04-13 13:56:17 +020093};
Sebastian Jansson66fa5352018-04-30 16:54:57 +020094
Sebastian Jansson72bba622018-11-19 11:17:12 +010095std::string ToString(TimeDelta value);
Sebastian Janssonb1138622019-04-11 16:48:15 +020096inline std::string ToLogString(TimeDelta value) {
97 return ToString(value);
98}
Sebastian Jansson2afd2812018-08-23 14:44:05 +020099
Andrey Logvinb95d90b2020-12-09 12:49:39 +0000100#ifdef WEBRTC_UNIT_TEST
Sebastian Jansson2afd2812018-08-23 14:44:05 +0200101inline 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}
Andrey Logvinb95d90b2020-12-09 12:49:39 +0000106#endif // WEBRTC_UNIT_TEST
Sebastian Jansson2afd2812018-08-23 14:44:05 +0200107
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200108} // namespace webrtc
109
Sebastian Jansson6fae6ec2018-05-08 10:43:18 +0200110#endif // API_UNITS_TIME_DELTA_H_