* BCountry : use lazy initialisation for the date and time formatters, since they are not always used. Makes instancaiating a country faster, and
so, loading the locale preflet is also faster and use less memory. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@37696 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -68,12 +68,12 @@ class BCountry {
|
|||||||
status_t DateFields(BDateElement*& fields, int& fieldCount,
|
status_t DateFields(BDateElement*& fields, int& fieldCount,
|
||||||
bool longFormat);
|
bool longFormat);
|
||||||
|
|
||||||
bool DateFormat(BString&, bool longFormat) const;
|
bool DateFormat(BString&, bool longFormat);
|
||||||
void SetDateFormat(const char* formatString,
|
void SetDateFormat(const char* formatString,
|
||||||
bool longFormat = true);
|
bool longFormat = true);
|
||||||
void SetTimeFormat(const char* formatString,
|
void SetTimeFormat(const char* formatString,
|
||||||
bool longFormat = true);
|
bool longFormat = true);
|
||||||
bool TimeFormat(BString&, bool longFormat) const;
|
bool TimeFormat(BString&, bool longFormat);
|
||||||
|
|
||||||
int StartOfWeek();
|
int StartOfWeek();
|
||||||
|
|
||||||
@@ -112,6 +112,9 @@ class BCountry {
|
|||||||
status_t GetTimeZones(BMessage* timezones);
|
status_t GetTimeZones(BMessage* timezones);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
icu_44::DateFormat* DateFormatter(bool longFormat);
|
||||||
|
icu_44::DateFormat* TimeFormatter(bool longFormat);
|
||||||
|
|
||||||
icu_44::DateFormat* fICULongDateFormatter;
|
icu_44::DateFormat* fICULongDateFormatter;
|
||||||
icu_44::DateFormat* fICUShortDateFormatter;
|
icu_44::DateFormat* fICUShortDateFormatter;
|
||||||
icu_44::DateFormat* fICULongTimeFormatter;
|
icu_44::DateFormat* fICULongTimeFormatter;
|
||||||
|
|||||||
+106
-66
@@ -38,42 +38,58 @@ using BPrivate::B_WEEK_START_SUNDAY;
|
|||||||
BCountry::BCountry(const char* languageCode, const char* countryCode)
|
BCountry::BCountry(const char* languageCode, const char* countryCode)
|
||||||
{
|
{
|
||||||
fICULocale = new ICU_VERSION::Locale(languageCode, countryCode);
|
fICULocale = new ICU_VERSION::Locale(languageCode, countryCode);
|
||||||
fICULongDateFormatter = DateFormat::createDateInstance(
|
fICULongDateFormatter = NULL;
|
||||||
DateFormat::FULL, *fICULocale);
|
fICUShortDateFormatter = NULL;
|
||||||
fICUShortDateFormatter = DateFormat::createDateInstance(
|
fICULongTimeFormatter = NULL;
|
||||||
DateFormat::SHORT, *fICULocale);
|
fICUShortTimeFormatter = NULL;
|
||||||
fICULongTimeFormatter = DateFormat::createTimeInstance(
|
|
||||||
DateFormat::MEDIUM, *fICULocale);
|
|
||||||
fICUShortTimeFormatter = DateFormat::createTimeInstance(
|
|
||||||
DateFormat::SHORT, *fICULocale);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
BCountry::BCountry(const char* languageAndCountryCode)
|
BCountry::BCountry(const char* languageAndCountryCode)
|
||||||
{
|
{
|
||||||
fICULocale = new ICU_VERSION::Locale(languageAndCountryCode);
|
fICULocale = new ICU_VERSION::Locale(languageAndCountryCode);
|
||||||
fICULongDateFormatter = DateFormat::createDateInstance(
|
|
||||||
DateFormat::FULL, *fICULocale);
|
// These will be initialized the first time they are actually needed
|
||||||
fICUShortDateFormatter = DateFormat::createDateInstance(
|
// this way the class is more lightweight when you just want the country
|
||||||
DateFormat::SHORT, *fICULocale);
|
// name or some other information
|
||||||
fICULongTimeFormatter = DateFormat::createTimeInstance(
|
fICULongDateFormatter = NULL;
|
||||||
DateFormat::MEDIUM, *fICULocale);
|
fICUShortDateFormatter = NULL;
|
||||||
fICUShortTimeFormatter = DateFormat::createTimeInstance(
|
fICULongTimeFormatter = NULL;
|
||||||
DateFormat::SHORT, *fICULocale);
|
fICUShortTimeFormatter = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
BCountry::BCountry(const BCountry& other)
|
BCountry::BCountry(const BCountry& other)
|
||||||
{
|
{
|
||||||
fICULocale = new ICU_VERSION::Locale(*other.fICULocale);
|
fICULocale = new ICU_VERSION::Locale(*other.fICULocale);
|
||||||
fICULongDateFormatter = new ICU_VERSION::SimpleDateFormat(
|
|
||||||
*static_cast<SimpleDateFormat*>(other.fICULongDateFormatter));
|
// We could copy these, but we can recreate them when needed from the locale anyway
|
||||||
fICUShortDateFormatter = new ICU_VERSION::SimpleDateFormat(
|
fICULongDateFormatter = NULL;
|
||||||
*static_cast<SimpleDateFormat*>(other.fICUShortDateFormatter));
|
fICUShortDateFormatter = NULL;
|
||||||
fICULongTimeFormatter = new ICU_VERSION::SimpleDateFormat(
|
fICULongTimeFormatter = NULL;
|
||||||
*static_cast<SimpleDateFormat*>(other.fICULongTimeFormatter));
|
fICUShortTimeFormatter = NULL;
|
||||||
fICUShortTimeFormatter = new ICU_VERSION::SimpleDateFormat(
|
|
||||||
*static_cast<SimpleDateFormat*>(other.fICUShortTimeFormatter));
|
/*
|
||||||
|
if (other.fICULongDateFormatter) {
|
||||||
|
fICULongDateFormatter = new ICU_VERSION::SimpleDateFormat(
|
||||||
|
*static_cast<SimpleDateFormat*>(other.fICULongDateFormatter));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (other.fICUShortDateFormatter) {
|
||||||
|
fICUShortDateFormatter = new ICU_VERSION::SimpleDateFormat(
|
||||||
|
*static_cast<SimpleDateFormat*>(other.fICUShortDateFormatter));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (other.fICULongTimeFormatter) {
|
||||||
|
fICULongTimeFormatter = new ICU_VERSION::SimpleDateFormat(
|
||||||
|
*static_cast<SimpleDateFormat*>(other.fICULongTimeFormatter));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (other.fICUShortTimeFormatter) {
|
||||||
|
fICUShortTimeFormatter = new ICU_VERSION::SimpleDateFormat(
|
||||||
|
*static_cast<SimpleDateFormat*>(other.fICUShortTimeFormatter));
|
||||||
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -90,14 +106,12 @@ BCountry::operator=(const BCountry& other)
|
|||||||
delete fICULocale;
|
delete fICULocale;
|
||||||
|
|
||||||
fICULocale = new ICU_VERSION::Locale(*other.fICULocale);
|
fICULocale = new ICU_VERSION::Locale(*other.fICULocale);
|
||||||
fICULongDateFormatter = new ICU_VERSION::SimpleDateFormat(
|
|
||||||
*static_cast<SimpleDateFormat*>(other.fICULongDateFormatter));
|
// We could copy these, but we can recreate them when needed from the locale anyway
|
||||||
fICUShortDateFormatter = new ICU_VERSION::SimpleDateFormat(
|
fICULongDateFormatter = NULL;
|
||||||
*static_cast<SimpleDateFormat*>(other.fICUShortDateFormatter));
|
fICUShortDateFormatter = NULL;
|
||||||
fICULongTimeFormatter = new ICU_VERSION::SimpleDateFormat(
|
fICULongTimeFormatter = NULL;
|
||||||
*static_cast<SimpleDateFormat*>(other.fICULongTimeFormatter));
|
fICUShortTimeFormatter = NULL;
|
||||||
fICUShortTimeFormatter = new ICU_VERSION::SimpleDateFormat(
|
|
||||||
*static_cast<SimpleDateFormat*>(other.fICUShortTimeFormatter));
|
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -165,6 +179,47 @@ BCountry::GetIcon(BBitmap* result)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// #pragma mark - Date and Time
|
||||||
|
|
||||||
|
|
||||||
|
DateFormat*
|
||||||
|
BCountry::DateFormatter(bool longFormat)
|
||||||
|
{
|
||||||
|
if (longFormat) {
|
||||||
|
if (fICULongDateFormatter == NULL) {
|
||||||
|
fICULongDateFormatter = DateFormat::createDateInstance(
|
||||||
|
DateFormat::FULL, *fICULocale);
|
||||||
|
}
|
||||||
|
return fICULongDateFormatter;
|
||||||
|
} else {
|
||||||
|
if (fICUShortDateFormatter == NULL) {
|
||||||
|
fICUShortDateFormatter = DateFormat::createDateInstance(
|
||||||
|
DateFormat::SHORT, *fICULocale);
|
||||||
|
}
|
||||||
|
return fICUShortDateFormatter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
DateFormat*
|
||||||
|
BCountry::TimeFormatter(bool longFormat)
|
||||||
|
{
|
||||||
|
if (longFormat) {
|
||||||
|
if (fICULongTimeFormatter == NULL) {
|
||||||
|
fICULongTimeFormatter = DateFormat::createTimeInstance(
|
||||||
|
DateFormat::MEDIUM, *fICULocale);
|
||||||
|
}
|
||||||
|
return fICULongTimeFormatter;
|
||||||
|
} else {
|
||||||
|
if (fICUShortTimeFormatter == NULL) {
|
||||||
|
fICUShortTimeFormatter = DateFormat::createTimeInstance(
|
||||||
|
DateFormat::SHORT, *fICULocale);
|
||||||
|
}
|
||||||
|
return fICUShortTimeFormatter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
BCountry::FormatDate(char* string, size_t maxSize, time_t time, bool longFormat)
|
BCountry::FormatDate(char* string, size_t maxSize, time_t time, bool longFormat)
|
||||||
{
|
{
|
||||||
@@ -179,10 +234,9 @@ BCountry::FormatDate(BString *string, time_t time, bool longFormat)
|
|||||||
{
|
{
|
||||||
// TODO: ICU allows for 4 different levels of expansion :
|
// TODO: ICU allows for 4 different levels of expansion :
|
||||||
// short, medium, long, and full. Our bool parameter is not enough...
|
// short, medium, long, and full. Our bool parameter is not enough...
|
||||||
ICU_VERSION::DateFormat* dateFormatter
|
|
||||||
= longFormat ? fICULongDateFormatter : fICUShortDateFormatter;
|
|
||||||
UnicodeString ICUString;
|
UnicodeString ICUString;
|
||||||
ICUString = dateFormatter->format((UDate)time * 1000, ICUString);
|
ICUString = DateFormatter(longFormat)->format((UDate)time * 1000,
|
||||||
|
ICUString);
|
||||||
|
|
||||||
string->Truncate(0);
|
string->Truncate(0);
|
||||||
BStringByteSink stringConverter(string);
|
BStringByteSink stringConverter(string);
|
||||||
@@ -197,11 +251,9 @@ BCountry::FormatDate(BString* string, int*& fieldPositions, int& fieldCount,
|
|||||||
{
|
{
|
||||||
fieldPositions = NULL;
|
fieldPositions = NULL;
|
||||||
UErrorCode error = U_ZERO_ERROR;
|
UErrorCode error = U_ZERO_ERROR;
|
||||||
ICU_VERSION::DateFormat* dateFormatter;
|
|
||||||
ICU_VERSION::FieldPositionIterator positionIterator;
|
ICU_VERSION::FieldPositionIterator positionIterator;
|
||||||
dateFormatter = longFormat ? fICULongDateFormatter : fICUShortDateFormatter;
|
|
||||||
UnicodeString ICUString;
|
UnicodeString ICUString;
|
||||||
ICUString = dateFormatter->format((UDate)time * 1000, ICUString,
|
ICUString = DateFormatter(longFormat)->format((UDate)time * 1000, ICUString,
|
||||||
&positionIterator, error);
|
&positionIterator, error);
|
||||||
|
|
||||||
if (error != U_ZERO_ERROR)
|
if (error != U_ZERO_ERROR)
|
||||||
@@ -244,10 +296,9 @@ BCountry::FormatTime(BString* string, time_t time, bool longFormat)
|
|||||||
{
|
{
|
||||||
// TODO: ICU allows for 4 different levels of expansion :
|
// TODO: ICU allows for 4 different levels of expansion :
|
||||||
// short, medium, long, and full. Our bool parameter is not enough...
|
// short, medium, long, and full. Our bool parameter is not enough...
|
||||||
ICU_VERSION::DateFormat* timeFormatter;
|
|
||||||
timeFormatter = longFormat ? fICULongTimeFormatter : fICUShortTimeFormatter;
|
|
||||||
UnicodeString ICUString;
|
UnicodeString ICUString;
|
||||||
ICUString = timeFormatter->format((UDate)time * 1000, ICUString);
|
ICUString = TimeFormatter(longFormat)->format((UDate)time * 1000,
|
||||||
|
ICUString);
|
||||||
|
|
||||||
string->Truncate(0);
|
string->Truncate(0);
|
||||||
BStringByteSink stringConverter(string);
|
BStringByteSink stringConverter(string);
|
||||||
@@ -262,11 +313,9 @@ BCountry::FormatTime(BString* string, int*& fieldPositions, int& fieldCount,
|
|||||||
{
|
{
|
||||||
fieldPositions = NULL;
|
fieldPositions = NULL;
|
||||||
UErrorCode error = U_ZERO_ERROR;
|
UErrorCode error = U_ZERO_ERROR;
|
||||||
ICU_VERSION::DateFormat* timeFormatter;
|
|
||||||
ICU_VERSION::FieldPositionIterator positionIterator;
|
ICU_VERSION::FieldPositionIterator positionIterator;
|
||||||
timeFormatter = longFormat ? fICULongTimeFormatter : fICUShortTimeFormatter;
|
|
||||||
UnicodeString ICUString;
|
UnicodeString ICUString;
|
||||||
ICUString = timeFormatter->format((UDate)time * 1000, ICUString,
|
ICUString = TimeFormatter(longFormat)->format((UDate)time * 1000, ICUString,
|
||||||
&positionIterator, error);
|
&positionIterator, error);
|
||||||
|
|
||||||
if (error != U_ZERO_ERROR)
|
if (error != U_ZERO_ERROR)
|
||||||
@@ -300,13 +349,11 @@ BCountry::TimeFields(BDateElement*& fields, int& fieldCount, bool longFormat)
|
|||||||
{
|
{
|
||||||
fields = NULL;
|
fields = NULL;
|
||||||
UErrorCode error = U_ZERO_ERROR;
|
UErrorCode error = U_ZERO_ERROR;
|
||||||
ICU_VERSION::DateFormat* timeFormatter;
|
|
||||||
ICU_VERSION::FieldPositionIterator positionIterator;
|
ICU_VERSION::FieldPositionIterator positionIterator;
|
||||||
timeFormatter = longFormat ? fICULongTimeFormatter : fICUShortTimeFormatter;
|
|
||||||
UnicodeString ICUString;
|
UnicodeString ICUString;
|
||||||
time_t now;
|
time_t now;
|
||||||
ICUString = timeFormatter->format((UDate)time(&now) * 1000, ICUString,
|
ICUString = TimeFormatter(longFormat)->format((UDate)time(&now) * 1000,
|
||||||
&positionIterator, error);
|
ICUString, &positionIterator, error);
|
||||||
|
|
||||||
if (error != U_ZERO_ERROR)
|
if (error != U_ZERO_ERROR)
|
||||||
return B_ERROR;
|
return B_ERROR;
|
||||||
@@ -353,13 +400,11 @@ BCountry::DateFields(BDateElement*& fields, int& fieldCount, bool longFormat)
|
|||||||
{
|
{
|
||||||
fields = NULL;
|
fields = NULL;
|
||||||
UErrorCode error = U_ZERO_ERROR;
|
UErrorCode error = U_ZERO_ERROR;
|
||||||
ICU_VERSION::DateFormat* dateFormatter;
|
|
||||||
ICU_VERSION::FieldPositionIterator positionIterator;
|
ICU_VERSION::FieldPositionIterator positionIterator;
|
||||||
dateFormatter = longFormat ? fICULongDateFormatter : fICUShortDateFormatter;
|
|
||||||
UnicodeString ICUString;
|
UnicodeString ICUString;
|
||||||
time_t now;
|
time_t now;
|
||||||
ICUString = dateFormatter->format((UDate)time(&now) * 1000, ICUString,
|
ICUString = DateFormatter(longFormat)->format((UDate)time(&now) * 1000,
|
||||||
&positionIterator, error);
|
ICUString, &positionIterator, error);
|
||||||
|
|
||||||
if (error != U_ZERO_ERROR)
|
if (error != U_ZERO_ERROR)
|
||||||
return B_ERROR;
|
return B_ERROR;
|
||||||
@@ -396,12 +441,10 @@ BCountry::DateFields(BDateElement*& fields, int& fieldCount, bool longFormat)
|
|||||||
|
|
||||||
|
|
||||||
bool
|
bool
|
||||||
BCountry::DateFormat(BString& format, bool longFormat) const
|
BCountry::DateFormat(BString& format, bool longFormat)
|
||||||
{
|
{
|
||||||
ICU_VERSION::DateFormat* dateFormatter
|
|
||||||
= longFormat ? fICULongDateFormatter : fICUShortDateFormatter;
|
|
||||||
SimpleDateFormat* dateFormatterImpl
|
SimpleDateFormat* dateFormatterImpl
|
||||||
= static_cast<SimpleDateFormat*>(dateFormatter);
|
= static_cast<SimpleDateFormat*>(DateFormatter(longFormat));
|
||||||
|
|
||||||
UnicodeString ICUString;
|
UnicodeString ICUString;
|
||||||
ICUString = dateFormatterImpl->toPattern(ICUString);
|
ICUString = dateFormatterImpl->toPattern(ICUString);
|
||||||
@@ -417,10 +460,8 @@ BCountry::DateFormat(BString& format, bool longFormat) const
|
|||||||
void
|
void
|
||||||
BCountry::SetDateFormat(const char* formatString, bool longFormat)
|
BCountry::SetDateFormat(const char* formatString, bool longFormat)
|
||||||
{
|
{
|
||||||
ICU_VERSION::DateFormat* dateFormatter
|
|
||||||
= longFormat ? fICULongDateFormatter : fICUShortDateFormatter;
|
|
||||||
SimpleDateFormat* dateFormatterImpl
|
SimpleDateFormat* dateFormatterImpl
|
||||||
= static_cast<SimpleDateFormat*>(dateFormatter);
|
= static_cast<SimpleDateFormat*>(DateFormatter(longFormat));
|
||||||
|
|
||||||
UnicodeString pattern(formatString);
|
UnicodeString pattern(formatString);
|
||||||
dateFormatterImpl->applyPattern(pattern);
|
dateFormatterImpl->applyPattern(pattern);
|
||||||
@@ -430,10 +471,8 @@ BCountry::SetDateFormat(const char* formatString, bool longFormat)
|
|||||||
void
|
void
|
||||||
BCountry::SetTimeFormat(const char* formatString, bool longFormat)
|
BCountry::SetTimeFormat(const char* formatString, bool longFormat)
|
||||||
{
|
{
|
||||||
ICU_VERSION::DateFormat* dateFormatter
|
|
||||||
= longFormat ? fICULongTimeFormatter : fICUShortTimeFormatter;
|
|
||||||
SimpleDateFormat* dateFormatterImpl
|
SimpleDateFormat* dateFormatterImpl
|
||||||
= static_cast<SimpleDateFormat*>(dateFormatter);
|
= static_cast<SimpleDateFormat*>(DateFormatter(longFormat));
|
||||||
|
|
||||||
UnicodeString pattern(formatString);
|
UnicodeString pattern(formatString);
|
||||||
dateFormatterImpl->applyPattern(pattern);
|
dateFormatterImpl->applyPattern(pattern);
|
||||||
@@ -441,12 +480,10 @@ BCountry::SetTimeFormat(const char* formatString, bool longFormat)
|
|||||||
|
|
||||||
|
|
||||||
bool
|
bool
|
||||||
BCountry::TimeFormat(BString& format, bool longFormat) const
|
BCountry::TimeFormat(BString& format, bool longFormat)
|
||||||
{
|
{
|
||||||
ICU_VERSION::DateFormat* dateFormatter;
|
|
||||||
dateFormatter = longFormat ? fICULongTimeFormatter : fICUShortTimeFormatter;
|
|
||||||
SimpleDateFormat* dateFormatterImpl
|
SimpleDateFormat* dateFormatterImpl
|
||||||
= static_cast<SimpleDateFormat*>(dateFormatter);
|
= static_cast<SimpleDateFormat*>(DateFormatter(longFormat));
|
||||||
|
|
||||||
UnicodeString ICUString;
|
UnicodeString ICUString;
|
||||||
ICUString = dateFormatterImpl->toPattern(ICUString);
|
ICUString = dateFormatterImpl->toPattern(ICUString);
|
||||||
@@ -476,6 +513,9 @@ BCountry::StartOfWeek()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// #pragma mark - Numbers
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
BCountry::FormatNumber(char* string, size_t maxSize, double value)
|
BCountry::FormatNumber(char* string, size_t maxSize, double value)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user