1// Copyright 2013 The Flutter 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 FLUTTER_FML_TIME_TIME_POINT_H_
6#define FLUTTER_FML_TIME_TIME_POINT_H_
7
8#include <stdint.h>
9
10#include <iosfwd>
11
12#include "flutter/fml/time/time_delta.h"
13
14namespace fml {
15
16// A TimePoint represents a point in time represented as an integer number of
17// nanoseconds elapsed since an arbitrary point in the past.
18//
19// WARNING: This class should not be serialized across reboots, or across
20// devices: the reference point is only stable for a given device between
21// reboots.
22class TimePoint {
23 public:
24 // Default TimePoint with internal value 0 (epoch).
25 constexpr TimePoint() = default;
26
27 static TimePoint Now();
28
29 static constexpr TimePoint Min() {
30 return TimePoint(std::numeric_limits<int64_t>::min());
31 }
32
33 static constexpr TimePoint Max() {
34 return TimePoint(std::numeric_limits<int64_t>::max());
35 }
36
37 static constexpr TimePoint FromEpochDelta(TimeDelta ticks) {
38 return TimePoint(ticks.ToNanoseconds());
39 }
40
41 TimeDelta ToEpochDelta() const { return TimeDelta::FromNanoseconds(ticks_); }
42
43 // Compute the difference between two time points.
44 TimeDelta operator-(TimePoint other) const {
45 return TimeDelta::FromNanoseconds(ticks_ - other.ticks_);
46 }
47
48 TimePoint operator+(TimeDelta duration) const {
49 return TimePoint(ticks_ + duration.ToNanoseconds());
50 }
51 TimePoint operator-(TimeDelta duration) const {
52 return TimePoint(ticks_ - duration.ToNanoseconds());
53 }
54
55 bool operator==(TimePoint other) const { return ticks_ == other.ticks_; }
56 bool operator!=(TimePoint other) const { return ticks_ != other.ticks_; }
57 bool operator<(TimePoint other) const { return ticks_ < other.ticks_; }
58 bool operator<=(TimePoint other) const { return ticks_ <= other.ticks_; }
59 bool operator>(TimePoint other) const { return ticks_ > other.ticks_; }
60 bool operator>=(TimePoint other) const { return ticks_ >= other.ticks_; }
61
62 private:
63 explicit constexpr TimePoint(int64_t ticks) : ticks_(ticks) {}
64
65 int64_t ticks_ = 0;
66};
67
68} // namespace fml
69
70#endif // FLUTTER_FML_TIME_TIME_POINT_H_
71