Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: base/time/time.h

Issue 1121463005: Fixit: Split base::TimeTicks --> TimeTicks + ThreadTicks + TraceTicks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More Windows compile fixes. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/threading/thread_perftest.cc ('k') | base/time/time.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 namespace time_internal {
63
64 // Add or subtract |value| from a TimeDelta. The int64 argument and return value
65 // are are in terms of a microsecond timebase.
66 BASE_EXPORT int64 SaturatedAdd(TimeDelta delta, int64 value);
67 BASE_EXPORT int64 SaturatedSub(TimeDelta delta, int64 value);
68
69 // Clamp |value| on overflow and underflow conditions. The int64 argument and
70 // return value are are in terms of a microsecond timebase.
71 BASE_EXPORT int64 FromCheckedNumeric(const CheckedNumeric<int64> value);
72
73 } // namespace time_internal
62 74
63 // TimeDelta ------------------------------------------------------------------ 75 // TimeDelta ------------------------------------------------------------------
64 76
65 class BASE_EXPORT TimeDelta { 77 class BASE_EXPORT TimeDelta {
66 public: 78 public:
67 TimeDelta() : delta_(0) { 79 TimeDelta() : delta_(0) {
68 } 80 }
69 81
70 // Converts units of time to TimeDeltas. 82 // Converts units of time to TimeDeltas.
71 static TimeDelta FromDays(int days); 83 static TimeDelta FromDays(int days);
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 int64 InMillisecondsRoundedUp() const; 146 int64 InMillisecondsRoundedUp() const;
135 int64 InMicroseconds() const; 147 int64 InMicroseconds() const;
136 148
137 TimeDelta& operator=(TimeDelta other) { 149 TimeDelta& operator=(TimeDelta other) {
138 delta_ = other.delta_; 150 delta_ = other.delta_;
139 return *this; 151 return *this;
140 } 152 }
141 153
142 // Computations with other deltas. 154 // Computations with other deltas.
143 TimeDelta operator+(TimeDelta other) const { 155 TimeDelta operator+(TimeDelta other) const {
144 return TimeDelta(SaturatedAdd(other.delta_)); 156 return TimeDelta(time_internal::SaturatedAdd(*this, other.delta_));
145 } 157 }
146 TimeDelta operator-(TimeDelta other) const { 158 TimeDelta operator-(TimeDelta other) const {
147 return TimeDelta(SaturatedSub(other.delta_)); 159 return TimeDelta(time_internal::SaturatedSub(*this, other.delta_));
148 } 160 }
149 161
150 TimeDelta& operator+=(TimeDelta other) { 162 TimeDelta& operator+=(TimeDelta other) {
151 delta_ = SaturatedAdd(other.delta_); 163 return *this = (*this + other);
152 return *this;
153 } 164 }
154 TimeDelta& operator-=(TimeDelta other) { 165 TimeDelta& operator-=(TimeDelta other) {
155 delta_ = SaturatedSub(other.delta_); 166 return *this = (*this - other);
156 return *this;
157 } 167 }
158 TimeDelta operator-() const { 168 TimeDelta operator-() const {
159 return TimeDelta(-delta_); 169 return TimeDelta(-delta_);
160 } 170 }
161 171
162 // Computations with numeric types. 172 // Computations with numeric types.
163 template<typename T> 173 template<typename T>
164 TimeDelta operator*(T a) const { 174 TimeDelta operator*(T a) const {
165 CheckedNumeric<int64> rv(delta_); 175 CheckedNumeric<int64> rv(delta_);
166 rv *= a; 176 rv *= a;
167 return TimeDelta(FromCheckedNumeric(rv)); 177 return TimeDelta(time_internal::FromCheckedNumeric(rv));
168 } 178 }
169 template<typename T> 179 template<typename T>
170 TimeDelta operator/(T a) const { 180 TimeDelta operator/(T a) const {
171 CheckedNumeric<int64> rv(delta_); 181 CheckedNumeric<int64> rv(delta_);
172 rv /= a; 182 rv /= a;
173 return TimeDelta(FromCheckedNumeric(rv)); 183 return TimeDelta(time_internal::FromCheckedNumeric(rv));
174 } 184 }
175 template<typename T> 185 template<typename T>
176 TimeDelta& operator*=(T a) { 186 TimeDelta& operator*=(T a) {
177 CheckedNumeric<int64> rv(delta_); 187 return *this = (*this * a);
178 rv *= a;
179 delta_ = FromCheckedNumeric(rv);
180 return *this;
181 } 188 }
182 template<typename T> 189 template<typename T>
183 TimeDelta& operator/=(T a) { 190 TimeDelta& operator/=(T a) {
184 CheckedNumeric<int64> rv(delta_); 191 return *this = (*this / a);
185 rv /= a;
186 delta_ = FromCheckedNumeric(rv);
187 return *this;
188 } 192 }
189 193
190 int64 operator/(TimeDelta a) const { 194 int64 operator/(TimeDelta a) const {
191 return delta_ / a.delta_; 195 return delta_ / a.delta_;
192 } 196 }
193 197 TimeDelta operator%(TimeDelta a) const {
194 // Defined below because it depends on the definition of the other classes. 198 return TimeDelta(delta_ % a.delta_);
195 Time operator+(Time t) const; 199 }
196 TimeTicks operator+(TimeTicks t) const;
197 200
198 // Comparison operators. 201 // Comparison operators.
199 bool operator==(TimeDelta other) const { 202 bool operator==(TimeDelta other) const {
200 return delta_ == other.delta_; 203 return delta_ == other.delta_;
201 } 204 }
202 bool operator!=(TimeDelta other) const { 205 bool operator!=(TimeDelta other) const {
203 return delta_ != other.delta_; 206 return delta_ != other.delta_;
204 } 207 }
205 bool operator<(TimeDelta other) const { 208 bool operator<(TimeDelta other) const {
206 return delta_ < other.delta_; 209 return delta_ < other.delta_;
207 } 210 }
208 bool operator<=(TimeDelta other) const { 211 bool operator<=(TimeDelta other) const {
209 return delta_ <= other.delta_; 212 return delta_ <= other.delta_;
210 } 213 }
211 bool operator>(TimeDelta other) const { 214 bool operator>(TimeDelta other) const {
212 return delta_ > other.delta_; 215 return delta_ > other.delta_;
213 } 216 }
214 bool operator>=(TimeDelta other) const { 217 bool operator>=(TimeDelta other) const {
215 return delta_ >= other.delta_; 218 return delta_ >= other.delta_;
216 } 219 }
217 220
218 private: 221 private:
219 friend class Time; 222 friend int64 time_internal::SaturatedAdd(TimeDelta delta, int64 value);
220 friend class TimeTicks; 223 friend int64 time_internal::SaturatedSub(TimeDelta delta, int64 value);
221 224
222 // Constructs a delta given the duration in microseconds. This is private 225 // Constructs a delta given the duration in microseconds. This is private
223 // to avoid confusion by callers with an integer constructor. Use 226 // to avoid confusion by callers with an integer constructor. Use
224 // FromSeconds, FromMilliseconds, etc. instead. 227 // FromSeconds, FromMilliseconds, etc. instead.
225 explicit TimeDelta(int64 delta_us) : delta_(delta_us) { 228 explicit TimeDelta(int64 delta_us) : delta_(delta_us) {
226 } 229 }
227 230
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. 231 // Delta in microseconds.
236 int64 delta_; 232 int64 delta_;
237 }; 233 };
238 234
239 template<typename T> 235 template<typename T>
240 inline TimeDelta operator*(T a, TimeDelta td) { 236 inline TimeDelta operator*(T a, TimeDelta td) {
241 return td * a; 237 return td * a;
242 } 238 }
243 239
244 // For logging use only. 240 // For logging use only.
245 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeDelta time_delta); 241 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeDelta time_delta);
246 242
247 // Time ----------------------------------------------------------------------- 243 namespace time_internal {
248 244
249 // Represents a wall clock time in UTC. 245 // TimeBase--------------------------------------------------------------------
250 class BASE_EXPORT Time { 246
247 // Provides value storage and comparison/math operations common to all time
248 // classes. Each subclass provides for strong type-checking to ensure
249 // semantically meaningful comparison/math of time values from the same clock
250 // source or timeline.
251 template<class TimeClass>
252 class TimeBase {
251 public: 253 public:
252 static const int64 kHoursPerDay = 24; 254 static const int64 kHoursPerDay = 24;
253 static const int64 kMillisecondsPerSecond = 1000; 255 static const int64 kMillisecondsPerSecond = 1000;
254 static const int64 kMillisecondsPerDay = kMillisecondsPerSecond * 60 * 60 * 256 static const int64 kMillisecondsPerDay = kMillisecondsPerSecond * 60 * 60 *
255 kHoursPerDay; 257 kHoursPerDay;
256 static const int64 kMicrosecondsPerMillisecond = 1000; 258 static const int64 kMicrosecondsPerMillisecond = 1000;
257 static const int64 kMicrosecondsPerSecond = kMicrosecondsPerMillisecond * 259 static const int64 kMicrosecondsPerSecond = kMicrosecondsPerMillisecond *
258 kMillisecondsPerSecond; 260 kMillisecondsPerSecond;
259 static const int64 kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60; 261 static const int64 kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60;
260 static const int64 kMicrosecondsPerHour = kMicrosecondsPerMinute * 60; 262 static const int64 kMicrosecondsPerHour = kMicrosecondsPerMinute * 60;
261 static const int64 kMicrosecondsPerDay = kMicrosecondsPerHour * kHoursPerDay; 263 static const int64 kMicrosecondsPerDay = kMicrosecondsPerHour * kHoursPerDay;
262 static const int64 kMicrosecondsPerWeek = kMicrosecondsPerDay * 7; 264 static const int64 kMicrosecondsPerWeek = kMicrosecondsPerDay * 7;
263 static const int64 kNanosecondsPerMicrosecond = 1000; 265 static const int64 kNanosecondsPerMicrosecond = 1000;
264 static const int64 kNanosecondsPerSecond = kNanosecondsPerMicrosecond * 266 static const int64 kNanosecondsPerSecond = kNanosecondsPerMicrosecond *
265 kMicrosecondsPerSecond; 267 kMicrosecondsPerSecond;
266 268
269 // Returns true if this object has not been initialized.
270 bool is_null() const {
271 return us_ == 0;
272 }
273
274 // Returns true if this object represents the maximum time.
275 bool is_max() const {
276 return us_ == std::numeric_limits<int64>::max();
277 }
278
279 // For serializing only. Use FromInternalValue() to reconstitute. Please don't
280 // use this and do arithmetic on it, as it is more error prone than using the
281 // provided operators.
282 int64 ToInternalValue() const {
283 return us_;
284 }
285
286 TimeClass& operator=(TimeClass other) {
287 us_ = other.us_;
288 return *(static_cast<TimeClass*>(this));
289 }
290
291 // Compute the difference between two times.
292 TimeDelta operator-(TimeClass other) const {
293 return TimeDelta::FromMicroseconds(us_ - other.us_);
294 }
295
296 // Return a new time modified by some delta.
297 TimeClass operator+(TimeDelta delta) const {
298 return TimeClass(time_internal::SaturatedAdd(delta, us_));
299 }
300 TimeClass operator-(TimeDelta delta) const {
301 return TimeClass(-time_internal::SaturatedSub(delta, us_));
302 }
303
304 // Modify by some time delta.
305 TimeClass& operator+=(TimeDelta delta) {
306 return static_cast<TimeClass&>(*this = (*this + delta));
307 }
308 TimeClass& operator-=(TimeDelta delta) {
309 return static_cast<TimeClass&>(*this = (*this - delta));
310 }
311
312 // Comparison operators
313 bool operator==(TimeClass other) const {
314 return us_ == other.us_;
315 }
316 bool operator!=(TimeClass other) const {
317 return us_ != other.us_;
318 }
319 bool operator<(TimeClass other) const {
320 return us_ < other.us_;
321 }
322 bool operator<=(TimeClass other) const {
323 return us_ <= other.us_;
324 }
325 bool operator>(TimeClass other) const {
326 return us_ > other.us_;
327 }
328 bool operator>=(TimeClass other) const {
329 return us_ >= other.us_;
330 }
331
332 // Converts an integer value representing TimeClass to a class. This is used
333 // when deserializing a |TimeClass| structure, using a value known to be
334 // compatible. It is not provided as a constructor because the integer type
335 // may be unclear from the perspective of a caller.
336 static TimeClass FromInternalValue(int64 us) {
337 return TimeClass(us);
338 }
339
340 protected:
341 explicit TimeBase(int64 us) : us_(us) {
342 }
343
344 // Time value in a microsecond timebase.
345 int64 us_;
346 };
347
348 } // namespace time_internal
349
350 template<class TimeClass>
351 inline TimeClass operator+(TimeDelta delta, TimeClass t) {
352 return t + delta;
353 }
354
355 // Time -----------------------------------------------------------------------
356
357 // Represents a wall clock time in UTC. Values are not guaranteed to be
358 // monotonically non-decreasing and are subject to large amounts of skew.
359 class BASE_EXPORT Time : public time_internal::TimeBase<Time> {
360 public:
267 // The representation of Jan 1, 1970 UTC in microseconds since the 361 // The representation of Jan 1, 1970 UTC in microseconds since the
268 // platform-dependent epoch. 362 // platform-dependent epoch.
269 static const int64 kTimeTToMicrosecondsOffset; 363 static const int64 kTimeTToMicrosecondsOffset;
270 364
271 #if !defined(OS_WIN) 365 #if !defined(OS_WIN)
272 // On Mac & Linux, this value is the delta from the Windows epoch of 1601 to 366 // 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 367 // 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 368 // 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 369 // this global header and put in the platform-specific ones when we remove the
276 // migration code. 370 // migration code.
(...skipping 19 matching lines...) Expand all
296 // seconds which may take it up to 60). 390 // seconds which may take it up to 60).
297 int millisecond; // Milliseconds within the current second (0-999) 391 int millisecond; // Milliseconds within the current second (0-999)
298 392
299 // A cursory test for whether the data members are within their 393 // A cursory test for whether the data members are within their
300 // respective ranges. A 'true' return value does not guarantee the 394 // respective ranges. A 'true' return value does not guarantee the
301 // Exploded value can be successfully converted to a Time value. 395 // Exploded value can be successfully converted to a Time value.
302 bool HasValidValues() const; 396 bool HasValidValues() const;
303 }; 397 };
304 398
305 // Contains the NULL time. Use Time::Now() to get the current time. 399 // Contains the NULL time. Use Time::Now() to get the current time.
306 Time() : us_(0) { 400 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 } 401 }
318 402
319 // Returns the time for epoch in Unix-like system (Jan 1, 1970). 403 // Returns the time for epoch in Unix-like system (Jan 1, 1970).
320 static Time UnixEpoch(); 404 static Time UnixEpoch();
321 405
322 // Returns the current time. Watch out, the system might adjust its clock 406 // 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 407 // 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. 408 // times are increasing, or that two calls to Now() won't be the same.
325 static Time Now(); 409 static Time Now();
326 410
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 489
406 // Converts an exploded structure representing either the local time or UTC 490 // Converts an exploded structure representing either the local time or UTC
407 // into a Time class. 491 // into a Time class.
408 static Time FromUTCExploded(const Exploded& exploded) { 492 static Time FromUTCExploded(const Exploded& exploded) {
409 return FromExploded(false, exploded); 493 return FromExploded(false, exploded);
410 } 494 }
411 static Time FromLocalExploded(const Exploded& exploded) { 495 static Time FromLocalExploded(const Exploded& exploded) {
412 return FromExploded(true, exploded); 496 return FromExploded(true, exploded);
413 } 497 }
414 498
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. 499 // Converts a string representation of time to a Time object.
424 // An example of a time string which is converted is as below:- 500 // 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 501 // "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 502 // 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 503 // 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. 504 // specified in RFC822) is treated as if the timezone is not specified.
429 // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to 505 // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to
430 // a new time converter class. 506 // a new time converter class.
431 static bool FromString(const char* time_string, Time* parsed_time) { 507 static bool FromString(const char* time_string, Time* parsed_time) {
432 return FromStringInternal(time_string, true, parsed_time); 508 return FromStringInternal(time_string, true, parsed_time);
433 } 509 }
434 static bool FromUTCString(const char* time_string, Time* parsed_time) { 510 static bool FromUTCString(const char* time_string, Time* parsed_time) {
435 return FromStringInternal(time_string, false, parsed_time); 511 return FromStringInternal(time_string, false, parsed_time);
436 } 512 }
437 513
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 514 // Fills the given exploded structure with either the local time or UTC from
446 // this time structure (containing UTC). 515 // this time structure (containing UTC).
447 void UTCExplode(Exploded* exploded) const { 516 void UTCExplode(Exploded* exploded) const {
448 return Explode(false, exploded); 517 return Explode(false, exploded);
449 } 518 }
450 void LocalExplode(Exploded* exploded) const { 519 void LocalExplode(Exploded* exploded) const {
451 return Explode(true, exploded); 520 return Explode(true, exploded);
452 } 521 }
453 522
454 // Rounds this time down to the nearest day in local time. It will represent 523 // Rounds this time down to the nearest day in local time. It will represent
455 // midnight on that day. 524 // midnight on that day.
456 Time LocalMidnight() const; 525 Time LocalMidnight() const;
457 526
458 Time& operator=(Time other) { 527 private:
459 us_ = other.us_; 528 friend class time_internal::TimeBase<Time>;
460 return *this;
461 }
462 529
463 // Compute the difference between two times. 530 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 } 531 }
511 532
512 // Explodes the given time to either local time |is_local = true| or UTC 533 // Explodes the given time to either local time |is_local = true| or UTC
513 // |is_local = false|. 534 // |is_local = false|.
514 void Explode(bool is_local, Exploded* exploded) const; 535 void Explode(bool is_local, Exploded* exploded) const;
515 536
516 // Unexplodes a given time assuming the source is either local time 537 // Unexplodes a given time assuming the source is either local time
517 // |is_local = true| or UTC |is_local = false|. 538 // |is_local = true| or UTC |is_local = false|.
518 static Time FromExploded(bool is_local, const Exploded& exploded); 539 static Time FromExploded(bool is_local, const Exploded& exploded);
519 540
520 // Converts a string representation of time to a Time object. 541 // Converts a string representation of time to a Time object.
521 // An example of a time string which is converted is as below:- 542 // 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 543 // "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 544 // in the input string, local time |is_local = true| or
524 // UTC |is_local = false| is assumed. A timezone that cannot be parsed 545 // 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 546 // (e.g. "UTC" which is not specified in RFC822) is treated as if the
526 // timezone is not specified. 547 // timezone is not specified.
527 static bool FromStringInternal(const char* time_string, 548 static bool FromStringInternal(const char* time_string,
528 bool is_local, 549 bool is_local,
529 Time* parsed_time); 550 Time* parsed_time);
530
531 // Time in microseconds in UTC.
532 int64 us_;
533 }; 551 };
534 552
535 // Inline the TimeDelta factory methods, for fast TimeDelta construction. 553 // Inline the TimeDelta factory methods, for fast TimeDelta construction.
536 554
537 // static 555 // static
538 inline TimeDelta TimeDelta::FromDays(int days) { 556 inline TimeDelta TimeDelta::FromDays(int days) {
539 // Preserve max to prevent overflow. 557 // Preserve max to prevent overflow.
540 if (days == std::numeric_limits<int>::max()) 558 if (days == std::numeric_limits<int>::max())
541 return Max(); 559 return Max();
542 return TimeDelta(days * Time::kMicrosecondsPerDay); 560 return TimeDelta(days * Time::kMicrosecondsPerDay);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
591 } 609 }
592 610
593 // static 611 // static
594 inline TimeDelta TimeDelta::FromMicroseconds(int64 us) { 612 inline TimeDelta TimeDelta::FromMicroseconds(int64 us) {
595 // Preserve max to prevent overflow. 613 // Preserve max to prevent overflow.
596 if (us == std::numeric_limits<int64>::max()) 614 if (us == std::numeric_limits<int64>::max())
597 return Max(); 615 return Max();
598 return TimeDelta(us); 616 return TimeDelta(us);
599 } 617 }
600 618
601 inline Time TimeDelta::operator+(Time t) const {
602 return Time(SaturatedAdd(t.us_));
603 }
604
605 // For logging use only. 619 // For logging use only.
606 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); 620 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time);
607 621
608 // TimeTicks ------------------------------------------------------------------ 622 // TimeTicks ------------------------------------------------------------------
609 623
610 class BASE_EXPORT TimeTicks { 624 // Represents monotonically non-decreasing clock time.
625 class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> {
611 public: 626 public:
612 // We define this even without OS_CHROMEOS for seccomp sandbox testing. 627 TimeTicks() : TimeBase(0) {
613 #if defined(OS_LINUX)
614 // 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
616 // with glibc et al.
617 static const clockid_t kClockSystemTrace = 11;
618 #endif
619
620 TimeTicks() : ticks_(0) {
621 } 628 }
622 629
623 // Platform-dependent tick count representing "right now." When 630 // Platform-dependent tick count representing "right now." When
624 // IsHighResolution() returns false, the resolution of the clock could be 631 // 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 632 // as coarse as ~15.6ms. Otherwise, the resolution should be no worse than one
626 // microsecond. 633 // microsecond.
627 static TimeTicks Now(); 634 static TimeTicks Now();
628 635
629 // Returns true if the high resolution clock is working on this system and 636 // 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 637 // Now() will return high resolution values. Note that, on systems where the
631 // high resolution clock works but is deemed inefficient, the low resolution 638 // high resolution clock works but is deemed inefficient, the low resolution
632 // clock will be used instead. 639 // clock will be used instead.
633 static bool IsHighResolution(); 640 static bool IsHighResolution();
634 641
635 // Returns true if ThreadNow() is supported on this system.
636 static bool IsThreadNowSupported() {
637 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \
638 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID)
639 return true;
640 #else
641 return false;
642 #endif
643 }
644
645 // Returns thread-specific CPU-time on systems that support this feature.
646 // Needs to be guarded with a call to IsThreadNowSupported(). Use this timer
647 // to (approximately) measure how much time the calling thread spent doing
648 // actual work vs. being de-scheduled. May return bogus results if the thread
649 // migrates to another CPU between two calls.
650 //
651 // WARNING: The returned value might NOT have the same origin as Now(). Do not
652 // perform math between TimeTicks values returned by Now() and ThreadNow() and
653 // expect meaningful results.
654 // TODO(miu): Since the timeline of these values is different, the values
655 // should be of a different type.
656 static TimeTicks ThreadNow();
657
658 // Returns the current system trace time or, if not available on this
659 // platform, a high-resolution time value; or a low-resolution time value if
660 // neither are avalable. On systems where a global trace clock is defined,
661 // timestamping TraceEvents's with this value guarantees synchronization
662 // between events collected inside chrome and events collected outside
663 // (e.g. kernel, X server).
664 //
665 // WARNING: The returned value might NOT have the same origin as Now(). Do not
666 // perform math between TimeTicks values returned by Now() and
667 // NowFromSystemTraceTime() and expect meaningful results.
668 // TODO(miu): Since the timeline of these values is different, the values
669 // should be of a different type.
670 static TimeTicks NowFromSystemTraceTime();
671
672 #if defined(OS_WIN) 642 #if defined(OS_WIN)
673 // Translates an absolute QPC timestamp into a TimeTicks value. The returned 643 // 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 644 // value has the same origin as Now(). Do NOT attempt to use this if
675 // IsHighResolution() returns false. 645 // IsHighResolution() returns false.
676 static TimeTicks FromQPCValue(LONGLONG qpc_value); 646 static TimeTicks FromQPCValue(LONGLONG qpc_value);
677 #endif 647 #endif
678 648
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 649 // 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. 650 // 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 651 // 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 652 // 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 653 // value for the duration of the application, but will be different in future
702 // application runs. 654 // application runs.
703 static TimeTicks UnixEpoch(); 655 static TimeTicks UnixEpoch();
704 656
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 657 // Returns |this| snapped to the next tick, given a |tick_phase| and
712 // repeating |tick_interval| in both directions. |this| may be before, 658 // repeating |tick_interval| in both directions. |this| may be before,
713 // after, or equal to the |tick_phase|. 659 // after, or equal to the |tick_phase|.
714 TimeTicks SnappedToNextTick(TimeTicks tick_phase, 660 TimeTicks SnappedToNextTick(TimeTicks tick_phase,
715 TimeDelta tick_interval) const; 661 TimeDelta tick_interval) const;
716 662
717 TimeTicks& operator=(TimeTicks other) { 663 #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: 664 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); 665 typedef DWORD (*TickFunctionType)(void);
778 static TickFunctionType SetMockTickFunction(TickFunctionType ticker); 666 static TickFunctionType SetMockTickFunction(TickFunctionType ticker);
779 #endif 667 #endif
668
669 private:
670 friend class time_internal::TimeBase<TimeTicks>;
671
672 // Please use Now() to create a new object. This is for internal use
673 // and testing.
674 explicit TimeTicks(int64 us) : TimeBase(us) {
675 }
780 }; 676 };
781 677
782 inline TimeTicks TimeDelta::operator+(TimeTicks t) const {
783 return TimeTicks(SaturatedAdd(t.ticks_));
784 }
785
786 // For logging use only. 678 // For logging use only.
787 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks); 679 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks);
788 680
681 // ThreadTicks ----------------------------------------------------------------
682
683 // Represents a clock, specific to a particular thread, than runs only while the
684 // thread is running.
685 class BASE_EXPORT ThreadTicks : public time_internal::TimeBase<ThreadTicks> {
686 public:
687 ThreadTicks() : TimeBase(0) {
688 }
689
690 // Returns true if ThreadTicks::Now() is supported on this system.
691 static bool IsSupported() {
692 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \
693 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID)
694 return true;
695 #else
696 return false;
697 #endif
698 }
699
700 // Returns thread-specific CPU-time on systems that support this feature.
701 // Needs to be guarded with a call to IsSupported(). Use this timer
702 // to (approximately) measure how much time the calling thread spent doing
703 // actual work vs. being de-scheduled. May return bogus results if the thread
704 // migrates to another CPU between two calls.
705 static ThreadTicks Now();
706
707 private:
708 friend class time_internal::TimeBase<ThreadTicks>;
709
710 // Please use Now() to create a new object. This is for internal use
711 // and testing.
712 explicit ThreadTicks(int64 us) : TimeBase(us) {
713 }
714 };
715
716 // For logging use only.
717 BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks);
718
719 // TraceTicks ----------------------------------------------------------------
720
721 // Represents high-resolution system trace clock time.
722 class BASE_EXPORT TraceTicks : public time_internal::TimeBase<TraceTicks> {
723 public:
724 // We define this even without OS_CHROMEOS for seccomp sandbox testing.
725 #if defined(OS_LINUX)
726 // Force definition of the system trace clock; it is a chromeos-only api
727 // at the moment and surfacing it in the right place requires mucking
728 // with glibc et al.
729 static const clockid_t kClockSystemTrace = 11;
730 #endif
731
732 TraceTicks() : TimeBase(0) {
733 }
734
735 // Returns the current system trace time or, if not available on this
736 // platform, a high-resolution time value; or a low-resolution time value if
737 // neither are avalable. On systems where a global trace clock is defined,
738 // timestamping TraceEvents's with this value guarantees synchronization
739 // between events collected inside chrome and events collected outside
740 // (e.g. kernel, X server).
741 static TraceTicks Now();
742
743 private:
744 friend class time_internal::TimeBase<TraceTicks>;
745
746 // Please use Now() to create a new object. This is for internal use
747 // and testing.
748 explicit TraceTicks(int64 us) : TimeBase(us) {
749 }
750 };
751
752 // For logging use only.
753 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TraceTicks time_ticks);
754
789 } // namespace base 755 } // namespace base
790 756
791 #endif // BASE_TIME_TIME_H_ 757 #endif // BASE_TIME_TIME_H_
OLDNEW
« no previous file with comments | « base/threading/thread_perftest.cc ('k') | base/time/time.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698