From 9ef134d918080b32962b6cc0522ba45efc0c26b8 Mon Sep 17 00:00:00 2001 From: Oliver Tappe Date: Thu, 26 Aug 2010 22:17:40 +0000 Subject: [PATCH] * added timezone-support to some more date/time-formatting methods in BLocale git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@38381 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/os/locale/Locale.h | 6 +++-- src/kits/locale/Locale.cpp | 48 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/headers/os/locale/Locale.h b/headers/os/locale/Locale.h index 535af8ae0d..fc79f81738 100644 --- a/headers/os/locale/Locale.h +++ b/headers/os/locale/Locale.h @@ -62,7 +62,8 @@ public: 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 @@ public: 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); diff --git a/src/kits/locale/Locale.cpp b/src/kits/locale/Locale.cpp index 4810e81076..979a926c78 100644 --- a/src/kits/locale/Locale.cpp +++ b/src/kits/locale/Locale.cpp @@ -164,7 +164,8 @@ BLocale::FormatDate(char* string, size_t maxSize, time_t time, bool longFormat) 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 @@ BLocale::FormatDate(BString *string, time_t time, bool longFormat) if (dateFormatter.Get() == NULL) return B_NO_MEMORY; + if (timeZone != NULL) { + ObjectDeleter 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 @@ BLocale::FormatDateTime(char* target, size_t maxSize, time_t time, } +status_t +BLocale::FormatDateTime(BString* target, time_t time, bool longFormat, + const BTimeZone* timeZone) +{ + ObjectDeleter dateFormatter = CreateDateFormat(longFormat, + *fICULocale, longFormat ? fLongDateFormat : fShortDateFormat); + if (dateFormatter.Get() == NULL) + return B_NO_MEMORY; + + ObjectDeleter timeFormatter = CreateTimeFormat(longFormat, + *fICULocale, longFormat ? fLongTimeFormat : fShortTimeFormat); + if (timeFormatter.Get() == NULL) + return B_NO_MEMORY; + + if (timeZone != NULL) { + ObjectDeleter 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