Author: zooey Date: 2010-08-27 00:17:40 +0200 (Fri, 27 Aug 2010) New Revision: 38381 Changeset: http://dev.haiku-os.org/changeset/38381 Modified: haiku/trunk/headers/os/locale/Locale.h haiku/trunk/src/kits/locale/Locale.cpp Log: * added timezone-support to some more date/time-formatting methods in BLocale Modified: haiku/trunk/headers/os/locale/Locale.h =================================================================== --- haiku/trunk/headers/os/locale/Locale.h 2010-08-26 22:16:40 UTC (rev 38380) +++ haiku/trunk/headers/os/locale/Locale.h 2010-08-26 22:17:40 UTC (rev 38381) @@ -62,7 +62,8 @@ status_t FormatDateTime(char* target, size_t maxSize, time_t time, bool longFormat); status_t FormatDateTime(BString* buffer, time_t time, - bool longFormat); + bool longFormat, + const BTimeZone* timeZone = NULL); // Date @@ -71,7 +72,8 @@ status_t FormatDate(char* string, size_t maxSize, time_t time, bool longFormat); status_t FormatDate(BString* string, time_t time, - bool longFormat); + bool longFormat, + const BTimeZone* timeZone = NULL); status_t FormatDate(BString* string, int*& fieldPositions, int& fieldCount, time_t time, bool longFormat); Modified: haiku/trunk/src/kits/locale/Locale.cpp =================================================================== --- haiku/trunk/src/kits/locale/Locale.cpp 2010-08-26 22:16:40 UTC (rev 38380) +++ haiku/trunk/src/kits/locale/Locale.cpp 2010-08-26 22:17:40 UTC (rev 38381) @@ -164,7 +164,8 @@ status_t -BLocale::FormatDate(BString *string, time_t time, bool longFormat) +BLocale::FormatDate(BString *string, time_t time, bool longFormat, + const BTimeZone* timeZone) { string->Truncate(0); // We make the string empty, this way even in cases where ICU fail we at @@ -174,11 +175,18 @@ if (dateFormatter.Get() == NULL) return B_NO_MEMORY; + if (timeZone != NULL) { + ObjectDeleter<TimeZone> icuTimeZone + = TimeZone::createTimeZone(timeZone->ID().String()); + if (icuTimeZone.Get() == NULL) + return B_NO_MEMORY; + dateFormatter->setTimeZone(*icuTimeZone.Get()); + } + UnicodeString ICUString; ICUString = dateFormatter->format((UDate)time * 1000, ICUString); BStringByteSink stringConverter(string); - ICUString.toUTF8(stringConverter); return B_OK; @@ -369,6 +377,42 @@ } +status_t +BLocale::FormatDateTime(BString* target, time_t time, bool longFormat, + const BTimeZone* timeZone) +{ + ObjectDeleter<DateFormat> dateFormatter = CreateDateFormat(longFormat, + *fICULocale, longFormat ? fLongDateFormat : fShortDateFormat); + if (dateFormatter.Get() == NULL) + return B_NO_MEMORY; + + ObjectDeleter<DateFormat> timeFormatter = CreateTimeFormat(longFormat, + *fICULocale, longFormat ? fLongTimeFormat : fShortTimeFormat); + if (timeFormatter.Get() == NULL) + return B_NO_MEMORY; + + if (timeZone != NULL) { + ObjectDeleter<TimeZone> icuTimeZone + = TimeZone::createTimeZone(timeZone->ID().String()); + if (icuTimeZone.Get() == NULL) + return B_NO_MEMORY; + timeFormatter->setTimeZone(*icuTimeZone.Get()); + } + + UnicodeString ICUString; + ICUString = dateFormatter->format((UDate)time * 1000, ICUString); + + ICUString.append(UnicodeString::fromUTF8(", ")); + + ICUString = timeFormatter->format((UDate)time * 1000, ICUString); + + BStringByteSink stringConverter(target); + ICUString.toUTF8(stringConverter); + + return B_OK; +} + + // #pragma mark - Time