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. | |
601 if (days == std::numeric_limits<int>::max()) | 603 if (days == std::numeric_limits<int>::max()) |
602 return Max(); | 604 return Max(); |
603 return TimeDelta(days * Time::kMicrosecondsPerDay); | 605 return TimeDelta(days * Time::kMicrosecondsPerDay); |
604 } | 606 } |
605 | 607 |
606 // static | 608 // static |
607 inline TimeDelta TimeDelta::FromHours(int hours) { | 609 inline TimeDelta TimeDelta::FromHours(int hours) { |
608 // Preserve max to prevent overflow. | |
609 if (hours == std::numeric_limits<int>::max()) | 610 if (hours == std::numeric_limits<int>::max()) |
610 return Max(); | 611 return Max(); |
611 return TimeDelta(hours * Time::kMicrosecondsPerHour); | 612 return TimeDelta(hours * Time::kMicrosecondsPerHour); |
612 } | 613 } |
613 | 614 |
614 // static | 615 // static |
615 inline TimeDelta TimeDelta::FromMinutes(int minutes) { | 616 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 // Preserve max to prevent overflow. | 624 return TimeDelta(secs) * Time::kMicrosecondsPerSecond; |
625 if (secs == std::numeric_limits<int64>::max()) | |
626 return Max(); | |
627 return TimeDelta(secs * Time::kMicrosecondsPerSecond); | |
628 } | 625 } |
629 | 626 |
630 // static | 627 // static |
631 inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) { | 628 inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) { |
632 // Preserve max to prevent overflow. | 629 return TimeDelta(ms) * Time::kMicrosecondsPerMillisecond; |
633 if (ms == std::numeric_limits<int64>::max()) | |
634 return Max(); | |
635 return TimeDelta(ms * Time::kMicrosecondsPerMillisecond); | |
636 } | 630 } |
637 | 631 |
638 // static | 632 // static |
639 inline TimeDelta TimeDelta::FromSecondsD(double secs) { | 633 inline TimeDelta TimeDelta::FromSecondsD(double secs) { |
640 // Preserve max to prevent overflow. | 634 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 } | 635 } |
645 | 636 |
646 // static | 637 // static |
647 inline TimeDelta TimeDelta::FromMillisecondsD(double ms) { | 638 inline TimeDelta TimeDelta::FromMillisecondsD(double ms) { |
648 // Preserve max to prevent overflow. | 639 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 } | 640 } |
653 | 641 |
654 // static | 642 // static |
655 inline TimeDelta TimeDelta::FromMicroseconds(int64 us) { | 643 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); | 644 return TimeDelta(us); |
660 } | 645 } |
661 | 646 |
| 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 |
662 // For logging use only. | 658 // For logging use only. |
663 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); | 659 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); |
664 | 660 |
665 // TimeTicks ------------------------------------------------------------------ | 661 // TimeTicks ------------------------------------------------------------------ |
666 | 662 |
667 // Represents monotonically non-decreasing clock time. | 663 // Represents monotonically non-decreasing clock time. |
668 class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> { | 664 class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> { |
669 public: | 665 public: |
670 TimeTicks() : TimeBase(0) { | 666 TimeTicks() : TimeBase(0) { |
671 } | 667 } |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
801 explicit TraceTicks(int64 us) : TimeBase(us) { | 797 explicit TraceTicks(int64 us) : TimeBase(us) { |
802 } | 798 } |
803 }; | 799 }; |
804 | 800 |
805 // For logging use only. | 801 // For logging use only. |
806 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TraceTicks time_ticks); | 802 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TraceTicks time_ticks); |
807 | 803 |
808 } // namespace base | 804 } // namespace base |
809 | 805 |
810 #endif // BASE_TIME_TIME_H_ | 806 #endif // BASE_TIME_TIME_H_ |
OLD | NEW |