blob: 40f3e922efa19558cc8fcf815ca63cc054c2a013 [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
14#include <stdint.h>
15#include <limits>
16#include <string>
17
Sebastian Jansson6fae6ec2018-05-08 10:43:18 +020018#include "api/units/time_delta.h"
Sebastian Jansson30bd4032018-04-13 13:56:17 +020019#include "rtc_base/checks.h"
20
21namespace webrtc {
22namespace timestamp_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 timestamp_impl
26
27// Timestamp represents the time that has passed since some unspecified epoch.
28// The epoch is assumed to be before any represented timestamps, this means that
29// negative values are not valid. The most notable feature is that the
30// difference of two Timestamps results in a TimeDelta.
31class Timestamp {
32 public:
Sebastian Jansson3b69b192018-05-07 13:51:51 +020033 Timestamp() = delete;
Sebastian Jansson30bd4032018-04-13 13:56:17 +020034 static Timestamp Infinity() {
35 return Timestamp(timestamp_impl::kPlusInfinityVal);
36 }
Sebastian Janssonf7ffd942018-04-16 11:46:42 +020037 static Timestamp seconds(int64_t seconds) {
Sebastian Jansson30bd4032018-04-13 13:56:17 +020038 return Timestamp::us(seconds * 1000000);
39 }
40 static Timestamp ms(int64_t millis) { return Timestamp::us(millis * 1000); }
41 static Timestamp us(int64_t micros) {
42 RTC_DCHECK_GE(micros, 0);
43 return Timestamp(micros);
44 }
Sebastian Janssonf7ffd942018-04-16 11:46:42 +020045 int64_t seconds() const { return (us() + 500000) / 1000000; }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020046 int64_t ms() const { return (us() + 500) / 1000; }
47 int64_t us() const {
48 RTC_DCHECK(IsFinite());
49 return microseconds_;
50 }
51
52 double SecondsAsDouble() const;
53
54 bool IsInfinite() const {
55 return microseconds_ == timestamp_impl::kPlusInfinityVal;
56 }
Sebastian Jansson3b69b192018-05-07 13:51:51 +020057 bool IsFinite() const { return !IsInfinite(); }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020058 TimeDelta operator-(const Timestamp& other) const {
59 return TimeDelta::us(us() - other.us());
60 }
61 Timestamp operator-(const TimeDelta& delta) const {
62 return Timestamp::us(us() - delta.us());
63 }
64 Timestamp operator+(const TimeDelta& delta) const {
65 return Timestamp::us(us() + delta.us());
66 }
67 Timestamp& operator-=(const TimeDelta& other) {
68 microseconds_ -= other.us();
69 return *this;
70 }
71 Timestamp& operator+=(const TimeDelta& other) {
72 microseconds_ += other.us();
73 return *this;
74 }
75 bool operator==(const Timestamp& other) const {
76 return microseconds_ == other.microseconds_;
77 }
78 bool operator!=(const Timestamp& other) const {
79 return microseconds_ != other.microseconds_;
80 }
Sebastian Janssonec2eb222018-05-24 12:32:05 +020081 bool operator<=(const Timestamp& other) const {
82 return microseconds_ <= other.microseconds_;
83 }
84 bool operator>=(const Timestamp& other) const {
85 return microseconds_ >= other.microseconds_;
86 }
87 bool operator>(const Timestamp& other) const {
88 return microseconds_ > other.microseconds_;
89 }
90 bool operator<(const Timestamp& other) const {
91 return microseconds_ < other.microseconds_;
92 }
Sebastian Jansson30bd4032018-04-13 13:56:17 +020093
94 private:
95 explicit Timestamp(int64_t us) : microseconds_(us) {}
96 int64_t microseconds_;
97};
98
99std::string ToString(const Timestamp& value);
100
101} // namespace webrtc
102
Sebastian Jansson6fae6ec2018-05-08 10:43:18 +0200103#endif // API_UNITS_TIMESTAMP_H_