blob: 2491920cdbb2f3b4832a73c959c004bfb91cb9e7 [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 }
Sebastian Jansson5f83cf02018-05-08 14:52:22 +020064 int64_t ns() const {
65 RTC_DCHECK(us() > std::numeric_limits<int64_t>::min() / 1000);
66 RTC_DCHECK(us() < std::numeric_limits<int64_t>::max() / 1000);
67 return us() * 1000;
68 }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020069
70 double SecondsAsDouble() const;
71
72 TimeDelta Abs() const { return TimeDelta::us(std::abs(us())); }
73 bool IsZero() const { return microseconds_ == 0; }
Sebastian Jansson3b69b192018-05-07 13:51:51 +020074 bool IsFinite() const { return !IsInfinite(); }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020075 bool IsInfinite() const {
76 return microseconds_ == timedelta_impl::kPlusInfinityVal ||
77 microseconds_ == timedelta_impl::kMinusInfinityVal;
78 }
79 bool IsPlusInfinity() const {
80 return microseconds_ == timedelta_impl::kPlusInfinityVal;
81 }
82 bool IsMinusInfinity() const {
83 return microseconds_ == timedelta_impl::kMinusInfinityVal;
84 }
85 TimeDelta operator+(const TimeDelta& other) const {
86 return TimeDelta::us(us() + other.us());
87 }
88 TimeDelta operator-(const TimeDelta& other) const {
89 return TimeDelta::us(us() - other.us());
90 }
91 TimeDelta& operator-=(const TimeDelta& other) {
92 microseconds_ -= other.us();
93 return *this;
94 }
95 TimeDelta& operator+=(const TimeDelta& other) {
96 microseconds_ += other.us();
97 return *this;
98 }
Sebastian Jansson66fa5352018-04-30 16:54:57 +020099
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200100 bool operator==(const TimeDelta& other) const {
101 return microseconds_ == other.microseconds_;
102 }
103 bool operator!=(const TimeDelta& other) const {
104 return microseconds_ != other.microseconds_;
105 }
106 bool operator<=(const TimeDelta& other) const {
107 return microseconds_ <= other.microseconds_;
108 }
109 bool operator>=(const TimeDelta& other) const {
110 return microseconds_ >= other.microseconds_;
111 }
112 bool operator>(const TimeDelta& other) const {
113 return microseconds_ > other.microseconds_;
114 }
115 bool operator<(const TimeDelta& other) const {
116 return microseconds_ < other.microseconds_;
117 }
118
119 private:
120 explicit TimeDelta(int64_t us) : microseconds_(us) {}
121 int64_t microseconds_;
122};
Sebastian Jansson66fa5352018-04-30 16:54:57 +0200123
124inline TimeDelta operator*(const TimeDelta& delta, const double& scalar) {
125 return TimeDelta::us(std::round(delta.us() * scalar));
126}
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200127inline TimeDelta operator*(const double& scalar, const TimeDelta& delta) {
128 return delta * scalar;
129}
Sebastian Jansson66fa5352018-04-30 16:54:57 +0200130inline TimeDelta operator*(const TimeDelta& delta, const int64_t& scalar) {
131 return TimeDelta::us(delta.us() * scalar);
132}
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200133inline TimeDelta operator*(const int64_t& scalar, const TimeDelta& delta) {
134 return delta * scalar;
135}
Sebastian Jansson66fa5352018-04-30 16:54:57 +0200136inline TimeDelta operator*(const TimeDelta& delta, const int32_t& scalar) {
137 return TimeDelta::us(delta.us() * scalar);
138}
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200139inline TimeDelta operator*(const int32_t& scalar, const TimeDelta& delta) {
140 return delta * scalar;
141}
142
Sebastian Jansson66fa5352018-04-30 16:54:57 +0200143inline TimeDelta operator/(const TimeDelta& delta, const int64_t& scalar) {
144 return TimeDelta::us(delta.us() / scalar);
145}
146
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200147std::string ToString(const TimeDelta& value);
148} // namespace webrtc
149
Sebastian Jansson6fae6ec2018-05-08 10:43:18 +0200150#endif // API_UNITS_TIME_DELTA_H_