Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 1 | /* |
| 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 Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 11 | #ifndef API_UNITS_TIMESTAMP_H_ |
| 12 | #define API_UNITS_TIMESTAMP_H_ |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 13 | |
| 14 | #include <stdint.h> |
| 15 | #include <limits> |
| 16 | #include <string> |
| 17 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 18 | #include "api/units/time_delta.h" |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 19 | #include "rtc_base/checks.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | namespace timestamp_impl { |
| 23 | constexpr int64_t kPlusInfinityVal = std::numeric_limits<int64_t>::max(); |
| 24 | constexpr int64_t kMinusInfinityVal = std::numeric_limits<int64_t>::min(); |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 25 | } // 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. |
| 31 | class Timestamp { |
| 32 | public: |
Sebastian Jansson | 3b69b19 | 2018-05-07 13:51:51 +0200 | [diff] [blame] | 33 | Timestamp() = delete; |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 34 | static Timestamp Infinity() { |
| 35 | return Timestamp(timestamp_impl::kPlusInfinityVal); |
| 36 | } |
Sebastian Jansson | f7ffd94 | 2018-04-16 11:46:42 +0200 | [diff] [blame] | 37 | static Timestamp seconds(int64_t seconds) { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 38 | 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 Jansson | f7ffd94 | 2018-04-16 11:46:42 +0200 | [diff] [blame] | 45 | int64_t seconds() const { return (us() + 500000) / 1000000; } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 46 | 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 Jansson | 3b69b19 | 2018-05-07 13:51:51 +0200 | [diff] [blame] | 57 | bool IsFinite() const { return !IsInfinite(); } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 58 | 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 Jansson | ec2eb22 | 2018-05-24 12:32:05 +0200 | [diff] [blame] | 81 | 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 Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 93 | |
| 94 | private: |
| 95 | explicit Timestamp(int64_t us) : microseconds_(us) {} |
| 96 | int64_t microseconds_; |
| 97 | }; |
| 98 | |
| 99 | std::string ToString(const Timestamp& value); |
| 100 | |
| 101 | } // namespace webrtc |
| 102 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 103 | #endif // API_UNITS_TIMESTAMP_H_ |