ICU-11985 Handle 'j' and 'J' in date time pattern skeletons.

R=mark.edward.davis@gmail.com, markus.icu@gmail.com, pedberg@apple.com

Review URL: https://codereview.appspot.com/279780043 .

X-SVN-Rev: 38089
This commit is contained in:
Fredrik Roubert 2015-11-18 12:29:17 +00:00
parent d1b77c4e66
commit 25f9110090
7 changed files with 73 additions and 2 deletions

View File

@ -202,6 +202,8 @@ static const dtTypeElem dtTypes[] = {
{LOW_X, UDATPG_ZONE_FIELD, DT_NARROW - DT_DELTA, 1, 0},
{LOW_X, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 2, 0},
{LOW_X, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0},
{LOW_J, UDATPG_HOUR_FIELD, DT_NUMERIC, 1, 2}, // 12/24 hour
{CAP_J, UDATPG_HOUR_FIELD, DT_NUMERIC, 1, 2}, // 12/24 hour no AM/PM
{0, UDATPG_FIELD_COUNT, 0, 0, 0} , // last row of dtTypes[]
};

View File

@ -113,6 +113,8 @@ void DateFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &nam
TESTCASE_AUTO(TestDFSCreateForLocaleWithCalendarInLocale);
TESTCASE_AUTO(TestChangeCalendar);
TESTCASE_AUTO(TestPatternFromSkeleton);
TESTCASE_AUTO_END;
}
@ -4835,6 +4837,33 @@ void DateFormatTest::TestChangeCalendar() {
assertEquals("format yMMMd", "Iyar 29, 5758", result);
}
void DateFormatTest::TestPatternFromSkeleton() {
static const struct {
const Locale& locale;
const char* const skeleton;
const char* const pattern;
} TESTDATA[] = {
// Ticket #11985
{Locale::getEnglish(), "jjmm", "h:mm a"},
{Locale::getEnglish(), "JJmm", "hh:mm"},
{Locale::getGerman(), "jjmm", "HH:mm"},
{Locale::getGerman(), "JJmm", "HH:mm"}
};
for (size_t i = 0; i < sizeof TESTDATA / sizeof *TESTDATA; i++) {
UErrorCode status = U_ZERO_ERROR;
LocalPointer<DateFormat> fmt(
DateFormat::createInstanceForSkeleton(
TESTDATA[i].skeleton, TESTDATA[i].locale, status));
if (!assertSuccess("createInstanceForSkeleton", status)) {
return;
}
UnicodeString pattern;
static_cast<const SimpleDateFormat*>(fmt.getAlias())->toPattern(pattern);
assertEquals("Format pattern", TESTDATA[i].pattern, pattern);
}
}
#endif /* #if !UCONFIG_NO_FORMATTING */

View File

@ -1,6 +1,6 @@
/********************************************************************
* COPYRIGHT:
* Copyright (c) 1997-2014, International Business Machines Corporation and
* Copyright (c) 1997-2015, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
@ -250,6 +250,8 @@ public:
void TestDFSCreateForLocaleWithCalendarInLocale();
void TestChangeCalendar();
void TestPatternFromSkeleton();
private:
UBool showParse(DateFormat &format, const UnicodeString &formattedString);

View File

@ -50,6 +50,7 @@ void DateIntervalFormatTest::runIndexedTest( int32_t index, UBool exec, const ch
TESTCASE(4, testYearFormats);
TESTCASE(5, testStress);
TESTCASE(6, testTicket11583_2);
TESTCASE(7, testTicket11985);
default: name = ""; break;
}
}
@ -1530,4 +1531,17 @@ void DateIntervalFormatTest::testTicket11583_2() {
}
}
void DateIntervalFormatTest::testTicket11985() {
UErrorCode status = U_ZERO_ERROR;
LocalPointer<DateIntervalFormat> fmt(
DateIntervalFormat::createInstance(UDAT_HOUR_MINUTE, Locale::getEnglish(), status));
if (!assertSuccess("createInstance", status)) {
return;
}
UnicodeString pattern;
static_cast<const SimpleDateFormat*>(fmt->getDateFormat())->toPattern(pattern);
assertEquals("Format pattern", "h:mm a", pattern);
}
#endif /* #if !UCONFIG_NO_FORMATTING */

View File

@ -54,6 +54,8 @@ public:
void testTicket11583_2();
void testTicket11985();
private:
/**
* Test formatting against expected result

View File

@ -30,6 +30,7 @@ void IntlTestDateTimePatternGeneratorAPI::runIndexedTest( int32_t index, UBool e
TESTCASE(0, testAPI);
TESTCASE(1, testOptions);
TESTCASE(2, testAllFieldPatterns);
TESTCASE(3, testStaticGetSkeleton);
default: name = ""; break;
}
}
@ -1057,4 +1058,24 @@ void IntlTestDateTimePatternGeneratorAPI::testAllFieldPatterns(/*char *par*/)
}
}
}
void IntlTestDateTimePatternGeneratorAPI::testStaticGetSkeleton(/*char *par*/)
{
// Verify that staticGetSkeleton() doesn't mangle skeletons. (Ticket #11985)
static const char* const testData[] = {
"jmm",
"jjmm",
"Jmm",
"JJmm"
};
for (size_t i = 0; i < sizeof testData / sizeof *testData; i++) {
UErrorCode status = U_ZERO_ERROR;
UnicodeString skeleton = DateTimePatternGenerator::staticGetSkeleton(testData[i], status);
if (!assertSuccess("staticGetSkeleton", status)) {
return;
}
assertEquals("Skeleton", testData[i], skeleton);
}
}
#endif /* #if !UCONFIG_NO_FORMATTING */

View File

@ -1,6 +1,6 @@
/********************************************************************
* COPYRIGHT:
* Copyright (c) 1997-2001,2009,2013, International Business Machines Corporation and
* Copyright (c) 1997-2001,2009,2013,2015 International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
@ -26,6 +26,7 @@ private:
void testAPI(/* char* par */);
void testOptions(/* char* par */);
void testAllFieldPatterns(/* char* par */);
void testStaticGetSkeleton(/* char* par */);
};
#endif /* #if !UCONFIG_NO_FORMATTING */