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

Unified Diff: base/time/time.h

Issue 1229853004: Base: Make TimeDelta constructors deal with overflow. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/time/time.cc » ('j') | base/time/time.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/time/time.h
diff --git a/base/time/time.h b/base/time/time.h
index a02fbeb01ffb7e82761d524fd8c8bcacb9f618eb..b972bd723257df73e0407cbfe30b7fff84cbcd9a 100644
--- a/base/time/time.h
+++ b/base/time/time.h
@@ -264,6 +264,9 @@ class BASE_EXPORT TimeDelta {
explicit TimeDelta(int64 delta_us) : delta_(delta_us) {
}
+ // Private method to build a delta from a double.
+ static TimeDelta FromDouble(double value);
+
// Delta in microseconds.
int64 delta_;
};
@@ -597,7 +600,7 @@ class BASE_EXPORT Time : public time_internal::TimeBase<Time> {
// static
inline TimeDelta TimeDelta::FromDays(int days) {
- // Preserve max to prevent overflow.
+ // 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
if (days == std::numeric_limits<int>::max())
return Max();
return TimeDelta(days * Time::kMicrosecondsPerDay);
Nico 2015/07/17 17:00:23 doesn't days * Time::kMicrosecondsPerDay overflow
@@ -605,7 +608,7 @@ inline TimeDelta TimeDelta::FromDays(int days) {
// static
inline TimeDelta TimeDelta::FromHours(int hours) {
- // Preserve max to prevent overflow.
+ // Special-case max.
if (hours == std::numeric_limits<int>::max())
return Max();
return TimeDelta(hours * Time::kMicrosecondsPerHour);
@@ -613,7 +616,7 @@ inline TimeDelta TimeDelta::FromHours(int hours) {
// static
inline TimeDelta TimeDelta::FromMinutes(int minutes) {
- // Preserve max to prevent overflow.
+ // Special-case max.
if (minutes == std::numeric_limits<int>::max())
return Max();
return TimeDelta(minutes * Time::kMicrosecondsPerMinute);
@@ -621,44 +624,40 @@ inline TimeDelta TimeDelta::FromMinutes(int minutes) {
// static
inline TimeDelta TimeDelta::FromSeconds(int64 secs) {
- // Preserve max to prevent overflow.
- if (secs == std::numeric_limits<int64>::max())
- return Max();
- return TimeDelta(secs * Time::kMicrosecondsPerSecond);
+ 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
}
// static
inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) {
- // Preserve max to prevent overflow.
- if (ms == std::numeric_limits<int64>::max())
- return Max();
- return TimeDelta(ms * Time::kMicrosecondsPerMillisecond);
+ return TimeDelta(ms) * Time::kMicrosecondsPerMillisecond;
}
// static
inline TimeDelta TimeDelta::FromSecondsD(double secs) {
- // Preserve max to prevent overflow.
- if (secs == std::numeric_limits<double>::infinity())
- return Max();
- return TimeDelta(static_cast<int64>(secs * Time::kMicrosecondsPerSecond));
+ return FromDouble(secs * Time::kMicrosecondsPerSecond);
}
// static
inline TimeDelta TimeDelta::FromMillisecondsD(double ms) {
- // Preserve max to prevent overflow.
- if (ms == std::numeric_limits<double>::infinity())
- return Max();
- return TimeDelta(static_cast<int64>(ms * Time::kMicrosecondsPerMillisecond));
+ return FromDouble(ms * Time::kMicrosecondsPerMillisecond);
}
// static
inline TimeDelta TimeDelta::FromMicroseconds(int64 us) {
- // Preserve max to prevent overflow.
- if (us == std::numeric_limits<int64>::max())
- return Max();
return TimeDelta(us);
}
+// static
+inline TimeDelta TimeDelta::FromDouble(double value) {
+ double max_magnitude = std::numeric_limits<int64>::max();
+ TimeDelta delta = TimeDelta(static_cast<int64>(value));
+ if (value > max_magnitude)
+ delta = Max();
+ else if (value < -max_magnitude)
+ delta = -Max();
+ return delta;
+}
+
// For logging use only.
BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time);
« no previous file with comments | « no previous file | base/time/time.cc » ('j') | base/time/time.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698