Index: base/time/time.h |
diff --git a/base/time/time.h b/base/time/time.h |
index a02fbeb01ffb7e82761d524fd8c8bcacb9f618eb..e0a6ea37eaa9f2c2035a8b04ad80b9d3d7818223 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,6 @@ class BASE_EXPORT Time : public time_internal::TimeBase<Time> { |
// static |
inline TimeDelta TimeDelta::FromDays(int days) { |
- // Preserve max to prevent overflow. |
if (days == std::numeric_limits<int>::max()) |
return Max(); |
return TimeDelta(days * Time::kMicrosecondsPerDay); |
@@ -605,7 +607,6 @@ inline TimeDelta TimeDelta::FromDays(int days) { |
// static |
inline TimeDelta TimeDelta::FromHours(int hours) { |
- // Preserve max to prevent overflow. |
if (hours == std::numeric_limits<int>::max()) |
return Max(); |
return TimeDelta(hours * Time::kMicrosecondsPerHour); |
@@ -613,7 +614,6 @@ inline TimeDelta TimeDelta::FromHours(int hours) { |
// static |
inline TimeDelta TimeDelta::FromMinutes(int minutes) { |
- // Preserve max to prevent overflow. |
if (minutes == std::numeric_limits<int>::max()) |
return Max(); |
return TimeDelta(minutes * Time::kMicrosecondsPerMinute); |
@@ -621,44 +621,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; |
} |
// 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); |