1 /*
2  * Copyright (C) 2016 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_H_
18 #define CHRE_UTIL_TIME_H_
19 
20 #include <cstdint>
21 
22 namespace chre {
23 
24 //! The number of milliseconds in one min.
25 constexpr uint64_t kOneMinuteInMilliseconds(60000);
26 
27 //! The number of milliseconds in one second.
28 constexpr uint64_t kOneSecondInMilliseconds(1000);
29 
30 //! The number of nanoseconds in one second.
31 constexpr uint64_t kOneSecondInNanoseconds(1000000000);
32 
33 //! The number of nanoseconds in one millisecond.
34 constexpr uint64_t kOneMillisecondInNanoseconds(1000000);
35 
36 //! The number of nanoseconds in one microsecond.
37 constexpr uint64_t kOneMicrosecondInNanoseconds(1000);
38 
39 //! The number of microseconds in one millisecond.
40 constexpr uint64_t kOneMillisecondInMicroseconds(1000);
41 
42 // Forward declare classes for unit-conversion constructors.
43 class Milliseconds;
44 class Microseconds;
45 class Nanoseconds;
46 
47 
48 class Seconds {
49  public:
50   /**
51    * Construct a Seconds time duration given a value.
52    */
53   constexpr explicit Seconds(uint64_t seconds);
54 
55   /**
56    * Converts the underlying seconds to a raw uint64_t representation of
57    * nanoseconds. Handles overflyw by returning UINT64_MAX.
58    *
59    * @return the value of seconds converted to nanoseconds
60    */
61   constexpr uint64_t toRawNanoseconds() const;
62 
63   /**
64    * Obtains the number of Milliseconds stored by this time duration.
65    *
66    * @return the value of milliseconds.
67    */
68   constexpr uint64_t getMilliseconds() const;
69 
70  private:
71   uint64_t mSeconds;
72 };
73 
74 /**
75  * Represents a duration of time in milliseconds.
76  */
77 class Milliseconds {
78  public:
79   /**
80    * Default constructs a milliseconds time duration to zero.
81    */
82   constexpr Milliseconds();
83 
84   /**
85    * Construct a Milliseconds time duration given a value.
86    */
87   constexpr explicit Milliseconds(uint64_t milliseconds);
88 
89   /**
90    * Constructs a Microseconds time duration given nanoseconds.
91    */
92   constexpr Milliseconds(Nanoseconds nanoseconds);
93 
94   /**
95    * Converts the underlying milliseconds to a raw uint64_t representation of
96    * nanoseconds. Handles overflow by returning UINT64_MAX.
97    *
98    * @return the value of milliseconds converted to nanoseconds
99    */
100   constexpr uint64_t toRawNanoseconds() const;
101 
102   /**
103    * Obtains the number of Microseconds stored by this time duration.
104    *
105    * @return the value of microseconds.
106    */
107   constexpr uint64_t getMicroseconds() const;
108 
109   /**
110    * Obtains the number of Milliseconds stored by this time duration.
111    *
112    * @return the value of milliseconds.
113    */
114   constexpr uint64_t getMilliseconds() const;
115 
116   /**
117    * Performs an equality comparison to another Milliseconds value.
118    *
119    * @return Returns true if this milliseconds object is equal to another.
120    */
121   constexpr bool operator==(const Milliseconds& millis) const;
122 
123  private:
124   //! Store the time duration.
125   uint64_t mMilliseconds;
126 };
127 
128 /**
129  * Represents a duration of time in microseconds.
130  */
131 class Microseconds {
132  public:
133   /**
134    * Construct a Microseconds time duration given a value.
135    */
136   constexpr explicit Microseconds(uint64_t microseconds);
137 
138   /**
139    * Constructs a Microseconds time duration given nanoseconds.
140    */
141   constexpr Microseconds(Nanoseconds nanoseconds);
142 
143   /**
144    * Converts the underlying microseconds to a raw uint64_t representation of
145    * nanoseconds. Handles overflow by returning UINT64_MAX.
146    *
147    * @return the value of microseconds converted to nanoseconds.
148    */
149   constexpr uint64_t toRawNanoseconds() const;
150 
151   /**
152    * Obtains the number of Microseconds stored by this time duration.
153    *
154    * @return the value of microseconds.
155    */
156   constexpr uint64_t getMicroseconds() const;
157 
158   /**
159    * Obtains the rounded-down number of Milliseconds stored by this time
160    * duration.
161    *
162    * @return the value of milliseconds.
163    */
164   constexpr uint64_t getMilliseconds() const;
165 
166  private:
167   //! Store the time duration.
168   uint64_t mMicroseconds;
169 };
170 
171 /**
172  * Represents a duration of time in nanoseconds.
173  */
174 class Nanoseconds {
175  public:
176   /**
177    * Default constructs a Nanoseconds time duration to zero.
178    */
179   constexpr Nanoseconds();
180 
181   /**
182    * Constructs a Nanoseconds time duration given a value.
183    */
184   constexpr explicit Nanoseconds(uint64_t nanoseconds);
185 
186   /**
187    * Converts a seconds value to nanoseconds.
188    */
189   constexpr Nanoseconds(Seconds seconds);
190 
191   /**
192    * Converts a milliseconds value to nanoseconds.
193    */
194   constexpr Nanoseconds(Milliseconds milliseconds);
195 
196   /**
197    * Constructs a Nanoseconds time duration given microseconds.
198    */
199   constexpr Nanoseconds(Microseconds microseconds);
200 
201   /**
202    * Converts the underlying nanoseconds to a raw uint64_t representation of
203    * nanoseconds.
204    *
205    * @return the value of nanoseconds
206    */
207   constexpr uint64_t toRawNanoseconds() const;
208 
209   /**
210    * Performs an equality comparison to another Nanoseconds value.
211    *
212    * @return Returns true if this nanoseconds object is equal to another.
213    */
214   constexpr bool operator==(const Nanoseconds& nanos) const;
215 
216   /**
217    * Performs an inequality comparison to another Nanoseconds value.
218    *
219    * @return Returns true if this nanoseconds object is not equal to another.
220    */
221   constexpr bool operator!=(const Nanoseconds& nanos) const;
222 
223  private:
224   uint64_t mNanoseconds;
225 };
226 
227 /**
228  * Add seconds to nanoseconds.
229  *
230  * @param seconds the seconds duration
231  * @param nanoseconds the nanoseconds duration
232  * @return the added time quantity expressed in nanoseconds
233  */
234 constexpr Nanoseconds operator+(const Seconds& secs, const Nanoseconds& nanos);
235 
236 /**
237  * Add nanoseconds to nanoseconds.
238  *
239  * @param nanos_a The first nanoseconds duration
240  * @param nanos_b The second nanoseconds duration
241  * @return The added time quantity expressed in nanoseconds
242  */
243 constexpr Nanoseconds operator+(const Nanoseconds& nanos_a,
244                                 const Nanoseconds& nanos_b);
245 
246 /**
247  * Subtract two nanosecond durations.
248  *
249  * @param nanos_a the first nanoseconds duration
250  * @param nanos_b the second nanoseconds duration
251  * @return the difference between the two durations
252  */
253 constexpr Nanoseconds operator-(const Nanoseconds& nanos_a,
254                                 const Nanoseconds& nanos_b);
255 
256 /**
257  * Performs a greater than or equal to comparison on two nanoseconds values.
258  *
259  * @param nanos_a the first nanoseconds duration
260  * @param nanos_b the second nanoseconds duration
261  * @return Whether nanos_a is greater than or equal to nanos_b.
262  */
263 constexpr bool operator>=(const Nanoseconds& nanos_a,
264                           const Nanoseconds& nanos_b);
265 
266 /**
267  * Performs a less than or equal to comparison on two nanoseconds values.
268  *
269  * @param nanos_a the first nanoseconds duration
270  * @param nanos_b the second nanoseconds duration
271  * @return Whether nanos_a is less than or equal to nanos_b.
272  */
273 constexpr bool operator<=(const Nanoseconds& nanos_a,
274                           const Nanoseconds& nanos_b);
275 
276 /**
277  * Performs a less than comparison on two nanoseconds values.
278  *
279  * @param nanos_a the first nanoseconds duration
280  * @param nanos_b the second nanoseconds duration
281  * @return Whether nanos_a is less than nanos_b.
282  */
283 constexpr bool operator<(const Nanoseconds& nanos_a,
284                          const Nanoseconds& nanos_b);
285 
286 /**
287  * Performs a greater than comparison on two nanoseconds values.
288  *
289  * @param nanos_a the first nanoseconds duration
290  * @param nanos_b the second nanoseconds duration
291  * @return Whether nanos_a is less than nanos_b.
292  */
293 constexpr bool operator>(const Nanoseconds& nanos_a,
294                          const Nanoseconds& nanos_b);
295 
296 }  // namespace chre
297 
298 #include "chre/util/time_impl.h"
299 
300 #endif // CHRE_UTIL_TIME_H_
301