From be6ad1b759550b145f96671f8ec27df911b5bed3 Mon Sep 17 00:00:00 2001
From: Akshay Agarwal <agarwal.akshay.akshay8@gmail.com>
Date: Tue, 16 May 2017 22:23:21 +0530
Subject: [PATCH] Fix style formatting issue in BTimeUnitFormat and 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.
---
headers/os/locale/DurationFormat.h | 10 +++++-----
headers/os/locale/TimeUnitFormat.h | 11 ++++++-----
src/kits/locale/DurationFormat.cpp | 14 ++++++++------
src/kits/locale/TimeUnitFormat.cpp | 30 +++++++++++++++++++++++-------
4 files changed, 42 insertions(+), 23 deletions(-)
diff --git a/headers/os/locale/DurationFormat.h b/headers/os/locale/DurationFormat.h
index 9a529ec..fa0e92c 100644
a
|
b
|
class BDurationFormat : public BFormat {
|
28 | 28 | public: |
29 | 29 | BDurationFormat(const BLanguage& language, |
30 | 30 | const BFormattingConventions& conventions, |
31 | | const BString& separator = ", "); |
32 | | BDurationFormat( |
33 | | const BString& separator = ", "); |
| 31 | const BString& separator = ", ", |
| 32 | const time_unit_style style = B_TIME_UNIT_FULL); |
| 33 | BDurationFormat(const BString& separator = ", ", |
| 34 | const time_unit_style style = B_TIME_UNIT_FULL); |
34 | 35 | BDurationFormat(const BDurationFormat& other); |
35 | 36 | virtual ~BDurationFormat(); |
36 | 37 | |
… |
… |
public:
|
39 | 40 | |
40 | 41 | status_t Format(BString& buffer, |
41 | 42 | const bigtime_t startValue, |
42 | | const bigtime_t stopValue, |
43 | | time_unit_style style = B_TIME_UNIT_FULL |
| 43 | const bigtime_t stopValue |
44 | 44 | ) const; |
45 | 45 | |
46 | 46 | private: |
diff --git a/headers/os/locale/TimeUnitFormat.h b/headers/os/locale/TimeUnitFormat.h
index 447284b..0a34089 100644
a
|
b
|
namespace U_ICU_NAMESPACE {
|
21 | 21 | |
22 | 22 | |
23 | 23 | enum time_unit_style { |
24 | | B_TIME_UNIT_ABBREVIATED, // e.g. '5 hrs.' |
25 | 24 | B_TIME_UNIT_FULL, // e.g. '5 hours' |
| 25 | B_TIME_UNIT_ABBREVIATED, // e.g. '5 hrs.' |
26 | 26 | }; |
27 | 27 | |
28 | 28 | |
… |
… |
class BTimeUnitFormat : public BFormat {
|
43 | 43 | typedef BFormat Inherited; |
44 | 44 | |
45 | 45 | public: |
46 | | BTimeUnitFormat(); |
| 46 | BTimeUnitFormat(const time_unit_style style = |
| 47 | B_TIME_UNIT_FULL); |
47 | 48 | BTimeUnitFormat(const BLanguage& language, |
48 | | const BFormattingConventions& conventions); |
| 49 | const BFormattingConventions& conventions, |
| 50 | const time_unit_style style = B_TIME_UNIT_FULL); |
49 | 51 | BTimeUnitFormat(const BTimeUnitFormat& other); |
50 | 52 | virtual ~BTimeUnitFormat(); |
51 | 53 | |
52 | 54 | status_t Format(BString& buffer, |
53 | 55 | const int32 value, |
54 | | const time_unit_element unit, |
55 | | time_unit_style style = B_TIME_UNIT_FULL |
| 56 | const time_unit_element unit |
56 | 57 | ) const; |
57 | 58 | |
58 | 59 | private: |
diff --git a/src/kits/locale/DurationFormat.cpp b/src/kits/locale/DurationFormat.cpp
index 9536480..8a07cdd 100644
a
|
b
|
static const UCalendarDateFields skUnitMap[] = {
|
35 | 35 | |
36 | 36 | |
37 | 37 | BDurationFormat::BDurationFormat(const BLanguage& language, |
38 | | const BFormattingConventions& conventions, const BString& separator) |
| 38 | const BFormattingConventions& conventions, |
| 39 | const BString& separator, const time_unit_style style) |
39 | 40 | : |
40 | 41 | Inherited(language, conventions), |
41 | 42 | fSeparator(separator), |
42 | | fTimeUnitFormat(language, conventions) |
| 43 | fTimeUnitFormat(language, conventions, style) |
43 | 44 | { |
44 | 45 | UErrorCode icuStatus = U_ZERO_ERROR; |
45 | 46 | fCalendar = new GregorianCalendar(icuStatus); |
… |
… |
BDurationFormat::BDurationFormat(const BLanguage& language,
|
50 | 51 | } |
51 | 52 | |
52 | 53 | |
53 | | BDurationFormat::BDurationFormat(const BString& separator) |
| 54 | BDurationFormat::BDurationFormat(const BString& separator, |
| 55 | const time_unit_style style) |
54 | 56 | : |
55 | 57 | Inherited(), |
56 | 58 | fSeparator(separator), |
57 | | fTimeUnitFormat() |
| 59 | fTimeUnitFormat(style) |
58 | 60 | { |
59 | 61 | UErrorCode icuStatus = U_ZERO_ERROR; |
60 | 62 | fCalendar = new GregorianCalendar(icuStatus); |
… |
… |
BDurationFormat::SetTimeZone(const BTimeZone* timeZone)
|
118 | 120 | |
119 | 121 | status_t |
120 | 122 | BDurationFormat::Format(BString& buffer, const bigtime_t startValue, |
121 | | const bigtime_t stopValue, time_unit_style style) const |
| 123 | const bigtime_t stopValue) const |
122 | 124 | { |
123 | 125 | UErrorCode icuStatus = U_ZERO_ERROR; |
124 | 126 | fCalendar->setTime((UDate)startValue / 1000, icuStatus); |
… |
… |
BDurationFormat::Format(BString& buffer, const bigtime_t startValue,
|
139 | 141 | else |
140 | 142 | needSeparator = true; |
141 | 143 | status_t status = fTimeUnitFormat.Format(buffer, delta, |
142 | | (time_unit_element)unit, style); |
| 144 | (time_unit_element)unit); |
143 | 145 | if (status != B_OK) |
144 | 146 | return status; |
145 | 147 | } |
diff --git a/src/kits/locale/TimeUnitFormat.cpp b/src/kits/locale/TimeUnitFormat.cpp
index 61a8ba5..cd9abb4 100644
a
|
b
|
static const TimeUnit::UTimeUnitFields skUnitMap[] = {
|
34 | 34 | TimeUnit::UTIMEUNIT_SECOND, |
35 | 35 | }; |
36 | 36 | |
| 37 | //maps our unit style to the corresponding ICU unit |
| 38 | static const UTimeUnitFormatStyle skStyleMap[] = { |
| 39 | UTMUTFMT_FULL_STYLE, |
| 40 | UTMUTFMT_ABBREVIATED_STYLE, |
| 41 | }; |
| 42 | |
37 | 43 | |
38 | | BTimeUnitFormat::BTimeUnitFormat() |
| 44 | BTimeUnitFormat::BTimeUnitFormat(const time_unit_style style) |
39 | 45 | : Inherited() |
40 | 46 | { |
41 | 47 | Locale icuLocale(fLanguage.Code()); |
42 | 48 | UErrorCode icuStatus = U_ZERO_ERROR; |
43 | | fFormatter = new TimeUnitFormat(icuLocale, icuStatus); |
| 49 | if (style != B_TIME_UNIT_ABBREVIATED && style != B_TIME_UNIT_FULL) { |
| 50 | fInitStatus = B_BAD_VALUE; |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | fFormatter = new TimeUnitFormat(icuLocale, skStyleMap[style], icuStatus); |
44 | 55 | if (fFormatter == NULL) { |
45 | 56 | fInitStatus = B_NO_MEMORY; |
46 | 57 | return; |
… |
… |
BTimeUnitFormat::BTimeUnitFormat()
|
52 | 63 | |
53 | 64 | |
54 | 65 | BTimeUnitFormat::BTimeUnitFormat(const BLanguage& language, |
55 | | const BFormattingConventions& conventions) |
| 66 | const BFormattingConventions& conventions, |
| 67 | const time_unit_style style) |
56 | 68 | : Inherited(language, conventions) |
57 | 69 | { |
58 | 70 | Locale icuLocale(fLanguage.Code()); |
59 | 71 | UErrorCode icuStatus = U_ZERO_ERROR; |
60 | | fFormatter = new TimeUnitFormat(icuLocale, icuStatus); |
| 72 | if (style != B_TIME_UNIT_ABBREVIATED && style != B_TIME_UNIT_FULL) { |
| 73 | fInitStatus = B_BAD_VALUE; |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | fFormatter = new TimeUnitFormat(icuLocale, skStyleMap[style], icuStatus); |
61 | 78 | if (fFormatter == NULL) { |
62 | 79 | fInitStatus = B_NO_MEMORY; |
63 | 80 | return; |
… |
… |
BTimeUnitFormat::~BTimeUnitFormat()
|
87 | 104 | |
88 | 105 | status_t |
89 | 106 | BTimeUnitFormat::Format(BString& buffer, const int32 value, |
90 | | const time_unit_element unit, time_unit_style style) const |
| 107 | const time_unit_element unit) const |
91 | 108 | { |
92 | | if (unit < 0 || unit > B_TIME_UNIT_LAST |
93 | | || (style != B_TIME_UNIT_ABBREVIATED && style != B_TIME_UNIT_FULL)) |
| 109 | if (unit < 0 || unit > B_TIME_UNIT_LAST) |
94 | 110 | return B_BAD_VALUE; |
95 | 111 | |
96 | 112 | if (fFormatter == NULL) |