Commit f06754a8 authored by hichris123's avatar hichris123 Committed by Commit bot

Make dates default to the local timezone if none specified

In ES5, dates were supposed to default to UTC if no timezone was specified. However, this changed in ES6, which specified that dates should be in the local timezone if no timezone was specified. This CL updates our behavior to match that part of the ES6 spec.

BUG=chromium:391730, v8:4242
LOG=Y

Review URL: https://codereview.chromium.org/1229903004

Cr-Commit-Position: refs/heads/master@{#29854}
parent a02219e9
......@@ -22,9 +22,9 @@ bool DateParser::Parse(Vector<Char> str,
DayComposer day;
// Specification:
// Accept ES5 ISO 8601 date-time-strings or legacy dates compatible
// Accept ES6 ISO 8601 date-time-strings or legacy dates compatible
// with Safari.
// ES5 ISO 8601 dates:
// ES6 ISO 8601 dates:
// [('-'|'+')yy]yyyy[-MM[-DD]][THH:mm[:ss[.sss]][Z|(+|-)hh:mm]]
// where yyyy is in the range 0000..9999 and
// +/-yyyyyy is in the range -999999..+999999 -
......@@ -39,8 +39,7 @@ bool DateParser::Parse(Vector<Char> str,
// sss is in the range 000..999,
// hh is in the range 00..23,
// mm, ss, and sss default to 00 if missing, and
// timezone defaults to Z if missing
// (following Safari, ISO actually demands local time).
// timezone defaults to local time if missing.
// Extensions:
// We also allow sss to have more or less than three digits (but at
// least one).
......@@ -62,15 +61,13 @@ bool DateParser::Parse(Vector<Char> str,
// is allowed).
// Intersection of the two:
// A string that matches both formats (e.g. 1970-01-01) will be
// parsed as an ES5 date-time string - which means it will default
// to UTC time-zone. That's unavoidable if following the ES5
// specification.
// After a valid "T" has been read while scanning an ES5 datetime string,
// parsed as an ES6 date-time string.
// After a valid "T" has been read while scanning an ES6 datetime string,
// the input can no longer be a valid legacy date, since the "T" is a
// garbage string after a number has been read.
// First try getting as far as possible with as ES5 Date Time String.
DateToken next_unhandled_token = ParseES5DateTime(&scanner, &day, &time, &tz);
// First try getting as far as possible with as ES6 Date Time String.
DateToken next_unhandled_token = ParseES6DateTime(&scanner, &day, &time, &tz);
if (next_unhandled_token.IsInvalid()) return false;
bool has_read_number = !day.IsEmpty();
// If there's anything left, continue with the legacy parser.
......@@ -195,7 +192,7 @@ DateParser::DateToken DateParser::DateStringTokenizer<CharType>::Scan() {
template <typename Char>
DateParser::DateToken DateParser::ParseES5DateTime(
DateParser::DateToken DateParser::ParseES6DateTime(
DateStringTokenizer<Char>* scanner,
DayComposer* day,
TimeComposer* time,
......@@ -233,7 +230,7 @@ DateParser::DateToken DateParser::ParseES5DateTime(
if (!scanner->Peek().IsKeywordType(TIME_SEPARATOR)) {
if (!scanner->Peek().IsEndOfInput()) return scanner->Next();
} else {
// ES5 Date Time String time part is present.
// ES6 Date Time String time part is present.
scanner->Next();
if (!scanner->Peek().IsFixedLengthNumber(2) ||
!Between(scanner->Peek().number(), 0, 24)) {
......@@ -299,8 +296,7 @@ DateParser::DateToken DateParser::ParseES5DateTime(
}
if (!scanner->Peek().IsEndOfInput()) return DateToken::Invalid();
}
// Successfully parsed ES5 Date Time String. Default to UTC if no TZ given.
if (tz->IsEmpty()) tz->Set(0);
// Successfully parsed ES6 Date Time String.
day->set_iso_date();
return DateToken::EndOfInput();
}
......
......@@ -367,13 +367,13 @@ class DateParser : public AllStatic {
bool is_iso_date_;
};
// Tries to parse an ES5 Date Time String. Returns the next token
// Tries to parse an ES6 Date Time String. Returns the next token
// to continue with in the legacy date string parser. If parsing is
// complete, returns DateToken::EndOfInput(). If terminally unsuccessful,
// returns DateToken::Invalid(). Otherwise parsing continues in the
// legacy parser.
template <typename Char>
static DateParser::DateToken ParseES5DateTime(
static DateParser::DateToken ParseES6DateTime(
DateStringTokenizer<Char>* scanner,
DayComposer* day,
TimeComposer* time,
......
......@@ -245,9 +245,9 @@ var testCasesES5Misc = [
['2000-01T08:00:00.099Z', 946713600099],
['2000-01T08:00:00.999Z', 946713600999],
['2000-01T00:00:00.001-08:00', 946713600001],
['2000-01-01T24:00', 946771200000],
['2000-01-01T24:00:00', 946771200000],
['2000-01-01T24:00:00.000', 946771200000],
['2000-01-01T24:00Z', 946771200000],
['2000-01-01T24:00:00Z', 946771200000],
['2000-01-01T24:00:00.000Z', 946771200000],
['2000-01-01T24:00:00.000Z', 946771200000]];
var testCasesES5MiscNegative = [
......
......@@ -203,110 +203,110 @@ assertEquals(-8640000000000000, Date.UTC(1970, 0, 1 - 100000001, 24));
// Parsing ES5 ISO-8601 dates.
// When TZ is omitted, it defaults to 'Z' meaning UTC.
// When TZ is omitted, it defaults to the local timezone
// Check epoch.
assertEquals(0, Date.parse("1970-01-01T00:00:00.000+00:00"));
assertEquals(0, Date.parse("1970-01-01T00:00:00.000-00:00"));
assertEquals(0, Date.parse("1970-01-01T00:00:00.000Z"));
assertEquals(0, Date.parse("1970-01-01T00:00:00.000"));
assertEquals(0, Date.parse("1970-01-01T00:00:00"));
assertEquals(0, Date.parse("1970-01-01T00:00"));
assertEquals(0, Date.parse("1970-01-01"));
assertEquals(0, Date.parse("1970-01-01T00:00:00.000Z"));
assertEquals(0, Date.parse("1970-01-01T00:00:00Z"));
assertEquals(0, Date.parse("1970-01-01T00:00Z"));
assertEquals(0, Date.parse("1970-01-01Z"));
assertEquals(0, Date.parse("1970-01T00:00:00.000+00:00"));
assertEquals(0, Date.parse("1970-01T00:00:00.000-00:00"));
assertEquals(0, Date.parse("1970-01T00:00:00.000Z"));
assertEquals(0, Date.parse("1970-01T00:00:00.000"));
assertEquals(0, Date.parse("1970-01T00:00:00"));
assertEquals(0, Date.parse("1970-01T00:00"));
assertEquals(0, Date.parse("1970-01"));
assertEquals(0, Date.parse("1970-01T00:00:00.000Z"));
assertEquals(0, Date.parse("1970-01T00:00:00Z"));
assertEquals(0, Date.parse("1970-01T00:00Z"));
assertEquals(0, Date.parse("1970-01Z"));
assertEquals(0, Date.parse("1970T00:00:00.000+00:00"));
assertEquals(0, Date.parse("1970T00:00:00.000-00:00"));
assertEquals(0, Date.parse("1970T00:00:00.000Z"));
assertEquals(0, Date.parse("1970T00:00:00.000"));
assertEquals(0, Date.parse("1970T00:00:00"));
assertEquals(0, Date.parse("1970T00:00"));
assertEquals(0, Date.parse("1970"));
assertEquals(0, Date.parse("1970T00:00:00.000Z"));
assertEquals(0, Date.parse("1970T00:00:00Z"));
assertEquals(0, Date.parse("1970T00:00Z"));
assertEquals(0, Date.parse("1970Z"));
assertEquals(0, Date.parse("+001970-01-01T00:00:00.000+00:00"));
assertEquals(0, Date.parse("+001970-01-01T00:00:00.000-00:00"));
assertEquals(0, Date.parse("+001970-01-01T00:00:00.000Z"));
assertEquals(0, Date.parse("+001970-01-01T00:00:00.000"));
assertEquals(0, Date.parse("+001970-01-01T00:00:00"));
assertEquals(0, Date.parse("+001970-01-01T00:00"));
assertEquals(0, Date.parse("+001970-01-01"));
assertEquals(0, Date.parse("+001970-01-01T00:00:00.000Z"));
assertEquals(0, Date.parse("+001970-01-01T00:00:00Z"));
assertEquals(0, Date.parse("+001970-01-01T00:00Z"));
assertEquals(0, Date.parse("+001970-01-01Z"));
assertEquals(0, Date.parse("+001970-01T00:00:00.000+00:00"));
assertEquals(0, Date.parse("+001970-01T00:00:00.000-00:00"));
assertEquals(0, Date.parse("+001970-01T00:00:00.000Z"));
assertEquals(0, Date.parse("+001970-01T00:00:00.000"));
assertEquals(0, Date.parse("+001970-01T00:00:00"));
assertEquals(0, Date.parse("+001970-01T00:00"));
assertEquals(0, Date.parse("+001970-01"));
assertEquals(0, Date.parse("+001970-01T00:00:00.000Z"));
assertEquals(0, Date.parse("+001970-01T00:00:00Z"));
assertEquals(0, Date.parse("+001970-01T00:00Z"));
assertEquals(0, Date.parse("+001970-01Z"));
assertEquals(0, Date.parse("+001970T00:00:00.000+00:00"));
assertEquals(0, Date.parse("+001970T00:00:00.000-00:00"));
assertEquals(0, Date.parse("+001970T00:00:00.000Z"));
assertEquals(0, Date.parse("+001970T00:00:00.000"));
assertEquals(0, Date.parse("+001970T00:00:00"));
assertEquals(0, Date.parse("+001970T00:00"));
assertEquals(0, Date.parse("+001970"));
assertEquals(0, Date.parse("+001970T00:00:00.000Z"));
assertEquals(0, Date.parse("+001970T00:00:00Z"));
assertEquals(0, Date.parse("+001970T00:00Z"));
assertEquals(0, Date.parse("+001970Z"));
// Check random date.
assertEquals(70671003500, Date.parse("1972-03-28T23:50:03.500+01:00"));
assertEquals(70674603500, Date.parse("1972-03-28T23:50:03.500Z"));
assertEquals(70674603500, Date.parse("1972-03-28T23:50:03.500"));
assertEquals(70674603000, Date.parse("1972-03-28T23:50:03"));
assertEquals(70674600000, Date.parse("1972-03-28T23:50"));
assertEquals(70588800000, Date.parse("1972-03-28"));
assertEquals(70674603500, Date.parse("1972-03-28T23:50:03.500Z"));
assertEquals(70674603000, Date.parse("1972-03-28T23:50:03Z"));
assertEquals(70674600000, Date.parse("1972-03-28T23:50Z"));
assertEquals(70588800000, Date.parse("1972-03-28Z"));
assertEquals(68338203500, Date.parse("1972-03T23:50:03.500+01:00"));
assertEquals(68341803500, Date.parse("1972-03T23:50:03.500Z"));
assertEquals(68341803500, Date.parse("1972-03T23:50:03.500"));
assertEquals(68341803000, Date.parse("1972-03T23:50:03"));
assertEquals(68341800000, Date.parse("1972-03T23:50"));
assertEquals(68256000000, Date.parse("1972-03"));
assertEquals(68341803500, Date.parse("1972-03T23:50:03.500Z"));
assertEquals(68341803000, Date.parse("1972-03T23:50:03Z"));
assertEquals(68341800000, Date.parse("1972-03T23:50Z"));
assertEquals(68256000000, Date.parse("1972-03Z"));
assertEquals(63154203500, Date.parse("1972T23:50:03.500+01:00"));
assertEquals(63157803500, Date.parse("1972T23:50:03.500Z"));
assertEquals(63157803500, Date.parse("1972T23:50:03.500"));
assertEquals(63157803000, Date.parse("1972T23:50:03"));
assertEquals(63072000000, Date.parse("1972"));
assertEquals(63157803500, Date.parse("1972T23:50:03.500Z"));
assertEquals(63157803000, Date.parse("1972T23:50:03Z"));
assertEquals(63072000000, Date.parse("1972Z"));
assertEquals(70671003500, Date.parse("+001972-03-28T23:50:03.500+01:00"));
assertEquals(70674603500, Date.parse("+001972-03-28T23:50:03.500Z"));
assertEquals(70674603500, Date.parse("+001972-03-28T23:50:03.500"));
assertEquals(70674603000, Date.parse("+001972-03-28T23:50:03"));
assertEquals(70674600000, Date.parse("+001972-03-28T23:50"));
assertEquals(70588800000, Date.parse("+001972-03-28"));
assertEquals(70674603500, Date.parse("+001972-03-28T23:50:03.500Z"));
assertEquals(70674603000, Date.parse("+001972-03-28T23:50:03Z"));
assertEquals(70674600000, Date.parse("+001972-03-28T23:50Z"));
assertEquals(70588800000, Date.parse("+001972-03-28Z"));
assertEquals(68338203500, Date.parse("+001972-03T23:50:03.500+01:00"));
assertEquals(68341803500, Date.parse("+001972-03T23:50:03.500Z"));
assertEquals(68341803500, Date.parse("+001972-03T23:50:03.500"));
assertEquals(68341803000, Date.parse("+001972-03T23:50:03"));
assertEquals(68341800000, Date.parse("+001972-03T23:50"));
assertEquals(68256000000, Date.parse("+001972-03"));
assertEquals(68341803500, Date.parse("+001972-03T23:50:03.500Z"));
assertEquals(68341803000, Date.parse("+001972-03T23:50:03Z"));
assertEquals(68341800000, Date.parse("+001972-03T23:50Z"));
assertEquals(68256000000, Date.parse("+001972-03Z"));
assertEquals(63154203500, Date.parse("+001972T23:50:03.500+01:00"));
assertEquals(63157803500, Date.parse("+001972T23:50:03.500Z"));
assertEquals(63157803500, Date.parse("+001972T23:50:03.500"));
assertEquals(63157803000, Date.parse("+001972T23:50:03"));
assertEquals(63072000000, Date.parse("+001972"));
assertEquals(63157803500, Date.parse("+001972T23:50:03.500Z"));
assertEquals(63157803000, Date.parse("+001972T23:50:03Z"));
assertEquals(63072000000, Date.parse("+001972Z"));
// Ensure that ISO-years in the range 00-99 aren't translated to the range
// 1950..2049.
assertEquals(-60904915200000, Date.parse("0040-01-01"));
assertEquals(-60273763200000, Date.parse("0060-01-01"));
assertEquals(-62167219200000, Date.parse("0000-01-01"));
assertEquals(-62167219200000, Date.parse("+000000-01-01"));
assertEquals(-60904915200000, Date.parse("0040-01-01T00:00Z"));
assertEquals(-60273763200000, Date.parse("0060-01-01T00:00Z"));
assertEquals(-62167219200000, Date.parse("0000-01-01T00:00Z"));
assertEquals(-62167219200000, Date.parse("+000000-01-01T00:00Z"));
// Test negative years.
assertEquals(-63429523200000, Date.parse("-000040-01-01"));
assertEquals(-64060675200000, Date.parse("-000060-01-01"));
assertEquals(-124397510400000, Date.parse("-001972-01-01"));
assertEquals(-63429523200000, Date.parse("-000040-01-01Z"));
assertEquals(-64060675200000, Date.parse("-000060-01-01Z"));
assertEquals(-124397510400000, Date.parse("-001972-01-01Z"));
// Check time-zones.
assertEquals(70674603500, Date.parse("1972-03-28T23:50:03.500Z"));
......
......@@ -371,9 +371,6 @@
'built-ins/Symbol/species/builtin-getter-name': [FAIL],
'built-ins/Symbol/species/subclassing': [FAIL],
# https://code.google.com/p/v8/issues/detail?id=4242
'built-ins/Date/15.9.1.15-1': [FAIL],
# https://code.google.com/p/v8/issues/detail?id=4004
'built-ins/Date/prototype/setFullYear/15.9.5.40_1': [FAIL],
......
......@@ -284,6 +284,9 @@
# ES6 RegExp test calls ToLength, not ToUint32
'S15.10.6.2_A5_T3': [FAIL],
# ES6 says for dates to default to the local timezone if none is specified
'15.9.1.15-1': [FAIL],
######################## NEEDS INVESTIGATION ###########################
# These test failures are specific to the intl402 suite and need investigation
......
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