From 25f91100902bf22d7895978d803441681cca8b4c Mon Sep 17 00:00:00 2001 From: Fredrik Roubert Date: Wed, 18 Nov 2015 12:29:17 +0000 Subject: [PATCH] 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 --- icu4c/source/i18n/dtptngen.cpp | 2 ++ icu4c/source/test/intltest/dtfmttst.cpp | 29 +++++++++++++++++++++++++ icu4c/source/test/intltest/dtfmttst.h | 4 +++- icu4c/source/test/intltest/dtifmtts.cpp | 14 ++++++++++++ icu4c/source/test/intltest/dtifmtts.h | 2 ++ icu4c/source/test/intltest/dtptngts.cpp | 21 ++++++++++++++++++ icu4c/source/test/intltest/dtptngts.h | 3 ++- 7 files changed, 73 insertions(+), 2 deletions(-) diff --git a/icu4c/source/i18n/dtptngen.cpp b/icu4c/source/i18n/dtptngen.cpp index 0a7d4b3833b..e8287985a51 100644 --- a/icu4c/source/i18n/dtptngen.cpp +++ b/icu4c/source/i18n/dtptngen.cpp @@ -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[] }; diff --git a/icu4c/source/test/intltest/dtfmttst.cpp b/icu4c/source/test/intltest/dtfmttst.cpp index 1329e952fa5..2d8da5f56e4 100644 --- a/icu4c/source/test/intltest/dtfmttst.cpp +++ b/icu4c/source/test/intltest/dtfmttst.cpp @@ -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 fmt( + DateFormat::createInstanceForSkeleton( + TESTDATA[i].skeleton, TESTDATA[i].locale, status)); + if (!assertSuccess("createInstanceForSkeleton", status)) { + return; + } + UnicodeString pattern; + static_cast(fmt.getAlias())->toPattern(pattern); + assertEquals("Format pattern", TESTDATA[i].pattern, pattern); + } +} + #endif /* #if !UCONFIG_NO_FORMATTING */ diff --git a/icu4c/source/test/intltest/dtfmttst.h b/icu4c/source/test/intltest/dtfmttst.h index 19d7c0cb64d..f8b97820930 100644 --- a/icu4c/source/test/intltest/dtfmttst.h +++ b/icu4c/source/test/intltest/dtfmttst.h @@ -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); diff --git a/icu4c/source/test/intltest/dtifmtts.cpp b/icu4c/source/test/intltest/dtifmtts.cpp index 0777fa78c70..02b1f05c79a 100644 --- a/icu4c/source/test/intltest/dtifmtts.cpp +++ b/icu4c/source/test/intltest/dtifmtts.cpp @@ -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 fmt( + DateIntervalFormat::createInstance(UDAT_HOUR_MINUTE, Locale::getEnglish(), status)); + if (!assertSuccess("createInstance", status)) { + return; + } + UnicodeString pattern; + static_cast(fmt->getDateFormat())->toPattern(pattern); + assertEquals("Format pattern", "h:mm a", pattern); +} + #endif /* #if !UCONFIG_NO_FORMATTING */ diff --git a/icu4c/source/test/intltest/dtifmtts.h b/icu4c/source/test/intltest/dtifmtts.h index 3d50f72e6a3..af4318c5fd8 100644 --- a/icu4c/source/test/intltest/dtifmtts.h +++ b/icu4c/source/test/intltest/dtifmtts.h @@ -54,6 +54,8 @@ public: void testTicket11583_2(); + void testTicket11985(); + private: /** * Test formatting against expected result diff --git a/icu4c/source/test/intltest/dtptngts.cpp b/icu4c/source/test/intltest/dtptngts.cpp index 47b3e2a601d..03ca9d4b91e 100644 --- a/icu4c/source/test/intltest/dtptngts.cpp +++ b/icu4c/source/test/intltest/dtptngts.cpp @@ -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 */ diff --git a/icu4c/source/test/intltest/dtptngts.h b/icu4c/source/test/intltest/dtptngts.h index 40c24d7869f..dd368457ba1 100644 --- a/icu4c/source/test/intltest/dtptngts.h +++ b/icu4c/source/test/intltest/dtptngts.h @@ -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 */