ICU-13215 DayPeriodRules.getInstance should ignore keywords to get correct data and not hang

X-SVN-Rev: 40196
This commit is contained in:
Peter Edberg 2017-06-23 05:02:12 +00:00
parent acd53b0c18
commit d515102c5a
4 changed files with 19 additions and 2 deletions
icu4c/source
icu4j/main
classes/core/src/com/ibm/icu/impl
tests/core/src/com/ibm/icu/dev/test/format

View file

@ -340,7 +340,7 @@ const DayPeriodRules *DayPeriodRules::getInstance(const Locale &locale, UErrorCo
// does), return NULL.
if(U_FAILURE(errorCode)) { return NULL; }
const char *localeCode = locale.getName();
const char *localeCode = locale.getBaseName();
char name[ULOC_FULLNAME_CAPACITY];
char parentName[ULOC_FULLNAME_CAPACITY];

View file

@ -5251,6 +5251,15 @@ void DateFormatTest::TestDayPeriodWithLocales() {
sdf.applyPattern(UnicodeString("hh:mm:ss BBBB"));
assertEquals("hh:mm:ss BBBB | 01:00:00 | es", "01:00:00 de la madrugada",
sdf.format(k010000, out.remove()));
// #13215: for locales with keywords, check hang in DayPeriodRules""getInstance(const Locale, ...),
// which is called in SimpleDateFormat::format for patterns that include 'B'.
sdf = SimpleDateFormat(UnicodeString(), Locale("en@calendar=buddhist"), errorCode);
sdf.setTimeZone(*tz);
sdf.applyPattern(UnicodeString("hh:mm:ss BBBB"));
assertEquals("hh:mm:ss BBBB | 01:00:00 | en@calendar=buddhist", "01:00:00 at night",
sdf.format(k010000, out.remove()));
}
void DateFormatTest::TestMinuteSecondFieldsInOddPlaces() {

View file

@ -242,7 +242,7 @@ public final class DayPeriodRules {
* @return a DayPeriodRules object for `locale`.
*/
public static DayPeriodRules getInstance(ULocale locale) {
String localeCode = locale.getName();
String localeCode = locale.getBaseName();
if (localeCode.isEmpty()) { localeCode = "root"; }
Integer ruleSetNum = null;

View file

@ -5195,6 +5195,14 @@ public class DateFormatTest extends com.ibm.icu.dev.test.TestFmwk {
sdf.applyPattern("hh:mm:ss BBBB");
assertEquals("hh:mm:ss BBBB | 01:00:00 | es", "01:00:00 de la madrugada", sdf.format(k010000));
// #13215: for locales with keywords, check hang in DayPeriodRules.getInstance(ULocale),
// which is called in SimpleDateFormat.format for patterns that include 'B'.
sdf = new SimpleDateFormat("", new ULocale("en@calendar=buddhist"));
sdf.setTimeZone(TimeZone.GMT_ZONE);
sdf.applyPattern("hh:mm:ss BBBB");
assertEquals("hh:mm:ss BBBB | 01:00:00 | en@calendar=buddhist", "01:00:00 at night", sdf.format(k010000));
}
@Test