Skip to content

Commit 98c0cd9

Browse files
jungshikCommit Bot
authored andcommitted
Use the base locale when getting the best match pattern
This is to fix an assertion failure in formatToParts when Chinese calendar is specified with 'u-ca-chinese'. See tc39/ecma402#225 . This CL is a temporary work-around to get v8 match the spec in terms of the external behavior, but it's not taking the steps in the spec, yet. Moreover, the spec may have to be revised as to how to pick the best match pattern when the default calendar for a locale is different from the calendar specified via 'u-ca'. How to handle 'related year' part also needs to be specified. Bug: chromium:826549 Test: intl/date-format/format-with-extensions Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng Change-Id: I1f9a2467e86e71e024bc9babe18f16e49746008e Reviewed-on: https://chromium-review.googlesource.com/1006915 Reviewed-by: Sathya Gunasekaran <[email protected]> Reviewed-by: Jakob Kummerow <[email protected]> Commit-Queue: Jungshik Shin <[email protected]> Cr-Commit-Position: refs/heads/master@{#52556}
1 parent 2b24df9 commit 98c0cd9

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

src/objects/intl-objects.cc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,20 @@ icu::SimpleDateFormat* CreateICUDateFormat(Isolate* isolate,
123123
icu::SimpleDateFormat* date_format = nullptr;
124124
icu::UnicodeString skeleton;
125125
if (ExtractStringSetting(isolate, options, "skeleton", &skeleton)) {
126+
// See https://github.com/tc39/ecma402/issues/225 . The best pattern
127+
// generation needs to be done in the base locale according to the
128+
// current spec however odd it may be. See also crbug.com/826549 .
129+
// This is a temporary work-around to get v8's external behavior to match
130+
// the current spec, but does not follow the spec provisions mentioned
131+
// in the above Ecma 402 issue.
132+
// TODO(jshin): The spec may need to be revised because using the base
133+
// locale for the pattern match is not quite right. Moreover, what to
134+
// do with 'related year' part when 'chinese/dangi' calendar is specified
135+
// has to be discussed. Revisit once the spec is clarified/revised.
136+
icu::Locale no_extension_locale(icu_locale.getBaseName());
126137
std::unique_ptr<icu::DateTimePatternGenerator> generator(
127-
icu::DateTimePatternGenerator::createInstance(icu_locale, status));
138+
icu::DateTimePatternGenerator::createInstance(no_extension_locale,
139+
status));
128140
icu::UnicodeString pattern;
129141
if (U_SUCCESS(status))
130142
pattern = generator->getBestPattern(skeleton, status);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2018 the V8 project authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
const d = new Date(2018, 5, 21); // 2018-06-21
6+
7+
function checkFormat(locale, options, expected) {
8+
let df = new Intl.DateTimeFormat(locale, options);
9+
let resolvedOptions = df.resolvedOptions();
10+
assertEquals(expected.cal, resolvedOptions.calendar);
11+
assertEquals(expected.numSys, resolvedOptions.numberingSystem);
12+
13+
let formattedParts = df.formatToParts(d);
14+
let formattedReconstructedFromParts = formattedParts.map((part) => part.value)
15+
.reduce((accumulated, part) => accumulated + part);
16+
let formatted = df.format(d);
17+
18+
assertEquals(formatted, formattedReconstructedFromParts);
19+
assertEquals(expected.types, formattedParts.map((part) => part.type));
20+
assertEquals(expected.formatted, formatted);
21+
}
22+
23+
// Even though the calendar is Chinese, the best pattern search for formatting
24+
// should be done in the base locale (i.e. en or en-GB instead of
25+
// en-u-ca-chinese or en-GB-u-ca-chinese). Otherwise, {year: 'numeric'} would
26+
// results in '35 (wu-su)' where 'wu-su' is the designation for year 35 in the
27+
// 60-year cycle. See https://github.com/tc39/ecma402/issues/225 .
28+
[
29+
["en", "gregory", "latn", "2018"],
30+
["en-GB", "gregory", "latn", "2018"],
31+
["en-u-ca-chinese", "chinese", "latn", "35"],
32+
["en-GB-u-ca-chinese", "chinese", "latn", "35"],
33+
["en-u-ca-chinese-nu-deva", "chinese", "deva", "३५"],
34+
["en-GB-u-ca-chinese-nu-deva", "chinese", "deva", "३५"],
35+
].forEach(function(entry) {
36+
checkFormat(entry[0], {year: 'numeric'},
37+
{ cal: entry[1],
38+
numSys: entry[2],
39+
formatted: entry[3],
40+
types: ["year"],
41+
});
42+
});
43+
44+
const enUSTypes = ["month", "literal", "day", "literal", "year"];
45+
const enGBTypes = ["day", "literal", "month", "literal", "year"];
46+
47+
[
48+
["en", "gregory", "latn", "6/21/2018", enUSTypes],
49+
["en-GB", "gregory", "latn", "21/06/2018", enGBTypes],
50+
["en-u-nu-deva", "gregory", "deva", "६/२१/२०१८", enUSTypes],
51+
["en-u-ca-chinese", "chinese", "latn", "5/8/35", enUSTypes],
52+
["en-GB-u-ca-chinese", "chinese", "latn", "08/05/35", enGBTypes],
53+
["en-u-ca-chinese-nu-deva", "chinese", "deva", "५/८/३५", enUSTypes],
54+
].forEach(function(entry) {
55+
checkFormat(entry[0], {},
56+
{ cal: entry[1],
57+
numSys: entry[2],
58+
formatted: entry[3],
59+
types: entry[4],
60+
});
61+
});

0 commit comments

Comments
 (0)