blob: c8dcf03bd3a1410412d0012bc66abbf01abb6ed8 [file] [log] [blame]
Taylor Brandstetterb3c68102016-05-27 14:15:43 -07001/*
2 * Copyright 2016 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_TIMEDELTA_H_
12#define RTC_BASE_TIMEDELTA_H_
Taylor Brandstetterb3c68102016-05-27 14:15:43 -070013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <stdint.h>
pbosc7c26a02017-01-02 08:42:32 -080015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/timeutils.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020017
18// Convenience class to convert between different units of relative time.
19// Stores time to precision of nanoseconds, as int64_t internally.
20// Doesn't check for overflow/underflow.
21//
22// Based on TimeDelta in:
23// https://code.google.com/p/chromium/codesearch#chromium/src/base/time/time.h
24namespace rtc {
25
26class TimeDelta {
27 public:
28 TimeDelta() : delta_(0) {}
29
30 // Converts units of time to TimeDeltas.
31 static constexpr TimeDelta FromSeconds(int64_t secs) {
32 return TimeDelta(secs * kNumNanosecsPerSec);
33 }
34 static constexpr TimeDelta FromMilliseconds(int64_t ms) {
35 return TimeDelta(ms * kNumNanosecsPerMillisec);
36 }
37 static constexpr TimeDelta FromMicroseconds(int64_t us) {
38 return TimeDelta(us * kNumNanosecsPerMicrosec);
39 }
40 static constexpr TimeDelta FromNanoseconds(int64_t ns) {
41 return TimeDelta(ns);
42 }
43
44 // Returns true if the time delta is zero.
45 bool is_zero() const { return delta_ == 0; }
46
47 // Converts TimeDelta to units of time.
48 int64_t ToSeconds() const { return delta_ / kNumNanosecsPerSec; }
49 int64_t ToMilliseconds() const { return delta_ / kNumNanosecsPerMillisec; }
50 int64_t ToMicroseconds() const { return delta_ / kNumNanosecsPerMicrosec; }
51 int64_t ToNanoseconds() const { return delta_; }
52
53 TimeDelta& operator=(TimeDelta other) {
54 delta_ = other.delta_;
55 return *this;
56 }
57
58 // Computations with other deltas.
59 TimeDelta operator+(TimeDelta other) const {
60 return TimeDelta(delta_ + other.delta_);
61 }
62 TimeDelta operator-(TimeDelta other) const {
63 return TimeDelta(delta_ + other.delta_);
64 }
65
66 TimeDelta& operator+=(TimeDelta other) { return *this = (*this + other); }
67 TimeDelta& operator-=(TimeDelta other) { return *this = (*this - other); }
68 TimeDelta operator-() const { return TimeDelta(-delta_); }
69
70 // Computations with numeric types.
71 template <typename T>
72 TimeDelta operator*(T a) const {
73 return TimeDelta(delta_ * a);
74 }
75 template <typename T>
76 TimeDelta operator/(T a) const {
77 return TimeDelta(delta_ / a);
78 }
79 template <typename T>
80 TimeDelta& operator*=(T a) {
81 return *this = (*this * a);
82 }
83 template <typename T>
84 TimeDelta& operator/=(T a) {
85 return *this = (*this / a);
86 }
87
88 TimeDelta operator%(TimeDelta a) const {
89 return TimeDelta(delta_ % a.delta_);
90 }
91
92 // Comparison operators.
93 constexpr bool operator==(TimeDelta other) const {
94 return delta_ == other.delta_;
95 }
96 constexpr bool operator!=(TimeDelta other) const {
97 return delta_ != other.delta_;
98 }
99 constexpr bool operator<(TimeDelta other) const {
100 return delta_ < other.delta_;
101 }
102 constexpr bool operator<=(TimeDelta other) const {
103 return delta_ <= other.delta_;
104 }
105 constexpr bool operator>(TimeDelta other) const {
106 return delta_ > other.delta_;
107 }
108 constexpr bool operator>=(TimeDelta other) const {
109 return delta_ >= other.delta_;
110 }
111
112 private:
113 // Constructs a delta given the duration in nanoseconds. This is private
114 // to avoid confusion by callers with an integer constructor. Use
115 // FromSeconds, FromMilliseconds, etc. instead.
116 constexpr explicit TimeDelta(int64_t delta_ns) : delta_(delta_ns) {}
117
118 // Delta in nanoseconds.
119 int64_t delta_;
120};
121
122template <typename T>
123inline TimeDelta operator*(T a, TimeDelta td) {
124 return td * a;
125}
126
127} // namespace rtc
Taylor Brandstetterb3c68102016-05-27 14:15:43 -0700128
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200129#endif // RTC_BASE_TIMEDELTA_H_