Add an API to get month names.
This commit is contained in:
@@ -69,6 +69,7 @@ public:
|
|||||||
) const;
|
) const;
|
||||||
|
|
||||||
status_t GetStartOfWeek(BWeekday* weekday) const;
|
status_t GetStartOfWeek(BWeekday* weekday) const;
|
||||||
|
status_t GetMonthName(int month, BString& outName);
|
||||||
|
|
||||||
// TODO parsing
|
// TODO parsing
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
#include <ICUWrapper.h>
|
#include <ICUWrapper.h>
|
||||||
|
|
||||||
#include <unicode/datefmt.h>
|
#include <unicode/datefmt.h>
|
||||||
|
#include <unicode/dtfmtsym.h>
|
||||||
#include <unicode/smpdtfmt.h>
|
#include <unicode/smpdtfmt.h>
|
||||||
|
|
||||||
#include <vector>
|
#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*
|
DateFormat*
|
||||||
BDateFormat::_CreateDateFormatter(const BString& format) const
|
BDateFormat::_CreateDateFormatter(const BString& format) const
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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
|
/*static*/ void
|
||||||
DateFormatTest::AddTests(BTestSuite& parent)
|
DateFormatTest::AddTests(BTestSuite& parent)
|
||||||
{
|
{
|
||||||
@@ -134,6 +155,8 @@ DateFormatTest::AddTests(BTestSuite& parent)
|
|||||||
"DateFormatTest::TestFormat", &DateFormatTest::TestFormat));
|
"DateFormatTest::TestFormat", &DateFormatTest::TestFormat));
|
||||||
suite.addTest(new CppUnit::TestCaller<DateFormatTest>(
|
suite.addTest(new CppUnit::TestCaller<DateFormatTest>(
|
||||||
"DateFormatTest::TestFormatDate", &DateFormatTest::TestFormatDate));
|
"DateFormatTest::TestFormatDate", &DateFormatTest::TestFormatDate));
|
||||||
|
suite.addTest(new CppUnit::TestCaller<DateFormatTest>(
|
||||||
|
"DateFormatTest::TestMonthNames", &DateFormatTest::TestMonthNames));
|
||||||
|
|
||||||
parent.addTest("DateFormatTest", &suite);
|
parent.addTest("DateFormatTest", &suite);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ public:
|
|||||||
void TestCustomFormat();
|
void TestCustomFormat();
|
||||||
void TestFormat();
|
void TestFormat();
|
||||||
void TestFormatDate();
|
void TestFormatDate();
|
||||||
|
void TestMonthNames();
|
||||||
|
|
||||||
static void AddTests(BTestSuite& suite);
|
static void AddTests(BTestSuite& suite);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user