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