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

Unified Diff: base/time.cc

Issue 10916089: Fixing Time::Max()'s behavior with Time::ToTimeT() and friends. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Windows. :( Created 8 years, 3 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 | « base/time.h ('k') | base/time_mac.cc » ('j') | base/time_unittest.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/time.cc
diff --git a/base/time.cc b/base/time.cc
index 7055311e80e7d53df843b61dd63a67b816a74faa..e59de53d44718b4dd0e429a29e3e3d31108cd8d8 100644
--- a/base/time.cc
+++ b/base/time.cc
@@ -74,12 +74,18 @@ Time Time::Max() {
Time Time::FromTimeT(time_t tt) {
if (tt == 0)
return Time(); // Preserve 0 so we can tell it doesn't exist.
+ if (tt == std::numeric_limits<time_t>::max())
+ return Max();
return Time((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset);
}
time_t Time::ToTimeT() const {
- if (us_ == 0)
+ if (is_null())
return 0; // Preserve 0 so we can tell it doesn't exist.
+ if (is_max()) {
+ // Preserve max without offset to prevent overflow.
+ return std::numeric_limits<time_t>::max();
+ }
return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond;
msarda 2012/09/06 15:04:34 There is still a risk of overflow here: us_ might
Mike West 2012/09/06 19:51:16 Good point. I've added the log. This one looks lik
}
@@ -87,14 +93,20 @@ time_t Time::ToTimeT() const {
Time Time::FromDoubleT(double dt) {
if (dt == 0 || isnan(dt))
return Time(); // Preserve 0 so we can tell it doesn't exist.
+ if (dt == std::numeric_limits<double>::max())
+ return Max();
return Time(static_cast<int64>((dt *
static_cast<double>(kMicrosecondsPerSecond)) +
kTimeTToMicrosecondsOffset));
}
double Time::ToDoubleT() const {
- if (us_ == 0)
+ if (is_null())
return 0; // Preserve 0 so we can tell it doesn't exist.
+ if (is_max()) {
+ // Preserve max without offset to prevent overflow.
+ return std::numeric_limits<double>::max();
+ }
return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
static_cast<double>(kMicrosecondsPerSecond));
}
@@ -103,15 +115,21 @@ double Time::ToDoubleT() const {
Time Time::FromJsTime(double ms_since_epoch) {
// The epoch is a valid time, so this constructor doesn't interpret
// 0 as the null time.
+ if (ms_since_epoch == std::numeric_limits<double>::max())
+ return Max();
return Time(static_cast<int64>(ms_since_epoch * kMicrosecondsPerMillisecond) +
kTimeTToMicrosecondsOffset);
}
double Time::ToJsTime() const {
- if (us_ == 0) {
+ if (is_null()) {
// Preserve 0 so the invalid result doesn't depend on the platform.
return 0;
}
+ if (is_max()) {
+ // Preserve max without offset to prevent overflow.
+ return std::numeric_limits<double>::max();
+ }
return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
kMicrosecondsPerMillisecond);
}
« no previous file with comments | « base/time.h ('k') | base/time_mac.cc » ('j') | base/time_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698