1 #include "chre/util/time.h" 2 3 //! The number of nanoseconds in one second. 4 constexpr uint64_t kOneSecondInNanoseconds(1000000000); 5 6 //! The number of nanoseconds in one millisecond. 7 constexpr uint64_t kOneMillisecondInNanoseconds(1000000); 8 9 //! The number of nanoseconds in one millisecond. 10 constexpr uint64_t kOneMicrosecondInNanoseconds(1000); 11 12 namespace chre { 13 Seconds(uint64_t seconds)14constexpr Seconds::Seconds(uint64_t seconds) 15 : mSeconds(seconds) {} 16 toRawNanoseconds()17constexpr uint64_t Seconds::toRawNanoseconds() const { 18 // Perform the simple unit conversion. Warning: overflow is caught and 19 // handled by returning UINT64_MAX. A ternary expression is used because 20 // constexpr requires it. 21 return mSeconds > (UINT64_MAX / kOneSecondInNanoseconds) ? UINT64_MAX 22 : mSeconds * kOneSecondInNanoseconds; 23 } 24 Milliseconds(uint64_t milliseconds)25constexpr Milliseconds::Milliseconds(uint64_t milliseconds) 26 : mMilliseconds(milliseconds) {} 27 Milliseconds(Nanoseconds nanoseconds)28constexpr Milliseconds::Milliseconds(Nanoseconds nanoseconds) 29 : mMilliseconds( 30 nanoseconds.toRawNanoseconds() / kOneMillisecondInNanoseconds) {} 31 toRawNanoseconds()32constexpr uint64_t Milliseconds::toRawNanoseconds() const { 33 // Perform the simple unit conversion. Warning: overflow is caught and 34 // handled by returning UINT64_MAX. A ternary expression is used because 35 // constexpr requires it. 36 return mMilliseconds > (UINT64_MAX / kOneMillisecondInNanoseconds) ? UINT64_MAX 37 : mMilliseconds * kOneMillisecondInNanoseconds; 38 } 39 getMilliseconds()40constexpr uint64_t Milliseconds::getMilliseconds() const { 41 return mMilliseconds; 42 } 43 Microseconds(uint64_t microseconds)44constexpr Microseconds::Microseconds(uint64_t microseconds) 45 : mMicroseconds(microseconds) {} 46 Microseconds(Nanoseconds nanoseconds)47constexpr Microseconds::Microseconds(Nanoseconds nanoseconds) 48 : mMicroseconds( 49 nanoseconds.toRawNanoseconds() / kOneMicrosecondInNanoseconds) {} 50 toRawNanoseconds()51constexpr uint64_t Microseconds::toRawNanoseconds() const { 52 // Perform the simple unit conversion. Warning: overflow is caught and 53 // handled by returning UINT64_MAX. A ternary expression is used because 54 // constexpr requires it. 55 return mMicroseconds > (UINT64_MAX / kOneMicrosecondInNanoseconds) ? UINT64_MAX 56 : mMicroseconds * kOneMicrosecondInNanoseconds; 57 } 58 getMicroseconds()59constexpr uint64_t Microseconds::getMicroseconds() const { 60 return mMicroseconds; 61 } 62 Nanoseconds()63constexpr Nanoseconds::Nanoseconds() 64 : mNanoseconds(0) {} 65 Nanoseconds(uint64_t nanoseconds)66constexpr Nanoseconds::Nanoseconds(uint64_t nanoseconds) 67 : mNanoseconds(nanoseconds) {} 68 Nanoseconds(Seconds seconds)69constexpr Nanoseconds::Nanoseconds(Seconds seconds) 70 : mNanoseconds(seconds.toRawNanoseconds()) {} 71 Nanoseconds(Milliseconds milliseconds)72constexpr Nanoseconds::Nanoseconds(Milliseconds milliseconds) 73 : mNanoseconds(milliseconds.toRawNanoseconds()) {} 74 Nanoseconds(Microseconds microseconds)75constexpr Nanoseconds::Nanoseconds(Microseconds microseconds) 76 : mNanoseconds(microseconds.toRawNanoseconds()) {} 77 toRawNanoseconds()78constexpr uint64_t Nanoseconds::toRawNanoseconds() const { 79 return mNanoseconds; 80 } 81 82 constexpr bool Nanoseconds::operator==(const Nanoseconds& nanos) const { 83 return (mNanoseconds == nanos.mNanoseconds); 84 } 85 86 constexpr bool Nanoseconds::operator!=(const Nanoseconds& nanos) const { 87 return !(mNanoseconds == nanos.mNanoseconds); 88 } 89 90 constexpr Nanoseconds operator+(const Seconds& secs, 91 const Nanoseconds& nanos) { 92 return Nanoseconds(secs.toRawNanoseconds() + nanos.toRawNanoseconds()); 93 } 94 95 constexpr Nanoseconds operator+(const Nanoseconds& nanos_a, 96 const Nanoseconds& nanos_b) { 97 return Nanoseconds(nanos_a.toRawNanoseconds() + nanos_b.toRawNanoseconds()); 98 } 99 100 constexpr Nanoseconds operator-(const Nanoseconds& nanos_a, 101 const Nanoseconds& nanos_b) { 102 return Nanoseconds(nanos_a.toRawNanoseconds() - nanos_b.toRawNanoseconds()); 103 } 104 105 constexpr bool operator>=(const Nanoseconds& nanos_a, 106 const Nanoseconds& nanos_b) { 107 return nanos_a.toRawNanoseconds() >= nanos_b.toRawNanoseconds(); 108 } 109 110 constexpr bool operator<(const Nanoseconds& nanos_a, 111 const Nanoseconds& nanos_b) { 112 return nanos_a.toRawNanoseconds() < nanos_b.toRawNanoseconds(); 113 } 114 115 constexpr bool operator>(const Nanoseconds& nanos_a, 116 const Nanoseconds& nanos_b) { 117 return nanos_a.toRawNanoseconds() > nanos_b.toRawNanoseconds(); 118 } 119 120 } // namespace chre 121