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) (See http://crbug.com/14734). System-dependent | 7 // (1601-01-01 00:00:00 UTC) (See http://crbug.com/14734). System-dependent |
8 // clock interface routines are defined in time_PLATFORM.cc. | 8 // clock interface routines are defined in time_PLATFORM.cc. |
9 // | 9 // |
10 // TimeDelta represents a duration of time, internally represented in | 10 // TimeDelta represents a duration of time, internally represented in |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
50 #if defined(OS_WIN) | 50 #if defined(OS_WIN) |
51 // For FILETIME in FromFileTime, until it moves to a new converter class. | 51 // For FILETIME in FromFileTime, until it moves to a new converter class. |
52 // See TODO(iyengar) below. | 52 // See TODO(iyengar) below. |
53 #include <windows.h> | 53 #include <windows.h> |
54 #endif | 54 #endif |
55 | 55 |
56 #include <limits> | 56 #include <limits> |
57 | 57 |
58 namespace base { | 58 namespace base { |
59 | 59 |
60 class Time; | 60 class TimeDelta; |
61 class TimeTicks; | 61 |
62 // The functions in the time_internal namespace are meant to be used only by the | |
63 // time classes and functions. Please use the math operators defined in the | |
64 // time classes instead. | |
65 namespace time_internal { | |
66 | |
67 // Add or subtract |value| from a TimeDelta. The int64 argument and return value | |
68 // are in terms of a microsecond timebase. | |
69 BASE_EXPORT int64 SaturatedAdd(TimeDelta delta, int64 value); | |
70 BASE_EXPORT int64 SaturatedSub(TimeDelta delta, int64 value); | |
71 | |
72 // Clamp |value| on overflow and underflow conditions. The int64 argument and | |
73 // return value are in terms of a microsecond timebase. | |
74 BASE_EXPORT int64 FromCheckedNumeric(const CheckedNumeric<int64> value); | |
75 | |
76 } // namespace time_internal | |
62 | 77 |
63 // TimeDelta ------------------------------------------------------------------ | 78 // TimeDelta ------------------------------------------------------------------ |
64 | 79 |
65 class BASE_EXPORT TimeDelta { | 80 class BASE_EXPORT TimeDelta { |
66 public: | 81 public: |
67 TimeDelta() : delta_(0) { | 82 TimeDelta() : delta_(0) { |
68 } | 83 } |
69 | 84 |
70 // Converts units of time to TimeDeltas. | 85 // Converts units of time to TimeDeltas. |
71 static TimeDelta FromDays(int days); | 86 static TimeDelta FromDays(int days); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
103 | 118 |
104 // Returns the magnitude (absolute value) of this TimeDelta. | 119 // Returns the magnitude (absolute value) of this TimeDelta. |
105 TimeDelta magnitude() const { | 120 TimeDelta magnitude() const { |
106 // Some toolchains provide an incomplete C++11 implementation and lack an | 121 // Some toolchains provide an incomplete C++11 implementation and lack an |
107 // int64 overload for std::abs(). The following is a simple branchless | 122 // int64 overload for std::abs(). The following is a simple branchless |
108 // implementation: | 123 // implementation: |
109 const int64 mask = delta_ >> (sizeof(delta_) * 8 - 1); | 124 const int64 mask = delta_ >> (sizeof(delta_) * 8 - 1); |
110 return TimeDelta((delta_ + mask) ^ mask); | 125 return TimeDelta((delta_ + mask) ^ mask); |
111 } | 126 } |
112 | 127 |
128 // Returns true if the time delta is zero. | |
129 bool is_zero() const { | |
130 return delta_ == 0; | |
131 } | |
132 | |
113 // Returns true if the time delta is the maximum time delta. | 133 // Returns true if the time delta is the maximum time delta. |
114 bool is_max() const { | 134 bool is_max() const { |
115 return delta_ == std::numeric_limits<int64>::max(); | 135 return delta_ == std::numeric_limits<int64>::max(); |
116 } | 136 } |
117 | 137 |
118 #if defined(OS_POSIX) | 138 #if defined(OS_POSIX) |
119 struct timespec ToTimeSpec() const; | 139 struct timespec ToTimeSpec() const; |
120 #endif | 140 #endif |
121 | 141 |
122 // Returns the time delta in some unit. The F versions return a floating | 142 // Returns the time delta in some unit. The F versions return a floating |
(...skipping 11 matching lines...) Expand all Loading... | |
134 int64 InMillisecondsRoundedUp() const; | 154 int64 InMillisecondsRoundedUp() const; |
135 int64 InMicroseconds() const; | 155 int64 InMicroseconds() const; |
136 | 156 |
137 TimeDelta& operator=(TimeDelta other) { | 157 TimeDelta& operator=(TimeDelta other) { |
138 delta_ = other.delta_; | 158 delta_ = other.delta_; |
139 return *this; | 159 return *this; |
140 } | 160 } |
141 | 161 |
142 // Computations with other deltas. | 162 // Computations with other deltas. |
143 TimeDelta operator+(TimeDelta other) const { | 163 TimeDelta operator+(TimeDelta other) const { |
144 return TimeDelta(SaturatedAdd(other.delta_)); | 164 return TimeDelta(time_internal::SaturatedAdd(*this, other.delta_)); |
145 } | 165 } |
146 TimeDelta operator-(TimeDelta other) const { | 166 TimeDelta operator-(TimeDelta other) const { |
147 return TimeDelta(SaturatedSub(other.delta_)); | 167 return TimeDelta(time_internal::SaturatedSub(*this, other.delta_)); |
148 } | 168 } |
149 | 169 |
150 TimeDelta& operator+=(TimeDelta other) { | 170 TimeDelta& operator+=(TimeDelta other) { |
151 delta_ = SaturatedAdd(other.delta_); | 171 return *this = (*this + other); |
152 return *this; | |
153 } | 172 } |
154 TimeDelta& operator-=(TimeDelta other) { | 173 TimeDelta& operator-=(TimeDelta other) { |
155 delta_ = SaturatedSub(other.delta_); | 174 return *this = (*this - other); |
156 return *this; | |
157 } | 175 } |
158 TimeDelta operator-() const { | 176 TimeDelta operator-() const { |
159 return TimeDelta(-delta_); | 177 return TimeDelta(-delta_); |
160 } | 178 } |
161 | 179 |
162 // Computations with numeric types. | 180 // Computations with numeric types. |
163 template<typename T> | 181 template<typename T> |
164 TimeDelta operator*(T a) const { | 182 TimeDelta operator*(T a) const { |
165 CheckedNumeric<int64> rv(delta_); | 183 CheckedNumeric<int64> rv(delta_); |
166 rv *= a; | 184 rv *= a; |
167 return TimeDelta(FromCheckedNumeric(rv)); | 185 return TimeDelta(time_internal::FromCheckedNumeric(rv)); |
168 } | 186 } |
169 template<typename T> | 187 template<typename T> |
170 TimeDelta operator/(T a) const { | 188 TimeDelta operator/(T a) const { |
171 CheckedNumeric<int64> rv(delta_); | 189 CheckedNumeric<int64> rv(delta_); |
172 rv /= a; | 190 rv /= a; |
173 return TimeDelta(FromCheckedNumeric(rv)); | 191 return TimeDelta(time_internal::FromCheckedNumeric(rv)); |
174 } | 192 } |
175 template<typename T> | 193 template<typename T> |
176 TimeDelta& operator*=(T a) { | 194 TimeDelta& operator*=(T a) { |
177 CheckedNumeric<int64> rv(delta_); | 195 return *this = (*this * a); |
178 rv *= a; | |
179 delta_ = FromCheckedNumeric(rv); | |
180 return *this; | |
181 } | 196 } |
182 template<typename T> | 197 template<typename T> |
183 TimeDelta& operator/=(T a) { | 198 TimeDelta& operator/=(T a) { |
184 CheckedNumeric<int64> rv(delta_); | 199 return *this = (*this / a); |
185 rv /= a; | |
186 delta_ = FromCheckedNumeric(rv); | |
187 return *this; | |
188 } | 200 } |
189 | 201 |
190 int64 operator/(TimeDelta a) const { | 202 int64 operator/(TimeDelta a) const { |
191 return delta_ / a.delta_; | 203 return delta_ / a.delta_; |
192 } | 204 } |
193 | 205 TimeDelta operator%(TimeDelta a) const { |
194 // Defined below because it depends on the definition of the other classes. | 206 return TimeDelta(delta_ % a.delta_); |
195 Time operator+(Time t) const; | 207 } |
196 TimeTicks operator+(TimeTicks t) const; | |
197 | 208 |
198 // Comparison operators. | 209 // Comparison operators. |
199 bool operator==(TimeDelta other) const { | 210 bool operator==(TimeDelta other) const { |
200 return delta_ == other.delta_; | 211 return delta_ == other.delta_; |
201 } | 212 } |
202 bool operator!=(TimeDelta other) const { | 213 bool operator!=(TimeDelta other) const { |
203 return delta_ != other.delta_; | 214 return delta_ != other.delta_; |
204 } | 215 } |
205 bool operator<(TimeDelta other) const { | 216 bool operator<(TimeDelta other) const { |
206 return delta_ < other.delta_; | 217 return delta_ < other.delta_; |
207 } | 218 } |
208 bool operator<=(TimeDelta other) const { | 219 bool operator<=(TimeDelta other) const { |
209 return delta_ <= other.delta_; | 220 return delta_ <= other.delta_; |
210 } | 221 } |
211 bool operator>(TimeDelta other) const { | 222 bool operator>(TimeDelta other) const { |
212 return delta_ > other.delta_; | 223 return delta_ > other.delta_; |
213 } | 224 } |
214 bool operator>=(TimeDelta other) const { | 225 bool operator>=(TimeDelta other) const { |
215 return delta_ >= other.delta_; | 226 return delta_ >= other.delta_; |
216 } | 227 } |
217 | 228 |
218 private: | 229 private: |
219 friend class Time; | 230 friend int64 time_internal::SaturatedAdd(TimeDelta delta, int64 value); |
220 friend class TimeTicks; | 231 friend int64 time_internal::SaturatedSub(TimeDelta delta, int64 value); |
221 | 232 |
222 // Constructs a delta given the duration in microseconds. This is private | 233 // Constructs a delta given the duration in microseconds. This is private |
223 // to avoid confusion by callers with an integer constructor. Use | 234 // to avoid confusion by callers with an integer constructor. Use |
224 // FromSeconds, FromMilliseconds, etc. instead. | 235 // FromSeconds, FromMilliseconds, etc. instead. |
225 explicit TimeDelta(int64 delta_us) : delta_(delta_us) { | 236 explicit TimeDelta(int64 delta_us) : delta_(delta_us) { |
226 } | 237 } |
227 | 238 |
228 // Add or subtract |value| from this delta. | |
229 int64 SaturatedAdd(int64 value) const; | |
230 int64 SaturatedSub(int64 value) const; | |
231 | |
232 // Clamp |value| on overflow and underflow conditions. | |
233 static int64 FromCheckedNumeric(const CheckedNumeric<int64> value); | |
234 | |
235 // Delta in microseconds. | 239 // Delta in microseconds. |
236 int64 delta_; | 240 int64 delta_; |
237 }; | 241 }; |
238 | 242 |
239 template<typename T> | 243 template<typename T> |
240 inline TimeDelta operator*(T a, TimeDelta td) { | 244 inline TimeDelta operator*(T a, TimeDelta td) { |
241 return td * a; | 245 return td * a; |
242 } | 246 } |
243 | 247 |
244 // For logging use only. | 248 // For logging use only. |
245 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeDelta time_delta); | 249 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeDelta time_delta); |
246 | 250 |
247 // Time ----------------------------------------------------------------------- | 251 // Do not reference the time_internal::TimeBase template class directly. Please |
252 // use one of the time subclasses instead, and only reference the public | |
253 // TimeBase members via those classes. | |
254 namespace time_internal { | |
248 | 255 |
249 // Represents a wall clock time in UTC. | 256 // TimeBase-------------------------------------------------------------------- |
250 class BASE_EXPORT Time { | 257 |
258 // Provides value storage and comparison/math operations common to all time | |
259 // classes. Each subclass provides for strong type-checking to ensure | |
260 // semantically meaningful comparison/math of time values from the same clock | |
261 // source or timeline. | |
262 template<class TimeClass> | |
263 class TimeBase { | |
251 public: | 264 public: |
252 static const int64 kHoursPerDay = 24; | 265 static const int64 kHoursPerDay = 24; |
253 static const int64 kMillisecondsPerSecond = 1000; | 266 static const int64 kMillisecondsPerSecond = 1000; |
254 static const int64 kMillisecondsPerDay = kMillisecondsPerSecond * 60 * 60 * | 267 static const int64 kMillisecondsPerDay = kMillisecondsPerSecond * 60 * 60 * |
255 kHoursPerDay; | 268 kHoursPerDay; |
256 static const int64 kMicrosecondsPerMillisecond = 1000; | 269 static const int64 kMicrosecondsPerMillisecond = 1000; |
257 static const int64 kMicrosecondsPerSecond = kMicrosecondsPerMillisecond * | 270 static const int64 kMicrosecondsPerSecond = kMicrosecondsPerMillisecond * |
258 kMillisecondsPerSecond; | 271 kMillisecondsPerSecond; |
259 static const int64 kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60; | 272 static const int64 kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60; |
260 static const int64 kMicrosecondsPerHour = kMicrosecondsPerMinute * 60; | 273 static const int64 kMicrosecondsPerHour = kMicrosecondsPerMinute * 60; |
261 static const int64 kMicrosecondsPerDay = kMicrosecondsPerHour * kHoursPerDay; | 274 static const int64 kMicrosecondsPerDay = kMicrosecondsPerHour * kHoursPerDay; |
262 static const int64 kMicrosecondsPerWeek = kMicrosecondsPerDay * 7; | 275 static const int64 kMicrosecondsPerWeek = kMicrosecondsPerDay * 7; |
263 static const int64 kNanosecondsPerMicrosecond = 1000; | 276 static const int64 kNanosecondsPerMicrosecond = 1000; |
264 static const int64 kNanosecondsPerSecond = kNanosecondsPerMicrosecond * | 277 static const int64 kNanosecondsPerSecond = kNanosecondsPerMicrosecond * |
265 kMicrosecondsPerSecond; | 278 kMicrosecondsPerSecond; |
266 | 279 |
280 // Returns true if this object has not been initialized. | |
281 // | |
282 // Warning: Be careful when writing code that performs math on time values, | |
miu
2015/05/06 18:29:17
BTW--As I was looking through the code again, I re
| |
283 // since it's possible to produce a valid "zero" result that should not be | |
284 // interpreted as a "null" value. | |
285 bool is_null() const { | |
286 return us_ == 0; | |
287 } | |
288 | |
289 // Returns true if this object represents the maximum time. | |
290 bool is_max() const { | |
291 return us_ == std::numeric_limits<int64>::max(); | |
292 } | |
293 | |
294 // For serializing only. Use FromInternalValue() to reconstitute. Please don't | |
295 // use this and do arithmetic on it, as it is more error prone than using the | |
296 // provided operators. | |
297 int64 ToInternalValue() const { | |
298 return us_; | |
299 } | |
300 | |
301 TimeClass& operator=(TimeClass other) { | |
302 us_ = other.us_; | |
303 return *(static_cast<TimeClass*>(this)); | |
304 } | |
305 | |
306 // Compute the difference between two times. | |
307 TimeDelta operator-(TimeClass other) const { | |
308 return TimeDelta::FromMicroseconds(us_ - other.us_); | |
309 } | |
310 | |
311 // Return a new time modified by some delta. | |
312 TimeClass operator+(TimeDelta delta) const { | |
313 return TimeClass(time_internal::SaturatedAdd(delta, us_)); | |
314 } | |
315 TimeClass operator-(TimeDelta delta) const { | |
316 return TimeClass(-time_internal::SaturatedSub(delta, us_)); | |
317 } | |
318 | |
319 // Modify by some time delta. | |
320 TimeClass& operator+=(TimeDelta delta) { | |
321 return static_cast<TimeClass&>(*this = (*this + delta)); | |
322 } | |
323 TimeClass& operator-=(TimeDelta delta) { | |
324 return static_cast<TimeClass&>(*this = (*this - delta)); | |
325 } | |
326 | |
327 // Comparison operators | |
328 bool operator==(TimeClass other) const { | |
329 return us_ == other.us_; | |
330 } | |
331 bool operator!=(TimeClass other) const { | |
332 return us_ != other.us_; | |
333 } | |
334 bool operator<(TimeClass other) const { | |
335 return us_ < other.us_; | |
336 } | |
337 bool operator<=(TimeClass other) const { | |
338 return us_ <= other.us_; | |
339 } | |
340 bool operator>(TimeClass other) const { | |
341 return us_ > other.us_; | |
342 } | |
343 bool operator>=(TimeClass other) const { | |
344 return us_ >= other.us_; | |
345 } | |
346 | |
347 // Converts an integer value representing TimeClass to a class. This is used | |
348 // when deserializing a |TimeClass| structure, using a value known to be | |
349 // compatible. It is not provided as a constructor because the integer type | |
350 // may be unclear from the perspective of a caller. | |
351 static TimeClass FromInternalValue(int64 us) { | |
352 return TimeClass(us); | |
353 } | |
354 | |
355 protected: | |
356 explicit TimeBase(int64 us) : us_(us) { | |
357 } | |
358 | |
359 // Time value in a microsecond timebase. | |
360 int64 us_; | |
361 }; | |
362 | |
363 } // namespace time_internal | |
364 | |
365 template<class TimeClass> | |
366 inline TimeClass operator+(TimeDelta delta, TimeClass t) { | |
367 return t + delta; | |
368 } | |
369 | |
370 // Time ----------------------------------------------------------------------- | |
371 | |
372 // Represents a wall clock time in UTC. Values are not guaranteed to be | |
373 // monotonically non-decreasing and are subject to large amounts of skew. | |
374 class BASE_EXPORT Time : public time_internal::TimeBase<Time> { | |
375 public: | |
267 // The representation of Jan 1, 1970 UTC in microseconds since the | 376 // The representation of Jan 1, 1970 UTC in microseconds since the |
268 // platform-dependent epoch. | 377 // platform-dependent epoch. |
269 static const int64 kTimeTToMicrosecondsOffset; | 378 static const int64 kTimeTToMicrosecondsOffset; |
270 | 379 |
271 #if !defined(OS_WIN) | 380 #if !defined(OS_WIN) |
272 // On Mac & Linux, this value is the delta from the Windows epoch of 1601 to | 381 // On Mac & Linux, this value is the delta from the Windows epoch of 1601 to |
273 // the Posix delta of 1970. This is used for migrating between the old | 382 // the Posix delta of 1970. This is used for migrating between the old |
274 // 1970-based epochs to the new 1601-based ones. It should be removed from | 383 // 1970-based epochs to the new 1601-based ones. It should be removed from |
275 // this global header and put in the platform-specific ones when we remove the | 384 // this global header and put in the platform-specific ones when we remove the |
276 // migration code. | 385 // migration code. |
(...skipping 19 matching lines...) Expand all Loading... | |
296 // seconds which may take it up to 60). | 405 // seconds which may take it up to 60). |
297 int millisecond; // Milliseconds within the current second (0-999) | 406 int millisecond; // Milliseconds within the current second (0-999) |
298 | 407 |
299 // A cursory test for whether the data members are within their | 408 // A cursory test for whether the data members are within their |
300 // respective ranges. A 'true' return value does not guarantee the | 409 // respective ranges. A 'true' return value does not guarantee the |
301 // Exploded value can be successfully converted to a Time value. | 410 // Exploded value can be successfully converted to a Time value. |
302 bool HasValidValues() const; | 411 bool HasValidValues() const; |
303 }; | 412 }; |
304 | 413 |
305 // Contains the NULL time. Use Time::Now() to get the current time. | 414 // Contains the NULL time. Use Time::Now() to get the current time. |
306 Time() : us_(0) { | 415 Time() : TimeBase(0) { |
307 } | |
308 | |
309 // Returns true if the time object has not been initialized. | |
310 bool is_null() const { | |
311 return us_ == 0; | |
312 } | |
313 | |
314 // Returns true if the time object is the maximum time. | |
315 bool is_max() const { | |
316 return us_ == std::numeric_limits<int64>::max(); | |
317 } | 416 } |
318 | 417 |
319 // Returns the time for epoch in Unix-like system (Jan 1, 1970). | 418 // Returns the time for epoch in Unix-like system (Jan 1, 1970). |
320 static Time UnixEpoch(); | 419 static Time UnixEpoch(); |
321 | 420 |
322 // Returns the current time. Watch out, the system might adjust its clock | 421 // Returns the current time. Watch out, the system might adjust its clock |
323 // in which case time will actually go backwards. We don't guarantee that | 422 // in which case time will actually go backwards. We don't guarantee that |
324 // times are increasing, or that two calls to Now() won't be the same. | 423 // times are increasing, or that two calls to Now() won't be the same. |
325 static Time Now(); | 424 static Time Now(); |
326 | 425 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
405 | 504 |
406 // Converts an exploded structure representing either the local time or UTC | 505 // Converts an exploded structure representing either the local time or UTC |
407 // into a Time class. | 506 // into a Time class. |
408 static Time FromUTCExploded(const Exploded& exploded) { | 507 static Time FromUTCExploded(const Exploded& exploded) { |
409 return FromExploded(false, exploded); | 508 return FromExploded(false, exploded); |
410 } | 509 } |
411 static Time FromLocalExploded(const Exploded& exploded) { | 510 static Time FromLocalExploded(const Exploded& exploded) { |
412 return FromExploded(true, exploded); | 511 return FromExploded(true, exploded); |
413 } | 512 } |
414 | 513 |
415 // Converts an integer value representing Time to a class. This is used | |
416 // when deserializing a |Time| structure, using a value known to be | |
417 // compatible. It is not provided as a constructor because the integer type | |
418 // may be unclear from the perspective of a caller. | |
419 static Time FromInternalValue(int64 us) { | |
420 return Time(us); | |
421 } | |
422 | |
423 // Converts a string representation of time to a Time object. | 514 // Converts a string representation of time to a Time object. |
424 // An example of a time string which is converted is as below:- | 515 // An example of a time string which is converted is as below:- |
425 // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified | 516 // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified |
426 // in the input string, FromString assumes local time and FromUTCString | 517 // in the input string, FromString assumes local time and FromUTCString |
427 // assumes UTC. A timezone that cannot be parsed (e.g. "UTC" which is not | 518 // assumes UTC. A timezone that cannot be parsed (e.g. "UTC" which is not |
428 // specified in RFC822) is treated as if the timezone is not specified. | 519 // specified in RFC822) is treated as if the timezone is not specified. |
429 // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to | 520 // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to |
430 // a new time converter class. | 521 // a new time converter class. |
431 static bool FromString(const char* time_string, Time* parsed_time) { | 522 static bool FromString(const char* time_string, Time* parsed_time) { |
432 return FromStringInternal(time_string, true, parsed_time); | 523 return FromStringInternal(time_string, true, parsed_time); |
433 } | 524 } |
434 static bool FromUTCString(const char* time_string, Time* parsed_time) { | 525 static bool FromUTCString(const char* time_string, Time* parsed_time) { |
435 return FromStringInternal(time_string, false, parsed_time); | 526 return FromStringInternal(time_string, false, parsed_time); |
436 } | 527 } |
437 | 528 |
438 // For serializing, use FromInternalValue to reconstitute. Please don't use | |
439 // this and do arithmetic on it, as it is more error prone than using the | |
440 // provided operators. | |
441 int64 ToInternalValue() const { | |
442 return us_; | |
443 } | |
444 | |
445 // Fills the given exploded structure with either the local time or UTC from | 529 // Fills the given exploded structure with either the local time or UTC from |
446 // this time structure (containing UTC). | 530 // this time structure (containing UTC). |
447 void UTCExplode(Exploded* exploded) const { | 531 void UTCExplode(Exploded* exploded) const { |
448 return Explode(false, exploded); | 532 return Explode(false, exploded); |
449 } | 533 } |
450 void LocalExplode(Exploded* exploded) const { | 534 void LocalExplode(Exploded* exploded) const { |
451 return Explode(true, exploded); | 535 return Explode(true, exploded); |
452 } | 536 } |
453 | 537 |
454 // Rounds this time down to the nearest day in local time. It will represent | 538 // Rounds this time down to the nearest day in local time. It will represent |
455 // midnight on that day. | 539 // midnight on that day. |
456 Time LocalMidnight() const; | 540 Time LocalMidnight() const; |
457 | 541 |
458 Time& operator=(Time other) { | 542 private: |
459 us_ = other.us_; | 543 friend class time_internal::TimeBase<Time>; |
460 return *this; | |
461 } | |
462 | 544 |
463 // Compute the difference between two times. | 545 explicit Time(int64 us) : TimeBase(us) { |
464 TimeDelta operator-(Time other) const { | |
465 return TimeDelta(us_ - other.us_); | |
466 } | |
467 | |
468 // Modify by some time delta. | |
469 Time& operator+=(TimeDelta delta) { | |
470 us_ = delta.SaturatedAdd(us_); | |
471 return *this; | |
472 } | |
473 Time& operator-=(TimeDelta delta) { | |
474 us_ = -delta.SaturatedSub(us_); | |
475 return *this; | |
476 } | |
477 | |
478 // Return a new time modified by some delta. | |
479 Time operator+(TimeDelta delta) const { | |
480 return Time(delta.SaturatedAdd(us_)); | |
481 } | |
482 Time operator-(TimeDelta delta) const { | |
483 return Time(-delta.SaturatedSub(us_)); | |
484 } | |
485 | |
486 // Comparison operators | |
487 bool operator==(Time other) const { | |
488 return us_ == other.us_; | |
489 } | |
490 bool operator!=(Time other) const { | |
491 return us_ != other.us_; | |
492 } | |
493 bool operator<(Time other) const { | |
494 return us_ < other.us_; | |
495 } | |
496 bool operator<=(Time other) const { | |
497 return us_ <= other.us_; | |
498 } | |
499 bool operator>(Time other) const { | |
500 return us_ > other.us_; | |
501 } | |
502 bool operator>=(Time other) const { | |
503 return us_ >= other.us_; | |
504 } | |
505 | |
506 private: | |
507 friend class TimeDelta; | |
508 | |
509 explicit Time(int64 us) : us_(us) { | |
510 } | 546 } |
511 | 547 |
512 // Explodes the given time to either local time |is_local = true| or UTC | 548 // Explodes the given time to either local time |is_local = true| or UTC |
513 // |is_local = false|. | 549 // |is_local = false|. |
514 void Explode(bool is_local, Exploded* exploded) const; | 550 void Explode(bool is_local, Exploded* exploded) const; |
515 | 551 |
516 // Unexplodes a given time assuming the source is either local time | 552 // Unexplodes a given time assuming the source is either local time |
517 // |is_local = true| or UTC |is_local = false|. | 553 // |is_local = true| or UTC |is_local = false|. |
518 static Time FromExploded(bool is_local, const Exploded& exploded); | 554 static Time FromExploded(bool is_local, const Exploded& exploded); |
519 | 555 |
520 // Converts a string representation of time to a Time object. | 556 // Converts a string representation of time to a Time object. |
521 // An example of a time string which is converted is as below:- | 557 // An example of a time string which is converted is as below:- |
522 // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified | 558 // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified |
523 // in the input string, local time |is_local = true| or | 559 // in the input string, local time |is_local = true| or |
524 // UTC |is_local = false| is assumed. A timezone that cannot be parsed | 560 // UTC |is_local = false| is assumed. A timezone that cannot be parsed |
525 // (e.g. "UTC" which is not specified in RFC822) is treated as if the | 561 // (e.g. "UTC" which is not specified in RFC822) is treated as if the |
526 // timezone is not specified. | 562 // timezone is not specified. |
527 static bool FromStringInternal(const char* time_string, | 563 static bool FromStringInternal(const char* time_string, |
528 bool is_local, | 564 bool is_local, |
529 Time* parsed_time); | 565 Time* parsed_time); |
530 | |
531 // Time in microseconds in UTC. | |
532 int64 us_; | |
533 }; | 566 }; |
534 | 567 |
535 // Inline the TimeDelta factory methods, for fast TimeDelta construction. | 568 // Inline the TimeDelta factory methods, for fast TimeDelta construction. |
536 | 569 |
537 // static | 570 // static |
538 inline TimeDelta TimeDelta::FromDays(int days) { | 571 inline TimeDelta TimeDelta::FromDays(int days) { |
539 // Preserve max to prevent overflow. | 572 // Preserve max to prevent overflow. |
540 if (days == std::numeric_limits<int>::max()) | 573 if (days == std::numeric_limits<int>::max()) |
541 return Max(); | 574 return Max(); |
542 return TimeDelta(days * Time::kMicrosecondsPerDay); | 575 return TimeDelta(days * Time::kMicrosecondsPerDay); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
591 } | 624 } |
592 | 625 |
593 // static | 626 // static |
594 inline TimeDelta TimeDelta::FromMicroseconds(int64 us) { | 627 inline TimeDelta TimeDelta::FromMicroseconds(int64 us) { |
595 // Preserve max to prevent overflow. | 628 // Preserve max to prevent overflow. |
596 if (us == std::numeric_limits<int64>::max()) | 629 if (us == std::numeric_limits<int64>::max()) |
597 return Max(); | 630 return Max(); |
598 return TimeDelta(us); | 631 return TimeDelta(us); |
599 } | 632 } |
600 | 633 |
601 inline Time TimeDelta::operator+(Time t) const { | |
602 return Time(SaturatedAdd(t.us_)); | |
603 } | |
604 | |
605 // For logging use only. | 634 // For logging use only. |
606 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); | 635 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); |
607 | 636 |
608 // TimeTicks ------------------------------------------------------------------ | 637 // TimeTicks ------------------------------------------------------------------ |
609 | 638 |
610 class BASE_EXPORT TimeTicks { | 639 // Represents monotonically non-decreasing clock time. |
640 class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> { | |
611 public: | 641 public: |
612 // We define this even without OS_CHROMEOS for seccomp sandbox testing. | 642 // We define this even without OS_CHROMEOS for seccomp sandbox testing. |
613 #if defined(OS_LINUX) | 643 #if defined(OS_LINUX) |
614 // Force definition of the system trace clock; it is a chromeos-only api | 644 // Force definition of the system trace clock; it is a chromeos-only api |
615 // at the moment and surfacing it in the right place requires mucking | 645 // at the moment and surfacing it in the right place requires mucking |
616 // with glibc et al. | 646 // with glibc et al. |
617 static const clockid_t kClockSystemTrace = 11; | 647 static const clockid_t kClockSystemTrace = 11; |
618 #endif | 648 #endif |
619 | 649 |
620 TimeTicks() : ticks_(0) { | 650 TimeTicks() : TimeBase(0) { |
621 } | 651 } |
622 | 652 |
623 // Platform-dependent tick count representing "right now." When | 653 // Platform-dependent tick count representing "right now." When |
624 // IsHighResolution() returns false, the resolution of the clock could be | 654 // IsHighResolution() returns false, the resolution of the clock could be |
625 // as coarse as ~15.6ms. Otherwise, the resolution should be no worse than one | 655 // as coarse as ~15.6ms. Otherwise, the resolution should be no worse than one |
626 // microsecond. | 656 // microsecond. |
627 static TimeTicks Now(); | 657 static TimeTicks Now(); |
628 | 658 |
629 // Returns true if the high resolution clock is working on this system and | 659 // Returns true if the high resolution clock is working on this system and |
630 // Now() will return high resolution values. Note that, on systems where the | 660 // Now() will return high resolution values. Note that, on systems where the |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
669 // should be of a different type. | 699 // should be of a different type. |
670 static TimeTicks NowFromSystemTraceTime(); | 700 static TimeTicks NowFromSystemTraceTime(); |
671 | 701 |
672 #if defined(OS_WIN) | 702 #if defined(OS_WIN) |
673 // Translates an absolute QPC timestamp into a TimeTicks value. The returned | 703 // Translates an absolute QPC timestamp into a TimeTicks value. The returned |
674 // value has the same origin as Now(). Do NOT attempt to use this if | 704 // value has the same origin as Now(). Do NOT attempt to use this if |
675 // IsHighResolution() returns false. | 705 // IsHighResolution() returns false. |
676 static TimeTicks FromQPCValue(LONGLONG qpc_value); | 706 static TimeTicks FromQPCValue(LONGLONG qpc_value); |
677 #endif | 707 #endif |
678 | 708 |
679 // Returns true if this object has not been initialized. | |
680 bool is_null() const { | |
681 return ticks_ == 0; | |
682 } | |
683 | |
684 // Returns true if the time delta is the maximum delta. | |
685 bool is_max() const { | |
686 return ticks_ == std::numeric_limits<int64>::max(); | |
687 } | |
688 | |
689 // Converts an integer value representing TimeTicks to a class. This is used | |
690 // when deserializing a |TimeTicks| structure, using a value known to be | |
691 // compatible. It is not provided as a constructor because the integer type | |
692 // may be unclear from the perspective of a caller. | |
693 static TimeTicks FromInternalValue(int64 ticks) { | |
694 return TimeTicks(ticks); | |
695 } | |
696 | |
697 // Get the TimeTick value at the time of the UnixEpoch. This is useful when | 709 // Get the TimeTick value at the time of the UnixEpoch. This is useful when |
698 // you need to relate the value of TimeTicks to a real time and date. | 710 // you need to relate the value of TimeTicks to a real time and date. |
699 // Note: Upon first invocation, this function takes a snapshot of the realtime | 711 // Note: Upon first invocation, this function takes a snapshot of the realtime |
700 // clock to establish a reference point. This function will return the same | 712 // clock to establish a reference point. This function will return the same |
701 // value for the duration of the application, but will be different in future | 713 // value for the duration of the application, but will be different in future |
702 // application runs. | 714 // application runs. |
703 static TimeTicks UnixEpoch(); | 715 static TimeTicks UnixEpoch(); |
704 | 716 |
705 // Returns the internal numeric value of the TimeTicks object. | |
706 // For serializing, use FromInternalValue to reconstitute. | |
707 int64 ToInternalValue() const { | |
708 return ticks_; | |
709 } | |
710 | |
711 // Returns |this| snapped to the next tick, given a |tick_phase| and | 717 // Returns |this| snapped to the next tick, given a |tick_phase| and |
712 // repeating |tick_interval| in both directions. |this| may be before, | 718 // repeating |tick_interval| in both directions. |this| may be before, |
713 // after, or equal to the |tick_phase|. | 719 // after, or equal to the |tick_phase|. |
714 TimeTicks SnappedToNextTick(TimeTicks tick_phase, | 720 TimeTicks SnappedToNextTick(TimeTicks tick_phase, |
715 TimeDelta tick_interval) const; | 721 TimeDelta tick_interval) const; |
716 | 722 |
717 TimeTicks& operator=(TimeTicks other) { | 723 #if defined(OS_WIN) |
718 ticks_ = other.ticks_; | |
719 return *this; | |
720 } | |
721 | |
722 // Compute the difference between two times. | |
723 TimeDelta operator-(TimeTicks other) const { | |
724 return TimeDelta(ticks_ - other.ticks_); | |
725 } | |
726 | |
727 // Modify by some time delta. | |
728 TimeTicks& operator+=(TimeDelta delta) { | |
729 ticks_ = delta.SaturatedAdd(ticks_); | |
730 return *this; | |
731 } | |
732 TimeTicks& operator-=(TimeDelta delta) { | |
733 ticks_ = -delta.SaturatedSub(ticks_); | |
734 return *this; | |
735 } | |
736 | |
737 // Return a new TimeTicks modified by some delta. | |
738 TimeTicks operator+(TimeDelta delta) const { | |
739 return TimeTicks(delta.SaturatedAdd(ticks_)); | |
740 } | |
741 TimeTicks operator-(TimeDelta delta) const { | |
742 return TimeTicks(-delta.SaturatedSub(ticks_)); | |
743 } | |
744 | |
745 // Comparison operators | |
746 bool operator==(TimeTicks other) const { | |
747 return ticks_ == other.ticks_; | |
748 } | |
749 bool operator!=(TimeTicks other) const { | |
750 return ticks_ != other.ticks_; | |
751 } | |
752 bool operator<(TimeTicks other) const { | |
753 return ticks_ < other.ticks_; | |
754 } | |
755 bool operator<=(TimeTicks other) const { | |
756 return ticks_ <= other.ticks_; | |
757 } | |
758 bool operator>(TimeTicks other) const { | |
759 return ticks_ > other.ticks_; | |
760 } | |
761 bool operator>=(TimeTicks other) const { | |
762 return ticks_ >= other.ticks_; | |
763 } | |
764 | |
765 protected: | 724 protected: |
766 friend class TimeDelta; | |
767 | |
768 // Please use Now() to create a new object. This is for internal use | |
769 // and testing. Ticks is in microseconds. | |
770 explicit TimeTicks(int64 ticks) : ticks_(ticks) { | |
771 } | |
772 | |
773 // Tick count in microseconds. | |
774 int64 ticks_; | |
775 | |
776 #if defined(OS_WIN) | |
777 typedef DWORD (*TickFunctionType)(void); | 725 typedef DWORD (*TickFunctionType)(void); |
778 static TickFunctionType SetMockTickFunction(TickFunctionType ticker); | 726 static TickFunctionType SetMockTickFunction(TickFunctionType ticker); |
779 #endif | 727 #endif |
728 | |
729 private: | |
730 friend class time_internal::TimeBase<TimeTicks>; | |
731 | |
732 // Please use Now() to create a new object. This is for internal use | |
733 // and testing. | |
734 explicit TimeTicks(int64 us) : TimeBase(us) { | |
735 } | |
780 }; | 736 }; |
781 | 737 |
782 inline TimeTicks TimeDelta::operator+(TimeTicks t) const { | |
783 return TimeTicks(SaturatedAdd(t.ticks_)); | |
784 } | |
785 | |
786 // For logging use only. | 738 // For logging use only. |
787 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks); | 739 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks); |
788 | 740 |
789 } // namespace base | 741 } // namespace base |
790 | 742 |
791 #endif // BASE_TIME_TIME_H_ | 743 #endif // BASE_TIME_TIME_H_ |
OLD | NEW |