blob: bfbd90b41002488c29e009347e8ba2f74cac7817 [file] [log] [blame]
mark a. foltzc28ca402018-07-19 16:11:32 -07001// 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
10namespace openscreen {
11namespace platform {
12
btolsch5292c942018-07-26 00:06:22 -070013class 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. foltzc28ca402018-07-19 16:11:32 -070021
btolsch5292c942018-07-26 00:06:22 -070022 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
54TimeDelta GetMonotonicTimeNow();
55TimeDelta GetUTCNow();
mark a. foltzc28ca402018-07-19 16:11:32 -070056
57} // namespace platform
58} // namespace openscreen
59
btolscha21e8ed2018-08-30 15:13:48 -070060#endif // PLATFORM_API_TIME_H_