Commit 4343e1a9 authored by Jungshik Shin's avatar Jungshik Shin Committed by Commit Bot

Accept Etc/GMT* as a valid time zone id.

Etc/GMT* time zones are  listed in the INAN time zone database and
they should be accepted as valid.

This CL will be followed by a CL for moving time zone name checks to C++
that will accept all the time zone names (e.g. EST5EDT, Hongkong, ROK, Zulu).

Bug: chromium:364374
Test: intl/date-format/timezone.js
Test: mjsunit/regress/regress-crbug-364374
Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng
Change-Id: If0e5327d7e980504a9cb3d2b641e907ebce61180
Reviewed-on: https://chromium-review.googlesource.com/1159546
Commit-Queue: Jungshik Shin <jshin@chromium.org>
Reviewed-by: 's avatarSathya Gunasekaran <gsathya@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54879}
parent 7a75d582
......@@ -194,6 +194,7 @@ function GetServiceRE() {
* Matches valid IANA time zone names.
*/
var TIMEZONE_NAME_CHECK_RE = UNDEFINED;
var GMT_OFFSET_TIMEZONE_NAME_CHECK_RE = UNDEFINED;
function GetTimezoneNameCheckRE() {
if (IS_UNDEFINED(TIMEZONE_NAME_CHECK_RE)) {
......@@ -203,6 +204,14 @@ function GetTimezoneNameCheckRE() {
return TIMEZONE_NAME_CHECK_RE;
}
function GetGMTOffsetTimezoneNameCheckRE() {
if (IS_UNDEFINED(GMT_OFFSET_TIMEZONE_NAME_CHECK_RE)) {
GMT_OFFSET_TIMEZONE_NAME_CHECK_RE = new GlobalRegExp(
'^(?:ETC/GMT)(?<offset>0|[+-](?:[0-9]|1[0-4]))$');
}
return GMT_OFFSET_TIMEZONE_NAME_CHECK_RE;
}
/**
* Matches valid location parts of IANA time zone names.
*/
......@@ -1510,7 +1519,7 @@ function CreateDateTimeFormat(locales, options) {
{__proto__: null, skeleton: ldmlString, timeZone: tz}, resolved);
if (resolved.timeZone === "Etc/Unknown") {
throw %make_range_error(kUnsupportedTimeZone, tz);
throw %make_range_error(kInvalidTimeZone, tz);
}
%MarkAsInitializedIntlObjectOfType(dateFormat, DATE_TIME_FORMAT_TYPE);
......@@ -1644,18 +1653,28 @@ function canonicalizeTimeZoneID(tzID) {
return 'UTC';
}
// TODO(jshin): Add support for Etc/GMT[+-]([1-9]|1[0-2])
// We expect only _, '-' and / beside ASCII letters.
// All inputs should conform to Area/Location(/Location)* from now on.
var match = %regexp_internal_match(GetTimezoneNameCheckRE(), tzID);
if (IS_NULL(match)) throw %make_range_error(kExpectedTimezoneID, tzID);
// All inputs should conform to Area/Location(/Location)*, or Etc/GMT* .
// TODO(jshin): 1. Support 'GB-Eire", 'EST5EDT", "ROK', 'US/*', 'NZ' and many
// other aliases/linked names when moving timezone validation code to C++.
// See crbug.com/364374 and crbug.com/v8/8007 .
// 2. Resolve the difference betwee CLDR/ICU and IANA time zone db.
// See http://unicode.org/cldr/trac/ticket/9892 and crbug.com/645807 .
let match = %regexp_internal_match(GetTimezoneNameCheckRE(), tzID);
if (IS_NULL(match)) {
let match =
%regexp_internal_match(GetGMTOffsetTimezoneNameCheckRE(), upperID);
if (!IS_NULL(match) && match.length == 2)
return "Etc/GMT" + match.groups.offset;
else
throw %make_range_error(kInvalidTimeZone, tzID);
}
var result = toTitleCaseTimezoneLocation(match[1]) + '/' +
let result = toTitleCaseTimezoneLocation(match[1]) + '/' +
toTitleCaseTimezoneLocation(match[2]);
if (!IS_UNDEFINED(match[3]) && 3 < match.length) {
var locations = %StringSplit(match[3], '/', kMaxUint32);
let locations = %StringSplit(match[3], '/', kMaxUint32);
// The 1st element is empty. Starts with i=1.
for (var i = 1; i < locations.length; i++) {
result = result + '/' + toTitleCaseTimezoneLocation(locations[i]);
......
......@@ -533,8 +533,6 @@ class ErrorUtils : public AllStatic {
T(BigIntNegativeExponent, "Exponent must be positive") \
T(BigIntTooBig, "Maximum BigInt size exceeded") \
T(DateRange, "Provided date is not in valid range.") \
T(ExpectedTimezoneID, \
"Expected Area/Location(/Location)* for time zone, got %") \
T(ExpectedLocation, \
"Expected letters optionally connected with underscores or hyphens for " \
"a location, got %") \
......@@ -556,6 +554,7 @@ class ErrorUtils : public AllStatic {
T(InvalidWeakSetValue, "Invalid value used in weak set") \
T(InvalidStringLength, "Invalid string length") \
T(InvalidTimeValue, "Invalid time value") \
T(InvalidTimeZone, "Invalid time zone specified: %") \
T(InvalidTypedArrayAlignment, "% of % should be a multiple of %") \
T(InvalidTypedArrayIndex, "Invalid typed array index") \
T(InvalidTypedArrayLength, "Invalid typed array length: %") \
......@@ -576,7 +575,6 @@ class ErrorUtils : public AllStatic {
T(ToRadixFormatRange, "toString() radix argument must be between 2 and 36") \
T(TypedArraySetOffsetOutOfBounds, "offset is out of bounds") \
T(TypedArraySetSourceTooLarge, "Source is too large") \
T(UnsupportedTimeZone, "Unsupported time zone specified %") \
T(ValueOutOfRange, "Value % out of range for % options property %") \
/* SyntaxError */ \
T(AmbiguousExport, \
......
......@@ -225,11 +225,10 @@ void SetResolvedDateSettings(Isolate* isolate, const icu::Locale& icu_locale,
if (U_SUCCESS(status)) {
// In CLDR (http://unicode.org/cldr/trac/ticket/9943), Etc/UTC is made
// a separate timezone ID from Etc/GMT even though they're still the same
// timezone. We'd not have "Etc/GMT" here because we canonicalize it and
// other GMT-variants to "UTC" in intl.js and "UTC" is turned to "Etc/UTC"
// by ICU before getting here.
// TODO(jshin): Figure out the cause of crbug.com/719609 and re-enable
// DCHECK(canonical_time_zone != UNICODE_STRING_SIMPLE("Etc/GMT")) .
// timezone. We have Etc/UTC because 'UTC', 'Etc/Universal',
// 'Etc/Zulu' and others are turned to 'Etc/UTC' by ICU. Etc/GMT comes
// from Etc/GMT0, Etc/GMT+0, Etc/GMT-0, Etc/Greenwich.
// ecma402##sec-canonicalizetimezonename step 3
if (canonical_time_zone == UNICODE_STRING_SIMPLE("Etc/UTC") ||
canonical_time_zone == UNICODE_STRING_SIMPLE("Etc/GMT")) {
JSObject::SetProperty(
......
......@@ -31,11 +31,15 @@
// var df = Intl.DateTimeFormat();
// assertEquals(getDefaultTimeZone(), df.resolvedOptions().timeZone);
df = Intl.DateTimeFormat(undefined, {timeZone: 'UtC'});
assertEquals('UTC', df.resolvedOptions().timeZone);
[
'UtC', 'gmt', 'Etc/UTC', 'Etc/GMT', 'Etc/GMT0', 'Etc/GMT+0',
'etc/gmt-0', 'etc/zulu', 'Etc/universal', 'etc/greenwich'
].forEach((timezone) => {
const df = Intl.DateTimeFormat(undefined, {timeZone: timezone});
assertEquals('UTC', df.resolvedOptions().timeZone);
})
df = Intl.DateTimeFormat(undefined, {timeZone: 'gmt'});
assertEquals('UTC', df.resolvedOptions().timeZone);
// See test/mjsunit/regress/regress-crbug-364374.js for additional/ tests.
df = Intl.DateTimeFormat(undefined, {timeZone: 'America/Los_Angeles'});
assertEquals('America/Los_Angeles', df.resolvedOptions().timeZone);
......@@ -43,22 +47,29 @@ assertEquals('America/Los_Angeles', df.resolvedOptions().timeZone);
df = Intl.DateTimeFormat(undefined, {timeZone: 'Europe/Belgrade'});
assertEquals('Europe/Belgrade', df.resolvedOptions().timeZone);
// Check Etc/XXX variants. They should work too.
df = Intl.DateTimeFormat(undefined, {timeZone: 'Etc/UTC'});
assertEquals('UTC', df.resolvedOptions().timeZone);
df = Intl.DateTimeFormat(undefined, {timeZone: 'Etc/GMT'});
assertEquals('UTC', df.resolvedOptions().timeZone);
df = Intl.DateTimeFormat(undefined, {timeZone: 'euRope/beLGRade'});
assertEquals('Europe/Belgrade', df.resolvedOptions().timeZone);
// Etc/GMT-14 to Etc/GMT+12 are valid.
df = Intl.DateTimeFormat(undefined, {timeZone: 'etc/gmt+12'});
assertEquals('Etc/GMT+12', df.resolvedOptions().timeZone);
df = Intl.DateTimeFormat(undefined, {timeZone: 'etc/gmt+9'});
assertEquals('Etc/GMT+9', df.resolvedOptions().timeZone);
df = Intl.DateTimeFormat(undefined, {timeZone: 'etc/gmt-9'});
assertEquals('Etc/GMT-9', df.resolvedOptions().timeZone);
df = Intl.DateTimeFormat(undefined, {timeZone: 'etc/gmt-14'});
assertEquals('Etc/GMT-14', df.resolvedOptions().timeZone);
assertThrows('Intl.DateTimeFormat(undefined, {timeZone: \'Etc/GMT+13\'})');
// : + - are not allowed, only / _ are.
assertThrows('Intl.DateTimeFormat(undefined, {timeZone: \'GMT+07:00\'})');
assertThrows('Intl.DateTimeFormat(undefined, {timeZone: \'GMT+0700\'})');
assertThrows('Intl.DateTimeFormat(undefined, {timeZone: \'GMT-05:00\'})');
assertThrows('Intl.DateTimeFormat(undefined, {timeZone: \'GMT-0500\'})');
assertThrows('Intl.DateTimeFormat(undefined, {timeZone: \'Etc/GMT+0\'})');
assertThrows('Intl.DateTimeFormat(undefined, ' +
'{timeZone: \'America/Los-Angeles\'})');
......
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