Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: base/time/time.h

Issue 1976703005: Allow constexpr variables of TimeDelta type. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkcr
Patch Set: Fix overflow from doubles, and test all the boundaries better. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/time/time_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
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) {
627 // Note that TimeDelta::FromMicroseconds(numeric_limits<int64_t>::min()) is
Jeffrey Yasskin 2016/05/16 18:59:17 The fact that this function is happy to produce va
danakj 2016/05/17 20:28:29 Hm this is rather awkward. FromDouble appears to b
Jeffrey Yasskin 2016/05/17 20:48:51 The other use is in FromCheckedNumeric() but spell
628 // less than the value returned when an operation underflows.
623 return TimeDelta(us); 629 return TimeDelta(us);
624 } 630 }
625 631
626 // static 632 // static
627 inline TimeDelta TimeDelta::FromDouble(double value) { 633 constexpr inline TimeDelta TimeDelta::FromDouble(double value) {
628 double max_magnitude = std::numeric_limits<int64_t>::max(); 634 // IsValueInRangeForNumericType is needed instead of a simple comparison
629 TimeDelta delta = TimeDelta(static_cast<int64_t>(value)); 635 // against numeric_limits<int64_t>::max() because
630 if (value > max_magnitude) 636 // numeric_limits<int64_t>::max() is 2**63 - 1, which rounds to 2**63
631 delta = Max(); 637 // when comparing against a double value. (The largest int64_t that converts
632 else if (value < -max_magnitude) 638 // to a double value distinct from 2**63 is 2**63-1024.)
633 delta = -Max(); 639 // static_cast<int64_t>(2**63) is undefined behavior per [conv.fpint] because
634 return delta; 640 // the double is out of int64_t's range. However,
641 // IsValueInRangeForNumericType() isn't sufficient because it'll allow -2**63
642 // through, which is less than -Max().
643 return IsValueInRangeForNumericType<int64_t>(value) &&
644 static_cast<int64_t>(value) >=
645 -std::numeric_limits<int64_t>::max()
646 ? TimeDelta(static_cast<int64_t>(value))
647 : value > 0 ? Max() : -Max();
648 }
649
650 // static
651 constexpr inline TimeDelta TimeDelta::FromProduct(int64_t value,
652 int microseconds_per) {
653 return value > std::numeric_limits<int64_t>::max() / microseconds_per
danakj 2016/05/17 20:28:29 Why do we want to make bad multiplications so well
Jeffrey Yasskin 2016/05/17 20:48:51 The old behavior of FromSeconds() and FromMillisec
danakj 2016/05/17 21:17:36 Ah, operator* was doing that. I see, and the other
654 ? Max()
655 : value < -std::numeric_limits<int64_t>::max() / microseconds_per
656 ? -Max()
657 : TimeDelta(value * microseconds_per);
635 } 658 }
636 659
637 // For logging use only. 660 // For logging use only.
638 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); 661 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time);
639 662
640 // TimeTicks ------------------------------------------------------------------ 663 // TimeTicks ------------------------------------------------------------------
641 664
642 // Represents monotonically non-decreasing clock time. 665 // Represents monotonically non-decreasing clock time.
643 class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> { 666 class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> {
644 public: 667 public:
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
776 static void WaitUntilInitializedWin(); 799 static void WaitUntilInitializedWin();
777 #endif 800 #endif
778 }; 801 };
779 802
780 // For logging use only. 803 // For logging use only.
781 BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks); 804 BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks);
782 805
783 } // namespace base 806 } // namespace base
784 807
785 #endif // BASE_TIME_TIME_H_ 808 #endif // BASE_TIME_TIME_H_
OLDNEW
« no previous file with comments | « no previous file | base/time/time_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698