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