Chromium Code Reviews| 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 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 private: | 257 private: |
| 258 friend int64 time_internal::SaturatedAdd(TimeDelta delta, int64 value); | 258 friend int64 time_internal::SaturatedAdd(TimeDelta delta, int64 value); |
| 259 friend int64 time_internal::SaturatedSub(TimeDelta delta, int64 value); | 259 friend int64 time_internal::SaturatedSub(TimeDelta delta, int64 value); |
| 260 | 260 |
| 261 // Constructs a delta given the duration in microseconds. This is private | 261 // Constructs a delta given the duration in microseconds. This is private |
| 262 // to avoid confusion by callers with an integer constructor. Use | 262 // to avoid confusion by callers with an integer constructor. Use |
| 263 // FromSeconds, FromMilliseconds, etc. instead. | 263 // FromSeconds, FromMilliseconds, etc. instead. |
| 264 explicit TimeDelta(int64 delta_us) : delta_(delta_us) { | 264 explicit TimeDelta(int64 delta_us) : delta_(delta_us) { |
| 265 } | 265 } |
| 266 | 266 |
| 267 // Private method to build a delta from a double. | |
| 268 static TimeDelta FromDouble(double value); | |
| 269 | |
| 267 // Delta in microseconds. | 270 // Delta in microseconds. |
| 268 int64 delta_; | 271 int64 delta_; |
| 269 }; | 272 }; |
| 270 | 273 |
| 271 template<typename T> | 274 template<typename T> |
| 272 inline TimeDelta operator*(T a, TimeDelta td) { | 275 inline TimeDelta operator*(T a, TimeDelta td) { |
| 273 return td * a; | 276 return td * a; |
| 274 } | 277 } |
| 275 | 278 |
| 276 // For logging use only. | 279 // For logging use only. |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 590 // timezone is not specified. | 593 // timezone is not specified. |
| 591 static bool FromStringInternal(const char* time_string, | 594 static bool FromStringInternal(const char* time_string, |
| 592 bool is_local, | 595 bool is_local, |
| 593 Time* parsed_time); | 596 Time* parsed_time); |
| 594 }; | 597 }; |
| 595 | 598 |
| 596 // Inline the TimeDelta factory methods, for fast TimeDelta construction. | 599 // Inline the TimeDelta factory methods, for fast TimeDelta construction. |
| 597 | 600 |
| 598 // static | 601 // static |
| 599 inline TimeDelta TimeDelta::FromDays(int days) { | 602 inline TimeDelta TimeDelta::FromDays(int days) { |
| 600 // Preserve max to prevent overflow. | 603 // Special-case max. |
|
Nico
2015/07/17 17:00:23
The previous comment answered "why?". From what I
rvargas (doing something else)
2015/07/17 19:18:10
yeah... I actually don't like this. FromDays(max_i
Nico
2015/07/17 20:21:20
Hm, maybe just remove the comment then I guess. As
| |
| 601 if (days == std::numeric_limits<int>::max()) | 604 if (days == std::numeric_limits<int>::max()) |
| 602 return Max(); | 605 return Max(); |
| 603 return TimeDelta(days * Time::kMicrosecondsPerDay); | 606 return TimeDelta(days * Time::kMicrosecondsPerDay); |
|
Nico
2015/07/17 17:00:23
doesn't days * Time::kMicrosecondsPerDay overflow
| |
| 604 } | 607 } |
| 605 | 608 |
| 606 // static | 609 // static |
| 607 inline TimeDelta TimeDelta::FromHours(int hours) { | 610 inline TimeDelta TimeDelta::FromHours(int hours) { |
| 608 // Preserve max to prevent overflow. | 611 // Special-case max. |
| 609 if (hours == std::numeric_limits<int>::max()) | 612 if (hours == std::numeric_limits<int>::max()) |
| 610 return Max(); | 613 return Max(); |
| 611 return TimeDelta(hours * Time::kMicrosecondsPerHour); | 614 return TimeDelta(hours * Time::kMicrosecondsPerHour); |
| 612 } | 615 } |
| 613 | 616 |
| 614 // static | 617 // static |
| 615 inline TimeDelta TimeDelta::FromMinutes(int minutes) { | 618 inline TimeDelta TimeDelta::FromMinutes(int minutes) { |
| 616 // Preserve max to prevent overflow. | 619 // Special-case max. |
| 617 if (minutes == std::numeric_limits<int>::max()) | 620 if (minutes == std::numeric_limits<int>::max()) |
| 618 return Max(); | 621 return Max(); |
| 619 return TimeDelta(minutes * Time::kMicrosecondsPerMinute); | 622 return TimeDelta(minutes * Time::kMicrosecondsPerMinute); |
| 620 } | 623 } |
| 621 | 624 |
| 622 // static | 625 // static |
| 623 inline TimeDelta TimeDelta::FromSeconds(int64 secs) { | 626 inline TimeDelta TimeDelta::FromSeconds(int64 secs) { |
| 624 // Preserve max to prevent overflow. | 627 return TimeDelta(secs) * Time::kMicrosecondsPerSecond; |
|
Nico
2015/07/17 17:00:23
Does TimeDelta have a saturating operator*?
rvargas (doing something else)
2015/07/17 19:18:10
Yes, all TimeDelta operators should do the right t
| |
| 625 if (secs == std::numeric_limits<int64>::max()) | |
| 626 return Max(); | |
| 627 return TimeDelta(secs * Time::kMicrosecondsPerSecond); | |
| 628 } | 628 } |
| 629 | 629 |
| 630 // static | 630 // static |
| 631 inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) { | 631 inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) { |
| 632 // Preserve max to prevent overflow. | 632 return TimeDelta(ms) * Time::kMicrosecondsPerMillisecond; |
| 633 if (ms == std::numeric_limits<int64>::max()) | |
| 634 return Max(); | |
| 635 return TimeDelta(ms * Time::kMicrosecondsPerMillisecond); | |
| 636 } | 633 } |
| 637 | 634 |
| 638 // static | 635 // static |
| 639 inline TimeDelta TimeDelta::FromSecondsD(double secs) { | 636 inline TimeDelta TimeDelta::FromSecondsD(double secs) { |
| 640 // Preserve max to prevent overflow. | 637 return FromDouble(secs * Time::kMicrosecondsPerSecond); |
| 641 if (secs == std::numeric_limits<double>::infinity()) | |
| 642 return Max(); | |
| 643 return TimeDelta(static_cast<int64>(secs * Time::kMicrosecondsPerSecond)); | |
| 644 } | 638 } |
| 645 | 639 |
| 646 // static | 640 // static |
| 647 inline TimeDelta TimeDelta::FromMillisecondsD(double ms) { | 641 inline TimeDelta TimeDelta::FromMillisecondsD(double ms) { |
| 648 // Preserve max to prevent overflow. | 642 return FromDouble(ms * Time::kMicrosecondsPerMillisecond); |
| 649 if (ms == std::numeric_limits<double>::infinity()) | |
| 650 return Max(); | |
| 651 return TimeDelta(static_cast<int64>(ms * Time::kMicrosecondsPerMillisecond)); | |
| 652 } | 643 } |
| 653 | 644 |
| 654 // static | 645 // static |
| 655 inline TimeDelta TimeDelta::FromMicroseconds(int64 us) { | 646 inline TimeDelta TimeDelta::FromMicroseconds(int64 us) { |
| 656 // Preserve max to prevent overflow. | |
| 657 if (us == std::numeric_limits<int64>::max()) | |
| 658 return Max(); | |
| 659 return TimeDelta(us); | 647 return TimeDelta(us); |
| 660 } | 648 } |
| 661 | 649 |
| 650 // static | |
| 651 inline TimeDelta TimeDelta::FromDouble(double value) { | |
| 652 double max_magnitude = std::numeric_limits<int64>::max(); | |
| 653 TimeDelta delta = TimeDelta(static_cast<int64>(value)); | |
| 654 if (value > max_magnitude) | |
| 655 delta = Max(); | |
| 656 else if (value < -max_magnitude) | |
| 657 delta = -Max(); | |
| 658 return delta; | |
| 659 } | |
| 660 | |
| 662 // For logging use only. | 661 // For logging use only. |
| 663 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); | 662 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); |
| 664 | 663 |
| 665 // TimeTicks ------------------------------------------------------------------ | 664 // TimeTicks ------------------------------------------------------------------ |
| 666 | 665 |
| 667 // Represents monotonically non-decreasing clock time. | 666 // Represents monotonically non-decreasing clock time. |
| 668 class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> { | 667 class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> { |
| 669 public: | 668 public: |
| 670 TimeTicks() : TimeBase(0) { | 669 TimeTicks() : TimeBase(0) { |
| 671 } | 670 } |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 801 explicit TraceTicks(int64 us) : TimeBase(us) { | 800 explicit TraceTicks(int64 us) : TimeBase(us) { |
| 802 } | 801 } |
| 803 }; | 802 }; |
| 804 | 803 |
| 805 // For logging use only. | 804 // For logging use only. |
| 806 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TraceTicks time_ticks); | 805 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TraceTicks time_ticks); |
| 807 | 806 |
| 808 } // namespace base | 807 } // namespace base |
| 809 | 808 |
| 810 #endif // BASE_TIME_TIME_H_ | 809 #endif // BASE_TIME_TIME_H_ |
| OLD | NEW |