From 526d4839991d4dcbb0dc361e7a9f6721481a3ebe Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Tue, 30 Sep 2014 11:49:47 +0200 Subject: [PATCH] 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). --- headers/os/locale/DateFormat.h | 8 ++++++ src/kits/locale/DateFormat.cpp | 39 ++++++++++++++++++++++++++++ src/kits/locale/LocaleRosterData.cpp | 5 ++++ 3 files changed, 52 insertions(+) diff --git a/headers/os/locale/DateFormat.h b/headers/os/locale/DateFormat.h index ec11d7fead..d0b1325ebd 100644 --- a/headers/os/locale/DateFormat.h +++ b/headers/os/locale/DateFormat.h @@ -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, diff --git a/src/kits/locale/DateFormat.cpp b/src/kits/locale/DateFormat.cpp index 8521a42e9b..bf1542f263 100644 --- a/src/kits/locale/DateFormat.cpp +++ b/src/kits/locale/DateFormat.cpp @@ -4,6 +4,7 @@ * * Authors: * Oliver Tappe + * Adrien Desutugues */ #include @@ -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 diff --git a/src/kits/locale/LocaleRosterData.cpp b/src/kits/locale/LocaleRosterData.cpp index 7aa72e2a4e..a632540818 100644 --- a/src/kits/locale/LocaleRosterData.cpp +++ b/src/kits/locale/LocaleRosterData.cpp @@ -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)