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 time, internally represented as | 5 // Time represents an absolute point in time, internally represented as |
6 // microseconds (s/1,000,000) since the Windows epoch (1601-01-01 00:00:00 UTC) | 6 // microseconds (s/1,000,000) since the Windows epoch (1601-01-01 00:00:00 UTC) |
7 // (See http://crbug.com/14734). System-dependent clock interface routines are | 7 // (See http://crbug.com/14734). System-dependent clock interface routines are |
8 // defined in time_PLATFORM.cc. | 8 // 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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 class TimeTicks; | 54 class TimeTicks; |
55 | 55 |
56 // TimeDelta ------------------------------------------------------------------ | 56 // TimeDelta ------------------------------------------------------------------ |
57 | 57 |
58 class BASE_EXPORT TimeDelta { | 58 class BASE_EXPORT TimeDelta { |
59 public: | 59 public: |
60 TimeDelta() : delta_(0) { | 60 TimeDelta() : delta_(0) { |
61 } | 61 } |
62 | 62 |
63 // Converts units of time to TimeDeltas. | 63 // Converts units of time to TimeDeltas. |
64 static TimeDelta FromDays(int64 days); | 64 static TimeDelta FromDays(int days); |
65 static TimeDelta FromHours(int64 hours); | 65 static TimeDelta FromHours(int hours); |
66 static TimeDelta FromMinutes(int64 minutes); | 66 static TimeDelta FromMinutes(int minutes); |
67 static TimeDelta FromSeconds(int64 secs); | 67 static TimeDelta FromSeconds(int64 secs); |
68 static TimeDelta FromMilliseconds(int64 ms); | 68 static TimeDelta FromMilliseconds(int64 ms); |
69 static TimeDelta FromMicroseconds(int64 us); | 69 static TimeDelta FromMicroseconds(int64 us); |
70 #if defined(OS_WIN) | 70 #if defined(OS_WIN) |
71 static TimeDelta FromQPCValue(LONGLONG qpc_value); | 71 static TimeDelta FromQPCValue(LONGLONG qpc_value); |
72 #endif | 72 #endif |
73 | 73 |
74 // Converts an integer value representing TimeDelta to a class. This is used | 74 // Converts an integer value representing TimeDelta to a class. This is used |
75 // when deserializing a |TimeDelta| structure, using a value known to be | 75 // when deserializing a |TimeDelta| structure, using a value known to be |
76 // compatible. It is not provided as a constructor because the integer type | 76 // compatible. It is not provided as a constructor because the integer type |
77 // may be unclear from the perspective of a caller. | 77 // may be unclear from the perspective of a caller. |
78 static TimeDelta FromInternalValue(int64 delta) { | 78 static TimeDelta FromInternalValue(int64 delta) { |
79 return TimeDelta(delta); | 79 return TimeDelta(delta); |
80 } | 80 } |
81 | 81 |
| 82 // Returns the maximum time delta, which should be greater than any reasonable |
| 83 // time delta we might compare it to. Adding or subtracting the maximum time |
| 84 // delta to a time or another time delta has an undefined result. |
| 85 static TimeDelta Max(); |
| 86 |
82 // Returns the internal numeric value of the TimeDelta object. Please don't | 87 // Returns the internal numeric value of the TimeDelta object. Please don't |
83 // use this and do arithmetic on it, as it is more error prone than using the | 88 // use this and do arithmetic on it, as it is more error prone than using the |
84 // provided operators. | 89 // provided operators. |
85 // For serializing, use FromInternalValue to reconstitute. | 90 // For serializing, use FromInternalValue to reconstitute. |
86 int64 ToInternalValue() const { | 91 int64 ToInternalValue() const { |
87 return delta_; | 92 return delta_; |
88 } | 93 } |
89 | 94 |
| 95 // Returns true if the time delta is the maximum time delta. |
| 96 bool is_max() const { |
| 97 return delta_ == std::numeric_limits<int64>::max(); |
| 98 } |
| 99 |
90 #if defined(OS_POSIX) | 100 #if defined(OS_POSIX) |
91 struct timespec ToTimeSpec() const; | 101 struct timespec ToTimeSpec() const; |
92 #endif | 102 #endif |
93 | 103 |
94 // Returns the time delta in some unit. The F versions return a floating | 104 // Returns the time delta in some unit. The F versions return a floating |
95 // point value, the "regular" versions return a rounded-down value. | 105 // point value, the "regular" versions return a rounded-down value. |
96 // | 106 // |
97 // InMillisecondsRoundedUp() instead returns an integer that is rounded up | 107 // InMillisecondsRoundedUp() instead returns an integer that is rounded up |
98 // to the next full millisecond. | 108 // to the next full millisecond. |
99 int InDays() const; | 109 int InDays() const; |
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 static int high_resolution_timer_activated_; | 496 static int high_resolution_timer_activated_; |
487 #endif | 497 #endif |
488 | 498 |
489 // Time in microseconds in UTC. | 499 // Time in microseconds in UTC. |
490 int64 us_; | 500 int64 us_; |
491 }; | 501 }; |
492 | 502 |
493 // Inline the TimeDelta factory methods, for fast TimeDelta construction. | 503 // Inline the TimeDelta factory methods, for fast TimeDelta construction. |
494 | 504 |
495 // static | 505 // static |
496 inline TimeDelta TimeDelta::FromDays(int64 days) { | 506 inline TimeDelta TimeDelta::FromDays(int days) { |
| 507 // Preserve max to prevent overflow. |
| 508 if (days == std::numeric_limits<int>::max()) |
| 509 return Max(); |
497 return TimeDelta(days * Time::kMicrosecondsPerDay); | 510 return TimeDelta(days * Time::kMicrosecondsPerDay); |
498 } | 511 } |
499 | 512 |
500 // static | 513 // static |
501 inline TimeDelta TimeDelta::FromHours(int64 hours) { | 514 inline TimeDelta TimeDelta::FromHours(int hours) { |
| 515 // Preserve max to prevent overflow. |
| 516 if (hours == std::numeric_limits<int>::max()) |
| 517 return Max(); |
502 return TimeDelta(hours * Time::kMicrosecondsPerHour); | 518 return TimeDelta(hours * Time::kMicrosecondsPerHour); |
503 } | 519 } |
504 | 520 |
505 // static | 521 // static |
506 inline TimeDelta TimeDelta::FromMinutes(int64 minutes) { | 522 inline TimeDelta TimeDelta::FromMinutes(int minutes) { |
| 523 // Preserve max to prevent overflow. |
| 524 if (minutes == std::numeric_limits<int>::max()) |
| 525 return Max(); |
507 return TimeDelta(minutes * Time::kMicrosecondsPerMinute); | 526 return TimeDelta(minutes * Time::kMicrosecondsPerMinute); |
508 } | 527 } |
509 | 528 |
510 // static | 529 // static |
511 inline TimeDelta TimeDelta::FromSeconds(int64 secs) { | 530 inline TimeDelta TimeDelta::FromSeconds(int64 secs) { |
| 531 // Preserve max to prevent overflow. |
| 532 if (secs == std::numeric_limits<int64>::max()) |
| 533 return Max(); |
512 return TimeDelta(secs * Time::kMicrosecondsPerSecond); | 534 return TimeDelta(secs * Time::kMicrosecondsPerSecond); |
513 } | 535 } |
514 | 536 |
515 // static | 537 // static |
516 inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) { | 538 inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) { |
| 539 // Preserve max to prevent overflow. |
| 540 if (ms == std::numeric_limits<int64>::max()) |
| 541 return Max(); |
517 return TimeDelta(ms * Time::kMicrosecondsPerMillisecond); | 542 return TimeDelta(ms * Time::kMicrosecondsPerMillisecond); |
518 } | 543 } |
519 | 544 |
520 // static | 545 // static |
521 inline TimeDelta TimeDelta::FromMicroseconds(int64 us) { | 546 inline TimeDelta TimeDelta::FromMicroseconds(int64 us) { |
| 547 // Preserve max to prevent overflow. |
| 548 if (us == std::numeric_limits<int64>::max()) |
| 549 return Max(); |
522 return TimeDelta(us); | 550 return TimeDelta(us); |
523 } | 551 } |
524 | 552 |
525 inline Time TimeDelta::operator+(Time t) const { | 553 inline Time TimeDelta::operator+(Time t) const { |
526 return Time(t.us_ + delta_); | 554 return Time(t.us_ + delta_); |
527 } | 555 } |
528 | 556 |
529 // TimeTicks ------------------------------------------------------------------ | 557 // TimeTicks ------------------------------------------------------------------ |
530 | 558 |
531 class BASE_EXPORT TimeTicks { | 559 class BASE_EXPORT TimeTicks { |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
684 #endif | 712 #endif |
685 }; | 713 }; |
686 | 714 |
687 inline TimeTicks TimeDelta::operator+(TimeTicks t) const { | 715 inline TimeTicks TimeDelta::operator+(TimeTicks t) const { |
688 return TimeTicks(t.ticks_ + delta_); | 716 return TimeTicks(t.ticks_ + delta_); |
689 } | 717 } |
690 | 718 |
691 } // namespace base | 719 } // namespace base |
692 | 720 |
693 #endif // BASE_TIME_TIME_H_ | 721 #endif // BASE_TIME_TIME_H_ |
OLD | NEW |