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 // Special case to catch divide by zero. |
| 219 if ((delta_ < 0) ^ (a <= 0)) |
| 220 return TimeDelta(-std::numeric_limits<int64_t>::max()); |
| 221 return TimeDelta(std::numeric_limits<int64_t>::max()); |
215 } | 222 } |
216 template<typename T> | 223 template<typename T> |
217 TimeDelta& operator*=(T a) { | 224 TimeDelta& operator*=(T a) { |
218 return *this = (*this * a); | 225 return *this = (*this * a); |
219 } | 226 } |
220 template<typename T> | 227 template<typename T> |
221 TimeDelta& operator/=(T a) { | 228 TimeDelta& operator/=(T a) { |
222 return *this = (*this / a); | 229 return *this = (*this / a); |
223 } | 230 } |
224 | 231 |
(...skipping 617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
842 static void WaitUntilInitializedWin(); | 849 static void WaitUntilInitializedWin(); |
843 #endif | 850 #endif |
844 }; | 851 }; |
845 | 852 |
846 // For logging use only. | 853 // For logging use only. |
847 BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks); | 854 BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks); |
848 | 855 |
849 } // namespace base | 856 } // namespace base |
850 | 857 |
851 #endif // BASE_TIME_TIME_H_ | 858 #endif // BASE_TIME_TIME_H_ |
OLD | NEW |