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: |
| 15 | constexpr static TimeDelta FromMilliseconds(int64_t msec) { |
| 16 | return TimeDelta(msec * 1000); |
| 17 | } |
| 18 | constexpr static TimeDelta FromMicroseconds(int64_t usec) { |
| 19 | return TimeDelta(usec); |
| 20 | } |
mark a. foltz | c28ca40 | 2018-07-19 16:11:32 -0700 | [diff] [blame] | 21 | |
btolsch | 5292c94 | 2018-07-26 00:06:22 -0700 | [diff] [blame^] | 22 | constexpr int64_t AsSeconds() const { return microseconds_ / 1000000; } |
| 23 | constexpr int64_t AsMilliseconds() const { return microseconds_ / 1000; } |
| 24 | constexpr int64_t AsMicroseconds() const { return microseconds_; } |
| 25 | |
| 26 | private: |
| 27 | constexpr explicit TimeDelta(int64_t usec) : microseconds_(usec) {} |
| 28 | |
| 29 | friend constexpr TimeDelta operator-(TimeDelta t1, TimeDelta t2) { |
| 30 | return TimeDelta(t1.microseconds_ - t2.microseconds_); |
| 31 | } |
| 32 | friend constexpr bool operator<(TimeDelta t1, TimeDelta t2) { |
| 33 | return t1.microseconds_ < t2.microseconds_; |
| 34 | } |
| 35 | friend constexpr bool operator<=(TimeDelta t1, TimeDelta t2) { |
| 36 | return t1.microseconds_ <= t2.microseconds_; |
| 37 | } |
| 38 | friend constexpr bool operator>(TimeDelta t1, TimeDelta t2) { |
| 39 | return t1.microseconds_ > t2.microseconds_; |
| 40 | } |
| 41 | friend constexpr bool operator>=(TimeDelta t1, TimeDelta t2) { |
| 42 | return t1.microseconds_ >= t2.microseconds_; |
| 43 | } |
| 44 | friend constexpr bool operator==(TimeDelta t1, TimeDelta t2) { |
| 45 | return t1.microseconds_ == t2.microseconds_; |
| 46 | } |
| 47 | friend constexpr bool operator!=(TimeDelta t1, TimeDelta t2) { |
| 48 | return t1.microseconds_ != t2.microseconds_; |
| 49 | } |
| 50 | |
| 51 | int64_t microseconds_; |
| 52 | }; |
| 53 | |
| 54 | TimeDelta GetMonotonicTimeNow(); |
| 55 | TimeDelta GetUTCNow(); |
mark a. foltz | c28ca40 | 2018-07-19 16:11:32 -0700 | [diff] [blame] | 56 | |
| 57 | } // namespace platform |
| 58 | } // namespace openscreen |
| 59 | |
| 60 | #endif |