From 090a0d76869e3b5e7ba7024a44b1ebcf84e23bed Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Tue, 7 Oct 2014 16:08:22 +0200 Subject: [PATCH] Add an API to get month names. --- headers/os/locale/DateFormat.h | 1 + src/kits/locale/DateFormat.cpp | 36 ++++++++++++++++++++++++ src/tests/kits/locale/DateFormatTest.cpp | 23 +++++++++++++++ src/tests/kits/locale/DateFormatTest.h | 1 + 4 files changed, 61 insertions(+) diff --git a/headers/os/locale/DateFormat.h b/headers/os/locale/DateFormat.h index b05227cf1c..1789768360 100644 --- a/headers/os/locale/DateFormat.h +++ b/headers/os/locale/DateFormat.h @@ -69,6 +69,7 @@ public: ) const; status_t GetStartOfWeek(BWeekday* weekday) const; + status_t GetMonthName(int month, BString& outName); // TODO parsing diff --git a/src/kits/locale/DateFormat.cpp b/src/kits/locale/DateFormat.cpp index d0b4c8347b..2ca66c7da0 100644 --- a/src/kits/locale/DateFormat.cpp +++ b/src/kits/locale/DateFormat.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -324,6 +325,41 @@ BDateFormat::GetStartOfWeek(BWeekday* startOfWeek) const } +status_t +BDateFormat::GetMonthName(int month, BString& outName) +{ + BAutolock lock(fLock); + if (!lock.IsLocked()) + return B_ERROR; + + BString pattern; + fConventions.GetDateFormat(B_LONG_DATE_FORMAT, pattern); + DateFormat* format = _CreateDateFormatter(pattern); + + SimpleDateFormat* simpleFormat = dynamic_cast(format); + if (simpleFormat == NULL) { + delete format; + return B_ERROR; + } + + const DateFormatSymbols* symbols = simpleFormat->getDateFormatSymbols(); + + int32_t count; + const UnicodeString* names = symbols->getMonths(count); + + if (month > count || month <= 0) { + delete simpleFormat; + return B_BAD_DATA; + } + + BStringByteSink stringConverter(&outName); + names[month - 1].toUTF8(stringConverter); + + delete simpleFormat; + return B_OK; +} + + DateFormat* BDateFormat::_CreateDateFormatter(const BString& format) const { diff --git a/src/tests/kits/locale/DateFormatTest.cpp b/src/tests/kits/locale/DateFormatTest.cpp index f95c9056f0..9430c2e9ef 100644 --- a/src/tests/kits/locale/DateFormatTest.cpp +++ b/src/tests/kits/locale/DateFormatTest.cpp @@ -123,6 +123,27 @@ DateFormatTest::TestFormatDate() } +void +DateFormatTest::TestMonthNames() +{ + BLanguage language("en"); + BFormattingConventions formatting("en_US"); + BDateFormat format(&language, &formatting); + + BString buffer; + status_t result = format.GetMonthName(1, buffer); + + CPPUNIT_ASSERT_EQUAL(BString("January"), buffer); + CPPUNIT_ASSERT_EQUAL(B_OK, result); + + buffer.Truncate(0); + result = format.GetMonthName(12, buffer); + + CPPUNIT_ASSERT_EQUAL(BString("December"), buffer); + CPPUNIT_ASSERT_EQUAL(B_OK, result); +} + + /*static*/ void DateFormatTest::AddTests(BTestSuite& parent) { @@ -134,6 +155,8 @@ DateFormatTest::AddTests(BTestSuite& parent) "DateFormatTest::TestFormat", &DateFormatTest::TestFormat)); suite.addTest(new CppUnit::TestCaller( "DateFormatTest::TestFormatDate", &DateFormatTest::TestFormatDate)); + suite.addTest(new CppUnit::TestCaller( + "DateFormatTest::TestMonthNames", &DateFormatTest::TestMonthNames)); parent.addTest("DateFormatTest", &suite); } diff --git a/src/tests/kits/locale/DateFormatTest.h b/src/tests/kits/locale/DateFormatTest.h index a5d9172af1..b0e52d4be8 100644 --- a/src/tests/kits/locale/DateFormatTest.h +++ b/src/tests/kits/locale/DateFormatTest.h @@ -18,6 +18,7 @@ public: void TestCustomFormat(); void TestFormat(); void TestFormatDate(); + void TestMonthNames(); static void AddTests(BTestSuite& suite); };