Commit 6650abf2 authored by majidvp's avatar majidvp Committed by Commit bot

Correctly handle -epoch time values when converting from JS time to base::Time

-11644473600 seconds (which represents windows epoch time of
|1601-01-01 00:00:00 UTC|) is a valid time value in Javascript. Incidentally
this value is internally represented by 0 which is mistakenly confused with a
null time value.

FromJsTime is meant to be used to convert time values coming from Javascript
for which 0 or -epoch do not represent null values. So the extra check was

* In fact there is a comment in FromJsTime method making it clear that 0 is a
valid value but this was missed in ToJsTime method.

TEST=./base_unittests --gtest_filter=TimeTest.JsTime


Cr-Commit-Position: refs/heads/master@{#440304}
parent bb22e9c7
......@@ -191,10 +191,6 @@ Time Time::FromJsTime(double ms_since_epoch) {
double Time::ToJsTime() const {
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>::infinity();
......@@ -159,6 +159,15 @@ TEST_F(TimeTest, JsTime) {
EXPECT_EQ(700.0003, t.ToDoubleT());
t = Time::FromDoubleT(800.73);
EXPECT_EQ(800730.0, t.ToJsTime());
// Correctly convert |- epoch offset| which is valid time in javascript.
Time minusEpoch = Time::FromJsTime(-11644473600000.0);
EXPECT_EQ(-11644473600000, minusEpoch.ToJsTime());
// Check conversion of boundary javascript time values.
// See
EXPECT_EQ(-8.46e15, Time::FromJsTime(-8.46e15).ToJsTime());
EXPECT_EQ(8.46e15, Time::FromJsTime(8.46e15).ToJsTime());
#if defined(OS_POSIX)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment