1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef CHRE_UTIL_TIME_IMPL_H_
18 #define CHRE_UTIL_TIME_IMPL_H_
19
20 #include "chre/util/time.h"
21
22 namespace chre {
23
Seconds(uint64_t seconds)24 constexpr Seconds::Seconds(uint64_t seconds) : mSeconds(seconds) {}
25
toRawNanoseconds()26 constexpr uint64_t Seconds::toRawNanoseconds() const {
27 // Perform the simple unit conversion. Warning: overflow is caught and
28 // handled by returning UINT64_MAX. A ternary expression is used because
29 // constexpr requires it.
30 return (mSeconds > (UINT64_MAX / kOneSecondInNanoseconds))
31 ? UINT64_MAX
32 : mSeconds * kOneSecondInNanoseconds;
33 }
34
getMilliseconds()35 constexpr uint64_t Seconds::getMilliseconds() const {
36 // Perform the simple unit conversion. Warning: overflow is caught and
37 // handled by returning UINT64_MAX. A ternary expression is used because
38 // constexpr requires it.
39 return (mSeconds > (UINT64_MAX / kOneSecondInMilliseconds))
40 ? UINT64_MAX
41 : mSeconds * kOneSecondInMilliseconds;
42 }
43
Milliseconds()44 constexpr Milliseconds::Milliseconds() : mMilliseconds(0) {}
45
Milliseconds(uint64_t milliseconds)46 constexpr Milliseconds::Milliseconds(uint64_t milliseconds)
47 : mMilliseconds(milliseconds) {}
48
Milliseconds(Nanoseconds nanoseconds)49 constexpr Milliseconds::Milliseconds(Nanoseconds nanoseconds)
50 : mMilliseconds(nanoseconds.toRawNanoseconds() /
51 kOneMillisecondInNanoseconds) {}
52
toRawNanoseconds()53 constexpr uint64_t Milliseconds::toRawNanoseconds() const {
54 // Perform the simple unit conversion. Warning: overflow is caught and
55 // handled by returning UINT64_MAX. A ternary expression is used because
56 // constexpr requires it.
57 return (mMilliseconds > (UINT64_MAX / kOneMillisecondInNanoseconds))
58 ? UINT64_MAX
59 : mMilliseconds * kOneMillisecondInNanoseconds;
60 }
61
getMicroseconds()62 constexpr uint64_t Milliseconds::getMicroseconds() const {
63 // Perform the simple unit conversion. Warning: overflow is caught and
64 // handled by returning UINT64_MAX. A ternary expression is used because
65 // constexpr requires it.
66 return (mMilliseconds > (UINT64_MAX / kOneMillisecondInMicroseconds))
67 ? UINT64_MAX
68 : mMilliseconds * kOneMillisecondInMicroseconds;
69 }
70
getMilliseconds()71 constexpr uint64_t Milliseconds::getMilliseconds() const {
72 return mMilliseconds;
73 }
74
75 constexpr bool Milliseconds::operator==(const Milliseconds &millis) const {
76 return (mMilliseconds == millis.mMilliseconds);
77 }
78
Microseconds(uint64_t microseconds)79 constexpr Microseconds::Microseconds(uint64_t microseconds)
80 : mMicroseconds(microseconds) {}
81
Microseconds(Nanoseconds nanoseconds)82 constexpr Microseconds::Microseconds(Nanoseconds nanoseconds)
83 : mMicroseconds(nanoseconds.toRawNanoseconds() /
84 kOneMicrosecondInNanoseconds) {}
85
toRawNanoseconds()86 constexpr uint64_t Microseconds::toRawNanoseconds() const {
87 // Perform the simple unit conversion. Warning: overflow is caught and
88 // handled by returning UINT64_MAX. A ternary expression is used because
89 // constexpr requires it.
90 return (mMicroseconds > (UINT64_MAX / kOneMicrosecondInNanoseconds))
91 ? UINT64_MAX
92 : mMicroseconds * kOneMicrosecondInNanoseconds;
93 }
94
getMicroseconds()95 constexpr uint64_t Microseconds::getMicroseconds() const {
96 return mMicroseconds;
97 }
98
getMilliseconds()99 constexpr uint64_t Microseconds::getMilliseconds() const {
100 return (mMicroseconds / kOneMillisecondInMicroseconds);
101 }
102
Nanoseconds()103 constexpr Nanoseconds::Nanoseconds() : mNanoseconds(0) {}
104
Nanoseconds(uint64_t nanoseconds)105 constexpr Nanoseconds::Nanoseconds(uint64_t nanoseconds)
106 : mNanoseconds(nanoseconds) {}
107
Nanoseconds(Seconds seconds)108 constexpr Nanoseconds::Nanoseconds(Seconds seconds)
109 : mNanoseconds(seconds.toRawNanoseconds()) {}
110
Nanoseconds(Milliseconds milliseconds)111 constexpr Nanoseconds::Nanoseconds(Milliseconds milliseconds)
112 : mNanoseconds(milliseconds.toRawNanoseconds()) {}
113
Nanoseconds(Microseconds microseconds)114 constexpr Nanoseconds::Nanoseconds(Microseconds microseconds)
115 : mNanoseconds(microseconds.toRawNanoseconds()) {}
116
toRawNanoseconds()117 constexpr uint64_t Nanoseconds::toRawNanoseconds() const {
118 return mNanoseconds;
119 }
120
121 constexpr bool Nanoseconds::operator==(const Nanoseconds &nanos) const {
122 return (mNanoseconds == nanos.mNanoseconds);
123 }
124
125 constexpr bool Nanoseconds::operator!=(const Nanoseconds &nanos) const {
126 return !(mNanoseconds == nanos.mNanoseconds);
127 }
128
129 constexpr Nanoseconds operator+(const Seconds &secs, const Nanoseconds &nanos) {
130 return Nanoseconds(secs.toRawNanoseconds() + nanos.toRawNanoseconds());
131 }
132
133 constexpr Nanoseconds operator+(const Nanoseconds &nanos_a,
134 const Nanoseconds &nanos_b) {
135 return Nanoseconds(nanos_a.toRawNanoseconds() + nanos_b.toRawNanoseconds());
136 }
137
138 constexpr Nanoseconds operator-(const Nanoseconds &nanos_a,
139 const Nanoseconds &nanos_b) {
140 return Nanoseconds(nanos_a.toRawNanoseconds() - nanos_b.toRawNanoseconds());
141 }
142
143 constexpr bool operator>=(const Nanoseconds &nanos_a,
144 const Nanoseconds &nanos_b) {
145 return nanos_a.toRawNanoseconds() >= nanos_b.toRawNanoseconds();
146 }
147
148 constexpr bool operator<=(const Nanoseconds &nanos_a,
149 const Nanoseconds &nanos_b) {
150 return nanos_a.toRawNanoseconds() <= nanos_b.toRawNanoseconds();
151 }
152
153 constexpr bool operator<(const Nanoseconds &nanos_a,
154 const Nanoseconds &nanos_b) {
155 return nanos_a.toRawNanoseconds() < nanos_b.toRawNanoseconds();
156 }
157
158 constexpr bool operator>(const Nanoseconds &nanos_a,
159 const Nanoseconds &nanos_b) {
160 return nanos_a.toRawNanoseconds() > nanos_b.toRawNanoseconds();
161 }
162
163 } // namespace chre
164
165 #endif // CHRE_UTIL_TIME_IMPL_H_
166