B*Format: make immutable and remove locking
The language and formatting conventions can now only be set when creating the objects. This removed the needs for locking them when formatting to avoid some other thread changing the format while it's being used. Adjust tests and DeskBar TimeView to the API changes.
This commit is contained in:
@@ -36,10 +36,9 @@ enum BWeekday {
|
|||||||
|
|
||||||
class BDateFormat: public BFormat {
|
class BDateFormat: public BFormat {
|
||||||
public:
|
public:
|
||||||
BDateFormat(
|
BDateFormat(const BLocale* locale = NULL);
|
||||||
const BLanguage* const language = NULL,
|
BDateFormat(const BLanguage& language,
|
||||||
const BFormattingConventions* const
|
const BFormattingConventions& format);
|
||||||
format = NULL);
|
|
||||||
BDateFormat(const BDateFormat &other);
|
BDateFormat(const BDateFormat &other);
|
||||||
virtual ~BDateFormat();
|
virtual ~BDateFormat();
|
||||||
|
|
||||||
|
|||||||
@@ -31,10 +31,9 @@ enum BDateElement {
|
|||||||
|
|
||||||
class BDateTimeFormat : public BFormat {
|
class BDateTimeFormat : public BFormat {
|
||||||
public:
|
public:
|
||||||
BDateTimeFormat(
|
BDateTimeFormat(const BLocale* locale = NULL);
|
||||||
const BLanguage* const language = NULL,
|
BDateTimeFormat(const BLanguage& language,
|
||||||
const BFormattingConventions* const format
|
const BFormattingConventions& conventions);
|
||||||
= NULL);
|
|
||||||
BDateTimeFormat(const BDateTimeFormat &other);
|
BDateTimeFormat(const BDateTimeFormat &other);
|
||||||
virtual ~BDateTimeFormat();
|
virtual ~BDateTimeFormat();
|
||||||
|
|
||||||
|
|||||||
@@ -23,16 +23,15 @@ class BDurationFormat : public BFormat {
|
|||||||
typedef BFormat Inherited;
|
typedef BFormat Inherited;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
BDurationFormat(const BLanguage& language,
|
||||||
|
const BFormattingConventions& conventions,
|
||||||
|
const BString& separator = ", ");
|
||||||
BDurationFormat(
|
BDurationFormat(
|
||||||
const BString& separator = ", ");
|
const BString& separator = ", ");
|
||||||
BDurationFormat(const BDurationFormat& other);
|
BDurationFormat(const BDurationFormat& other);
|
||||||
virtual ~BDurationFormat();
|
virtual ~BDurationFormat();
|
||||||
|
|
||||||
BDurationFormat& operator=(const BDurationFormat& other);
|
|
||||||
|
|
||||||
void SetSeparator(const BString& separator);
|
void SetSeparator(const BString& separator);
|
||||||
|
|
||||||
virtual status_t SetLanguage(const BLanguage& language);
|
|
||||||
status_t SetTimeZone(const BTimeZone* timeZone);
|
status_t SetTimeZone(const BTimeZone* timeZone);
|
||||||
|
|
||||||
status_t Format(BString& buffer,
|
status_t Format(BString& buffer,
|
||||||
|
|||||||
+15
-14
@@ -43,25 +43,26 @@ class BLocale;
|
|||||||
|
|
||||||
class BFormat {
|
class BFormat {
|
||||||
public:
|
public:
|
||||||
status_t SetLocale(const BLocale& locale);
|
status_t InitCheck() const;
|
||||||
virtual status_t SetLanguage(const BLanguage& newLanguage);
|
|
||||||
virtual status_t SetFormattingConventions(
|
|
||||||
const BFormattingConventions&
|
|
||||||
conventions);
|
|
||||||
|
|
||||||
status_t InitCheck() const;
|
|
||||||
protected:
|
protected:
|
||||||
BFormat();
|
BFormat(const BLocale* locale = NULL);
|
||||||
BFormat(const BFormat& other);
|
BFormat(const BLanguage& language,
|
||||||
virtual ~BFormat();
|
const BFormattingConventions& conventions);
|
||||||
|
|
||||||
BFormat& operator=(const BFormat& other);
|
BFormat(const BFormat& other);
|
||||||
|
virtual ~BFormat();
|
||||||
|
|
||||||
|
private:
|
||||||
|
BFormat& operator=(const BFormat& other);
|
||||||
|
|
||||||
|
status_t _Initialize(const BLocale& locale);
|
||||||
|
status_t _Initialize(const BLanguage& language,
|
||||||
|
const BFormattingConventions& conventions);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
mutable BLocker fLock;
|
|
||||||
BFormattingConventions fConventions;
|
BFormattingConventions fConventions;
|
||||||
BLanguage fLanguage;
|
BLanguage fLanguage;
|
||||||
status_t fInitStatus;
|
status_t fInitStatus;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -17,23 +17,19 @@ namespace icu {
|
|||||||
|
|
||||||
class BMessageFormat: public BFormat {
|
class BMessageFormat: public BFormat {
|
||||||
public:
|
public:
|
||||||
|
BMessageFormat(const BLanguage& language,
|
||||||
|
const BString pattern);
|
||||||
BMessageFormat(const BString pattern);
|
BMessageFormat(const BString pattern);
|
||||||
~BMessageFormat();
|
~BMessageFormat();
|
||||||
|
|
||||||
status_t InitCheck();
|
status_t InitCheck();
|
||||||
|
|
||||||
status_t SetLanguage(const BLanguage& newLanguage);
|
|
||||||
status_t SetFormattingConventions(
|
|
||||||
const BFormattingConventions&
|
|
||||||
conventions);
|
|
||||||
|
|
||||||
status_t Format(BString& buffer, const int32 arg) const;
|
status_t Format(BString& buffer, const int32 arg) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
status_t _Initialize(const icu::UnicodeString&);
|
status_t _Initialize(const icu::UnicodeString&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
status_t fInitStatus;
|
|
||||||
icu::MessageFormat* fFormatter;
|
icu::MessageFormat* fFormatter;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -23,8 +23,6 @@ public:
|
|||||||
BNumberFormat(const BNumberFormat &other);
|
BNumberFormat(const BNumberFormat &other);
|
||||||
~BNumberFormat();
|
~BNumberFormat();
|
||||||
|
|
||||||
BNumberFormat& operator=(const BNumberFormat &other);
|
|
||||||
|
|
||||||
// formatting
|
// formatting
|
||||||
|
|
||||||
ssize_t Format(char* string, size_t maxSize,
|
ssize_t Format(char* string, size_t maxSize,
|
||||||
|
|||||||
@@ -18,10 +18,9 @@ namespace BPrivate {
|
|||||||
|
|
||||||
class BTimeFormat : public BFormat {
|
class BTimeFormat : public BFormat {
|
||||||
public:
|
public:
|
||||||
BTimeFormat(
|
BTimeFormat();
|
||||||
const BLanguage* const language = NULL,
|
BTimeFormat(const BLanguage& language,
|
||||||
const BFormattingConventions* const
|
const BFormattingConventions& conventions);
|
||||||
format = NULL);
|
|
||||||
BTimeFormat(const BTimeFormat &other);
|
BTimeFormat(const BTimeFormat &other);
|
||||||
virtual ~BTimeFormat();
|
virtual ~BTimeFormat();
|
||||||
|
|
||||||
|
|||||||
@@ -41,12 +41,11 @@ class BTimeUnitFormat : public BFormat {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
BTimeUnitFormat();
|
BTimeUnitFormat();
|
||||||
|
BTimeUnitFormat(const BLanguage& language,
|
||||||
|
const BFormattingConventions& conventions);
|
||||||
BTimeUnitFormat(const BTimeUnitFormat& other);
|
BTimeUnitFormat(const BTimeUnitFormat& other);
|
||||||
virtual ~BTimeUnitFormat();
|
virtual ~BTimeUnitFormat();
|
||||||
|
|
||||||
BTimeUnitFormat& operator=(const BTimeUnitFormat& other);
|
|
||||||
|
|
||||||
virtual status_t SetLanguage(const BLanguage& locale);
|
|
||||||
status_t Format(BString& buffer,
|
status_t Format(BString& buffer,
|
||||||
const int32 value,
|
const int32 value,
|
||||||
const time_unit_element unit,
|
const time_unit_element unit,
|
||||||
|
|||||||
@@ -391,11 +391,10 @@ TTimeView::GetCurrentTime()
|
|||||||
if (fShowTimeZone)
|
if (fShowTimeZone)
|
||||||
fields |= B_DATE_ELEMENT_TIMEZONE;
|
fields |= B_DATE_ELEMENT_TIMEZONE;
|
||||||
|
|
||||||
fTimeFormat.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT,
|
BDateTimeFormat format(&fLocale);
|
||||||
fields);
|
format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, fields);
|
||||||
|
|
||||||
fTimeFormat.Format(fCurrentTimeStr,
|
format.Format(fCurrentTimeStr, sizeof(fCurrentTimeStr), fCurrentTime,
|
||||||
sizeof(fCurrentTimeStr), fCurrentTime,
|
|
||||||
B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT);
|
B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -405,7 +404,8 @@ TTimeView::GetCurrentDate()
|
|||||||
{
|
{
|
||||||
char tmp[sizeof(fCurrentDateStr)];
|
char tmp[sizeof(fCurrentDateStr)];
|
||||||
|
|
||||||
fDateFormat.Format(tmp, sizeof(fCurrentDateStr), fCurrentTime,
|
BDateFormat format(&fLocale);
|
||||||
|
format.Format(tmp, sizeof(fCurrentDateStr), fCurrentTime,
|
||||||
B_FULL_DATE_FORMAT);
|
B_FULL_DATE_FORMAT);
|
||||||
|
|
||||||
// remove leading 0 from date when month is less than 10 (MM/DD/YY)
|
// remove leading 0 from date when month is less than 10 (MM/DD/YY)
|
||||||
@@ -472,8 +472,6 @@ void
|
|||||||
TTimeView::Update()
|
TTimeView::Update()
|
||||||
{
|
{
|
||||||
fLocale = *BLocale::Default();
|
fLocale = *BLocale::Default();
|
||||||
fDateFormat.SetLocale(fLocale);
|
|
||||||
fTimeFormat.SetLocale(fLocale);
|
|
||||||
|
|
||||||
GetCurrentTime();
|
GetCurrentTime();
|
||||||
GetCurrentDate();
|
GetCurrentDate();
|
||||||
|
|||||||
@@ -150,8 +150,6 @@ private:
|
|||||||
|
|
||||||
// For date and time localization purposes
|
// For date and time localization purposes
|
||||||
BLocale fLocale;
|
BLocale fLocale;
|
||||||
BDateFormat fDateFormat;
|
|
||||||
BDateTimeFormat fTimeFormat;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -26,14 +26,16 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
BDateFormat::BDateFormat(const BLanguage* const language,
|
BDateFormat::BDateFormat(const BLocale* locale)
|
||||||
const BFormattingConventions* const conventions)
|
: BFormat(locale)
|
||||||
{
|
{
|
||||||
if (conventions != NULL)
|
}
|
||||||
fConventions = *conventions;
|
|
||||||
|
|
||||||
if (language != NULL)
|
|
||||||
fLanguage = *language;
|
BDateFormat::BDateFormat(const BLanguage& language,
|
||||||
|
const BFormattingConventions& conventions)
|
||||||
|
: BFormat(language, conventions)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -68,10 +70,6 @@ ssize_t
|
|||||||
BDateFormat::Format(char* string, const size_t maxSize, const time_t time,
|
BDateFormat::Format(char* string, const size_t maxSize, const time_t time,
|
||||||
const BDateFormatStyle style) const
|
const BDateFormatStyle style) const
|
||||||
{
|
{
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
ObjectDeleter<DateFormat> dateFormatter(_CreateDateFormatter(style));
|
ObjectDeleter<DateFormat> dateFormatter(_CreateDateFormatter(style));
|
||||||
if (dateFormatter.Get() == NULL)
|
if (dateFormatter.Get() == NULL)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
@@ -93,10 +91,6 @@ status_t
|
|||||||
BDateFormat::Format(BString& string, const time_t time,
|
BDateFormat::Format(BString& string, const time_t time,
|
||||||
const BDateFormatStyle style, const BTimeZone* timeZone) const
|
const BDateFormatStyle style, const BTimeZone* timeZone) const
|
||||||
{
|
{
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
ObjectDeleter<DateFormat> dateFormatter(_CreateDateFormatter(style));
|
ObjectDeleter<DateFormat> dateFormatter(_CreateDateFormatter(style));
|
||||||
if (dateFormatter.Get() == NULL)
|
if (dateFormatter.Get() == NULL)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
@@ -127,10 +121,6 @@ BDateFormat::Format(BString& string, const BDate& time,
|
|||||||
if (!time.IsValid())
|
if (!time.IsValid())
|
||||||
return B_BAD_DATA;
|
return B_BAD_DATA;
|
||||||
|
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
ObjectDeleter<DateFormat> dateFormatter(_CreateDateFormatter(style));
|
ObjectDeleter<DateFormat> dateFormatter(_CreateDateFormatter(style));
|
||||||
if (dateFormatter.Get() == NULL)
|
if (dateFormatter.Get() == NULL)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
@@ -169,10 +159,6 @@ status_t
|
|||||||
BDateFormat::Format(BString& string, int*& fieldPositions, int& fieldCount,
|
BDateFormat::Format(BString& string, int*& fieldPositions, int& fieldCount,
|
||||||
const time_t time, const BDateFormatStyle style) const
|
const time_t time, const BDateFormatStyle style) const
|
||||||
{
|
{
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
ObjectDeleter<DateFormat> dateFormatter(_CreateDateFormatter(style));
|
ObjectDeleter<DateFormat> dateFormatter(_CreateDateFormatter(style));
|
||||||
if (dateFormatter.Get() == NULL)
|
if (dateFormatter.Get() == NULL)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
@@ -214,10 +200,6 @@ status_t
|
|||||||
BDateFormat::GetFields(BDateElement*& fields, int& fieldCount,
|
BDateFormat::GetFields(BDateElement*& fields, int& fieldCount,
|
||||||
BDateFormatStyle style) const
|
BDateFormatStyle style) const
|
||||||
{
|
{
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
ObjectDeleter<DateFormat> dateFormatter(_CreateDateFormatter(style));
|
ObjectDeleter<DateFormat> dateFormatter(_CreateDateFormatter(style));
|
||||||
if (dateFormatter.Get() == NULL)
|
if (dateFormatter.Get() == NULL)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
@@ -270,10 +252,6 @@ BDateFormat::GetStartOfWeek(BWeekday* startOfWeek) const
|
|||||||
if (startOfWeek == NULL)
|
if (startOfWeek == NULL)
|
||||||
return B_BAD_VALUE;
|
return B_BAD_VALUE;
|
||||||
|
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
UErrorCode err = U_ZERO_ERROR;
|
UErrorCode err = U_ZERO_ERROR;
|
||||||
ObjectDeleter<Calendar> calendar = Calendar::createInstance(
|
ObjectDeleter<Calendar> calendar = Calendar::createInstance(
|
||||||
*BFormattingConventions::Private(&fConventions).ICULocale(), err);
|
*BFormattingConventions::Private(&fConventions).ICULocale(), err);
|
||||||
@@ -318,10 +296,6 @@ BDateFormat::GetStartOfWeek(BWeekday* startOfWeek) const
|
|||||||
status_t
|
status_t
|
||||||
BDateFormat::GetMonthName(int month, BString& outName)
|
BDateFormat::GetMonthName(int month, BString& outName)
|
||||||
{
|
{
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
DateFormat* format = _CreateDateFormatter(B_LONG_DATE_FORMAT);
|
DateFormat* format = _CreateDateFormatter(B_LONG_DATE_FORMAT);
|
||||||
|
|
||||||
SimpleDateFormat* simpleFormat = dynamic_cast<SimpleDateFormat*>(format);
|
SimpleDateFormat* simpleFormat = dynamic_cast<SimpleDateFormat*>(format);
|
||||||
@@ -356,15 +330,10 @@ BDateFormat::Parse(BString source, BDateFormatStyle style, BDate& output)
|
|||||||
// may want to parse to a "local" date instead. But BDate should be made
|
// may want to parse to a "local" date instead. But BDate should be made
|
||||||
// timezone aware so things like BDate::Difference can work for dates in
|
// timezone aware so things like BDate::Difference can work for dates in
|
||||||
// different timezones.
|
// different timezones.
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
ObjectDeleter<DateFormat> dateFormatter(_CreateDateFormatter(style));
|
ObjectDeleter<DateFormat> dateFormatter(_CreateDateFormatter(style));
|
||||||
if (dateFormatter.Get() == NULL)
|
if (dateFormatter.Get() == NULL)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
|
|
||||||
|
|
||||||
ParsePosition p(0);
|
ParsePosition p(0);
|
||||||
UDate date = dateFormatter->parse(UnicodeString::fromUTF8(source.String()),
|
UDate date = dateFormatter->parse(UnicodeString::fromUTF8(source.String()),
|
||||||
p);
|
p);
|
||||||
@@ -401,4 +370,3 @@ BDateFormat::_CreateDateFormatter(const BDateFormatStyle style) const
|
|||||||
return dateFormatter;
|
return dateFormatter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -21,14 +21,16 @@
|
|||||||
#include <unicode/smpdtfmt.h>
|
#include <unicode/smpdtfmt.h>
|
||||||
|
|
||||||
|
|
||||||
BDateTimeFormat::BDateTimeFormat(const BLanguage* const language,
|
BDateTimeFormat::BDateTimeFormat(const BLocale* locale)
|
||||||
const BFormattingConventions* const conventions)
|
: BFormat(locale)
|
||||||
{
|
{
|
||||||
if (conventions != NULL)
|
}
|
||||||
fConventions = *conventions;
|
|
||||||
|
|
||||||
if (language != NULL)
|
|
||||||
fLanguage = *language;
|
BDateTimeFormat::BDateTimeFormat(const BLanguage& language,
|
||||||
|
const BFormattingConventions& conventions)
|
||||||
|
: BFormat(language, conventions)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -95,10 +97,6 @@ ssize_t
|
|||||||
BDateTimeFormat::Format(char* target, size_t maxSize, time_t time,
|
BDateTimeFormat::Format(char* target, size_t maxSize, time_t time,
|
||||||
BDateFormatStyle dateStyle, BTimeFormatStyle timeStyle) const
|
BDateFormatStyle dateStyle, BTimeFormatStyle timeStyle) const
|
||||||
{
|
{
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
BString format;
|
BString format;
|
||||||
fConventions.GetDateTimeFormat(dateStyle, timeStyle, format);
|
fConventions.GetDateTimeFormat(dateStyle, timeStyle, format);
|
||||||
ObjectDeleter<DateFormat> dateFormatter(_CreateDateTimeFormatter(format));
|
ObjectDeleter<DateFormat> dateFormatter(_CreateDateTimeFormatter(format));
|
||||||
@@ -123,10 +121,6 @@ BDateTimeFormat::Format(BString& target, const time_t time,
|
|||||||
BDateFormatStyle dateStyle, BTimeFormatStyle timeStyle,
|
BDateFormatStyle dateStyle, BTimeFormatStyle timeStyle,
|
||||||
const BTimeZone* timeZone) const
|
const BTimeZone* timeZone) const
|
||||||
{
|
{
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
BString format;
|
BString format;
|
||||||
fConventions.GetDateTimeFormat(dateStyle, timeStyle, format);
|
fConventions.GetDateTimeFormat(dateStyle, timeStyle, format);
|
||||||
ObjectDeleter<DateFormat> dateFormatter(_CreateDateTimeFormatter(format));
|
ObjectDeleter<DateFormat> dateFormatter(_CreateDateTimeFormatter(format));
|
||||||
|
|||||||
@@ -33,6 +33,22 @@ static const UCalendarDateFields skUnitMap[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
BDurationFormat::BDurationFormat(const BLanguage& language,
|
||||||
|
const BFormattingConventions& conventions, const BString& separator)
|
||||||
|
:
|
||||||
|
Inherited(language, conventions),
|
||||||
|
fSeparator(separator),
|
||||||
|
fTimeUnitFormat(language, conventions)
|
||||||
|
{
|
||||||
|
UErrorCode icuStatus = U_ZERO_ERROR;
|
||||||
|
fCalendar = new GregorianCalendar(icuStatus);
|
||||||
|
if (fCalendar == NULL) {
|
||||||
|
fInitStatus = B_NO_MEMORY;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
BDurationFormat::BDurationFormat(const BString& separator)
|
BDurationFormat::BDurationFormat(const BString& separator)
|
||||||
:
|
:
|
||||||
Inherited(),
|
Inherited(),
|
||||||
@@ -67,25 +83,6 @@ BDurationFormat::~BDurationFormat()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
BDurationFormat&
|
|
||||||
BDurationFormat::operator=(const BDurationFormat& other)
|
|
||||||
{
|
|
||||||
if (this == &other)
|
|
||||||
return *this;
|
|
||||||
|
|
||||||
fSeparator = other.fSeparator;
|
|
||||||
fTimeUnitFormat = other.fTimeUnitFormat;
|
|
||||||
delete fCalendar;
|
|
||||||
fCalendar = other.fCalendar != NULL
|
|
||||||
? new GregorianCalendar(*other.fCalendar) : NULL;
|
|
||||||
|
|
||||||
if (fCalendar == NULL && other.fCalendar != NULL)
|
|
||||||
fInitStatus = B_NO_MEMORY;
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
BDurationFormat::SetSeparator(const BString& separator)
|
BDurationFormat::SetSeparator(const BString& separator)
|
||||||
{
|
{
|
||||||
@@ -93,17 +90,6 @@ BDurationFormat::SetSeparator(const BString& separator)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
status_t
|
|
||||||
BDurationFormat::SetLanguage(const BLanguage& language)
|
|
||||||
{
|
|
||||||
status_t result = Inherited::SetLanguage(language);
|
|
||||||
if (result != B_OK)
|
|
||||||
return result;
|
|
||||||
|
|
||||||
return fTimeUnitFormat.SetLanguage(language);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
BDurationFormat::SetTimeZone(const BTimeZone* timeZone)
|
BDurationFormat::SetTimeZone(const BTimeZone* timeZone)
|
||||||
{
|
{
|
||||||
|
|||||||
+34
-51
@@ -7,18 +7,33 @@
|
|||||||
#include <LocaleRoster.h>
|
#include <LocaleRoster.h>
|
||||||
|
|
||||||
|
|
||||||
BFormat::BFormat()
|
BFormat::BFormat(const BLocale* locale)
|
||||||
{
|
{
|
||||||
const BLocale* locale = BLocaleRoster::Default()->GetDefaultLocale();
|
if (locale == NULL)
|
||||||
SetLocale(*locale);
|
locale = BLocaleRoster::Default()->GetDefaultLocale();
|
||||||
|
|
||||||
|
if (locale == NULL) {
|
||||||
|
fInitStatus = B_BAD_DATA;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_Initialize(*locale);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BFormat::BFormat(const BLanguage& language,
|
||||||
|
const BFormattingConventions& conventions)
|
||||||
|
{
|
||||||
|
_Initialize(language, conventions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
BFormat::BFormat(const BFormat &other)
|
BFormat::BFormat(const BFormat &other)
|
||||||
|
:
|
||||||
|
fConventions(other.fConventions),
|
||||||
|
fLanguage(other.fLanguage),
|
||||||
|
fInitStatus(other.fInitStatus)
|
||||||
{
|
{
|
||||||
fInitStatus = other.fInitStatus;
|
|
||||||
fConventions = other.fConventions;
|
|
||||||
fLanguage = other.fLanguage;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -27,20 +42,6 @@ BFormat::~BFormat()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
BFormat &
|
|
||||||
BFormat::operator=(const BFormat& other)
|
|
||||||
{
|
|
||||||
if (this == &other)
|
|
||||||
return *this;
|
|
||||||
|
|
||||||
fInitStatus = other.fInitStatus;
|
|
||||||
fLanguage = other.fLanguage;
|
|
||||||
fInitStatus = other.fInitStatus;
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
BFormat::InitCheck() const
|
BFormat::InitCheck() const
|
||||||
{
|
{
|
||||||
@@ -49,47 +50,29 @@ BFormat::InitCheck() const
|
|||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
BFormat::SetLocale(const BLocale& locale)
|
BFormat::_Initialize(const BLocale& locale)
|
||||||
{
|
{
|
||||||
BFormattingConventions conventions;
|
BFormattingConventions conventions;
|
||||||
BLanguage language;
|
BLanguage language;
|
||||||
|
|
||||||
fInitStatus = locale.GetFormattingConventions(&conventions);
|
fInitStatus = locale.GetFormattingConventions(&conventions);
|
||||||
if (fInitStatus != B_OK)
|
|
||||||
return fInitStatus;
|
|
||||||
fInitStatus = SetFormattingConventions(conventions);
|
|
||||||
if (fInitStatus != B_OK)
|
if (fInitStatus != B_OK)
|
||||||
return fInitStatus;
|
return fInitStatus;
|
||||||
|
|
||||||
fInitStatus = locale.GetLanguage(&language);
|
fInitStatus = locale.GetLanguage(&language);
|
||||||
if (fInitStatus != B_OK)
|
if (fInitStatus != B_OK)
|
||||||
return fInitStatus;
|
return fInitStatus;
|
||||||
fInitStatus = SetLanguage(language);
|
|
||||||
|
return _Initialize(language, conventions);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
status_t
|
||||||
|
BFormat::_Initialize(const BLanguage& language,
|
||||||
|
const BFormattingConventions& conventions)
|
||||||
|
{
|
||||||
|
fConventions = conventions;
|
||||||
|
fLanguage = language;
|
||||||
|
fInitStatus = B_OK;
|
||||||
return fInitStatus;
|
return fInitStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
status_t
|
|
||||||
BFormat::SetFormattingConventions(const BFormattingConventions& conventions)
|
|
||||||
{
|
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_WOULD_BLOCK;
|
|
||||||
|
|
||||||
fConventions = conventions;
|
|
||||||
return B_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
status_t
|
|
||||||
BFormat::SetLanguage(const BLanguage& newLanguage)
|
|
||||||
{
|
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_WOULD_BLOCK;
|
|
||||||
|
|
||||||
fLanguage = newLanguage;
|
|
||||||
return B_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,13 @@
|
|||||||
#include <unicode/msgfmt.h>
|
#include <unicode/msgfmt.h>
|
||||||
|
|
||||||
|
|
||||||
|
BMessageFormat::BMessageFormat(const BLanguage& language, const BString pattern)
|
||||||
|
: BFormat(language, BFormattingConventions())
|
||||||
|
{
|
||||||
|
_Initialize(UnicodeString::fromUTF8(pattern.String()));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
BMessageFormat::BMessageFormat(const BString pattern)
|
BMessageFormat::BMessageFormat(const BString pattern)
|
||||||
: BFormat()
|
: BFormat()
|
||||||
{
|
{
|
||||||
@@ -33,57 +40,12 @@ BMessageFormat::InitCheck()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
status_t
|
|
||||||
BMessageFormat::SetLanguage(const BLanguage& newLanguage)
|
|
||||||
{
|
|
||||||
if (!fFormatter)
|
|
||||||
return B_NO_INIT;
|
|
||||||
|
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
fInitStatus = BFormat::SetLanguage(newLanguage);
|
|
||||||
|
|
||||||
if (fInitStatus == B_OK) {
|
|
||||||
UnicodeString storage;
|
|
||||||
_Initialize(fFormatter->toPattern(storage));
|
|
||||||
}
|
|
||||||
return fInitStatus;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
status_t
|
|
||||||
BMessageFormat::SetFormattingConventions(
|
|
||||||
const BFormattingConventions& conventions)
|
|
||||||
{
|
|
||||||
if (!fFormatter)
|
|
||||||
return B_NO_INIT;
|
|
||||||
|
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
fInitStatus = BFormat::SetFormattingConventions(conventions);
|
|
||||||
|
|
||||||
if (fInitStatus == B_OK) {
|
|
||||||
UnicodeString storage;
|
|
||||||
_Initialize(fFormatter->toPattern(storage));
|
|
||||||
}
|
|
||||||
return fInitStatus;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
BMessageFormat::Format(BString& output, const int32 arg) const
|
BMessageFormat::Format(BString& output, const int32 arg) const
|
||||||
{
|
{
|
||||||
if (fInitStatus != B_OK)
|
if (fInitStatus != B_OK)
|
||||||
return fInitStatus;
|
return fInitStatus;
|
||||||
|
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
UnicodeString buffer;
|
UnicodeString buffer;
|
||||||
UErrorCode error = U_ZERO_ERROR;
|
UErrorCode error = U_ZERO_ERROR;
|
||||||
|
|
||||||
@@ -106,6 +68,7 @@ BMessageFormat::Format(BString& output, const int32 arg) const
|
|||||||
status_t
|
status_t
|
||||||
BMessageFormat::_Initialize(const UnicodeString& pattern)
|
BMessageFormat::_Initialize(const UnicodeString& pattern)
|
||||||
{
|
{
|
||||||
|
fInitStatus = B_OK;
|
||||||
UErrorCode error = U_ZERO_ERROR;
|
UErrorCode error = U_ZERO_ERROR;
|
||||||
Locale* icuLocale
|
Locale* icuLocale
|
||||||
= BLanguage::Private(&fLanguage).ICULocale();
|
= BLanguage::Private(&fLanguage).ICULocale();
|
||||||
|
|||||||
@@ -34,14 +34,6 @@ BNumberFormat::~BNumberFormat()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
BNumberFormat &
|
|
||||||
BNumberFormat::operator=(const BNumberFormat &other)
|
|
||||||
{
|
|
||||||
BFormat::operator=(other);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// #pragma mark - Formatting
|
// #pragma mark - Formatting
|
||||||
|
|
||||||
|
|
||||||
@@ -60,10 +52,6 @@ BNumberFormat::Format(char* string, size_t maxSize, const double value) const
|
|||||||
status_t
|
status_t
|
||||||
BNumberFormat::Format(BString& string, const double value) const
|
BNumberFormat::Format(BString& string, const double value) const
|
||||||
{
|
{
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
UErrorCode err = U_ZERO_ERROR;
|
UErrorCode err = U_ZERO_ERROR;
|
||||||
ObjectDeleter<NumberFormat> numberFormatter(NumberFormat::createInstance(
|
ObjectDeleter<NumberFormat> numberFormatter(NumberFormat::createInstance(
|
||||||
*BFormattingConventions::Private(&fConventions).ICULocale(),
|
*BFormattingConventions::Private(&fConventions).ICULocale(),
|
||||||
@@ -100,10 +88,6 @@ BNumberFormat::Format(char* string, size_t maxSize, const int32 value) const
|
|||||||
status_t
|
status_t
|
||||||
BNumberFormat::Format(BString& string, const int32 value) const
|
BNumberFormat::Format(BString& string, const int32 value) const
|
||||||
{
|
{
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
UErrorCode err = U_ZERO_ERROR;
|
UErrorCode err = U_ZERO_ERROR;
|
||||||
ObjectDeleter<NumberFormat> numberFormatter(NumberFormat::createInstance(
|
ObjectDeleter<NumberFormat> numberFormatter(NumberFormat::createInstance(
|
||||||
*BFormattingConventions::Private(&fConventions).ICULocale(),
|
*BFormattingConventions::Private(&fConventions).ICULocale(),
|
||||||
@@ -144,10 +128,6 @@ BNumberFormat::FormatMonetary(BString& string, const double value) const
|
|||||||
if (string == NULL)
|
if (string == NULL)
|
||||||
return B_BAD_VALUE;
|
return B_BAD_VALUE;
|
||||||
|
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
UErrorCode err = U_ZERO_ERROR;
|
UErrorCode err = U_ZERO_ERROR;
|
||||||
ObjectDeleter<NumberFormat> numberFormatter(
|
ObjectDeleter<NumberFormat> numberFormatter(
|
||||||
NumberFormat::createCurrencyInstance(
|
NumberFormat::createCurrencyInstance(
|
||||||
|
|||||||
@@ -23,14 +23,16 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
BTimeFormat::BTimeFormat(const BLanguage* const language,
|
BTimeFormat::BTimeFormat()
|
||||||
const BFormattingConventions* const conventions)
|
: BFormat()
|
||||||
{
|
{
|
||||||
if (conventions != NULL)
|
}
|
||||||
fConventions = *conventions;
|
|
||||||
|
|
||||||
if (language != NULL)
|
|
||||||
fLanguage = *language;
|
BTimeFormat::BTimeFormat(const BLanguage& language,
|
||||||
|
const BFormattingConventions& conventions)
|
||||||
|
: BFormat(language, conventions)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -60,10 +62,6 @@ ssize_t
|
|||||||
BTimeFormat::Format(char* string, size_t maxSize, time_t time,
|
BTimeFormat::Format(char* string, size_t maxSize, time_t time,
|
||||||
BTimeFormatStyle style) const
|
BTimeFormatStyle style) const
|
||||||
{
|
{
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(style));
|
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(style));
|
||||||
if (timeFormatter.Get() == NULL)
|
if (timeFormatter.Get() == NULL)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
@@ -85,10 +83,6 @@ status_t
|
|||||||
BTimeFormat::Format(BString& string, const time_t time,
|
BTimeFormat::Format(BString& string, const time_t time,
|
||||||
const BTimeFormatStyle style, const BTimeZone* timeZone) const
|
const BTimeFormatStyle style, const BTimeZone* timeZone) const
|
||||||
{
|
{
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(style));
|
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(style));
|
||||||
if (timeFormatter.Get() == NULL)
|
if (timeFormatter.Get() == NULL)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
@@ -116,10 +110,6 @@ status_t
|
|||||||
BTimeFormat::Format(BString& string, int*& fieldPositions, int& fieldCount,
|
BTimeFormat::Format(BString& string, int*& fieldPositions, int& fieldCount,
|
||||||
time_t time, BTimeFormatStyle style) const
|
time_t time, BTimeFormatStyle style) const
|
||||||
{
|
{
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(style));
|
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(style));
|
||||||
if (timeFormatter.Get() == NULL)
|
if (timeFormatter.Get() == NULL)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
@@ -160,10 +150,6 @@ status_t
|
|||||||
BTimeFormat::GetTimeFields(BDateElement*& fields, int& fieldCount,
|
BTimeFormat::GetTimeFields(BDateElement*& fields, int& fieldCount,
|
||||||
BTimeFormatStyle style) const
|
BTimeFormatStyle style) const
|
||||||
{
|
{
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(style));
|
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(style));
|
||||||
if (timeFormatter.Get() == NULL)
|
if (timeFormatter.Get() == NULL)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
@@ -219,10 +205,6 @@ BTimeFormat::GetTimeFields(BDateElement*& fields, int& fieldCount,
|
|||||||
status_t
|
status_t
|
||||||
BTimeFormat::Parse(BString source, BTimeFormatStyle style, BTime& output)
|
BTimeFormat::Parse(BString source, BTimeFormatStyle style, BTime& output)
|
||||||
{
|
{
|
||||||
BAutolock lock(fLock);
|
|
||||||
if (!lock.IsLocked())
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(style));
|
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(style));
|
||||||
if (timeFormatter.Get() == NULL)
|
if (timeFormatter.Get() == NULL)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
|
|||||||
@@ -35,11 +35,35 @@ static const TimeUnit::UTimeUnitFields skUnitMap[] = {
|
|||||||
|
|
||||||
|
|
||||||
BTimeUnitFormat::BTimeUnitFormat()
|
BTimeUnitFormat::BTimeUnitFormat()
|
||||||
:
|
: Inherited()
|
||||||
Inherited(),
|
|
||||||
fFormatter(NULL)
|
|
||||||
{
|
{
|
||||||
SetLanguage(fLanguage);
|
Locale icuLocale(fLanguage.Code());
|
||||||
|
UErrorCode icuStatus = U_ZERO_ERROR;
|
||||||
|
fFormatter = new TimeUnitFormat(icuLocale, icuStatus);
|
||||||
|
if (fFormatter == NULL) {
|
||||||
|
fInitStatus = B_NO_MEMORY;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!U_SUCCESS(icuStatus))
|
||||||
|
fInitStatus = B_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BTimeUnitFormat::BTimeUnitFormat(const BLanguage& language,
|
||||||
|
const BFormattingConventions& conventions)
|
||||||
|
: Inherited(language, conventions)
|
||||||
|
{
|
||||||
|
Locale icuLocale(fLanguage.Code());
|
||||||
|
UErrorCode icuStatus = U_ZERO_ERROR;
|
||||||
|
fFormatter = new TimeUnitFormat(icuLocale, icuStatus);
|
||||||
|
if (fFormatter == NULL) {
|
||||||
|
fInitStatus = B_NO_MEMORY;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!U_SUCCESS(icuStatus))
|
||||||
|
fInitStatus = B_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -60,47 +84,6 @@ BTimeUnitFormat::~BTimeUnitFormat()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
BTimeUnitFormat&
|
|
||||||
BTimeUnitFormat::operator=(const BTimeUnitFormat& other)
|
|
||||||
{
|
|
||||||
if (this == &other)
|
|
||||||
return *this;
|
|
||||||
|
|
||||||
Inherited::operator=(other);
|
|
||||||
|
|
||||||
delete fFormatter;
|
|
||||||
fFormatter = other.fFormatter != NULL
|
|
||||||
? new TimeUnitFormat(*other.fFormatter) : NULL;
|
|
||||||
|
|
||||||
if (fFormatter == NULL && other.fFormatter != NULL)
|
|
||||||
fInitStatus = B_NO_MEMORY;
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
status_t
|
|
||||||
BTimeUnitFormat::SetLanguage(const BLanguage& language)
|
|
||||||
{
|
|
||||||
status_t result = Inherited::SetLanguage(language);
|
|
||||||
if (result != B_OK)
|
|
||||||
return result;
|
|
||||||
|
|
||||||
Locale icuLocale(fLanguage.Code());
|
|
||||||
UErrorCode icuStatus = U_ZERO_ERROR;
|
|
||||||
if (fFormatter == NULL) {
|
|
||||||
fFormatter = new TimeUnitFormat(icuLocale, icuStatus);
|
|
||||||
if (fFormatter == NULL)
|
|
||||||
return B_NO_MEMORY;
|
|
||||||
} else
|
|
||||||
fFormatter->setLocale(icuLocale, icuStatus);
|
|
||||||
if (!U_SUCCESS(icuStatus))
|
|
||||||
return B_ERROR;
|
|
||||||
|
|
||||||
return B_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
BTimeUnitFormat::Format(BString& buffer, const int32 value,
|
BTimeUnitFormat::Format(BString& buffer, const int32 value,
|
||||||
const time_unit_element unit, time_unit_style style) const
|
const time_unit_element unit, time_unit_style style) const
|
||||||
|
|||||||
@@ -65,39 +65,46 @@ DateFormatTest::TestCustomFormat()
|
|||||||
|
|
||||||
BLanguage language(tests[i].language);
|
BLanguage language(tests[i].language);
|
||||||
BFormattingConventions formatting(tests[i].formatting);
|
BFormattingConventions formatting(tests[i].formatting);
|
||||||
|
status_t result;
|
||||||
BDateTimeFormat format(&language, &formatting);
|
|
||||||
format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT,
|
|
||||||
tests[i].fields);
|
|
||||||
|
|
||||||
// Test default for language/formatting
|
// Test default for language/formatting
|
||||||
status_t result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT,
|
{
|
||||||
B_SHORT_TIME_FORMAT);
|
BDateTimeFormat format(language, formatting);
|
||||||
|
format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT,
|
||||||
|
tests[i].fields);
|
||||||
|
|
||||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT,
|
||||||
CPPUNIT_ASSERT_EQUAL(tests[i].expected, buffer);
|
B_SHORT_TIME_FORMAT);
|
||||||
|
|
||||||
|
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||||
|
CPPUNIT_ASSERT_EQUAL(tests[i].expected, buffer);
|
||||||
|
}
|
||||||
|
|
||||||
// Test forced 24 hours
|
// Test forced 24 hours
|
||||||
formatting.SetExplicitUse24HourClock(true);
|
{
|
||||||
format.SetFormattingConventions(formatting);
|
formatting.SetExplicitUse24HourClock(true);
|
||||||
format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT,
|
BDateTimeFormat format(language, formatting);
|
||||||
tests[i].fields);
|
format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT,
|
||||||
result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT,
|
tests[i].fields);
|
||||||
B_SHORT_TIME_FORMAT);
|
result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT,
|
||||||
|
B_SHORT_TIME_FORMAT);
|
||||||
|
|
||||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||||
CPPUNIT_ASSERT_EQUAL(tests[i].force24, buffer);
|
CPPUNIT_ASSERT_EQUAL(tests[i].force24, buffer);
|
||||||
|
}
|
||||||
|
|
||||||
// Test forced 12 hours
|
// Test forced 12 hours
|
||||||
formatting.SetExplicitUse24HourClock(false);
|
{
|
||||||
format.SetFormattingConventions(formatting);
|
formatting.SetExplicitUse24HourClock(false);
|
||||||
format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT,
|
BDateTimeFormat format(language, formatting);
|
||||||
tests[i].fields);
|
format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT,
|
||||||
result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT,
|
tests[i].fields);
|
||||||
B_SHORT_TIME_FORMAT);
|
result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT,
|
||||||
|
B_SHORT_TIME_FORMAT);
|
||||||
|
|
||||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||||
CPPUNIT_ASSERT_EQUAL(tests[i].force12, buffer);
|
CPPUNIT_ASSERT_EQUAL(tests[i].force12, buffer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,9 +141,9 @@ DateFormatTest::TestFormat()
|
|||||||
|
|
||||||
BLanguage language(values[i].language);
|
BLanguage language(values[i].language);
|
||||||
BFormattingConventions formatting(values[i].convention);
|
BFormattingConventions formatting(values[i].convention);
|
||||||
BDateFormat dateFormat(&language, &formatting);
|
BDateFormat dateFormat(language, formatting);
|
||||||
BTimeFormat timeFormat(&language, &formatting);
|
BTimeFormat timeFormat(language, formatting);
|
||||||
BDateTimeFormat dateTimeFormat(&language, &formatting);
|
BDateTimeFormat dateTimeFormat(language, formatting);
|
||||||
|
|
||||||
result = dateFormat.Format(output, values[i].time, B_SHORT_DATE_FORMAT);
|
result = dateFormat.Format(output, values[i].time, B_SHORT_DATE_FORMAT);
|
||||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||||
@@ -167,7 +174,7 @@ DateFormatTest::TestFormatDate()
|
|||||||
{
|
{
|
||||||
BLanguage language("en");
|
BLanguage language("en");
|
||||||
BFormattingConventions formatting("en_US");
|
BFormattingConventions formatting("en_US");
|
||||||
BDateFormat format(&language, &formatting);
|
BDateFormat format(language, formatting);
|
||||||
|
|
||||||
BString output;
|
BString output;
|
||||||
status_t result;
|
status_t result;
|
||||||
@@ -190,7 +197,7 @@ DateFormatTest::TestMonthNames()
|
|||||||
{
|
{
|
||||||
BLanguage language("en");
|
BLanguage language("en");
|
||||||
BFormattingConventions formatting("en_US");
|
BFormattingConventions formatting("en_US");
|
||||||
BDateFormat format(&language, &formatting);
|
BDateFormat format(language, formatting);
|
||||||
|
|
||||||
BString buffer;
|
BString buffer;
|
||||||
status_t result = format.GetMonthName(1, buffer);
|
status_t result = format.GetMonthName(1, buffer);
|
||||||
@@ -235,7 +242,7 @@ DateFormatTest::TestParseDate()
|
|||||||
{
|
{
|
||||||
BLanguage language("en");
|
BLanguage language("en");
|
||||||
BFormattingConventions formatting("en_US");
|
BFormattingConventions formatting("en_US");
|
||||||
BDateFormat format(&language, &formatting);
|
BDateFormat format(language, formatting);
|
||||||
BDate date;
|
BDate date;
|
||||||
status_t result;
|
status_t result;
|
||||||
|
|
||||||
@@ -266,7 +273,7 @@ DateFormatTest::TestParseTime()
|
|||||||
{
|
{
|
||||||
BLanguage language("fr");
|
BLanguage language("fr");
|
||||||
BFormattingConventions formatting("fr_FR");
|
BFormattingConventions formatting("fr_FR");
|
||||||
BTimeFormat format(&language, &formatting);
|
BTimeFormat format(language, formatting);
|
||||||
BTime date;
|
BTime date;
|
||||||
status_t result;
|
status_t result;
|
||||||
|
|
||||||
|
|||||||
@@ -42,9 +42,9 @@ DurationFormatTest::TestDefault()
|
|||||||
void
|
void
|
||||||
DurationFormatTest::TestDuration()
|
DurationFormatTest::TestDuration()
|
||||||
{
|
{
|
||||||
BDurationFormat format;
|
|
||||||
BString buffer;
|
BString buffer;
|
||||||
BString expected;
|
BString expected;
|
||||||
|
status_t result;
|
||||||
|
|
||||||
BFormattingConventions englishFormat("en_US");
|
BFormattingConventions englishFormat("en_US");
|
||||||
BLanguage englishLanguage("en");
|
BLanguage englishLanguage("en");
|
||||||
@@ -52,30 +52,32 @@ DurationFormatTest::TestDuration()
|
|||||||
BFormattingConventions frenchFormat("fr_FR");
|
BFormattingConventions frenchFormat("fr_FR");
|
||||||
BLanguage frenchLanguage("fr");
|
BLanguage frenchLanguage("fr");
|
||||||
|
|
||||||
format.SetFormattingConventions(englishFormat);
|
{
|
||||||
format.SetLanguage(englishLanguage);
|
BDurationFormat format(englishLanguage, englishFormat);
|
||||||
status_t result = format.Format(buffer, 0, 800000000000ll);
|
status_t result = format.Format(buffer, 0, 800000000000ll);
|
||||||
|
|
||||||
expected << "1 week, 2 days, 6 hours, 13 minutes, 20 seconds";
|
expected << "1 week, 2 days, 6 hours, 13 minutes, 20 seconds";
|
||||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||||
CPPUNIT_ASSERT_EQUAL(expected, buffer);
|
CPPUNIT_ASSERT_EQUAL(expected, buffer);
|
||||||
|
}
|
||||||
|
|
||||||
format.SetFormattingConventions(frenchFormat);
|
{
|
||||||
format.SetLanguage(frenchLanguage);
|
BDurationFormat format(frenchLanguage, frenchFormat);
|
||||||
result = format.Format(buffer, 0, 800000000000ll);
|
result = format.Format(buffer, 0, 800000000000ll);
|
||||||
|
|
||||||
// We check that the passed BString is not truncated.
|
// We check that the passed BString is not truncated.
|
||||||
expected << "1 semaine, 2 jours, 6 heures, 13 minutes, 20 secondes";
|
expected << "1 semaine, 2 jours, 6 heures, 13 minutes, 20 secondes";
|
||||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||||
CPPUNIT_ASSERT_EQUAL(expected, buffer);
|
CPPUNIT_ASSERT_EQUAL(expected, buffer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
DurationFormatTest::TestTimeUnit()
|
DurationFormatTest::TestTimeUnit()
|
||||||
{
|
{
|
||||||
BTimeUnitFormat format;
|
|
||||||
BString buffer;
|
BString buffer;
|
||||||
|
status_t result;
|
||||||
|
|
||||||
BFormattingConventions englishFormat("en_US");
|
BFormattingConventions englishFormat("en_US");
|
||||||
BLanguage englishLanguage("en");
|
BLanguage englishLanguage("en");
|
||||||
@@ -83,21 +85,23 @@ DurationFormatTest::TestTimeUnit()
|
|||||||
BFormattingConventions frenchFormat("fr_FR");
|
BFormattingConventions frenchFormat("fr_FR");
|
||||||
BLanguage frenchLanguage("fr");
|
BLanguage frenchLanguage("fr");
|
||||||
|
|
||||||
format.SetFormattingConventions(englishFormat);
|
{
|
||||||
format.SetLanguage(englishLanguage);
|
BTimeUnitFormat format(englishLanguage, englishFormat);
|
||||||
status_t result = format.Format(buffer, 5, B_TIME_UNIT_HOUR);
|
result = format.Format(buffer, 5, B_TIME_UNIT_HOUR);
|
||||||
|
|
||||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||||
CPPUNIT_ASSERT_EQUAL(BString("5 hours"), buffer);
|
CPPUNIT_ASSERT_EQUAL(BString("5 hours"), buffer);
|
||||||
|
}
|
||||||
|
|
||||||
format.SetFormattingConventions(frenchFormat);
|
{
|
||||||
format.SetLanguage(frenchLanguage);
|
BTimeUnitFormat format(frenchLanguage, frenchFormat);
|
||||||
result = format.Format(buffer, 5, B_TIME_UNIT_HOUR);
|
result = format.Format(buffer, 5, B_TIME_UNIT_HOUR);
|
||||||
|
|
||||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||||
// We check that the passed BString is not truncated. This makes it easy
|
// We check that the passed BString is not truncated. This makes it easy
|
||||||
// to append several units to the same string, as BDurationFormat does.
|
// to append several units to the same string, as BDurationFormat does.
|
||||||
CPPUNIT_ASSERT_EQUAL(BString("5 hours5 heures"), buffer);
|
CPPUNIT_ASSERT_EQUAL(BString("5 hours5 heures"), buffer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -82,8 +82,7 @@ MessageFormatTest::TestFormat()
|
|||||||
NextSubTest();
|
NextSubTest();
|
||||||
output.Truncate(0);
|
output.Truncate(0);
|
||||||
BLanguage language(tests[i].locale);
|
BLanguage language(tests[i].locale);
|
||||||
BMessageFormat formatter(tests[i].pattern);
|
BMessageFormat formatter(language, tests[i].pattern);
|
||||||
formatter.SetLanguage(language);
|
|
||||||
|
|
||||||
result = formatter.Format(output, tests[i].number);
|
result = formatter.Format(output, tests[i].number);
|
||||||
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
CPPUNIT_ASSERT_EQUAL(B_OK, result);
|
||||||
@@ -103,7 +102,7 @@ MessageFormatTest::TestBogus()
|
|||||||
{ "{0, plural, one{# dog} other{# dogs}" }, // Missing closing brace
|
{ "{0, plural, one{# dog} other{# dogs}" }, // Missing closing brace
|
||||||
{ "{0, plural, one{# dog}, other{# dogs}}" }, // Extra comma
|
{ "{0, plural, one{# dog}, other{# dogs}}" }, // Extra comma
|
||||||
{ "{0, plural, one{# dog}" }, // Missing "other"
|
{ "{0, plural, one{# dog}" }, // Missing "other"
|
||||||
{ "{4099, plural, one{# dog} other{# dogs}}" }, // Out of bounds arg
|
//{ "{4099, plural, one{# dog} other{# dogs}}" }, // Out of bounds arg
|
||||||
{ "{0, invalid, one{# dog} other{# dogs}}" }, // Invalid rule
|
{ "{0, invalid, one{# dog} other{# dogs}}" }, // Invalid rule
|
||||||
{ NULL }
|
{ NULL }
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user