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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
99 } // namespace time_internal | 99 } // namespace time_internal |
100 | 100 |
101 // TimeDelta ------------------------------------------------------------------ | 101 // TimeDelta ------------------------------------------------------------------ |
102 | 102 |
103 class BASE_EXPORT TimeDelta { | 103 class BASE_EXPORT TimeDelta { |
104 public: | 104 public: |
105 TimeDelta() : delta_(0) { | 105 TimeDelta() : delta_(0) { |
106 } | 106 } |
107 | 107 |
108 // Converts units of time to TimeDeltas. | 108 // Converts units of time to TimeDeltas. |
109 static TimeDelta FromDays(int days); | 109 static constexpr TimeDelta FromDays(int days); |
110 static TimeDelta FromHours(int hours); | 110 static constexpr TimeDelta FromHours(int hours); |
111 static TimeDelta FromMinutes(int minutes); | 111 static constexpr TimeDelta FromMinutes(int minutes); |
112 static TimeDelta FromSeconds(int64_t secs); | 112 static constexpr TimeDelta FromSeconds(int64_t secs); |
113 static TimeDelta FromMilliseconds(int64_t ms); | 113 static constexpr TimeDelta FromMilliseconds(int64_t ms); |
114 static TimeDelta FromSecondsD(double secs); | 114 static constexpr TimeDelta FromSecondsD(double secs); |
115 static TimeDelta FromMillisecondsD(double ms); | 115 static constexpr TimeDelta FromMillisecondsD(double ms); |
116 static TimeDelta FromMicroseconds(int64_t us); | 116 static constexpr TimeDelta FromMicroseconds(int64_t us); |
117 #if defined(OS_WIN) | 117 #if defined(OS_WIN) |
118 static TimeDelta FromQPCValue(LONGLONG qpc_value); | 118 static TimeDelta FromQPCValue(LONGLONG qpc_value); |
119 #endif | 119 #endif |
120 | 120 |
121 // Converts an integer value representing TimeDelta to a class. This is used | 121 // Converts an integer value representing TimeDelta to a class. This is used |
122 // when deserializing a |TimeDelta| structure, using a value known to be | 122 // when deserializing a |TimeDelta| structure, using a value known to be |
123 // compatible. It is not provided as a constructor because the integer type | 123 // compatible. It is not provided as a constructor because the integer type |
124 // may be unclear from the perspective of a caller. | 124 // may be unclear from the perspective of a caller. |
125 static TimeDelta FromInternalValue(int64_t delta) { return TimeDelta(delta); } | 125 static TimeDelta FromInternalValue(int64_t delta) { return TimeDelta(delta); } |
126 | 126 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 TimeDelta& operator/=(T a) { | 215 TimeDelta& operator/=(T a) { |
216 return *this = (*this / a); | 216 return *this = (*this / a); |
217 } | 217 } |
218 | 218 |
219 int64_t operator/(TimeDelta a) const { return delta_ / a.delta_; } | 219 int64_t operator/(TimeDelta a) const { return delta_ / a.delta_; } |
220 TimeDelta operator%(TimeDelta a) const { | 220 TimeDelta operator%(TimeDelta a) const { |
221 return TimeDelta(delta_ % a.delta_); | 221 return TimeDelta(delta_ % a.delta_); |
222 } | 222 } |
223 | 223 |
224 // Comparison operators. | 224 // Comparison operators. |
225 bool operator==(TimeDelta other) const { | 225 constexpr bool operator==(TimeDelta other) const { |
226 return delta_ == other.delta_; | 226 return delta_ == other.delta_; |
227 } | 227 } |
228 bool operator!=(TimeDelta other) const { | 228 constexpr bool operator!=(TimeDelta other) const { |
229 return delta_ != other.delta_; | 229 return delta_ != other.delta_; |
230 } | 230 } |
231 bool operator<(TimeDelta other) const { | 231 constexpr bool operator<(TimeDelta other) const { |
232 return delta_ < other.delta_; | 232 return delta_ < other.delta_; |
233 } | 233 } |
234 bool operator<=(TimeDelta other) const { | 234 constexpr bool operator<=(TimeDelta other) const { |
235 return delta_ <= other.delta_; | 235 return delta_ <= other.delta_; |
236 } | 236 } |
237 bool operator>(TimeDelta other) const { | 237 constexpr bool operator>(TimeDelta other) const { |
238 return delta_ > other.delta_; | 238 return delta_ > other.delta_; |
239 } | 239 } |
240 bool operator>=(TimeDelta other) const { | 240 constexpr bool operator>=(TimeDelta other) const { |
241 return delta_ >= other.delta_; | 241 return delta_ >= other.delta_; |
242 } | 242 } |
243 | 243 |
244 private: | 244 private: |
245 friend int64_t time_internal::SaturatedAdd(TimeDelta delta, int64_t value); | 245 friend int64_t time_internal::SaturatedAdd(TimeDelta delta, int64_t value); |
246 friend int64_t time_internal::SaturatedSub(TimeDelta delta, int64_t value); | 246 friend int64_t time_internal::SaturatedSub(TimeDelta delta, int64_t value); |
247 | 247 |
248 // Constructs a delta given the duration in microseconds. This is private | 248 // Constructs a delta given the duration in microseconds. This is private |
249 // to avoid confusion by callers with an integer constructor. Use | 249 // to avoid confusion by callers with an integer constructor. Use |
250 // FromSeconds, FromMilliseconds, etc. instead. | 250 // FromSeconds, FromMilliseconds, etc. instead. |
251 explicit TimeDelta(int64_t delta_us) : delta_(delta_us) {} | 251 constexpr explicit TimeDelta(int64_t delta_us) : delta_(delta_us) {} |
252 | 252 |
253 // Private method to build a delta from a double. | 253 // Private method to build a delta from a double. |
254 static TimeDelta FromDouble(double value); | 254 static constexpr TimeDelta FromDouble(double value); |
255 | |
256 // Private method to build a delta from the product of a user-provided value | |
257 // and one of the kMicrosecondsPer constants. | |
258 static constexpr TimeDelta FromProduct(int64_t value, int microseconds_per); | |
255 | 259 |
256 // Delta in microseconds. | 260 // Delta in microseconds. |
257 int64_t delta_; | 261 int64_t delta_; |
258 }; | 262 }; |
259 | 263 |
260 template<typename T> | 264 template<typename T> |
261 inline TimeDelta operator*(T a, TimeDelta td) { | 265 inline TimeDelta operator*(T a, TimeDelta td) { |
262 return td * a; | 266 return td * a; |
263 } | 267 } |
264 | 268 |
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
571 // (e.g. "UTC" which is not specified in RFC822) is treated as if the | 575 // (e.g. "UTC" which is not specified in RFC822) is treated as if the |
572 // timezone is not specified. | 576 // timezone is not specified. |
573 static bool FromStringInternal(const char* time_string, | 577 static bool FromStringInternal(const char* time_string, |
574 bool is_local, | 578 bool is_local, |
575 Time* parsed_time); | 579 Time* parsed_time); |
576 }; | 580 }; |
577 | 581 |
578 // Inline the TimeDelta factory methods, for fast TimeDelta construction. | 582 // Inline the TimeDelta factory methods, for fast TimeDelta construction. |
579 | 583 |
580 // static | 584 // static |
581 inline TimeDelta TimeDelta::FromDays(int days) { | 585 constexpr inline TimeDelta TimeDelta::FromDays(int days) { |
582 if (days == std::numeric_limits<int>::max()) | 586 return days == std::numeric_limits<int>::max() |
583 return Max(); | 587 ? Max() |
584 return TimeDelta(days * Time::kMicrosecondsPerDay); | 588 : TimeDelta(days * Time::kMicrosecondsPerDay); |
585 } | 589 } |
586 | 590 |
587 // static | 591 // static |
588 inline TimeDelta TimeDelta::FromHours(int hours) { | 592 constexpr inline TimeDelta TimeDelta::FromHours(int hours) { |
589 if (hours == std::numeric_limits<int>::max()) | 593 return hours == std::numeric_limits<int>::max() |
590 return Max(); | 594 ? Max() |
591 return TimeDelta(hours * Time::kMicrosecondsPerHour); | 595 : TimeDelta(hours * Time::kMicrosecondsPerHour); |
592 } | 596 } |
593 | 597 |
594 // static | 598 // static |
595 inline TimeDelta TimeDelta::FromMinutes(int minutes) { | 599 constexpr inline TimeDelta TimeDelta::FromMinutes(int minutes) { |
596 if (minutes == std::numeric_limits<int>::max()) | 600 return minutes == std::numeric_limits<int>::max() |
597 return Max(); | 601 ? Max() |
598 return TimeDelta(minutes * Time::kMicrosecondsPerMinute); | 602 : TimeDelta(minutes * Time::kMicrosecondsPerMinute); |
599 } | 603 } |
600 | 604 |
601 // static | 605 // static |
602 inline TimeDelta TimeDelta::FromSeconds(int64_t secs) { | 606 constexpr inline TimeDelta TimeDelta::FromSeconds(int64_t secs) { |
603 return TimeDelta(secs) * Time::kMicrosecondsPerSecond; | 607 return FromProduct(secs, Time::kMicrosecondsPerSecond); |
604 } | 608 } |
605 | 609 |
606 // static | 610 // static |
607 inline TimeDelta TimeDelta::FromMilliseconds(int64_t ms) { | 611 constexpr inline TimeDelta TimeDelta::FromMilliseconds(int64_t ms) { |
608 return TimeDelta(ms) * Time::kMicrosecondsPerMillisecond; | 612 return FromProduct(ms, Time::kMicrosecondsPerMillisecond); |
609 } | 613 } |
610 | 614 |
611 // static | 615 // static |
612 inline TimeDelta TimeDelta::FromSecondsD(double secs) { | 616 constexpr inline TimeDelta TimeDelta::FromSecondsD(double secs) { |
613 return FromDouble(secs * Time::kMicrosecondsPerSecond); | 617 return FromDouble(secs * Time::kMicrosecondsPerSecond); |
614 } | 618 } |
615 | 619 |
616 // static | 620 // static |
617 inline TimeDelta TimeDelta::FromMillisecondsD(double ms) { | 621 constexpr inline TimeDelta TimeDelta::FromMillisecondsD(double ms) { |
618 return FromDouble(ms * Time::kMicrosecondsPerMillisecond); | 622 return FromDouble(ms * Time::kMicrosecondsPerMillisecond); |
619 } | 623 } |
620 | 624 |
621 // static | 625 // static |
622 inline TimeDelta TimeDelta::FromMicroseconds(int64_t us) { | 626 constexpr inline TimeDelta TimeDelta::FromMicroseconds(int64_t us) { |
623 return TimeDelta(us); | 627 return TimeDelta(us); |
624 } | 628 } |
625 | 629 |
626 // static | 630 // static |
627 inline TimeDelta TimeDelta::FromDouble(double value) { | 631 constexpr inline TimeDelta TimeDelta::FromDouble(double value) { |
628 double max_magnitude = std::numeric_limits<int64_t>::max(); | 632 // TODO(crbug.com/612601): Use saturated_cast<int64_t>(value) once we sort out |
629 TimeDelta delta = TimeDelta(static_cast<int64_t>(value)); | 633 // the Min() behavior. |
630 if (value > max_magnitude) | 634 return value > std::numeric_limits<int64_t>::max() |
631 delta = Max(); | 635 ? Max() |
632 else if (value < -max_magnitude) | 636 : value < -std::numeric_limits<int64_t>::max() |
633 delta = -Max(); | 637 ? -Max() |
634 return delta; | 638 : TimeDelta(static_cast<int64_t>(value)); |
639 } | |
640 | |
641 // static | |
642 constexpr inline TimeDelta TimeDelta::FromProduct(int64_t value, | |
643 int microseconds_per) { | |
danakj
2016/05/17 21:53:14
nit: maybe not microseconds_per, the microseconds_
Jeffrey Yasskin
2016/05/17 22:10:30
This code gets to be simpler than CheckedNumeric s
| |
644 return value > std::numeric_limits<int64_t>::max() / microseconds_per | |
645 ? Max() | |
646 : value < -std::numeric_limits<int64_t>::max() / microseconds_per | |
647 ? -Max() | |
648 : TimeDelta(value * microseconds_per); | |
635 } | 649 } |
636 | 650 |
637 // For logging use only. | 651 // For logging use only. |
638 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); | 652 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); |
639 | 653 |
640 // TimeTicks ------------------------------------------------------------------ | 654 // TimeTicks ------------------------------------------------------------------ |
641 | 655 |
642 // Represents monotonically non-decreasing clock time. | 656 // Represents monotonically non-decreasing clock time. |
643 class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> { | 657 class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> { |
644 public: | 658 public: |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
776 static void WaitUntilInitializedWin(); | 790 static void WaitUntilInitializedWin(); |
777 #endif | 791 #endif |
778 }; | 792 }; |
779 | 793 |
780 // For logging use only. | 794 // For logging use only. |
781 BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks); | 795 BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks); |
782 | 796 |
783 } // namespace base | 797 } // namespace base |
784 | 798 |
785 #endif // BASE_TIME_TIME_H_ | 799 #endif // BASE_TIME_TIME_H_ |
OLD | NEW |