1 #include "chre/util/time.h"
2 
3 //! The number of nanoseconds in one second.
4 constexpr uint64_t kOneSecondInNanoseconds(1000000000);
5 
6 //! The number of nanoseconds in one millisecond.
7 constexpr uint64_t kOneMillisecondInNanoseconds(1000000);
8 
9 //! The number of nanoseconds in one millisecond.
10 constexpr uint64_t kOneMicrosecondInNanoseconds(1000);
11 
12 namespace chre {
13 
Seconds(uint64_t seconds)14 constexpr Seconds::Seconds(uint64_t seconds)
15     : mSeconds(seconds) {}
16 
toRawNanoseconds()17 constexpr uint64_t Seconds::toRawNanoseconds() const {
18   // Perform the simple unit conversion. Warning: overflow is caught and
19   // handled by returning UINT64_MAX. A ternary expression is used because
20   // constexpr requires it.
21   return mSeconds > (UINT64_MAX / kOneSecondInNanoseconds) ? UINT64_MAX
22       : mSeconds * kOneSecondInNanoseconds;
23 }
24 
Milliseconds(uint64_t milliseconds)25 constexpr Milliseconds::Milliseconds(uint64_t milliseconds)
26     : mMilliseconds(milliseconds) {}
27 
Milliseconds(Nanoseconds nanoseconds)28 constexpr Milliseconds::Milliseconds(Nanoseconds nanoseconds)
29     : mMilliseconds(
30         nanoseconds.toRawNanoseconds() / kOneMillisecondInNanoseconds) {}
31 
toRawNanoseconds()32 constexpr uint64_t Milliseconds::toRawNanoseconds() const {
33   // Perform the simple unit conversion. Warning: overflow is caught and
34   // handled by returning UINT64_MAX. A ternary expression is used because
35   // constexpr requires it.
36   return mMilliseconds > (UINT64_MAX / kOneMillisecondInNanoseconds) ? UINT64_MAX
37       : mMilliseconds * kOneMillisecondInNanoseconds;
38 }
39 
getMilliseconds()40 constexpr uint64_t Milliseconds::getMilliseconds() const {
41   return mMilliseconds;
42 }
43 
Microseconds(uint64_t microseconds)44 constexpr Microseconds::Microseconds(uint64_t microseconds)
45     : mMicroseconds(microseconds) {}
46 
Microseconds(Nanoseconds nanoseconds)47 constexpr Microseconds::Microseconds(Nanoseconds nanoseconds)
48     : mMicroseconds(
49         nanoseconds.toRawNanoseconds() / kOneMicrosecondInNanoseconds) {}
50 
toRawNanoseconds()51 constexpr uint64_t Microseconds::toRawNanoseconds() const {
52   // Perform the simple unit conversion. Warning: overflow is caught and
53   // handled by returning UINT64_MAX. A ternary expression is used because
54   // constexpr requires it.
55   return mMicroseconds > (UINT64_MAX / kOneMicrosecondInNanoseconds) ? UINT64_MAX
56       : mMicroseconds * kOneMicrosecondInNanoseconds;
57 }
58 
getMicroseconds()59 constexpr uint64_t Microseconds::getMicroseconds() const {
60   return mMicroseconds;
61 }
62 
Nanoseconds()63 constexpr Nanoseconds::Nanoseconds()
64     : mNanoseconds(0) {}
65 
Nanoseconds(uint64_t nanoseconds)66 constexpr Nanoseconds::Nanoseconds(uint64_t nanoseconds)
67     : mNanoseconds(nanoseconds) {}
68 
Nanoseconds(Seconds seconds)69 constexpr Nanoseconds::Nanoseconds(Seconds seconds)
70     : mNanoseconds(seconds.toRawNanoseconds()) {}
71 
Nanoseconds(Milliseconds milliseconds)72 constexpr Nanoseconds::Nanoseconds(Milliseconds milliseconds)
73     : mNanoseconds(milliseconds.toRawNanoseconds()) {}
74 
Nanoseconds(Microseconds microseconds)75 constexpr Nanoseconds::Nanoseconds(Microseconds microseconds)
76     : mNanoseconds(microseconds.toRawNanoseconds()) {}
77 
toRawNanoseconds()78 constexpr uint64_t Nanoseconds::toRawNanoseconds() const {
79   return mNanoseconds;
80 }
81 
82 constexpr bool Nanoseconds::operator==(const Nanoseconds& nanos) const {
83   return (mNanoseconds == nanos.mNanoseconds);
84 }
85 
86 constexpr bool Nanoseconds::operator!=(const Nanoseconds& nanos) const {
87   return !(mNanoseconds == nanos.mNanoseconds);
88 }
89 
90 constexpr Nanoseconds operator+(const Seconds& secs,
91                                 const Nanoseconds& nanos) {
92   return Nanoseconds(secs.toRawNanoseconds() + nanos.toRawNanoseconds());
93 }
94 
95 constexpr Nanoseconds operator+(const Nanoseconds& nanos_a,
96                                 const Nanoseconds& nanos_b) {
97   return Nanoseconds(nanos_a.toRawNanoseconds() + nanos_b.toRawNanoseconds());
98 }
99 
100 constexpr Nanoseconds operator-(const Nanoseconds& nanos_a,
101                                 const Nanoseconds& nanos_b) {
102   return Nanoseconds(nanos_a.toRawNanoseconds() - nanos_b.toRawNanoseconds());
103 }
104 
105 constexpr bool operator>=(const Nanoseconds& nanos_a,
106                           const Nanoseconds& nanos_b) {
107   return nanos_a.toRawNanoseconds() >= nanos_b.toRawNanoseconds();
108 }
109 
110 constexpr bool operator<(const Nanoseconds& nanos_a,
111                          const Nanoseconds& nanos_b) {
112   return nanos_a.toRawNanoseconds() < nanos_b.toRawNanoseconds();
113 }
114 
115 constexpr bool operator>(const Nanoseconds& nanos_a,
116                          const Nanoseconds& nanos_b) {
117   return nanos_a.toRawNanoseconds() > nanos_b.toRawNanoseconds();
118 }
119 
120 }  // namespace chre
121