* Bcountry : private methods start with underscore. The timezone list isnow actually a list of BTimeZone objects, that holdsall the needed info, not only the code. Also tweak the listing code to

remove duplicatesfromthe list generated by ICU.
 * BTimeZone : remove Code, rename Name to GetName. The class is likely to change again for further optimization.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@37719 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Adrien Destugues 2010-07-23 16:25:43 +00:00
parent 2774e142e1
commit 282582c005
4 changed files with 75 additions and 75 deletions

View File

@ -2,10 +2,11 @@
#define _COUNTRY_H_ #define _COUNTRY_H_
#include <SupportDefs.h> #include <List.h>
#include <LocaleStrings.h> #include <LocaleStrings.h>
#include <Locker.h> #include <Locker.h>
#include <String.h> #include <String.h>
#include <SupportDefs.h>
class BBitmap; class BBitmap;
@ -112,13 +113,13 @@ class BCountry {
virtual int32 MonFracDigits() const; virtual int32 MonFracDigits() const;
// timezones // timezones
status_t GetTimeZones(BMessage* timezones); int GetTimeZones(BList& timezones);
private: private:
icu_44::DateFormat* LockDateFormatter(bool longFormat); icu_44::DateFormat* _LockDateFormatter(bool longFormat);
icu_44::DateFormat* LockTimeFormatter(bool longFormat); icu_44::DateFormat* _LockTimeFormatter(bool longFormat);
void UnlockDateFormatter(bool longFormat); void _UnlockDateFormatter(bool longFormat);
void UnlockTimeFormatter(bool longFormat); void _UnlockTimeFormatter(bool longFormat);
icu_44::DateFormat* fICULongDateFormatter; icu_44::DateFormat* fICULongDateFormatter;
icu_44::DateFormat* fICUShortDateFormatter; icu_44::DateFormat* fICUShortDateFormatter;

View File

@ -19,8 +19,7 @@ class BTimeZone {
BTimeZone(const char* zoneCode); BTimeZone(const char* zoneCode);
~BTimeZone(); ~BTimeZone();
void Name(BString& name); void GetName(BString& name);
void Code(BString& code);
int OffsetFromGMT(); int OffsetFromGMT();
private: private:

View File

@ -7,15 +7,12 @@
#include <Country.h> #include <Country.h>
#include <assert.h>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <CalendarView.h> #include <CalendarView.h>
#include <IconUtils.h> #include <IconUtils.h>
#include <List.h>
#include <Resources.h> #include <Resources.h>
#include <String.h> #include <String.h>
#include <TimeZone.h>
#include <unicode/datefmt.h> #include <unicode/datefmt.h>
#include <unicode/dcfmtsym.h> #include <unicode/dcfmtsym.h>
@ -24,8 +21,13 @@
#include <unicode/smpdtfmt.h> #include <unicode/smpdtfmt.h>
#include <ICUWrapper.h> #include <ICUWrapper.h>
#include <assert.h>
#include <iostream>
#include <map>
#include <monetary.h> #include <monetary.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdlib.h>
#include <vector>
#define ICU_VERSION icu_44 #define ICU_VERSION icu_44
@ -195,7 +197,7 @@ BCountry::GetIcon(BBitmap* result)
DateFormat* DateFormat*
BCountry::LockDateFormatter(bool longFormat) BCountry::_LockDateFormatter(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...
@ -218,7 +220,7 @@ BCountry::LockDateFormatter(bool longFormat)
void void
BCountry::UnlockDateFormatter(bool longFormat) BCountry::_UnlockDateFormatter(bool longFormat)
{ {
if (longFormat) { if (longFormat) {
fLongDateLock.Unlock(); fLongDateLock.Unlock();
@ -229,7 +231,7 @@ BCountry::UnlockDateFormatter(bool longFormat)
DateFormat* DateFormat*
BCountry::LockTimeFormatter(bool longFormat) BCountry::_LockTimeFormatter(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...
@ -252,7 +254,7 @@ BCountry::LockTimeFormatter(bool longFormat)
void void
BCountry::UnlockTimeFormatter(bool longFormat) BCountry::_UnlockTimeFormatter(bool longFormat)
{ {
if (longFormat) { if (longFormat) {
fLongTimeLock.Unlock(); fLongTimeLock.Unlock();
@ -265,16 +267,16 @@ BCountry::UnlockTimeFormatter(bool longFormat)
status_t status_t
BCountry::FormatDate(char* string, size_t maxSize, time_t time, bool longFormat) BCountry::FormatDate(char* string, size_t maxSize, time_t time, bool longFormat)
{ {
ICU_VERSION::DateFormat* dateFormatter = LockDateFormatter(longFormat); ICU_VERSION::DateFormat* dateFormatter = _LockDateFormatter(longFormat);
if (dateFormatter == NULL) { if (dateFormatter == NULL) {
UnlockDateFormatter(longFormat); _UnlockDateFormatter(longFormat);
return B_NO_MEMORY; return B_NO_MEMORY;
} }
UnicodeString ICUString; UnicodeString ICUString;
ICUString = dateFormatter->format((UDate)time * 1000, ICUString); ICUString = dateFormatter->format((UDate)time * 1000, ICUString);
UnlockDateFormatter(longFormat); _UnlockDateFormatter(longFormat);
CheckedArrayByteSink stringConverter(string, maxSize); CheckedArrayByteSink stringConverter(string, maxSize);
@ -293,15 +295,15 @@ BCountry::FormatDate(BString *string, time_t time, bool longFormat)
string->Truncate(0); string->Truncate(0);
// We make the string empty, this way even in cases where ICU fail we at // We make the string empty, this way even in cases where ICU fail we at
// least return something sane // least return something sane
ICU_VERSION::DateFormat* dateFormatter = LockDateFormatter(longFormat); ICU_VERSION::DateFormat* dateFormatter = _LockDateFormatter(longFormat);
if (dateFormatter == NULL) { if (dateFormatter == NULL) {
UnlockDateFormatter(longFormat); _UnlockDateFormatter(longFormat);
return B_NO_MEMORY; return B_NO_MEMORY;
} }
UnicodeString ICUString; UnicodeString ICUString;
ICUString = dateFormatter->format((UDate)time * 1000, ICUString); ICUString = dateFormatter->format((UDate)time * 1000, ICUString);
UnlockDateFormatter(longFormat); _UnlockDateFormatter(longFormat);
BStringByteSink stringConverter(string); BStringByteSink stringConverter(string);
@ -317,9 +319,9 @@ BCountry::FormatDate(BString* string, int*& fieldPositions, int& fieldCount,
{ {
string->Truncate(0); string->Truncate(0);
ICU_VERSION::DateFormat* dateFormatter = LockDateFormatter(longFormat); ICU_VERSION::DateFormat* dateFormatter = _LockDateFormatter(longFormat);
if (dateFormatter == NULL) { if (dateFormatter == NULL) {
UnlockDateFormatter(longFormat); _UnlockDateFormatter(longFormat);
return B_NO_MEMORY; return B_NO_MEMORY;
} }
@ -329,7 +331,7 @@ BCountry::FormatDate(BString* string, int*& fieldPositions, int& fieldCount,
UnicodeString ICUString; UnicodeString ICUString;
ICUString = dateFormatter->format((UDate)time * 1000, ICUString, ICUString = dateFormatter->format((UDate)time * 1000, ICUString,
&positionIterator, error); &positionIterator, error);
UnlockDateFormatter(longFormat); _UnlockDateFormatter(longFormat);
if (error != U_ZERO_ERROR) if (error != U_ZERO_ERROR)
return B_ERROR; return B_ERROR;
@ -361,9 +363,9 @@ BCountry::DateFormat(BString& format, bool longFormat)
{ {
format.Truncate(0); format.Truncate(0);
ICU_VERSION::DateFormat* dateFormatter = LockDateFormatter(longFormat); ICU_VERSION::DateFormat* dateFormatter = _LockDateFormatter(longFormat);
if (dateFormatter == NULL) { if (dateFormatter == NULL) {
UnlockDateFormatter(longFormat); _UnlockDateFormatter(longFormat);
return B_NO_MEMORY; return B_NO_MEMORY;
} }
@ -372,7 +374,7 @@ BCountry::DateFormat(BString& format, bool longFormat)
UnicodeString ICUString; UnicodeString ICUString;
ICUString = dateFormatterImpl->toPattern(ICUString); ICUString = dateFormatterImpl->toPattern(ICUString);
UnlockDateFormatter(longFormat); _UnlockDateFormatter(longFormat);
BStringByteSink stringConverter(&format); BStringByteSink stringConverter(&format);
@ -385,9 +387,9 @@ BCountry::DateFormat(BString& format, bool longFormat)
status_t status_t
BCountry::SetDateFormat(const char* formatString, bool longFormat) BCountry::SetDateFormat(const char* formatString, bool longFormat)
{ {
ICU_VERSION::DateFormat* dateFormatter = LockDateFormatter(longFormat); ICU_VERSION::DateFormat* dateFormatter = _LockDateFormatter(longFormat);
if (dateFormatter == NULL) { if (dateFormatter == NULL) {
UnlockDateFormatter(longFormat); _UnlockDateFormatter(longFormat);
return B_NO_MEMORY; return B_NO_MEMORY;
} }
@ -396,7 +398,7 @@ BCountry::SetDateFormat(const char* formatString, bool longFormat)
UnicodeString pattern(formatString); UnicodeString pattern(formatString);
dateFormatterImpl->applyPattern(pattern); dateFormatterImpl->applyPattern(pattern);
UnlockDateFormatter(longFormat); _UnlockDateFormatter(longFormat);
return B_OK; return B_OK;
} }
@ -405,9 +407,9 @@ BCountry::SetDateFormat(const char* formatString, bool longFormat)
status_t status_t
BCountry::DateFields(BDateElement*& fields, int& fieldCount, bool longFormat) BCountry::DateFields(BDateElement*& fields, int& fieldCount, bool longFormat)
{ {
ICU_VERSION::DateFormat* dateFormatter = LockDateFormatter(longFormat); ICU_VERSION::DateFormat* dateFormatter = _LockDateFormatter(longFormat);
if (dateFormatter == NULL) { if (dateFormatter == NULL) {
UnlockDateFormatter(longFormat); _UnlockDateFormatter(longFormat);
return B_NO_MEMORY; return B_NO_MEMORY;
} }
@ -418,7 +420,7 @@ BCountry::DateFields(BDateElement*& fields, int& fieldCount, bool longFormat)
time_t now; time_t now;
ICUString = dateFormatter->format((UDate)time(&now) * 1000, ICUString, ICUString = dateFormatter->format((UDate)time(&now) * 1000, ICUString,
&positionIterator, error); &positionIterator, error);
UnlockDateFormatter(longFormat); _UnlockDateFormatter(longFormat);
if (error != U_ZERO_ERROR) if (error != U_ZERO_ERROR)
return B_ERROR; return B_ERROR;
@ -477,15 +479,15 @@ BCountry::StartOfWeek()
status_t status_t
BCountry::FormatTime(char* string, size_t maxSize, time_t time, bool longFormat) BCountry::FormatTime(char* string, size_t maxSize, time_t time, bool longFormat)
{ {
ICU_VERSION::DateFormat* timeFormatter = LockTimeFormatter(longFormat); ICU_VERSION::DateFormat* timeFormatter = _LockTimeFormatter(longFormat);
if (timeFormatter == NULL) { if (timeFormatter == NULL) {
UnlockTimeFormatter(longFormat); _UnlockTimeFormatter(longFormat);
return B_NO_MEMORY; return B_NO_MEMORY;
} }
UnicodeString ICUString; UnicodeString ICUString;
ICUString = timeFormatter->format((UDate)time * 1000, ICUString); ICUString = timeFormatter->format((UDate)time * 1000, ICUString);
UnlockTimeFormatter(longFormat); _UnlockTimeFormatter(longFormat);
CheckedArrayByteSink stringConverter(string, maxSize); CheckedArrayByteSink stringConverter(string, maxSize);
@ -503,15 +505,15 @@ BCountry::FormatTime(BString* string, time_t time, bool longFormat)
{ {
string->Truncate(0); string->Truncate(0);
ICU_VERSION::DateFormat* timeFormatter = LockTimeFormatter(longFormat); ICU_VERSION::DateFormat* timeFormatter = _LockTimeFormatter(longFormat);
if (timeFormatter == NULL) { if (timeFormatter == NULL) {
UnlockTimeFormatter(longFormat); _UnlockTimeFormatter(longFormat);
return B_NO_MEMORY; return B_NO_MEMORY;
} }
UnicodeString ICUString; UnicodeString ICUString;
ICUString = timeFormatter->format((UDate)time * 1000, ICUString); ICUString = timeFormatter->format((UDate)time * 1000, ICUString);
UnlockTimeFormatter(longFormat); _UnlockTimeFormatter(longFormat);
BStringByteSink stringConverter(string); BStringByteSink stringConverter(string);
@ -527,9 +529,9 @@ BCountry::FormatTime(BString* string, int*& fieldPositions, int& fieldCount,
{ {
string->Truncate(0); string->Truncate(0);
ICU_VERSION::DateFormat* timeFormatter = LockTimeFormatter(longFormat); ICU_VERSION::DateFormat* timeFormatter = _LockTimeFormatter(longFormat);
if (timeFormatter == NULL) { if (timeFormatter == NULL) {
UnlockTimeFormatter(longFormat); _UnlockTimeFormatter(longFormat);
return B_NO_MEMORY; return B_NO_MEMORY;
} }
@ -539,7 +541,7 @@ BCountry::FormatTime(BString* string, int*& fieldPositions, int& fieldCount,
UnicodeString ICUString; UnicodeString ICUString;
ICUString = timeFormatter->format((UDate)time * 1000, ICUString, ICUString = timeFormatter->format((UDate)time * 1000, ICUString,
&positionIterator, error); &positionIterator, error);
UnlockTimeFormatter(longFormat); _UnlockTimeFormatter(longFormat);
if (error != U_ZERO_ERROR) if (error != U_ZERO_ERROR)
return B_ERROR; return B_ERROR;
@ -569,9 +571,9 @@ BCountry::FormatTime(BString* string, int*& fieldPositions, int& fieldCount,
status_t status_t
BCountry::TimeFields(BDateElement*& fields, int& fieldCount, bool longFormat) BCountry::TimeFields(BDateElement*& fields, int& fieldCount, bool longFormat)
{ {
ICU_VERSION::DateFormat* timeFormatter = LockTimeFormatter(longFormat); ICU_VERSION::DateFormat* timeFormatter = _LockTimeFormatter(longFormat);
if (timeFormatter == NULL) { if (timeFormatter == NULL) {
UnlockTimeFormatter(longFormat); _UnlockTimeFormatter(longFormat);
return B_NO_MEMORY; return B_NO_MEMORY;
} }
@ -582,7 +584,7 @@ BCountry::TimeFields(BDateElement*& fields, int& fieldCount, bool longFormat)
time_t now; time_t now;
ICUString = timeFormatter->format((UDate)time(&now) * 1000, ICUString, ICUString = timeFormatter->format((UDate)time(&now) * 1000, ICUString,
&positionIterator, error); &positionIterator, error);
UnlockTimeFormatter(longFormat); _UnlockTimeFormatter(longFormat);
if (error != U_ZERO_ERROR) if (error != U_ZERO_ERROR)
return B_ERROR; return B_ERROR;
@ -627,9 +629,9 @@ BCountry::TimeFields(BDateElement*& fields, int& fieldCount, bool longFormat)
status_t status_t
BCountry::SetTimeFormat(const char* formatString, bool longFormat) BCountry::SetTimeFormat(const char* formatString, bool longFormat)
{ {
ICU_VERSION::DateFormat* timeFormatter = LockTimeFormatter(longFormat); ICU_VERSION::DateFormat* timeFormatter = _LockTimeFormatter(longFormat);
if (timeFormatter == NULL) { if (timeFormatter == NULL) {
UnlockTimeFormatter(longFormat); _UnlockTimeFormatter(longFormat);
return B_NO_MEMORY; return B_NO_MEMORY;
} }
@ -638,7 +640,7 @@ BCountry::SetTimeFormat(const char* formatString, bool longFormat)
UnicodeString pattern(formatString); UnicodeString pattern(formatString);
dateFormatterImpl->applyPattern(pattern); dateFormatterImpl->applyPattern(pattern);
UnlockTimeFormatter(longFormat); _UnlockTimeFormatter(longFormat);
return B_OK; return B_OK;
} }
@ -647,9 +649,9 @@ BCountry::SetTimeFormat(const char* formatString, bool longFormat)
status_t status_t
BCountry::TimeFormat(BString& format, bool longFormat) BCountry::TimeFormat(BString& format, bool longFormat)
{ {
ICU_VERSION::DateFormat* timeFormatter = LockTimeFormatter(longFormat); ICU_VERSION::DateFormat* timeFormatter = _LockTimeFormatter(longFormat);
if (timeFormatter == NULL) { if (timeFormatter == NULL) {
UnlockTimeFormatter(longFormat); _UnlockTimeFormatter(longFormat);
return B_NO_MEMORY; return B_NO_MEMORY;
} }
@ -658,7 +660,7 @@ BCountry::TimeFormat(BString& format, bool longFormat)
UnicodeString ICUString; UnicodeString ICUString;
ICUString = dateFormatterImpl->toPattern(ICUString); ICUString = dateFormatterImpl->toPattern(ICUString);
UnlockTimeFormatter(longFormat); _UnlockTimeFormatter(longFormat);
BStringByteSink stringConverter(&format); BStringByteSink stringConverter(&format);
@ -1050,26 +1052,35 @@ BCountry::MonFracDigits() const
// #pragma mark - Timezones // #pragma mark - Timezones
status_t int
BCountry::GetTimeZones(BMessage* timezones) BCountry::GetTimeZones(BList& timezones)
{ {
if (timezones == NULL)
return B_BAD_DATA;
StringEnumeration* icuTimeZoneList = TimeZone::createEnumeration( StringEnumeration* icuTimeZoneList = TimeZone::createEnumeration(
fICULocale->getCountry()); fICULocale->getCountry());
UErrorCode error = U_ZERO_ERROR; UErrorCode error = U_ZERO_ERROR;
const char* tzName; const char* tzName;
std::map<BString, BTimeZone*> timeZoneMap;
// The map allows us to remove duplicates and get a count of the
// remaining zones after that
while (tzName = icuTimeZoneList->next(NULL, error)) { while (tzName = icuTimeZoneList->next(NULL, error)) {
if (error == U_ZERO_ERROR) if (error == U_ZERO_ERROR) {
timezones->AddString("zones", tzName); BString readableName;
else BTimeZone* timeZone = new BTimeZone(tzName);
timeZone->GetName(readableName);
timeZoneMap.insert(std::pair<BString, BTimeZone*>(readableName,
timeZone));
} else
error = U_ZERO_ERROR; error = U_ZERO_ERROR;
} }
delete icuTimeZoneList; delete icuTimeZoneList;
return B_OK;
for (std::map<BString, BTimeZone*>::const_iterator timeZoneIterator
= timeZoneMap.begin(); timeZoneIterator != timeZoneMap.end();
timeZoneIterator++)
timezones.AddItem((*timeZoneIterator).second);
return timeZoneMap.size();
} }

View File

@ -25,7 +25,7 @@ BTimeZone::~BTimeZone()
void void
BTimeZone::Name(BString& name) BTimeZone::GetName(BString& name)
{ {
UnicodeString unicodeName; UnicodeString unicodeName;
fICUTimeZone->getDisplayName(unicodeName); fICUTimeZone->getDisplayName(unicodeName);
@ -35,17 +35,6 @@ BTimeZone::Name(BString& name)
} }
void
BTimeZone::Code(BString& code)
{
UnicodeString unicodeName;
fICUTimeZone->getID(unicodeName);
BStringByteSink converter(&code);
unicodeName.toUTF8(converter);
}
int int
BTimeZone::OffsetFromGMT() BTimeZone::OffsetFromGMT()
{ {