Flutter Engine
 
Loading...
Searching...
No Matches
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 <functional>
10#include <iosfwd>
11
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 using ClockSource = TimePoint (*)();
25
26 // Default TimePoint with internal value 0 (epoch).
27 constexpr TimePoint() = default;
28
29 static void SetClockSource(ClockSource source);
30
31 static TimePoint Now();
32
34
35 static constexpr TimePoint Min() {
36 return TimePoint(std::numeric_limits<int64_t>::min());
37 }
38
39 static constexpr TimePoint Max() {
40 return TimePoint(std::numeric_limits<int64_t>::max());
41 }
42
43 static constexpr TimePoint FromEpochDelta(TimeDelta ticks) {
44 return TimePoint(ticks.ToNanoseconds());
45 }
46
47 // Expects ticks in nanos.
48 static constexpr TimePoint FromTicks(int64_t ticks) {
49 return TimePoint(ticks);
50 }
51
52 constexpr TimeDelta ToEpochDelta() const {
53 return TimeDelta::FromNanoseconds(ticks_);
54 }
55
56 // Compute the difference between two time points.
57 constexpr TimeDelta operator-(TimePoint other) const {
58 return TimeDelta::FromNanoseconds(ticks_ - other.ticks_);
59 }
60
61 constexpr TimePoint operator+(TimeDelta duration) const {
62 return TimePoint(ticks_ + duration.ToNanoseconds());
63 }
64 constexpr TimePoint operator-(TimeDelta duration) const {
65 return TimePoint(ticks_ - duration.ToNanoseconds());
66 }
67
68 constexpr auto operator<=>(const TimePoint& other) const = default;
69
70 private:
71 explicit constexpr TimePoint(int64_t ticks) : ticks_(ticks) {}
72
73 int64_t ticks_ = 0;
74};
75
76} // namespace fml
77
78#endif // FLUTTER_FML_TIME_TIME_POINT_H_
static constexpr TimeDelta FromNanoseconds(int64_t nanos)
Definition time_delta.h:40
constexpr int64_t ToNanoseconds() const
Definition time_delta.h:61
constexpr auto operator<=>(const TimePoint &other) const =default
TimePoint(*)() ClockSource
Definition time_point.h:24
constexpr TimeDelta operator-(TimePoint other) const
Definition time_point.h:57
static constexpr TimePoint FromTicks(int64_t ticks)
Definition time_point.h:48
static constexpr TimePoint Max()
Definition time_point.h:39
static void SetClockSource(ClockSource source)
Definition time_point.cc:45
constexpr TimePoint operator+(TimeDelta duration) const
Definition time_point.h:61
constexpr TimeDelta ToEpochDelta() const
Definition time_point.h:52
static TimePoint CurrentWallTime()
Definition time_point.cc:57
static TimePoint Now()
Definition time_point.cc:49
constexpr TimePoint()=default
static constexpr TimePoint Min()
Definition time_point.h:35
constexpr TimePoint operator-(TimeDelta duration) const
Definition time_point.h:64
static constexpr TimePoint FromEpochDelta(TimeDelta ticks)
Definition time_point.h:43