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