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