Fix style formatting issue in BTimeUnitFormat, update BDurationFormat accordingly.

* Issue: BTimeUnitFormat doesn't incorporate style formatting while
formatting a time unit. Format() does take style as an argument but the
style is not used anywhere. So currently the abbreviated style doesn't
work and by default the time unit is formatted to the full style.

* Fix: Move the style flag from BTimeUnitFormat::Format() to the
BTimeUnitFormat constructors and call the relevant icu::TimeUnitFormat
constructor. Map the Haiku defined style unit to the corresponding ICU
unit. Move the style flag from BDurationFormat::Format() to the
BDurationFormat constructors to map the changes in BTimeUnitFormat.

Signed-off-by: Adrien Destugues <[email protected]>

Fixes #13508
This commit is contained in:
Akshay Agarwal
2017-05-17 19:53:46 +02:00
committed by Adrien Destugues
parent f286626cd0
commit f5c544b59a
4 changed files with 45 additions and 22 deletions
+5 -5
View File
@@ -28,9 +28,10 @@ class BDurationFormat : public BFormat {
public: public:
BDurationFormat(const BLanguage& language, BDurationFormat(const BLanguage& language,
const BFormattingConventions& conventions, const BFormattingConventions& conventions,
const BString& separator = ", "); const BString& separator = ", ",
BDurationFormat( const time_unit_style style = B_TIME_UNIT_FULL);
const BString& separator = ", "); BDurationFormat(const BString& separator = ", ",
const time_unit_style style = B_TIME_UNIT_FULL);
BDurationFormat(const BDurationFormat& other); BDurationFormat(const BDurationFormat& other);
virtual ~BDurationFormat(); virtual ~BDurationFormat();
@@ -39,8 +40,7 @@ public:
status_t Format(BString& buffer, status_t Format(BString& buffer,
const bigtime_t startValue, const bigtime_t startValue,
const bigtime_t stopValue, const bigtime_t stopValue
time_unit_style style = B_TIME_UNIT_FULL
) const; ) const;
private: private:
+5 -4
View File
@@ -43,16 +43,17 @@ class BTimeUnitFormat : public BFormat {
typedef BFormat Inherited; typedef BFormat Inherited;
public: public:
BTimeUnitFormat(); BTimeUnitFormat(const time_unit_style style =
B_TIME_UNIT_FULL);
BTimeUnitFormat(const BLanguage& language, BTimeUnitFormat(const BLanguage& language,
const BFormattingConventions& conventions); const BFormattingConventions& conventions,
const time_unit_style style = B_TIME_UNIT_FULL);
BTimeUnitFormat(const BTimeUnitFormat& other); BTimeUnitFormat(const BTimeUnitFormat& other);
virtual ~BTimeUnitFormat(); virtual ~BTimeUnitFormat();
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
time_unit_style style = B_TIME_UNIT_FULL
) const; ) const;
private: private:
+8 -6
View File
@@ -35,11 +35,12 @@ static const UCalendarDateFields skUnitMap[] = {
BDurationFormat::BDurationFormat(const BLanguage& language, BDurationFormat::BDurationFormat(const BLanguage& language,
const BFormattingConventions& conventions, const BString& separator) const BFormattingConventions& conventions,
const BString& separator, const time_unit_style style)
: :
Inherited(language, conventions), Inherited(language, conventions),
fSeparator(separator), fSeparator(separator),
fTimeUnitFormat(language, conventions) fTimeUnitFormat(language, conventions, style)
{ {
UErrorCode icuStatus = U_ZERO_ERROR; UErrorCode icuStatus = U_ZERO_ERROR;
fCalendar = new GregorianCalendar(icuStatus); fCalendar = new GregorianCalendar(icuStatus);
@@ -50,11 +51,12 @@ BDurationFormat::BDurationFormat(const BLanguage& language,
} }
BDurationFormat::BDurationFormat(const BString& separator) BDurationFormat::BDurationFormat(const BString& separator,
const time_unit_style style)
: :
Inherited(), Inherited(),
fSeparator(separator), fSeparator(separator),
fTimeUnitFormat() fTimeUnitFormat(style)
{ {
UErrorCode icuStatus = U_ZERO_ERROR; UErrorCode icuStatus = U_ZERO_ERROR;
fCalendar = new GregorianCalendar(icuStatus); fCalendar = new GregorianCalendar(icuStatus);
@@ -118,7 +120,7 @@ BDurationFormat::SetTimeZone(const BTimeZone* timeZone)
status_t status_t
BDurationFormat::Format(BString& buffer, const bigtime_t startValue, BDurationFormat::Format(BString& buffer, const bigtime_t startValue,
const bigtime_t stopValue, time_unit_style style) const const bigtime_t stopValue) const
{ {
UErrorCode icuStatus = U_ZERO_ERROR; UErrorCode icuStatus = U_ZERO_ERROR;
fCalendar->setTime((UDate)startValue / 1000, icuStatus); fCalendar->setTime((UDate)startValue / 1000, icuStatus);
@@ -139,7 +141,7 @@ BDurationFormat::Format(BString& buffer, const bigtime_t startValue,
else else
needSeparator = true; needSeparator = true;
status_t status = fTimeUnitFormat.Format(buffer, delta, status_t status = fTimeUnitFormat.Format(buffer, delta,
(time_unit_element)unit, style); (time_unit_element)unit);
if (status != B_OK) if (status != B_OK)
return status; return status;
} }
+27 -7
View File
@@ -34,13 +34,26 @@ static const TimeUnit::UTimeUnitFields skUnitMap[] = {
TimeUnit::UTIMEUNIT_SECOND, TimeUnit::UTIMEUNIT_SECOND,
}; };
//maps our unit style to the corresponding ICU unit
static const UTimeUnitFormatStyle kTimeUnitStyleToICU[] = {
UTMUTFMT_ABBREVIATED_STYLE,
UTMUTFMT_FULL_STYLE,
};
BTimeUnitFormat::BTimeUnitFormat()
BTimeUnitFormat::BTimeUnitFormat(const time_unit_style style)
: Inherited() : Inherited()
{ {
Locale icuLocale(fLanguage.Code()); Locale icuLocale(fLanguage.Code());
UErrorCode icuStatus = U_ZERO_ERROR; UErrorCode icuStatus = U_ZERO_ERROR;
fFormatter = new TimeUnitFormat(icuLocale, icuStatus); if (style != B_TIME_UNIT_ABBREVIATED && style != B_TIME_UNIT_FULL) {
fFormatter = NULL;
fInitStatus = B_BAD_VALUE;
return;
}
fFormatter = new TimeUnitFormat(icuLocale, kTimeUnitStyleToICU[style],
icuStatus);
if (fFormatter == NULL) { if (fFormatter == NULL) {
fInitStatus = B_NO_MEMORY; fInitStatus = B_NO_MEMORY;
return; return;
@@ -52,12 +65,20 @@ BTimeUnitFormat::BTimeUnitFormat()
BTimeUnitFormat::BTimeUnitFormat(const BLanguage& language, BTimeUnitFormat::BTimeUnitFormat(const BLanguage& language,
const BFormattingConventions& conventions) const BFormattingConventions& conventions,
const time_unit_style style)
: Inherited(language, conventions) : Inherited(language, conventions)
{ {
Locale icuLocale(fLanguage.Code()); Locale icuLocale(fLanguage.Code());
UErrorCode icuStatus = U_ZERO_ERROR; UErrorCode icuStatus = U_ZERO_ERROR;
fFormatter = new TimeUnitFormat(icuLocale, icuStatus); if (style != B_TIME_UNIT_ABBREVIATED && style != B_TIME_UNIT_FULL) {
fFormatter = NULL;
fInitStatus = B_BAD_VALUE;
return;
}
fFormatter = new TimeUnitFormat(icuLocale, kTimeUnitStyleToICU[style],
icuStatus);
if (fFormatter == NULL) { if (fFormatter == NULL) {
fInitStatus = B_NO_MEMORY; fInitStatus = B_NO_MEMORY;
return; return;
@@ -87,10 +108,9 @@ BTimeUnitFormat::~BTimeUnitFormat()
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) const
{ {
if (unit < 0 || unit > B_TIME_UNIT_LAST if (unit < 0 || unit > B_TIME_UNIT_LAST)
|| (style != B_TIME_UNIT_ABBREVIATED && style != B_TIME_UNIT_FULL))
return B_BAD_VALUE; return B_BAD_VALUE;
if (fFormatter == NULL) if (fFormatter == NULL)