diff options
author | jianli@chromium.org <jianli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-09 17:17:55 +0000 |
---|---|---|
committer | jianli@chromium.org <jianli@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-09 17:17:55 +0000 |
commit | c925ad166b4825c6412a318092a664da58b5f365 (patch) | |
tree | ea292d1cdcb394ef50c93f57b20cf7f35a1ffac0 | |
parent | feed0677b1f7f719498d3b08f0dab523a4c095e7 (diff) | |
download | chromium_src-c925ad166b4825c6412a318092a664da58b5f365.zip chromium_src-c925ad166b4825c6412a318092a664da58b5f365.tar.gz chromium_src-c925ad166b4825c6412a318092a664da58b5f365.tar.bz2 |
Fix Time::FromDoubleT so that it will return null time when 0 is passed.
This is the rework of this issue before previous submit is reverted. We need to
fix the usage of Time::FromDoubleT(0) in extension history API.
BUG=none
TEST=non
Review URL: http://codereview.chromium.org/3295001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@58953 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/time.cc | 9 | ||||
-rw-r--r-- | base/time.h | 6 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_cookies_api.cc | 5 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_history_api.cc | 7 |
4 files changed, 24 insertions, 3 deletions
diff --git a/base/time.cc b/base/time.cc index 383f939..766f599 100644 --- a/base/time.cc +++ b/base/time.cc @@ -66,6 +66,8 @@ time_t Time::ToTimeT() const { // static Time Time::FromDoubleT(double dt) { + if (dt == 0) + return Time(); // Preserve 0 so we can tell it doesn't exist. return Time(static_cast<int64>((dt * static_cast<double>(kMicrosecondsPerSecond)) + kTimeTToMicrosecondsOffset)); @@ -78,6 +80,13 @@ double Time::ToDoubleT() const { static_cast<double>(kMicrosecondsPerSecond)); } +// static +Time Time::UnixEpoch() { + Time time; + time.us_ = kTimeTToMicrosecondsOffset; + return time; +} + Time Time::LocalMidnight() const { Exploded exploded; LocalExplode(&exploded); diff --git a/base/time.h b/base/time.h index 0f5eb5d..0ed7957 100644 --- a/base/time.h +++ b/base/time.h @@ -230,6 +230,9 @@ class Time { return us_ == 0; } + // Returns the time for epoch in Unix-like system (Jan 1, 1970). + static Time UnixEpoch(); + // Returns the current time. Watch out, the system might adjust its clock // in which case time will actually go backwards. We don't guarantee that // times are increasing, or that two calls to Now() won't be the same. @@ -249,6 +252,9 @@ class Time { // Converts time to/from a double which is the number of seconds since epoch // (Jan 1, 1970). Webkit uses this format to represent time. + // Because WebKit initializes double time value to 0 to indicate "not + // initialized", we map it to empty Time object that also means "not + // initialized". static Time FromDoubleT(double dt); double ToDoubleT() const; diff --git a/chrome/browser/extensions/extension_cookies_api.cc b/chrome/browser/extensions/extension_cookies_api.cc index 5a2d456..e22213d 100644 --- a/chrome/browser/extensions/extension_cookies_api.cc +++ b/chrome/browser/extensions/extension_cookies_api.cc @@ -306,7 +306,10 @@ bool SetCookieFunction::RunImpl() { EXTENSION_FUNCTION_VALIDATE( expiration_date_value->GetAsReal(&expiration_date)); } - expiration_time_ = base::Time::FromDoubleT(expiration_date); + // Time::FromDoubleT converts double time 0 to empty Time object. So we need + // to do special handling here. + expiration_time_ = (expiration_date == 0) ? + base::Time::UnixEpoch() : base::Time::FromDoubleT(expiration_date); } URLRequestContextGetter* store_context = NULL; diff --git a/chrome/browser/extensions/extension_history_api.cc b/chrome/browser/extensions/extension_history_api.cc index 515e918b..fac0e1f 100644 --- a/chrome/browser/extensions/extension_history_api.cc +++ b/chrome/browser/extensions/extension_history_api.cc @@ -180,7 +180,10 @@ bool HistoryFunction::GetTimeFromValue(Value* value, base::Time* time) { // The history service has seconds resolution, while javascript Date() has // milliseconds resolution. double seconds_from_epoch = ms_from_epoch / 1000.0; - *time = base::Time::FromDoubleT(seconds_from_epoch); + // Time::FromDoubleT converts double time 0 to empty Time object. So we need + // to do special handling here. + *time = (seconds_from_epoch == 0) ? + base::Time::UnixEpoch() : base::Time::FromDoubleT(seconds_from_epoch); return true; } @@ -368,7 +371,7 @@ bool DeleteAllHistoryFunction::RunAsyncImpl() { HistoryService* hs = profile()->GetHistoryService(Profile::EXPLICIT_ACCESS); hs->ExpireHistoryBetween( restrict_urls, - base::Time::FromDoubleT(0), // From the beginning of the epoch. + base::Time::UnixEpoch(), // From the beginning of the epoch. base::Time::Now(), // To the current time. &cancelable_consumer_, NewCallback(this, &DeleteAllHistoryFunction::DeleteComplete)); |