• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DELTA_H_
6 #define FLUTTER_FML_TIME_TIME_DELTA_H_
7 
8 #include <stdint.h>
9 #include <time.h>
10 
11 #include <iosfwd>
12 #include <limits>
13 
14 namespace fml {
15 
16 // A TimeDelta represents the difference between two time points.
17 class TimeDelta {
18  public:
19   constexpr TimeDelta() = default;
20 
Zero()21   static constexpr TimeDelta Zero() { return TimeDelta(); }
Min()22   static constexpr TimeDelta Min() {
23     return TimeDelta(std::numeric_limits<int64_t>::min());
24   }
Max()25   static constexpr TimeDelta Max() {
26     return TimeDelta(std::numeric_limits<int64_t>::max());
27   }
FromNanoseconds(int64_t nanos)28   static constexpr TimeDelta FromNanoseconds(int64_t nanos) {
29     return TimeDelta(nanos);
30   }
FromMicroseconds(int64_t micros)31   static constexpr TimeDelta FromMicroseconds(int64_t micros) {
32     return FromNanoseconds(micros * 1000);
33   }
FromMilliseconds(int64_t millis)34   static constexpr TimeDelta FromMilliseconds(int64_t millis) {
35     return FromMicroseconds(millis * 1000);
36   }
FromSeconds(int64_t seconds)37   static constexpr TimeDelta FromSeconds(int64_t seconds) {
38     return FromMilliseconds(seconds * 1000);
39   }
40 
FromSecondsF(double seconds)41   static constexpr TimeDelta FromSecondsF(double seconds) {
42     return FromNanoseconds(seconds * (1000.0 * 1000.0 * 1000.0));
43   }
44 
ToNanoseconds()45   constexpr int64_t ToNanoseconds() const { return delta_; }
ToMicroseconds()46   constexpr int64_t ToMicroseconds() const { return ToNanoseconds() / 1000; }
ToMilliseconds()47   constexpr int64_t ToMilliseconds() const { return ToMicroseconds() / 1000; }
ToSeconds()48   constexpr int64_t ToSeconds() const { return ToMilliseconds() / 1000; }
49 
ToNanosecondsF()50   constexpr double ToNanosecondsF() const { return delta_; }
ToMicrosecondsF()51   constexpr double ToMicrosecondsF() const { return delta_ / 1000.0; }
ToMillisecondsF()52   constexpr double ToMillisecondsF() const {
53     return delta_ / (1000.0 * 1000.0);
54   }
ToSecondsF()55   constexpr double ToSecondsF() const {
56     return delta_ / (1000.0 * 1000.0 * 1000.0);
57   }
58 
59   constexpr TimeDelta operator-(TimeDelta other) const {
60     return TimeDelta::FromNanoseconds(delta_ - other.delta_);
61   }
62 
63   constexpr TimeDelta operator+(TimeDelta other) const {
64     return TimeDelta::FromNanoseconds(delta_ + other.delta_);
65   }
66 
67   constexpr TimeDelta operator/(int64_t divisor) const {
68     return TimeDelta::FromNanoseconds(delta_ / divisor);
69   }
70 
71   constexpr int64_t operator/(TimeDelta other) const {
72     return delta_ / other.delta_;
73   }
74 
75   constexpr TimeDelta operator*(int64_t multiplier) const {
76     return TimeDelta::FromNanoseconds(delta_ * multiplier);
77   }
78 
79   constexpr TimeDelta operator%(TimeDelta other) const {
80     return TimeDelta::FromNanoseconds(delta_ % other.delta_);
81   }
82 
83   bool operator==(TimeDelta other) const { return delta_ == other.delta_; }
84   bool operator!=(TimeDelta other) const { return delta_ != other.delta_; }
85   bool operator<(TimeDelta other) const { return delta_ < other.delta_; }
86   bool operator<=(TimeDelta other) const { return delta_ <= other.delta_; }
87   bool operator>(TimeDelta other) const { return delta_ > other.delta_; }
88   bool operator>=(TimeDelta other) const { return delta_ >= other.delta_; }
89 
FromTimespec(struct timespec ts)90   static constexpr TimeDelta FromTimespec(struct timespec ts) {
91     return TimeDelta::FromSeconds(ts.tv_sec) +
92            TimeDelta::FromNanoseconds(ts.tv_nsec);
93   }
ToTimespec()94   struct timespec ToTimespec() {
95     struct timespec ts;
96     constexpr int64_t kNanosecondsPerSecond = 1000000000ll;
97     ts.tv_sec = static_cast<time_t>(ToSeconds());
98     ts.tv_nsec = delta_ % kNanosecondsPerSecond;
99     return ts;
100   }
101 
102  private:
103   // Private, use one of the FromFoo() types
TimeDelta(int64_t delta)104   explicit constexpr TimeDelta(int64_t delta) : delta_(delta) {}
105 
106   int64_t delta_ = 0;
107 };
108 
109 }  // namespace fml
110 
111 #endif  // FLUTTER_FML_TIME_TIME_DELTA_H_
112