OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Time represents an absolute point in coordinated universal time (UTC), | 5 // Time represents an absolute point in coordinated universal time (UTC), |
6 // internally represented as microseconds (s/1,000,000) since the Windows epoch | 6 // internally represented as microseconds (s/1,000,000) since the Windows epoch |
7 // (1601-01-01 00:00:00 UTC). System-dependent clock interface routines are | 7 // (1601-01-01 00:00:00 UTC). System-dependent clock interface routines are |
8 // defined in time_PLATFORM.cc. Note that values for Time may skew and jump | 8 // defined in time_PLATFORM.cc. Note that values for Time may skew and jump |
9 // around as the operating system makes adjustments to synchronize (e.g., with | 9 // around as the operating system makes adjustments to synchronize (e.g., with |
10 // NTP servers). Thus, client code that uses the Time class must account for | 10 // NTP servers). Thus, client code that uses the Time class must account for |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 // The functions in the time_internal namespace are meant to be used only by the | 88 // The functions in the time_internal namespace are meant to be used only by the |
89 // time classes and functions. Please use the math operators defined in the | 89 // time classes and functions. Please use the math operators defined in the |
90 // time classes instead. | 90 // time classes instead. |
91 namespace time_internal { | 91 namespace time_internal { |
92 | 92 |
93 // Add or subtract |value| from a TimeDelta. The int64_t argument and return | 93 // Add or subtract |value| from a TimeDelta. The int64_t argument and return |
94 // value are in terms of a microsecond timebase. | 94 // value are in terms of a microsecond timebase. |
95 BASE_EXPORT int64_t SaturatedAdd(TimeDelta delta, int64_t value); | 95 BASE_EXPORT int64_t SaturatedAdd(TimeDelta delta, int64_t value); |
96 BASE_EXPORT int64_t SaturatedSub(TimeDelta delta, int64_t value); | 96 BASE_EXPORT int64_t SaturatedSub(TimeDelta delta, int64_t value); |
97 | 97 |
98 // Clamp |value| on overflow and underflow conditions. The int64_t argument and | |
99 // return value are in terms of a microsecond timebase. | |
100 BASE_EXPORT int64_t FromCheckedNumeric(const CheckedNumeric<int64_t> value); | |
101 | |
102 } // namespace time_internal | 98 } // namespace time_internal |
103 | 99 |
104 // TimeDelta ------------------------------------------------------------------ | 100 // TimeDelta ------------------------------------------------------------------ |
105 | 101 |
106 class BASE_EXPORT TimeDelta { | 102 class BASE_EXPORT TimeDelta { |
107 public: | 103 public: |
108 TimeDelta() : delta_(0) { | 104 TimeDelta() : delta_(0) { |
109 } | 105 } |
110 | 106 |
111 // Converts units of time to TimeDeltas. | 107 // Converts units of time to TimeDeltas. |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
198 } | 194 } |
199 TimeDelta operator-() const { | 195 TimeDelta operator-() const { |
200 return TimeDelta(-delta_); | 196 return TimeDelta(-delta_); |
201 } | 197 } |
202 | 198 |
203 // Computations with numeric types. | 199 // Computations with numeric types. |
204 template<typename T> | 200 template<typename T> |
205 TimeDelta operator*(T a) const { | 201 TimeDelta operator*(T a) const { |
206 CheckedNumeric<int64_t> rv(delta_); | 202 CheckedNumeric<int64_t> rv(delta_); |
207 rv *= a; | 203 rv *= a; |
208 return TimeDelta(time_internal::FromCheckedNumeric(rv)); | 204 if (rv.IsValid()) |
205 return TimeDelta(rv.ValueOrDie()); | |
206 // Matched sign overflows. Mismatched sign underflows. | |
207 if ((delta_ < 0) ^ (a < 0)) | |
208 return TimeDelta(-std::numeric_limits<int64_t>::max()); | |
209 return TimeDelta(std::numeric_limits<int64_t>::max()); | |
209 } | 210 } |
210 template<typename T> | 211 template<typename T> |
211 TimeDelta operator/(T a) const { | 212 TimeDelta operator/(T a) const { |
212 CheckedNumeric<int64_t> rv(delta_); | 213 CheckedNumeric<int64_t> rv(delta_); |
213 rv /= a; | 214 rv /= a; |
214 return TimeDelta(time_internal::FromCheckedNumeric(rv)); | 215 if (rv.IsValid()) |
216 return TimeDelta(rv.ValueOrDie()); | |
217 // Matched sign overflows. Mismatched sign underflows. | |
218 if ((delta_ < 0) ^ (a <= 0)) | |
Tom Sepez
2016/11/04 16:58:58
Maybe a comment about why a <= 0, it's right but n
jschuh
2016/11/04 17:14:07
Done.
| |
219 return TimeDelta(-std::numeric_limits<int64_t>::max()); | |
220 return TimeDelta(std::numeric_limits<int64_t>::max()); | |
215 } | 221 } |
216 template<typename T> | 222 template<typename T> |
217 TimeDelta& operator*=(T a) { | 223 TimeDelta& operator*=(T a) { |
218 return *this = (*this * a); | 224 return *this = (*this * a); |
219 } | 225 } |
220 template<typename T> | 226 template<typename T> |
221 TimeDelta& operator/=(T a) { | 227 TimeDelta& operator/=(T a) { |
222 return *this = (*this / a); | 228 return *this = (*this / a); |
223 } | 229 } |
224 | 230 |
(...skipping 617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
842 static void WaitUntilInitializedWin(); | 848 static void WaitUntilInitializedWin(); |
843 #endif | 849 #endif |
844 }; | 850 }; |
845 | 851 |
846 // For logging use only. | 852 // For logging use only. |
847 BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks); | 853 BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks); |
848 | 854 |
849 } // namespace base | 855 } // namespace base |
850 | 856 |
851 #endif // BASE_TIME_TIME_H_ | 857 #endif // BASE_TIME_TIME_H_ |
OLD | NEW |