• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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_IMPL_H_
18 #define CHRE_UTIL_TIME_IMPL_H_
19 
20 #include "chre/util/time.h"
21 
22 namespace chre {
23 
Seconds(uint64_t seconds)24 constexpr Seconds::Seconds(uint64_t seconds)
25     : mSeconds(seconds) {}
26 
toRawNanoseconds()27 constexpr uint64_t Seconds::toRawNanoseconds() const {
28   // Perform the simple unit conversion. Warning: overflow is caught and
29   // handled by returning UINT64_MAX. A ternary expression is used because
30   // constexpr requires it.
31   return (mSeconds > (UINT64_MAX / kOneSecondInNanoseconds)) ? UINT64_MAX
32       : mSeconds * kOneSecondInNanoseconds;
33 }
34 
getMilliseconds()35 constexpr uint64_t Seconds::getMilliseconds() const {
36   // Perform the simple unit conversion. Warning: overflow is caught and
37   // handled by returning UINT64_MAX. A ternary expression is used because
38   // constexpr requires it.
39   return (mSeconds > (UINT64_MAX / kOneSecondInMilliseconds)) ? UINT64_MAX
40       : mSeconds * kOneSecondInMilliseconds;
41 }
42 
Milliseconds()43 constexpr Milliseconds::Milliseconds()
44     : mMilliseconds(0) {}
45 
Milliseconds(uint64_t milliseconds)46 constexpr Milliseconds::Milliseconds(uint64_t milliseconds)
47     : mMilliseconds(milliseconds) {}
48 
Milliseconds(Nanoseconds nanoseconds)49 constexpr Milliseconds::Milliseconds(Nanoseconds nanoseconds)
50     : mMilliseconds(
51         nanoseconds.toRawNanoseconds() / kOneMillisecondInNanoseconds) {}
52 
toRawNanoseconds()53 constexpr uint64_t Milliseconds::toRawNanoseconds() const {
54   // Perform the simple unit conversion. Warning: overflow is caught and
55   // handled by returning UINT64_MAX. A ternary expression is used because
56   // constexpr requires it.
57   return (mMilliseconds > (UINT64_MAX / kOneMillisecondInNanoseconds))
58       ? UINT64_MAX : mMilliseconds * kOneMillisecondInNanoseconds;
59 }
60 
getMicroseconds()61 constexpr uint64_t Milliseconds::getMicroseconds() const {
62   // Perform the simple unit conversion. Warning: overflow is caught and
63   // handled by returning UINT64_MAX. A ternary expression is used because
64   // constexpr requires it.
65   return (mMilliseconds > (UINT64_MAX / kOneMillisecondInMicroseconds))
66       ? UINT64_MAX : mMilliseconds * kOneMillisecondInMicroseconds ;
67 }
68 
getMilliseconds()69 constexpr uint64_t Milliseconds::getMilliseconds() const {
70   return mMilliseconds;
71 }
72 
73 constexpr bool Milliseconds::operator==(const Milliseconds& millis) const {
74   return (mMilliseconds == millis.mMilliseconds);
75 }
76 
Microseconds(uint64_t microseconds)77 constexpr Microseconds::Microseconds(uint64_t microseconds)
78     : mMicroseconds(microseconds) {}
79 
Microseconds(Nanoseconds nanoseconds)80 constexpr Microseconds::Microseconds(Nanoseconds nanoseconds)
81     : mMicroseconds(
82         nanoseconds.toRawNanoseconds() / kOneMicrosecondInNanoseconds) {}
83 
toRawNanoseconds()84 constexpr uint64_t Microseconds::toRawNanoseconds() const {
85   // Perform the simple unit conversion. Warning: overflow is caught and
86   // handled by returning UINT64_MAX. A ternary expression is used because
87   // constexpr requires it.
88   return (mMicroseconds > (UINT64_MAX / kOneMicrosecondInNanoseconds))
89       ? UINT64_MAX : mMicroseconds * kOneMicrosecondInNanoseconds;
90 }
91 
getMicroseconds()92 constexpr uint64_t Microseconds::getMicroseconds() const {
93   return mMicroseconds;
94 }
95 
getMilliseconds()96 constexpr uint64_t Microseconds::getMilliseconds() const {
97   return (mMicroseconds / kOneMillisecondInMicroseconds);
98 }
99 
Nanoseconds()100 constexpr Nanoseconds::Nanoseconds()
101     : mNanoseconds(0) {}
102 
Nanoseconds(uint64_t nanoseconds)103 constexpr Nanoseconds::Nanoseconds(uint64_t nanoseconds)
104     : mNanoseconds(nanoseconds) {}
105 
Nanoseconds(Seconds seconds)106 constexpr Nanoseconds::Nanoseconds(Seconds seconds)
107     : mNanoseconds(seconds.toRawNanoseconds()) {}
108 
Nanoseconds(Milliseconds milliseconds)109 constexpr Nanoseconds::Nanoseconds(Milliseconds milliseconds)
110     : mNanoseconds(milliseconds.toRawNanoseconds()) {}
111 
Nanoseconds(Microseconds microseconds)112 constexpr Nanoseconds::Nanoseconds(Microseconds microseconds)
113     : mNanoseconds(microseconds.toRawNanoseconds()) {}
114 
toRawNanoseconds()115 constexpr uint64_t Nanoseconds::toRawNanoseconds() const {
116   return mNanoseconds;
117 }
118 
119 constexpr bool Nanoseconds::operator==(const Nanoseconds& nanos) const {
120   return (mNanoseconds == nanos.mNanoseconds);
121 }
122 
123 constexpr bool Nanoseconds::operator!=(const Nanoseconds& nanos) const {
124   return !(mNanoseconds == nanos.mNanoseconds);
125 }
126 
127 constexpr Nanoseconds operator+(const Seconds& secs, const Nanoseconds& nanos) {
128   return Nanoseconds(secs.toRawNanoseconds() + nanos.toRawNanoseconds());
129 }
130 
131 constexpr Nanoseconds operator+(const Nanoseconds& nanos_a,
132                                 const Nanoseconds& nanos_b) {
133   return Nanoseconds(nanos_a.toRawNanoseconds() + nanos_b.toRawNanoseconds());
134 }
135 
136 constexpr Nanoseconds operator-(const Nanoseconds& nanos_a,
137                                 const Nanoseconds& nanos_b) {
138   return Nanoseconds(nanos_a.toRawNanoseconds() - nanos_b.toRawNanoseconds());
139 }
140 
141 constexpr bool operator>=(const Nanoseconds& nanos_a,
142                           const Nanoseconds& nanos_b) {
143   return nanos_a.toRawNanoseconds() >= nanos_b.toRawNanoseconds();
144 }
145 
146 constexpr bool operator<=(const Nanoseconds& nanos_a,
147                           const Nanoseconds& nanos_b) {
148   return nanos_a.toRawNanoseconds() <= nanos_b.toRawNanoseconds();
149 }
150 
151 constexpr bool operator<(const Nanoseconds& nanos_a,
152                          const Nanoseconds& nanos_b) {
153   return nanos_a.toRawNanoseconds() < nanos_b.toRawNanoseconds();
154 }
155 
156 constexpr bool operator>(const Nanoseconds& nanos_a,
157                          const Nanoseconds& nanos_b) {
158   return nanos_a.toRawNanoseconds() > nanos_b.toRawNanoseconds();
159 }
160 
161 }  // namespace chre
162 
163 #endif  // CHRE_UTIL_TIME_IMPL_H_
164