Move DateTimeFormat and NumberFormat out of BLocale

* ... and adjust all callers
* Remove NumberFormatImpl: we rely on ICU to provide this and it can be
fully wrapped into the C++ file. The class was a stub anyway.
* "Monetary" format is included in NumberFormat for now. There may be a
more generic solution to handle monetary and BTimeUnitFormat (and other
arbitrary units)
This commit is contained in:
Adrien Destugues 2014-10-02 09:19:54 +02:00
parent 9581e328bd
commit e3857211d3
15 changed files with 370 additions and 433 deletions

View File

@ -7,16 +7,32 @@
#include <DateTime.h>
#include <DateTimeFormat.h>
#include <Format.h>
#include <FormattingConventions.h>
#include <Language.h>
#include <Locker.h>
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,

View File

@ -6,25 +6,15 @@
#define _B_DATE_TIME_FORMAT_H_
#include <DateFormat.h>
#include <Format.h>
#include <FormatParameters.h>
#include <TimeFormat.h>
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;
};

View File

@ -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 <Collator.h>
#include <DateTimeFormat.h>
#include <FormattingConventions.h>
#include <Language.h>
#include <Locker.h>
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;

View File

@ -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 <Format.h>
#include <NumberFormatParameters.h>
class BNumberFormatImpl;
#include <Format.h>
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;
};

View File

@ -1,22 +0,0 @@
#ifndef _B_NUMBER_FORMAT_IMPL_H_
#define _B_NUMBER_FORMAT_IMPL_H_
#include <FormatImpl.h>
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_

View File

@ -22,6 +22,7 @@
#include <AppFileInfo.h>
#include <Application.h>
#include <Bitmap.h>
#include <DateTimeFormat.h>
#include <DurationFormat.h>
#include <File.h>
#include <FindDirectory.h>
@ -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

View File

@ -14,6 +14,7 @@
#include <Alert.h>
#include <Box.h>
#include <Catalog.h>
#include <DateTimeFormat.h>
#include <Directory.h>
#include <File.h>
#include <FindDirectory.h>
@ -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.")

View File

@ -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 <DateTimeFormat.h>
#include <AutoDeleter.h>
#include <Autolock.h>
#include <FormattingConventionsPrivate.h>
#include <LanguagePrivate.h>
#include <TimeZone.h>
#include <ICUWrapper.h>
#include <unicode/datefmt.h>
#include <unicode/smpdtfmt.h>
// 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<DateFormat> dateFormatter(_CreateDateFormatter(format));
if (dateFormatter.Get() == NULL)
return B_NO_MEMORY;
fConventions.GetTimeFormat(timeStyle, format);
ObjectDeleter<DateFormat> 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<DateFormat> dateFormatter(_CreateDateFormatter(format));
if (dateFormatter.Get() == NULL)
return B_NO_MEMORY;
fConventions.GetTimeFormat(timeStyle, format);
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(format));
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;
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<SimpleDateFormat*>(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<SimpleDateFormat*>(timeFormatter);
UnicodeString pattern(format.String());
timeFormatterImpl->applyPattern(pattern);
return timeFormatter;
}

View File

@ -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
;

View File

@ -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 <AutoDeleter.h>
#include <Autolock.h>
#include <CalendarView.h>
#include <Catalog.h>
#include <FormattingConventionsPrivate.h>
#include <LanguagePrivate.h>
#include <Locale.h>
#include <LocaleRoster.h>
#include <TimeZone.h>
#include <ICUWrapper.h>
#include <unicode/datefmt.h>
#include <unicode/dcfmtsym.h>
#include <unicode/decimfmt.h>
#include <unicode/dtfmtsym.h>
#include <unicode/numfmt.h>
#include <unicode/smpdtfmt.h>
#include <unicode/ustring.h>
#include <vector>
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<DateFormat> dateFormatter(_CreateDateFormatter(format));
if (dateFormatter.Get() == NULL)
return B_NO_MEMORY;
fConventions.GetTimeFormat(timeStyle, format);
ObjectDeleter<DateFormat> 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<DateFormat> dateFormatter(_CreateDateFormatter(format));
if (dateFormatter.Get() == NULL)
return B_NO_MEMORY;
fConventions.GetTimeFormat(timeStyle, format);
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(format));
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;
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<NumberFormat> 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<NumberFormat> 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<NumberFormat> 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<SimpleDateFormat*>(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<SimpleDateFormat*>(timeFormatter);
UnicodeString pattern(format.String());
timeFormatterImpl->applyPattern(pattern);
return timeFormatter;
}

View File

@ -1,18 +1,39 @@
#include <NumberFormat.h>
#include <NumberFormatImpl.h>
/*
* 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 <NumberFormat.h>
#include <AutoDeleter.h>
#include <Autolock.h>
#include <FormattingConventionsPrivate.h>
#include <ICUWrapper.h>
#include <unicode/numfmt.h>
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<NumberFormat> 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<NumberFormat> 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<NumberFormat> 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;
}

View File

@ -1,28 +0,0 @@
#include <NumberFormatImpl.h>
#include <NumberFormatParameters.h>
// constructor
BNumberFormatImpl::BNumberFormatImpl()
: BFormatImpl()
{
}
// destructor
BNumberFormatImpl::~BNumberFormatImpl()
{
}
// DefaultFormatParameters
BFormatParameters *
BNumberFormatImpl::DefaultFormatParameters()
{
return DefaultNumberFormatParameters();
}
// DefaultFormatParameters
const BFormatParameters *
BNumberFormatImpl::DefaultFormatParameters() const
{
return DefaultNumberFormatParameters();
}

View File

@ -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,

View File

@ -46,6 +46,7 @@ All rights reserved.
#include <AppFileInfo.h>
#include <Catalog.h>
#include <DateFormat.h>
#include <DateTimeFormat.h>
#include <Debug.h>
#include <Locale.h>
#include <NodeInfo.h>
@ -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());
}
}

View File

@ -25,6 +25,7 @@
#include <Menu.h>
#include <MenuField.h>
#include <MenuItem.h>
#include <NumberFormat.h>
#include <PopUpMenu.h>
#include <RadioButton.h>
#include <ScrollView.h>
@ -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