Add an API to get month names.

This commit is contained in:
Adrien Destugues 2014-10-07 16:08:22 +02:00
parent 3051660362
commit 090a0d7686
4 changed files with 61 additions and 0 deletions

View File

@ -69,6 +69,7 @@ public:
) const;
status_t GetStartOfWeek(BWeekday* weekday) const;
status_t GetMonthName(int month, BString& outName);
// TODO parsing

View File

@ -20,6 +20,7 @@
#include <ICUWrapper.h>
#include <unicode/datefmt.h>
#include <unicode/dtfmtsym.h>
#include <unicode/smpdtfmt.h>
#include <vector>
@ -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<SimpleDateFormat*>(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
{

View File

@ -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>(
"DateFormatTest::TestFormatDate", &DateFormatTest::TestFormatDate));
suite.addTest(new CppUnit::TestCaller<DateFormatTest>(
"DateFormatTest::TestMonthNames", &DateFormatTest::TestMonthNames));
parent.addTest("DateFormatTest", &suite);
}

View File

@ -18,6 +18,7 @@ public:
void TestCustomFormat();
void TestFormat();
void TestFormatDate();
void TestMonthNames();
static void AddTests(BTestSuite& suite);
};