diff --git a/headers/os/locale/DateFormat.h b/headers/os/locale/DateFormat.h index 063b075ce3..e788551484 100644 --- a/headers/os/locale/DateFormat.h +++ b/headers/os/locale/DateFormat.h @@ -7,16 +7,32 @@ #include -#include +#include #include #include #include +namespace icu { + class DateFormat; +} + + class BString; class BTimeZone; +enum BDateElement { + B_DATE_ELEMENT_INVALID = B_BAD_DATA, + B_DATE_ELEMENT_YEAR = 0, + B_DATE_ELEMENT_MONTH, + B_DATE_ELEMENT_DAY, + B_DATE_ELEMENT_AM_PM, + B_DATE_ELEMENT_HOUR, + B_DATE_ELEMENT_MINUTE, + B_DATE_ELEMENT_SECOND +}; + enum BWeekday { B_WEEKDAY_MONDAY = 1, B_WEEKDAY_TUESDAY, diff --git a/headers/os/locale/DateTimeFormat.h b/headers/os/locale/DateTimeFormat.h index f423673322..7906d7ec07 100644 --- a/headers/os/locale/DateTimeFormat.h +++ b/headers/os/locale/DateTimeFormat.h @@ -6,25 +6,15 @@ #define _B_DATE_TIME_FORMAT_H_ +#include #include #include +#include class BString; -enum BDateElement { - B_DATE_ELEMENT_INVALID = B_BAD_DATA, - B_DATE_ELEMENT_YEAR = 0, - B_DATE_ELEMENT_MONTH, - B_DATE_ELEMENT_DAY, - B_DATE_ELEMENT_AM_PM, - B_DATE_ELEMENT_HOUR, - B_DATE_ELEMENT_MINUTE, - B_DATE_ELEMENT_SECOND -}; - - class BDateTimeFormat : public BFormat { public: BDateTimeFormat(); @@ -33,13 +23,20 @@ public: // formatting - // no-frills version: Simply appends the - // formatted date to the string buffer. - // Can fail only with B_NO_MEMORY or - // B_BAD_VALUE. - virtual status_t Format(bigtime_t value, BString* buffer) const; + ssize_t Format(char* target, const size_t maxSize, + const time_t time, + BDateFormatStyle dateStyle, + BTimeFormatStyle timeStyle) const; + status_t Format(BString& buffer, const time_t time, + BDateFormatStyle dateStyle, + BTimeFormatStyle timeStyle, + const BTimeZone* timeZone = NULL) const; - // TODO: ... basically, all of it! +private: + icu::DateFormat* _CreateDateFormatter( + const BString& format) const; + icu::DateFormat* _CreateTimeFormatter( + const BString& format) const; }; diff --git a/headers/os/locale/Locale.h b/headers/os/locale/Locale.h index 55b1639e64..51f4559ca7 100644 --- a/headers/os/locale/Locale.h +++ b/headers/os/locale/Locale.h @@ -1,5 +1,5 @@ /* - * Copyright 2003-2012, Haiku, Inc. + * Copyright 2003-2014, Haiku, Inc. * Distributed under the terms of the MIT License. */ #ifndef _B_LOCALE_H_ @@ -7,29 +7,13 @@ #include -#include #include #include #include -namespace icu { - class DateFormat; -} - - class BCatalog; -class BDateFormat; class BString; -class BTimeZone; - - -enum BNumberElement { - B_NUMBER_ELEMENT_INVALID = B_BAD_DATA, - B_NUMBER_ELEMENT_INTEGER = 0, - B_NUMBER_ELEMENT_FRACTIONAL, - B_NUMBER_ELEMENT_CURRENCY -}; class BLocale { @@ -57,36 +41,6 @@ public: // see definitions in LocaleStrings.h const char* GetString(uint32 id) const; - // DateTime - - // TODO: drop some of these once BDateTimeFormat - // has been implemented! - ssize_t FormatDateTime(char* target, size_t maxSize, - time_t time, BDateFormatStyle dateStyle, - BTimeFormatStyle timeStyle) const; - status_t FormatDateTime(BString* buffer, time_t time, - BDateFormatStyle dateStyle, - BTimeFormatStyle timeStyle, - const BTimeZone* timeZone = NULL) const; - - // numbers - - ssize_t FormatNumber(char* string, size_t maxSize, - double value) const; - status_t FormatNumber(BString* string, - double value) const; - ssize_t FormatNumber(char* string, size_t maxSize, - int32 value) const; - status_t FormatNumber(BString* string, - int32 value) const; - - // monetary - - ssize_t FormatMonetary(char* string, size_t maxSize, - double value) const; - status_t FormatMonetary(BString* string, - double value) const; - // Collator short-hands int StringCompare(const char* s1, const char* s2) const; @@ -97,11 +51,6 @@ public: BString* sortKey) const; private: - icu::DateFormat* _CreateDateFormatter( - const BString& format) const; - icu::DateFormat* _CreateTimeFormatter( - const BString& format) const; - mutable BLocker fLock; BCollator fCollator; BFormattingConventions fConventions; diff --git a/headers/os/locale/NumberFormat.h b/headers/os/locale/NumberFormat.h index 801c430750..feee9116af 100644 --- a/headers/os/locale/NumberFormat.h +++ b/headers/os/locale/NumberFormat.h @@ -1,19 +1,47 @@ +/* + * Copyright 2003-2014, Haiku, Inc. + * Distributed under the terms of the MIT License. + */ #ifndef _B_NUMBER_FORMAT_H_ #define _B_NUMBER_FORMAT_H_ -#include -#include -class BNumberFormatImpl; +#include + + +enum BNumberElement { + B_NUMBER_ELEMENT_INVALID = B_BAD_DATA, + B_NUMBER_ELEMENT_INTEGER = 0, + B_NUMBER_ELEMENT_FRACTIONAL, + B_NUMBER_ELEMENT_CURRENCY +}; + class BNumberFormat : public BFormat { - protected: +public: + BNumberFormat(); BNumberFormat(const BNumberFormat &other); ~BNumberFormat(); - BNumberFormat &operator=(const BNumberFormat &other); + BNumberFormat& operator=(const BNumberFormat &other); - BNumberFormat(); + // formatting + + ssize_t Format(char* string, size_t maxSize, + const double value) const; + status_t Format(BString& string, const double value) + const; + ssize_t Format(char* string, size_t maxSize, + const int32 value) const; + status_t Format(BString& string, const int32 value) + const; + + // monetary + + ssize_t FormatMonetary(char* string, size_t maxSize, + const double value) const; + status_t FormatMonetary(BString& string, + const double value) const; }; diff --git a/headers/os/locale/NumberFormatImpl.h b/headers/os/locale/NumberFormatImpl.h deleted file mode 100644 index f7465d868f..0000000000 --- a/headers/os/locale/NumberFormatImpl.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef _B_NUMBER_FORMAT_IMPL_H_ -#define _B_NUMBER_FORMAT_IMPL_H_ - -#include - -struct format_field_position; -class BNumberFormatParameters; - -class BNumberFormatImpl : public BFormatImpl { - public: - BNumberFormatImpl(); - virtual ~BNumberFormatImpl(); - - virtual BFormatParameters *DefaultFormatParameters(); - virtual const BFormatParameters *DefaultFormatParameters() const; - - virtual BNumberFormatParameters *DefaultNumberFormatParameters() = 0; - virtual const BNumberFormatParameters *DefaultNumberFormatParameters() - const = 0; -}; - -#endif // _B_NUMBER_FORMAT_IMPL_H_ diff --git a/src/apps/aboutsystem/AboutSystem.cpp b/src/apps/aboutsystem/AboutSystem.cpp index 38a59c9861..ea834d1d37 100644 --- a/src/apps/aboutsystem/AboutSystem.cpp +++ b/src/apps/aboutsystem/AboutSystem.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -514,11 +515,10 @@ AboutView::AboutView() kernelTimeDate << systemInfo.kernel_build_date << " " << systemInfo.kernel_build_time; BString buildTimeDate; - const BLocale* locale = BLocale::Default(); time_t buildTimeDateStamp = parsedate(kernelTimeDate, -1); if (buildTimeDateStamp > 0) { - if (locale->FormatDateTime(&buildTimeDate, buildTimeDateStamp, + if (BDateTimeFormat().Format(buildTimeDate, buildTimeDateStamp, B_LONG_DATE_FORMAT, B_MEDIUM_TIME_FORMAT) != B_OK) buildTimeDate.SetTo(kernelTimeDate); } else diff --git a/src/apps/poorman/PoorManWindow.cpp b/src/apps/poorman/PoorManWindow.cpp index ef572846ec..90943ef51b 100644 --- a/src/apps/poorman/PoorManWindow.cpp +++ b/src/apps/poorman/PoorManWindow.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -239,8 +240,8 @@ PoorManWindow::MessageReceived(BMessage* message) if (time != -1) { BString timeString; - if (BLocale::Default()->FormatDateTime(&timeString, time, - DATE_FORMAT, TIME_FORMAT) == B_OK) { + if (BDateTimeFormat().Format(timeString, time, DATE_FORMAT, + TIME_FORMAT) == B_OK) { line << '[' << timeString << "]: "; } } @@ -316,8 +317,7 @@ PoorManWindow::QuitRequested() if (fStatus) { time_t now = time(NULL); BString timeString; - BLocale::Default()->FormatDateTime(&timeString, now, - DATE_FORMAT, TIME_FORMAT); + BDateTimeFormat().Format(timeString, now, DATE_FORMAT, TIME_FORMAT); BString line; line << "[" << timeString << "]: " << B_TRANSLATE("Shutting down.") diff --git a/src/kits/locale/DateTimeFormat.cpp b/src/kits/locale/DateTimeFormat.cpp index fe2e4d0e50..419ef1b51c 100644 --- a/src/kits/locale/DateTimeFormat.cpp +++ b/src/kits/locale/DateTimeFormat.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2010, Haiku, Inc. All Rights Reserved. + * Copyright 2010-2014, Haiku, Inc. All Rights Reserved. * Distributed under the terms of the MIT License. * * Authors: @@ -8,30 +8,156 @@ #include +#include +#include +#include +#include +#include + +#include + +#include +#include + -// default constructor BDateTimeFormat::BDateTimeFormat() : BFormat() { } -// copy constructor BDateTimeFormat::BDateTimeFormat(const BDateTimeFormat &other) : BFormat(other) { } -// destructor BDateTimeFormat::~BDateTimeFormat() { } -// Format -status_t -BDateTimeFormat::Format(bigtime_t value, BString* buffer) const +// #pragma mark - Formatting + + +ssize_t +BDateTimeFormat::Format(char* target, size_t maxSize, time_t time, + BDateFormatStyle dateStyle, BTimeFormatStyle timeStyle) const { - return B_ERROR; + BAutolock lock(fLock); + if (!lock.IsLocked()) + return B_ERROR; + + BString format; + fConventions.GetDateFormat(dateStyle, format); + ObjectDeleter dateFormatter(_CreateDateFormatter(format)); + if (dateFormatter.Get() == NULL) + return B_NO_MEMORY; + + fConventions.GetTimeFormat(timeStyle, format); + ObjectDeleter timeFormatter(_CreateTimeFormatter(format)); + if (timeFormatter.Get() == NULL) + return B_NO_MEMORY; + + UnicodeString icuString; + dateFormatter->format((UDate)time * 1000, icuString); + + icuString.append(UnicodeString::fromUTF8(", ")); + + timeFormatter->format((UDate)time * 1000, icuString); + + CheckedArrayByteSink stringConverter(target, maxSize); + icuString.toUTF8(stringConverter); + + if (stringConverter.Overflowed()) + return B_BAD_VALUE; + + return stringConverter.NumberOfBytesWritten(); +} + + +status_t +BDateTimeFormat::Format(BString& target, const time_t time, + BDateFormatStyle dateStyle, BTimeFormatStyle timeStyle, + const BTimeZone* timeZone) const +{ + BAutolock lock(fLock); + if (!lock.IsLocked()) + return B_ERROR; + + BString format; + fConventions.GetDateFormat(dateStyle, format); + ObjectDeleter dateFormatter(_CreateDateFormatter(format)); + if (dateFormatter.Get() == NULL) + return B_NO_MEMORY; + + fConventions.GetTimeFormat(timeStyle, format); + ObjectDeleter timeFormatter(_CreateTimeFormatter(format)); + 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; + dateFormatter->format((UDate)time * 1000, icuString); + icuString.append(UnicodeString::fromUTF8(", ")); + timeFormatter->format((UDate)time * 1000, icuString); + + target.Truncate(0); + BStringByteSink stringConverter(&target); + icuString.toUTF8(stringConverter); + + return B_OK; +} + + +DateFormat* +BDateTimeFormat::_CreateDateFormatter(const BString& format) const +{ + Locale* icuLocale + = fConventions.UseStringsFromPreferredLanguage() + ? BLanguage::Private(&fLanguage).ICULocale() + : BFormattingConventions::Private(&fConventions).ICULocale(); + + icu::DateFormat* dateFormatter + = icu::DateFormat::createDateInstance(DateFormat::kShort, *icuLocale); + if (dateFormatter == NULL) + return NULL; + + SimpleDateFormat* dateFormatterImpl + = static_cast(dateFormatter); + + UnicodeString pattern(format.String()); + dateFormatterImpl->applyPattern(pattern); + + return dateFormatter; +} + + +DateFormat* +BDateTimeFormat::_CreateTimeFormatter(const BString& format) const +{ + Locale* icuLocale + = fConventions.UseStringsFromPreferredLanguage() + ? BLanguage::Private(&fLanguage).ICULocale() + : BFormattingConventions::Private(&fConventions).ICULocale(); + + icu::DateFormat* timeFormatter + = icu::DateFormat::createTimeInstance(DateFormat::kShort, *icuLocale); + if (timeFormatter == NULL) + return NULL; + + SimpleDateFormat* timeFormatterImpl + = static_cast(timeFormatter); + + UnicodeString pattern(format.String()); + timeFormatterImpl->applyPattern(pattern); + + return timeFormatter; } diff --git a/src/kits/locale/Jamfile b/src/kits/locale/Jamfile index d026593b7e..5b3f509eff 100644 --- a/src/kits/locale/Jamfile +++ b/src/kits/locale/Jamfile @@ -27,6 +27,7 @@ local sources = DateFormat.cpp DateTimeFormat.cpp DurationFormat.cpp + NumberFormat.cpp TimeFormat.cpp TimeUnitFormat.cpp Format.cpp # Used by some of the above. @@ -43,8 +44,6 @@ local sources = # IntegerFormat.cpp # IntegerFormatImpl.cpp # IntegerFormatParameters.cpp - # NumberFormat.cpp - # NumberFormatImpl.cpp # NumberFormatParameters.cpp ; diff --git a/src/kits/locale/Locale.cpp b/src/kits/locale/Locale.cpp index 9115db2dc9..34f702f164 100644 --- a/src/kits/locale/Locale.cpp +++ b/src/kits/locale/Locale.cpp @@ -2,34 +2,14 @@ * Copyright 2003, Axel Dörfler, axeld@pinc-software.de. * Copyright 2010-2011, Oliver Tappe, zooey@hirschkaefer.de. * Copyright 2012, John Scipione, jscipione@gmail.com - * All rights reserved. Distributed under the terms of the OpenBeOS License. + * All rights reserved. Distributed under the terms of the MIT License. */ -#include #include -#include #include -#include -#include #include #include -#include - -#include - -#include -#include -#include -#include -#include -#include -#include - -#include - - -using BPrivate::ObjectDeleter; BLocale::BLocale(const BLanguage* language, @@ -183,256 +163,3 @@ BLocale::SetLanguage(const BLanguage& newLanguage) fLanguage = newLanguage; } - - -// #pragma mark - Date - - -ssize_t -BLocale::FormatDateTime(char* target, size_t maxSize, time_t time, - BDateFormatStyle dateStyle, BTimeFormatStyle timeStyle) const -{ - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - - BString format; - fConventions.GetDateFormat(dateStyle, format); - ObjectDeleter dateFormatter(_CreateDateFormatter(format)); - if (dateFormatter.Get() == NULL) - return B_NO_MEMORY; - - fConventions.GetTimeFormat(timeStyle, format); - ObjectDeleter timeFormatter(_CreateTimeFormatter(format)); - if (timeFormatter.Get() == NULL) - return B_NO_MEMORY; - - UnicodeString icuString; - dateFormatter->format((UDate)time * 1000, icuString); - - icuString.append(UnicodeString::fromUTF8(", ")); - - timeFormatter->format((UDate)time * 1000, icuString); - - CheckedArrayByteSink stringConverter(target, maxSize); - icuString.toUTF8(stringConverter); - - if (stringConverter.Overflowed()) - return B_BAD_VALUE; - - return stringConverter.NumberOfBytesWritten(); -} - - -status_t -BLocale::FormatDateTime(BString* target, time_t time, - BDateFormatStyle dateStyle, BTimeFormatStyle timeStyle, - const BTimeZone* timeZone) const -{ - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - - BString format; - fConventions.GetDateFormat(dateStyle, format); - ObjectDeleter dateFormatter(_CreateDateFormatter(format)); - if (dateFormatter.Get() == NULL) - return B_NO_MEMORY; - - fConventions.GetTimeFormat(timeStyle, format); - ObjectDeleter timeFormatter(_CreateTimeFormatter(format)); - 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; - dateFormatter->format((UDate)time * 1000, icuString); - icuString.append(UnicodeString::fromUTF8(", ")); - timeFormatter->format((UDate)time * 1000, icuString); - - target->Truncate(0); - BStringByteSink stringConverter(target); - icuString.toUTF8(stringConverter); - - return B_OK; -} - - -// #pragma mark - Numbers - - -ssize_t -BLocale::FormatNumber(char* string, size_t maxSize, double value) const -{ - BString fullString; - status_t status = FormatNumber(&fullString, value); - if (status != B_OK) - return status; - - return strlcpy(string, fullString.String(), maxSize); -} - - -status_t -BLocale::FormatNumber(BString* string, double value) const -{ - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - - UErrorCode err = U_ZERO_ERROR; - ObjectDeleter numberFormatter(NumberFormat::createInstance( - *BFormattingConventions::Private(&fConventions).ICULocale(), - UNUM_DECIMAL, err)); - - if (numberFormatter.Get() == NULL) - return B_NO_MEMORY; - if (U_FAILURE(err)) - return B_BAD_VALUE; - - UnicodeString icuString; - numberFormatter->format(value, icuString); - - string->Truncate(0); - BStringByteSink stringConverter(string); - icuString.toUTF8(stringConverter); - - return B_OK; -} - - -ssize_t -BLocale::FormatNumber(char* string, size_t maxSize, int32 value) const -{ - BString fullString; - status_t status = FormatNumber(&fullString, value); - if (status != B_OK) - return status; - - return strlcpy(string, fullString.String(), maxSize); -} - - -status_t -BLocale::FormatNumber(BString* string, int32 value) const -{ - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - - UErrorCode err = U_ZERO_ERROR; - ObjectDeleter numberFormatter(NumberFormat::createInstance( - *BFormattingConventions::Private(&fConventions).ICULocale(), - UNUM_DECIMAL, err)); - - if (numberFormatter.Get() == NULL) - return B_NO_MEMORY; - if (U_FAILURE(err)) - return B_BAD_VALUE; - - UnicodeString icuString; - numberFormatter->format((int32_t)value, icuString); - - string->Truncate(0); - BStringByteSink stringConverter(string); - icuString.toUTF8(stringConverter); - - return B_OK; -} - - -ssize_t -BLocale::FormatMonetary(char* string, size_t maxSize, double value) const -{ - BString fullString; - status_t status = FormatMonetary(&fullString, value); - if (status != B_OK) - return status; - - return strlcpy(string, fullString.String(), maxSize); -} - - -status_t -BLocale::FormatMonetary(BString* string, double value) const -{ - if (string == NULL) - return B_BAD_VALUE; - - BAutolock lock(fLock); - if (!lock.IsLocked()) - return B_ERROR; - - UErrorCode err = U_ZERO_ERROR; - ObjectDeleter numberFormatter( - NumberFormat::createCurrencyInstance( - *BFormattingConventions::Private(&fConventions).ICULocale(), - err)); - - if (numberFormatter.Get() == NULL) - return B_NO_MEMORY; - if (U_FAILURE(err)) - return B_BAD_VALUE; - - UnicodeString icuString; - numberFormatter->format(value, icuString); - - string->Truncate(0); - BStringByteSink stringConverter(string); - icuString.toUTF8(stringConverter); - - return B_OK; -} - - -DateFormat* -BLocale::_CreateDateFormatter(const BString& format) const -{ - Locale* icuLocale - = fConventions.UseStringsFromPreferredLanguage() - ? BLanguage::Private(&fLanguage).ICULocale() - : BFormattingConventions::Private(&fConventions).ICULocale(); - - icu::DateFormat* dateFormatter - = icu::DateFormat::createDateInstance(DateFormat::kShort, *icuLocale); - if (dateFormatter == NULL) - return NULL; - - SimpleDateFormat* dateFormatterImpl - = static_cast(dateFormatter); - - UnicodeString pattern(format.String()); - dateFormatterImpl->applyPattern(pattern); - - return dateFormatter; -} - - -DateFormat* -BLocale::_CreateTimeFormatter(const BString& format) const -{ - Locale* icuLocale - = fConventions.UseStringsFromPreferredLanguage() - ? BLanguage::Private(&fLanguage).ICULocale() - : BFormattingConventions::Private(&fConventions).ICULocale(); - - icu::DateFormat* timeFormatter - = icu::DateFormat::createTimeInstance(DateFormat::kShort, *icuLocale); - if (timeFormatter == NULL) - return NULL; - - SimpleDateFormat* timeFormatterImpl - = static_cast(timeFormatter); - - UnicodeString pattern(format.String()); - timeFormatterImpl->applyPattern(pattern); - - return timeFormatter; -} diff --git a/src/kits/locale/NumberFormat.cpp b/src/kits/locale/NumberFormat.cpp index 40b1e1e77f..5eae902d36 100644 --- a/src/kits/locale/NumberFormat.cpp +++ b/src/kits/locale/NumberFormat.cpp @@ -1,18 +1,39 @@ -#include -#include +/* + * Copyright 2003, Axel Dörfler, axeld@pinc-software.de. + * Copyright 2010-2011, Oliver Tappe, zooey@hirschkaefer.de. + * Copyright 2012, John Scipione, jscipione@gmail.com + * All rights reserved. Distributed under the terms of the MIT License. + */ + + +#include + +#include +#include +#include + +#include + +#include + + +BNumberFormat::BNumberFormat() + : BFormat() +{ +} + -// copy constructor BNumberFormat::BNumberFormat(const BNumberFormat &other) : BFormat(other) { } -// destructor + BNumberFormat::~BNumberFormat() { } -// = + BNumberFormat & BNumberFormat::operator=(const BNumberFormat &other) { @@ -20,9 +41,130 @@ BNumberFormat::operator=(const BNumberFormat &other) return *this; } -// constructor -BNumberFormat::BNumberFormat() - : BFormat() + +// #pragma mark - Formatting + + +ssize_t +BNumberFormat::Format(char* string, size_t maxSize, const double value) const { + BString fullString; + status_t status = Format(fullString, value); + if (status != B_OK) + return status; + + return strlcpy(string, fullString.String(), maxSize); } + +status_t +BNumberFormat::Format(BString& string, const double value) const +{ + BAutolock lock(fLock); + if (!lock.IsLocked()) + return B_ERROR; + + UErrorCode err = U_ZERO_ERROR; + ObjectDeleter numberFormatter(NumberFormat::createInstance( + *BFormattingConventions::Private(&fConventions).ICULocale(), + UNUM_DECIMAL, err)); + + if (numberFormatter.Get() == NULL) + return B_NO_MEMORY; + if (U_FAILURE(err)) + return B_BAD_VALUE; + + UnicodeString icuString; + numberFormatter->format(value, icuString); + + string.Truncate(0); + BStringByteSink stringConverter(&string); + icuString.toUTF8(stringConverter); + + return B_OK; +} + + +ssize_t +BNumberFormat::Format(char* string, size_t maxSize, const int32 value) const +{ + BString fullString; + status_t status = Format(fullString, value); + if (status != B_OK) + return status; + + return strlcpy(string, fullString.String(), maxSize); +} + + +status_t +BNumberFormat::Format(BString& string, const int32 value) const +{ + BAutolock lock(fLock); + if (!lock.IsLocked()) + return B_ERROR; + + UErrorCode err = U_ZERO_ERROR; + ObjectDeleter numberFormatter(NumberFormat::createInstance( + *BFormattingConventions::Private(&fConventions).ICULocale(), + UNUM_DECIMAL, err)); + + if (numberFormatter.Get() == NULL) + return B_NO_MEMORY; + if (U_FAILURE(err)) + return B_BAD_VALUE; + + UnicodeString icuString; + numberFormatter->format((int32_t)value, icuString); + + string.Truncate(0); + BStringByteSink stringConverter(&string); + icuString.toUTF8(stringConverter); + + return B_OK; +} + + +ssize_t +BNumberFormat::FormatMonetary(char* string, size_t maxSize, const double value) + const +{ + BString fullString; + status_t status = FormatMonetary(fullString, value); + if (status != B_OK) + return status; + + return strlcpy(string, fullString.String(), maxSize); +} + + +status_t +BNumberFormat::FormatMonetary(BString& string, const double value) const +{ + if (string == NULL) + return B_BAD_VALUE; + + BAutolock lock(fLock); + if (!lock.IsLocked()) + return B_ERROR; + + UErrorCode err = U_ZERO_ERROR; + ObjectDeleter numberFormatter( + NumberFormat::createCurrencyInstance( + *BFormattingConventions::Private(&fConventions).ICULocale(), + err)); + + if (numberFormatter.Get() == NULL) + return B_NO_MEMORY; + if (U_FAILURE(err)) + return B_BAD_VALUE; + + UnicodeString icuString; + numberFormatter->format(value, icuString); + + string.Truncate(0); + BStringByteSink stringConverter(&string); + icuString.toUTF8(stringConverter); + + return B_OK; +} diff --git a/src/kits/locale/NumberFormatImpl.cpp b/src/kits/locale/NumberFormatImpl.cpp deleted file mode 100644 index dc9041e8d6..0000000000 --- a/src/kits/locale/NumberFormatImpl.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include - -// constructor -BNumberFormatImpl::BNumberFormatImpl() - : BFormatImpl() -{ -} - -// destructor -BNumberFormatImpl::~BNumberFormatImpl() -{ -} - -// DefaultFormatParameters -BFormatParameters * -BNumberFormatImpl::DefaultFormatParameters() -{ - return DefaultNumberFormatParameters(); -} - -// DefaultFormatParameters -const BFormatParameters * -BNumberFormatImpl::DefaultFormatParameters() const -{ - return DefaultNumberFormatParameters(); -} - diff --git a/src/kits/tracker/StatusWindow.cpp b/src/kits/tracker/StatusWindow.cpp index 9c5a87630a..3913758f18 100644 --- a/src/kits/tracker/StatusWindow.cpp +++ b/src/kits/tracker/StatusWindow.cpp @@ -796,9 +796,8 @@ BStatusView::_TimeStatusString(float availableSpace, float* _width) time_t finishTime = (time_t)(now + secondsRemaining); char timeText[32]; - const BLocale* locale = BLocale::Default(); if (finishTime - now > kSecondsPerDay) { - locale->FormatDateTime(timeText, sizeof(timeText), finishTime, + BDateTimeFormat().Format(timeText, sizeof(timeText), finishTime, B_MEDIUM_DATE_FORMAT, B_MEDIUM_TIME_FORMAT); } else { BTimeFormat().Format(timeText, sizeof(timeText), finishTime, diff --git a/src/kits/tracker/WidgetAttributeText.cpp b/src/kits/tracker/WidgetAttributeText.cpp index cb15844c65..7e4a7c2f4f 100644 --- a/src/kits/tracker/WidgetAttributeText.cpp +++ b/src/kits/tracker/WidgetAttributeText.cpp @@ -46,6 +46,7 @@ All rights reserved. #include #include #include +#include #include #include #include @@ -206,9 +207,10 @@ TruncTimeBase(BString* outString, int64 value, const View* view, float width) }; BString date; + BDateTimeFormat formatter; for (int i = 0; resultWidth > width && i < 5; ++i) { - if (BLocale::Default()->FormatDateTime(&date, timeValue, - formats[i].dateStyle, formats[i].timeStyle) == B_OK) { + if (formatter.Format(date, timeValue, formats[i].dateStyle, + formats[i].timeStyle) == B_OK) { resultWidth = view->StringWidth(date.String(), date.Length()); } } diff --git a/src/preferences/locale/FormatSettingsView.cpp b/src/preferences/locale/FormatSettingsView.cpp index 1abd32e2d4..6463c4d8a9 100644 --- a/src/preferences/locale/FormatSettingsView.cpp +++ b/src/preferences/locale/FormatSettingsView.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -366,6 +367,7 @@ FormatSettingsView::_UpdateExamples() // to get the updated settings from the locale roster. BDateFormat dateFormat; BTimeFormat timeFormat; + BNumberFormat numberFormat; dateFormat.Format(result, timeValue, B_FULL_DATE_FORMAT); fFullDateExampleView->SetText(result); @@ -391,25 +393,25 @@ FormatSettingsView::_UpdateExamples() timeFormat.Format(result, timeValue, B_SHORT_TIME_FORMAT); fShortTimeExampleView->SetText(result); - status_t status = BLocale::Default()->FormatNumber(&result, 1234.5678); + status_t status = numberFormat.Format(result, 1234.5678); if (status == B_OK) fPositiveNumberExampleView->SetText(result); else fPositiveNumberExampleView->SetText("ERROR"); - status = BLocale::Default()->FormatNumber(&result, -1234.5678); + status = numberFormat.Format(result, -1234.5678); if (status == B_OK) fNegativeNumberExampleView->SetText(result); else fNegativeNumberExampleView->SetText("ERROR"); - status = BLocale::Default()->FormatMonetary(&result, 1234.56); + status = numberFormat.FormatMonetary(result, 1234.56); if (status == B_OK) fPositiveMonetaryExampleView->SetText(result); else fPositiveMonetaryExampleView->SetText("ERROR"); - status = BLocale::Default()->FormatMonetary(&result, -1234.56); + status = numberFormat.FormatMonetary(result, -1234.56); if (status == B_OK) fNegativeMonetaryExampleView->SetText(result); else