From 1be577d48f7255b4ffcc494abb5d352ba713e56e Mon Sep 17 00:00:00 2001 From: Frank Tang Date: Mon, 28 Jan 2019 12:35:24 -0800 Subject: [PATCH] [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 Commit-Queue: Frank Tang Cr-Commit-Position: refs/heads/master@{#59146} --- src/objects/js-date-time-format.cc | 28 ++++++++++++++++++++-------- test/intl/regress-925216.js | 10 ++++++++++ 2 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 test/intl/regress-925216.js diff --git a/src/objects/js-date-time-format.cc b/src/objects/js-date-time-format.cc index 07d2084f81..8b48916772 100644 --- a/src/objects/js-date-time-format.cc +++ b/src/objects/js-date-time-format.cc @@ -799,14 +799,26 @@ std::unique_ptr 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; } diff --git a/test/intl/regress-925216.js b/test/intl/regress-925216.js new file mode 100644 index 0000000000..f9683dfc77 --- /dev/null +++ b/test/intl/regress-925216.js @@ -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);