blob: f9ed408a228387adba43ff0d3a54252acebd5e5b [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_TIMESTAMP_H_
12#define API_UNITS_TIMESTAMP_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
Sebastian Jansson30bd4032018-04-13 13:56:17 +020018#include <string>
Yves Gerey988cc082018-10-23 12:03:01 +020019#include <type_traits>
Sebastian Jansson30bd4032018-04-13 13:56:17 +020020
Sebastian Jansson6fae6ec2018-05-08 10:43:18 +020021#include "api/units/time_delta.h"
Sebastian Jansson30bd4032018-04-13 13:56:17 +020022#include "rtc_base/checks.h"
23
24namespace webrtc {
Sebastian Jansson30bd4032018-04-13 13:56:17 +020025// Timestamp represents the time that has passed since some unspecified epoch.
26// The epoch is assumed to be before any represented timestamps, this means that
27// negative values are not valid. The most notable feature is that the
28// difference of two Timestamps results in a TimeDelta.
Sebastian Jansson72bba622018-11-19 11:17:12 +010029class Timestamp final : public rtc_units_impl::UnitBase<Timestamp> {
Sebastian Jansson30bd4032018-04-13 13:56:17 +020030 public:
Sebastian Jansson3b69b192018-05-07 13:51:51 +020031 Timestamp() = delete;
Sebastian Jansson72bba622018-11-19 11:17:12 +010032
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020033 template <int64_t seconds>
34 static constexpr Timestamp Seconds() {
Danil Chapovalov7356a562020-01-20 13:02:44 +010035 return FromFraction(1'000'000, seconds);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020036 }
37 template <int64_t ms>
38 static constexpr Timestamp Millis() {
Danil Chapovalov7356a562020-01-20 13:02:44 +010039 return FromFraction(1000, ms);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020040 }
41 template <int64_t us>
42 static constexpr Timestamp Micros() {
Danil Chapovalov7356a562020-01-20 13:02:44 +010043 return FromValue(us);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020044 }
Sebastian Jansson942b3602018-05-30 15:47:44 +020045
Sebastian Jansson72bba622018-11-19 11:17:12 +010046 template <typename T>
Sebastian Janssond7fade52020-01-29 10:44:51 +010047 static constexpr Timestamp seconds(T seconds) {
Sebastian Jansson0c3f4d32018-11-30 10:02:44 +010048 static_assert(std::is_arithmetic<T>::value, "");
Danil Chapovalov7356a562020-01-20 13:02:44 +010049 return FromFraction(1'000'000, seconds);
Sebastian Jansson30bd4032018-04-13 13:56:17 +020050 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010051 template <typename T>
Sebastian Janssond7fade52020-01-29 10:44:51 +010052 static constexpr Timestamp ms(T milliseconds) {
Sebastian Jansson0c3f4d32018-11-30 10:02:44 +010053 static_assert(std::is_arithmetic<T>::value, "");
Danil Chapovalov7356a562020-01-20 13:02:44 +010054 return FromFraction(1000, milliseconds);
Sebastian Jansson942b3602018-05-30 15:47:44 +020055 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010056 template <typename T>
Sebastian Janssond7fade52020-01-29 10:44:51 +010057 static constexpr Timestamp us(T microseconds) {
Sebastian Jansson0c3f4d32018-11-30 10:02:44 +010058 static_assert(std::is_arithmetic<T>::value, "");
Sebastian Jansson72bba622018-11-19 11:17:12 +010059 return FromValue(microseconds);
Sebastian Jansson942b3602018-05-30 15:47:44 +020060 }
61 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010062 constexpr T seconds() const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010063 return ToFraction<1000000, T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020064 }
65 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010066 constexpr T ms() const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010067 return ToFraction<1000, T>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020068 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010069 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010070 constexpr T us() const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010071 return ToValue<T>();
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020072 }
73
74 constexpr int64_t seconds_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010075 return ToFractionOr<1000000>(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020076 }
77 constexpr int64_t ms_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010078 return ToFractionOr<1000>(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020079 }
80 constexpr int64_t us_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010081 return ToValueOr(fallback_value);
Sebastian Jansson942b3602018-05-30 15:47:44 +020082 }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020083
Sebastian Janssond7fade52020-01-29 10:44:51 +010084 constexpr Timestamp operator+(const TimeDelta delta) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010085 if (IsPlusInfinity() || delta.IsPlusInfinity()) {
Sebastian Jansson9de4ef42018-09-04 17:32:36 +020086 RTC_DCHECK(!IsMinusInfinity());
Sebastian Jansson72bba622018-11-19 11:17:12 +010087 RTC_DCHECK(!delta.IsMinusInfinity());
Sebastian Jansson9de4ef42018-09-04 17:32:36 +020088 return PlusInfinity();
Sebastian Jansson72bba622018-11-19 11:17:12 +010089 } else if (IsMinusInfinity() || delta.IsMinusInfinity()) {
Sebastian Jansson9de4ef42018-09-04 17:32:36 +020090 RTC_DCHECK(!IsPlusInfinity());
Sebastian Jansson72bba622018-11-19 11:17:12 +010091 RTC_DCHECK(!delta.IsPlusInfinity());
Sebastian Jansson9de4ef42018-09-04 17:32:36 +020092 return MinusInfinity();
93 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010094 return Timestamp::us(us() + delta.us());
Sebastian Jansson9de4ef42018-09-04 17:32:36 +020095 }
Sebastian Janssond7fade52020-01-29 10:44:51 +010096 constexpr Timestamp operator-(const TimeDelta delta) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010097 if (IsPlusInfinity() || delta.IsMinusInfinity()) {
Sebastian Jansson9de4ef42018-09-04 17:32:36 +020098 RTC_DCHECK(!IsMinusInfinity());
Sebastian Jansson72bba622018-11-19 11:17:12 +010099 RTC_DCHECK(!delta.IsPlusInfinity());
Sebastian Jansson9de4ef42018-09-04 17:32:36 +0200100 return PlusInfinity();
Sebastian Jansson72bba622018-11-19 11:17:12 +0100101 } else if (IsMinusInfinity() || delta.IsPlusInfinity()) {
Sebastian Jansson9de4ef42018-09-04 17:32:36 +0200102 RTC_DCHECK(!IsPlusInfinity());
Sebastian Jansson72bba622018-11-19 11:17:12 +0100103 RTC_DCHECK(!delta.IsMinusInfinity());
Sebastian Jansson9de4ef42018-09-04 17:32:36 +0200104 return MinusInfinity();
105 }
Sebastian Jansson72bba622018-11-19 11:17:12 +0100106 return Timestamp::us(us() - delta.us());
Sebastian Jansson9de4ef42018-09-04 17:32:36 +0200107 }
Sebastian Janssond7fade52020-01-29 10:44:51 +0100108 constexpr TimeDelta operator-(const Timestamp other) const {
Sebastian Jansson9de4ef42018-09-04 17:32:36 +0200109 if (IsPlusInfinity() || other.IsMinusInfinity()) {
110 RTC_DCHECK(!IsMinusInfinity());
111 RTC_DCHECK(!other.IsPlusInfinity());
112 return TimeDelta::PlusInfinity();
113 } else if (IsMinusInfinity() || other.IsPlusInfinity()) {
114 RTC_DCHECK(!IsPlusInfinity());
115 RTC_DCHECK(!other.IsMinusInfinity());
116 return TimeDelta::MinusInfinity();
117 }
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200118 return TimeDelta::us(us() - other.us());
119 }
Sebastian Janssond7fade52020-01-29 10:44:51 +0100120 constexpr Timestamp& operator-=(const TimeDelta delta) {
Sebastian Jansson72bba622018-11-19 11:17:12 +0100121 *this = *this - delta;
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200122 return *this;
123 }
Sebastian Janssond7fade52020-01-29 10:44:51 +0100124 constexpr Timestamp& operator+=(const TimeDelta delta) {
Sebastian Jansson72bba622018-11-19 11:17:12 +0100125 *this = *this + delta;
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200126 return *this;
127 }
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200128
129 private:
Sebastian Jansson72bba622018-11-19 11:17:12 +0100130 friend class rtc_units_impl::UnitBase<Timestamp>;
131 using UnitBase::UnitBase;
132 static constexpr bool one_sided = true;
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200133};
134
Sebastian Jansson72bba622018-11-19 11:17:12 +0100135std::string ToString(Timestamp value);
Sebastian Janssonb1138622019-04-11 16:48:15 +0200136inline std::string ToLogString(Timestamp value) {
137 return ToString(value);
138}
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200139
Sebastian Jansson2afd2812018-08-23 14:44:05 +0200140#ifdef UNIT_TEST
141inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
142 std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
143 Timestamp value) {
144 return stream << ToString(value);
145}
146#endif // UNIT_TEST
147
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200148} // namespace webrtc
149
Sebastian Jansson6fae6ec2018-05-08 10:43:18 +0200150#endif // API_UNITS_TIMESTAMP_H_