blob: 826e1a3d0c445568f685253cad65f92a770faa4a [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
14#include <stdint.h>
15#include <cmath>
16#include <limits>
17#include <string>
18
19#include "rtc_base/checks.h"
20
21namespace webrtc {
22namespace timedelta_impl {
23constexpr int64_t kPlusInfinityVal = std::numeric_limits<int64_t>::max();
24constexpr int64_t kMinusInfinityVal = std::numeric_limits<int64_t>::min();
Sebastian Jansson30bd4032018-04-13 13:56:17 +020025} // namespace timedelta_impl
26
27// TimeDelta represents the difference between two timestamps. Commonly this can
28// be a duration. However since two Timestamps are not guaranteed to have the
29// same epoch (they might come from different computers, making exact
30// synchronisation infeasible), the duration covered by a TimeDelta can be
31// undefined. To simplify usage, it can be constructed and converted to
32// different units, specifically seconds (s), milliseconds (ms) and
33// microseconds (us).
34class TimeDelta {
35 public:
Sebastian Jansson3b69b192018-05-07 13:51:51 +020036 TimeDelta() = delete;
Sebastian Jansson30bd4032018-04-13 13:56:17 +020037 static TimeDelta Zero() { return TimeDelta(0); }
38 static TimeDelta PlusInfinity() {
39 return TimeDelta(timedelta_impl::kPlusInfinityVal);
40 }
41 static TimeDelta MinusInfinity() {
42 return TimeDelta(timedelta_impl::kMinusInfinityVal);
43 }
Sebastian Janssonf7ffd942018-04-16 11:46:42 +020044 static TimeDelta seconds(int64_t seconds) {
Sebastian Jansson30bd4032018-04-13 13:56:17 +020045 return TimeDelta::us(seconds * 1000000);
46 }
47 static TimeDelta ms(int64_t milliseconds) {
48 return TimeDelta::us(milliseconds * 1000);
49 }
50 static TimeDelta us(int64_t microseconds) {
51 // Infinities only allowed via use of explicit constants.
52 RTC_DCHECK(microseconds > std::numeric_limits<int64_t>::min());
53 RTC_DCHECK(microseconds < std::numeric_limits<int64_t>::max());
Sebastian Jansson30bd4032018-04-13 13:56:17 +020054 return TimeDelta(microseconds);
55 }
Sebastian Janssonf7ffd942018-04-16 11:46:42 +020056 int64_t seconds() const {
Sebastian Jansson30bd4032018-04-13 13:56:17 +020057 return (us() + (us() >= 0 ? 500000 : -500000)) / 1000000;
58 }
59 int64_t ms() const { return (us() + (us() >= 0 ? 500 : -500)) / 1000; }
60 int64_t us() const {
61 RTC_DCHECK(IsFinite());
62 return microseconds_;
63 }
64
65 double SecondsAsDouble() const;
66
67 TimeDelta Abs() const { return TimeDelta::us(std::abs(us())); }
68 bool IsZero() const { return microseconds_ == 0; }
Sebastian Jansson3b69b192018-05-07 13:51:51 +020069 bool IsFinite() const { return !IsInfinite(); }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020070 bool IsInfinite() const {
71 return microseconds_ == timedelta_impl::kPlusInfinityVal ||
72 microseconds_ == timedelta_impl::kMinusInfinityVal;
73 }
74 bool IsPlusInfinity() const {
75 return microseconds_ == timedelta_impl::kPlusInfinityVal;
76 }
77 bool IsMinusInfinity() const {
78 return microseconds_ == timedelta_impl::kMinusInfinityVal;
79 }
80 TimeDelta operator+(const TimeDelta& other) const {
81 return TimeDelta::us(us() + other.us());
82 }
83 TimeDelta operator-(const TimeDelta& other) const {
84 return TimeDelta::us(us() - other.us());
85 }
86 TimeDelta& operator-=(const TimeDelta& other) {
87 microseconds_ -= other.us();
88 return *this;
89 }
90 TimeDelta& operator+=(const TimeDelta& other) {
91 microseconds_ += other.us();
92 return *this;
93 }
Sebastian Jansson66fa5352018-04-30 16:54:57 +020094
Sebastian Jansson30bd4032018-04-13 13:56:17 +020095 bool operator==(const TimeDelta& other) const {
96 return microseconds_ == other.microseconds_;
97 }
98 bool operator!=(const TimeDelta& other) const {
99 return microseconds_ != other.microseconds_;
100 }
101 bool operator<=(const TimeDelta& other) const {
102 return microseconds_ <= other.microseconds_;
103 }
104 bool operator>=(const TimeDelta& other) const {
105 return microseconds_ >= other.microseconds_;
106 }
107 bool operator>(const TimeDelta& other) const {
108 return microseconds_ > other.microseconds_;
109 }
110 bool operator<(const TimeDelta& other) const {
111 return microseconds_ < other.microseconds_;
112 }
113
114 private:
115 explicit TimeDelta(int64_t us) : microseconds_(us) {}
116 int64_t microseconds_;
117};
Sebastian Jansson66fa5352018-04-30 16:54:57 +0200118
119inline TimeDelta operator*(const TimeDelta& delta, const double& scalar) {
120 return TimeDelta::us(std::round(delta.us() * scalar));
121}
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200122inline TimeDelta operator*(const double& scalar, const TimeDelta& delta) {
123 return delta * scalar;
124}
Sebastian Jansson66fa5352018-04-30 16:54:57 +0200125inline TimeDelta operator*(const TimeDelta& delta, const int64_t& scalar) {
126 return TimeDelta::us(delta.us() * scalar);
127}
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200128inline TimeDelta operator*(const int64_t& scalar, const TimeDelta& delta) {
129 return delta * scalar;
130}
Sebastian Jansson66fa5352018-04-30 16:54:57 +0200131inline TimeDelta operator*(const TimeDelta& delta, const int32_t& scalar) {
132 return TimeDelta::us(delta.us() * scalar);
133}
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200134inline TimeDelta operator*(const int32_t& scalar, const TimeDelta& delta) {
135 return delta * scalar;
136}
137
Sebastian Jansson66fa5352018-04-30 16:54:57 +0200138inline TimeDelta operator/(const TimeDelta& delta, const int64_t& scalar) {
139 return TimeDelta::us(delta.us() / scalar);
140}
141
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200142std::string ToString(const TimeDelta& value);
143} // namespace webrtc
144
Sebastian Jansson6fae6ec2018-05-08 10:43:18 +0200145#endif // API_UNITS_TIME_DELTA_H_