[Intl] Fix DefaultHourCycle to skip hHkK in literal

Bug: chromium:925216
Change-Id: I29d71df0c4c7850a80a86cd0719dea04fcc61816
Reviewed-on: https://chromium-review.googlesource.com/c/1436597
Reviewed-by: Sathya Gunasekaran <gsathya@chromium.org>
Commit-Queue: Frank Tang <ftang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59146}
This commit is contained in:
Frank Tang 2019-01-28 12:35:24 -08:00 committed by Commit Bot
parent 4bc5932f29
commit 1be577d48f
2 changed files with 30 additions and 8 deletions

View File

@ -799,14 +799,26 @@ std::unique_ptr<icu::SimpleDateFormat> CreateICUDateFormat(
Intl::HourCycle HourCycleDefault(icu::SimpleDateFormat* date_format) {
icu::UnicodeString pattern;
date_format->toPattern(pattern);
if (pattern.indexOf('K') >= 0) {
return Intl::HourCycle::kH11;
} else if (pattern.indexOf('h') >= 0) {
return Intl::HourCycle::kH12;
} else if (pattern.indexOf('H') >= 0) {
return Intl::HourCycle::kH23;
} else if (pattern.indexOf('k') >= 0) {
return Intl::HourCycle::kH24;
bool in_quote = false;
for (int32_t i = 0; i < pattern.length(); i++) {
char16_t ch = pattern[i];
switch (ch) {
case '\'':
in_quote = !in_quote;
break;
case 'K':
if (!in_quote) return Intl::HourCycle::kH11;
break;
case 'h':
if (!in_quote) return Intl::HourCycle::kH12;
break;
case 'H':
if (!in_quote) return Intl::HourCycle::kH23;
break;
case 'k':
if (!in_quote) return Intl::HourCycle::kH24;
break;
}
}
return Intl::HourCycle::kUndefined;
}

View File

@ -0,0 +1,10 @@
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
assertTrue(new Intl.DateTimeFormat(
"en", { timeZone: 'UTC', hour: 'numeric'}).resolvedOptions().hour12);
assertFalse(new Intl.DateTimeFormat(
"fr", { timeZone: 'UTC', hour: 'numeric'}).resolvedOptions().hour12);
assertFalse(new Intl.DateTimeFormat(
"de", { timeZone: 'UTC', hour: 'numeric'}).resolvedOptions().hour12);