mark a. foltz | c28ca40 | 2018-07-19 16:11:32 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef PLATFORM_API_TIME_H_ |
| 6 | #define PLATFORM_API_TIME_H_ |
| 7 | |
| 8 | #include <cstdint> |
| 9 | |
| 10 | namespace openscreen { |
| 11 | namespace platform { |
| 12 | |
btolsch | 5292c94 | 2018-07-26 00:06:22 -0700 | [diff] [blame] | 13 | class TimeDelta { |
| 14 | public: |
btolsch | c11dc34 | 2019-01-16 16:37:34 -0800 | [diff] [blame] | 15 | constexpr static TimeDelta FromSeconds(int64_t sec) { |
| 16 | return TimeDelta(sec * 1000000); |
| 17 | } |
btolsch | 5292c94 | 2018-07-26 00:06:22 -0700 | [diff] [blame] | 18 | constexpr static TimeDelta FromMilliseconds(int64_t msec) { |
| 19 | return TimeDelta(msec * 1000); |
| 20 | } |
| 21 | constexpr static TimeDelta FromMicroseconds(int64_t usec) { |
| 22 | return TimeDelta(usec); |
| 23 | } |
mark a. foltz | c28ca40 | 2018-07-19 16:11:32 -0700 | [diff] [blame] | 24 | |
btolsch | 5292c94 | 2018-07-26 00:06:22 -0700 | [diff] [blame] | 25 | constexpr int64_t AsSeconds() const { return microseconds_ / 1000000; } |
| 26 | constexpr int64_t AsMilliseconds() const { return microseconds_ / 1000; } |
| 27 | constexpr int64_t AsMicroseconds() const { return microseconds_; } |
| 28 | |
btolsch | c11dc34 | 2019-01-16 16:37:34 -0800 | [diff] [blame] | 29 | constexpr TimeDelta& operator-=(TimeDelta t) { |
| 30 | microseconds_ -= t.microseconds_; |
| 31 | return *this; |
| 32 | } |
| 33 | |
| 34 | constexpr TimeDelta& operator+=(TimeDelta t) { |
| 35 | microseconds_ += t.microseconds_; |
| 36 | return *this; |
| 37 | } |
| 38 | |
btolsch | 5292c94 | 2018-07-26 00:06:22 -0700 | [diff] [blame] | 39 | private: |
| 40 | constexpr explicit TimeDelta(int64_t usec) : microseconds_(usec) {} |
| 41 | |
| 42 | friend constexpr TimeDelta operator-(TimeDelta t1, TimeDelta t2) { |
| 43 | return TimeDelta(t1.microseconds_ - t2.microseconds_); |
| 44 | } |
btolsch | c11dc34 | 2019-01-16 16:37:34 -0800 | [diff] [blame] | 45 | friend constexpr TimeDelta operator+(TimeDelta t1, TimeDelta t2) { |
| 46 | return TimeDelta(t1.microseconds_ + t2.microseconds_); |
| 47 | } |
btolsch | 5292c94 | 2018-07-26 00:06:22 -0700 | [diff] [blame] | 48 | friend constexpr bool operator<(TimeDelta t1, TimeDelta t2) { |
| 49 | return t1.microseconds_ < t2.microseconds_; |
| 50 | } |
| 51 | friend constexpr bool operator<=(TimeDelta t1, TimeDelta t2) { |
| 52 | return t1.microseconds_ <= t2.microseconds_; |
| 53 | } |
| 54 | friend constexpr bool operator>(TimeDelta t1, TimeDelta t2) { |
| 55 | return t1.microseconds_ > t2.microseconds_; |
| 56 | } |
| 57 | friend constexpr bool operator>=(TimeDelta t1, TimeDelta t2) { |
| 58 | return t1.microseconds_ >= t2.microseconds_; |
| 59 | } |
| 60 | friend constexpr bool operator==(TimeDelta t1, TimeDelta t2) { |
| 61 | return t1.microseconds_ == t2.microseconds_; |
| 62 | } |
| 63 | friend constexpr bool operator!=(TimeDelta t1, TimeDelta t2) { |
| 64 | return t1.microseconds_ != t2.microseconds_; |
| 65 | } |
| 66 | |
| 67 | int64_t microseconds_; |
| 68 | }; |
| 69 | |
| 70 | TimeDelta GetMonotonicTimeNow(); |
| 71 | TimeDelta GetUTCNow(); |
mark a. foltz | c28ca40 | 2018-07-19 16:11:32 -0700 | [diff] [blame] | 72 | |
| 73 | } // namespace platform |
| 74 | } // namespace openscreen |
| 75 | |
btolsch | a21e8ed | 2018-08-30 15:13:48 -0700 | [diff] [blame] | 76 | #endif // PLATFORM_API_TIME_H_ |