Make BDateFormat mutable

* Add setters for the language and formatting conventions
* Add shortcut getter and setter for the date format
* Use those in the locale roster to make the BDateFormat actually use
the system preferred language and format.
* Applications can also use this to extract specific information from
the system format (eg. set date format to "LLLL" to extract month
names), or define specific formats more easily (eg. for parsing and
generating e-mail headers or HTTP cookies).
This commit is contained in:
Adrien Destugues 2014-09-30 11:49:47 +02:00
parent f188c1defa
commit 526d483999
3 changed files with 52 additions and 0 deletions

View File

@ -37,6 +37,14 @@ public:
static const BDateFormat* Default();
void SetLanguage(const BLanguage& newLanguage);
void SetFormattingConventions(
const BFormattingConventions& conventions);
status_t GetDateFormat(BDateFormatStyle style,
BString& outFormat) const;
void SetDateFormat(BDateFormatStyle style,
const BString& format);
// formatting
ssize_t Format(char* string, const size_t maxSize,

View File

@ -4,6 +4,7 @@
*
* Authors:
* Oliver Tappe <zooey@hirschkaefer.de>
* Adrien Desutugues <pulkomandy@pulkomandy.tk>
*/
#include <DateFormat.h>
@ -58,6 +59,44 @@ BDateFormat::~BDateFormat()
}
void
BDateFormat::SetFormattingConventions(const BFormattingConventions& conventions)
{
BAutolock lock(fLock);
if (!lock.IsLocked())
return;
fConventions = conventions;
}
void
BDateFormat::SetLanguage(const BLanguage& newLanguage)
{
BAutolock lock(fLock);
if (!lock.IsLocked())
return;
fLanguage = newLanguage;
}
status_t
BDateFormat::GetDateFormat(BDateFormatStyle style,
BString& outFormat) const
{
return fConventions.GetDateFormat(style, outFormat);
}
void
BDateFormat::SetDateFormat(BDateFormatStyle style,
const BString& format)
{
fConventions.SetExplicitDateFormat(style, format);
}
ssize_t
BDateFormat::Format(char* string, const size_t maxSize, const time_t time,
const BDateFormatStyle style) const

View File

@ -467,6 +467,7 @@ LocaleRosterData::_LoadLocaleSettings()
if (status == B_OK) {
BFormattingConventions conventions(&settings);
fDefaultLocale.SetFormattingConventions(conventions);
fDefaultDateFormat.SetFormattingConventions(conventions);
_SetPreferredLanguages(&settings);
@ -485,8 +486,10 @@ LocaleRosterData::_LoadLocaleSettings()
fPreferredLanguages.AddString(kLanguageField, "en");
BLanguage defaultLanguage("en_US");
fDefaultLocale.SetLanguage(defaultLanguage);
fDefaultDateFormat.SetLanguage(defaultLanguage);
BFormattingConventions conventions("en_US");
fDefaultLocale.SetFormattingConventions(conventions);
fDefaultDateFormat.SetFormattingConventions(conventions);
return status;
}
@ -582,6 +585,7 @@ LocaleRosterData::_SetDefaultFormattingConventions(
const BFormattingConventions& newFormattingConventions)
{
fDefaultLocale.SetFormattingConventions(newFormattingConventions);
fDefaultDateFormat.SetFormattingConventions(newFormattingConventions);
UErrorCode icuError = U_ZERO_ERROR;
Locale icuLocale = Locale::createCanonical(newFormattingConventions.ID());
@ -618,6 +622,7 @@ LocaleRosterData::_SetPreferredLanguages(const BMessage* languages)
&& languages->FindString(kLanguageField, &langName) == B_OK) {
fDefaultLocale.SetCollator(BCollator(langName.String()));
fDefaultLocale.SetLanguage(BLanguage(langName.String()));
fDefaultDateFormat.SetLanguage(BLanguage(langName.String()));
fPreferredLanguages.RemoveName(kLanguageField);
for (int i = 0; languages->FindString(kLanguageField, i, &langName)