blob: 4ab83ec1c131d65590cc481e78a0e917bc101c5d [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
Sebastian Jansson2afd2812018-08-23 14:44:05 +020014#ifdef UNIT_TEST
15#include <ostream> // no-presubmit-check TODO(webrtc:8982)
16#endif // UNIT_TEST
17
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:
Sebastian Jansson3b69b192018-05-07 13:51:51 +020035 TimeDelta() = delete;
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020036 template <int64_t seconds>
37 static constexpr TimeDelta Seconds() {
Sebastian Jansson72bba622018-11-19 11:17:12 +010038 return FromStaticFraction<seconds, 1000000>();
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020039 }
40 template <int64_t ms>
41 static constexpr TimeDelta Millis() {
Sebastian Jansson72bba622018-11-19 11:17:12 +010042 return FromStaticFraction<ms, 1000>();
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020043 }
44 template <int64_t us>
45 static constexpr TimeDelta Micros() {
Sebastian Jansson72bba622018-11-19 11:17:12 +010046 return FromStaticValue<us>();
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020047 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010048 template <typename T>
Sebastian Jansson942b3602018-05-30 15:47:44 +020049 static TimeDelta seconds(T seconds) {
Sebastian Jansson0c3f4d32018-11-30 10:02:44 +010050 static_assert(std::is_arithmetic<T>::value, "");
Sebastian Jansson72bba622018-11-19 11:17:12 +010051 return FromFraction<1000000>(seconds);
Sebastian Jansson30bd4032018-04-13 13:56:17 +020052 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010053 template <typename T>
Sebastian Jansson942b3602018-05-30 15:47:44 +020054 static TimeDelta ms(T milliseconds) {
Sebastian Jansson0c3f4d32018-11-30 10:02:44 +010055 static_assert(std::is_arithmetic<T>::value, "");
Sebastian Jansson72bba622018-11-19 11:17:12 +010056 return FromFraction<1000>(milliseconds);
Sebastian Jansson30bd4032018-04-13 13:56:17 +020057 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010058 template <typename T>
Sebastian Jansson942b3602018-05-30 15:47:44 +020059 static TimeDelta us(T microseconds) {
Sebastian Jansson0c3f4d32018-11-30 10:02:44 +010060 static_assert(std::is_arithmetic<T>::value, "");
Sebastian Jansson72bba622018-11-19 11:17:12 +010061 return FromValue(microseconds);
Sebastian Jansson942b3602018-05-30 15:47:44 +020062 }
63 template <typename T = int64_t>
Sebastian Jansson72bba622018-11-19 11:17:12 +010064 T seconds() const {
65 return ToFraction<1000000, T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020066 }
67 template <typename T = int64_t>
Sebastian Jansson72bba622018-11-19 11:17:12 +010068 T ms() const {
69 return ToFraction<1000, T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020070 }
71 template <typename T = int64_t>
Sebastian Jansson72bba622018-11-19 11:17:12 +010072 T us() const {
73 return ToValue<T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020074 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010075 template <typename T = int64_t>
76 T ns() const {
77 return ToMultiple<1000, T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020078 }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020079
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020080 constexpr int64_t seconds_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010081 return ToFractionOr<1000000>(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020082 }
83 constexpr int64_t ms_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010084 return ToFractionOr<1000>(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020085 }
86 constexpr int64_t us_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010087 return ToValueOr(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020088 }
89
Sebastian Jansson30bd4032018-04-13 13:56:17 +020090 TimeDelta Abs() const { return TimeDelta::us(std::abs(us())); }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020091
92 private:
Sebastian Jansson72bba622018-11-19 11:17:12 +010093 friend class rtc_units_impl::UnitBase<TimeDelta>;
94 using RelativeUnit::RelativeUnit;
95 static constexpr bool one_sided = false;
Sebastian Jansson30bd4032018-04-13 13:56:17 +020096};
Sebastian Jansson66fa5352018-04-30 16:54:57 +020097
Sebastian Jansson72bba622018-11-19 11:17:12 +010098std::string ToString(TimeDelta value);
Sebastian Janssonb1138622019-04-11 16:48:15 +020099inline std::string ToLogString(TimeDelta value) {
100 return ToString(value);
101}
Sebastian Jansson2afd2812018-08-23 14:44:05 +0200102
103#ifdef UNIT_TEST
104inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
105 std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
106 TimeDelta value) {
107 return stream << ToString(value);
108}
109#endif // UNIT_TEST
110
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200111} // namespace webrtc
112
Sebastian Jansson6fae6ec2018-05-08 10:43:18 +0200113#endif // API_UNITS_TIME_DELTA_H_