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 | |
270 // Delta in microseconds. | 267 // Delta in microseconds. |
271 int64 delta_; | 268 int64 delta_; |
272 }; | 269 }; |
273 | 270 |
274 template<typename T> | 271 template<typename T> |
275 inline TimeDelta operator*(T a, TimeDelta td) { | 272 inline TimeDelta operator*(T a, TimeDelta td) { |
276 return td * a; | 273 return td * a; |
277 } | 274 } |
278 | 275 |
279 // For logging use only. | 276 // For logging use only. |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 // timezone is not specified. | 590 // timezone is not specified. |
594 static bool FromStringInternal(const char* time_string, | 591 static bool FromStringInternal(const char* time_string, |
595 bool is_local, | 592 bool is_local, |
596 Time* parsed_time); | 593 Time* parsed_time); |
597 }; | 594 }; |
598 | 595 |
599 // Inline the TimeDelta factory methods, for fast TimeDelta construction. | 596 // Inline the TimeDelta factory methods, for fast TimeDelta construction. |
600 | 597 |
601 // static | 598 // static |
602 inline TimeDelta TimeDelta::FromDays(int days) { | 599 inline TimeDelta TimeDelta::FromDays(int days) { |
| 600 // Preserve max to prevent overflow. |
603 if (days == std::numeric_limits<int>::max()) | 601 if (days == std::numeric_limits<int>::max()) |
604 return Max(); | 602 return Max(); |
605 return TimeDelta(days * Time::kMicrosecondsPerDay); | 603 return TimeDelta(days * Time::kMicrosecondsPerDay); |
606 } | 604 } |
607 | 605 |
608 // static | 606 // static |
609 inline TimeDelta TimeDelta::FromHours(int hours) { | 607 inline TimeDelta TimeDelta::FromHours(int hours) { |
| 608 // Preserve max to prevent overflow. |
610 if (hours == std::numeric_limits<int>::max()) | 609 if (hours == std::numeric_limits<int>::max()) |
611 return Max(); | 610 return Max(); |
612 return TimeDelta(hours * Time::kMicrosecondsPerHour); | 611 return TimeDelta(hours * Time::kMicrosecondsPerHour); |
613 } | 612 } |
614 | 613 |
615 // static | 614 // static |
616 inline TimeDelta TimeDelta::FromMinutes(int minutes) { | 615 inline TimeDelta TimeDelta::FromMinutes(int minutes) { |
| 616 // Preserve max to prevent overflow. |
617 if (minutes == std::numeric_limits<int>::max()) | 617 if (minutes == std::numeric_limits<int>::max()) |
618 return Max(); | 618 return Max(); |
619 return TimeDelta(minutes * Time::kMicrosecondsPerMinute); | 619 return TimeDelta(minutes * Time::kMicrosecondsPerMinute); |
620 } | 620 } |
621 | 621 |
622 // static | 622 // static |
623 inline TimeDelta TimeDelta::FromSeconds(int64 secs) { | 623 inline TimeDelta TimeDelta::FromSeconds(int64 secs) { |
624 return TimeDelta(secs) * Time::kMicrosecondsPerSecond; | 624 // Preserve max to prevent overflow. |
| 625 if (secs == std::numeric_limits<int64>::max()) |
| 626 return Max(); |
| 627 return TimeDelta(secs * Time::kMicrosecondsPerSecond); |
625 } | 628 } |
626 | 629 |
627 // static | 630 // static |
628 inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) { | 631 inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) { |
629 return TimeDelta(ms) * Time::kMicrosecondsPerMillisecond; | 632 // Preserve max to prevent overflow. |
| 633 if (ms == std::numeric_limits<int64>::max()) |
| 634 return Max(); |
| 635 return TimeDelta(ms * Time::kMicrosecondsPerMillisecond); |
630 } | 636 } |
631 | 637 |
632 // static | 638 // static |
633 inline TimeDelta TimeDelta::FromSecondsD(double secs) { | 639 inline TimeDelta TimeDelta::FromSecondsD(double secs) { |
634 return FromDouble(secs * Time::kMicrosecondsPerSecond); | 640 // Preserve max to prevent overflow. |
| 641 if (secs == std::numeric_limits<double>::infinity()) |
| 642 return Max(); |
| 643 return TimeDelta(static_cast<int64>(secs * Time::kMicrosecondsPerSecond)); |
635 } | 644 } |
636 | 645 |
637 // static | 646 // static |
638 inline TimeDelta TimeDelta::FromMillisecondsD(double ms) { | 647 inline TimeDelta TimeDelta::FromMillisecondsD(double ms) { |
639 return FromDouble(ms * Time::kMicrosecondsPerMillisecond); | 648 // Preserve max to prevent overflow. |
| 649 if (ms == std::numeric_limits<double>::infinity()) |
| 650 return Max(); |
| 651 return TimeDelta(static_cast<int64>(ms * Time::kMicrosecondsPerMillisecond)); |
640 } | 652 } |
641 | 653 |
642 // static | 654 // static |
643 inline TimeDelta TimeDelta::FromMicroseconds(int64 us) { | 655 inline TimeDelta TimeDelta::FromMicroseconds(int64 us) { |
| 656 // Preserve max to prevent overflow. |
| 657 if (us == std::numeric_limits<int64>::max()) |
| 658 return Max(); |
644 return TimeDelta(us); | 659 return TimeDelta(us); |
645 } | 660 } |
646 | 661 |
647 // static | |
648 inline TimeDelta TimeDelta::FromDouble(double value) { | |
649 double max_magnitude = std::numeric_limits<int64>::max(); | |
650 TimeDelta delta = TimeDelta(static_cast<int64>(value)); | |
651 if (value > max_magnitude) | |
652 delta = Max(); | |
653 else if (value < -max_magnitude) | |
654 delta = -Max(); | |
655 return delta; | |
656 } | |
657 | |
658 // For logging use only. | 662 // For logging use only. |
659 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); | 663 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); |
660 | 664 |
661 // TimeTicks ------------------------------------------------------------------ | 665 // TimeTicks ------------------------------------------------------------------ |
662 | 666 |
663 // Represents monotonically non-decreasing clock time. | 667 // Represents monotonically non-decreasing clock time. |
664 class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> { | 668 class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> { |
665 public: | 669 public: |
666 TimeTicks() : TimeBase(0) { | 670 TimeTicks() : TimeBase(0) { |
667 } | 671 } |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
797 explicit TraceTicks(int64 us) : TimeBase(us) { | 801 explicit TraceTicks(int64 us) : TimeBase(us) { |
798 } | 802 } |
799 }; | 803 }; |
800 | 804 |
801 // For logging use only. | 805 // For logging use only. |
802 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TraceTicks time_ticks); | 806 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TraceTicks time_ticks); |
803 | 807 |
804 } // namespace base | 808 } // namespace base |
805 | 809 |
806 #endif // BASE_TIME_TIME_H_ | 810 #endif // BASE_TIME_TIME_H_ |
OLD | NEW |