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