From 99dfb74fc9ccae11d79a7ef9af15169dd20d5b9b Mon Sep 17 00:00:00 2001
From: Akshay Agarwal <agarwal.akshay.akshay8@gmail.com>
Date: Wed, 17 May 2017 03:46:31 +0530
Subject: [PATCH] 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.
---
headers/os/locale/DurationFormat.h | 10 +++++-----
headers/os/locale/TimeUnitFormat.h | 9 +++++----
src/kits/locale/DurationFormat.cpp | 14 ++++++++------
src/kits/locale/TimeUnitFormat.cpp | 34 +++++++++++++++++++++++++++-------
4 files changed, 45 insertions(+), 22 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..632cdcb 100644
a
|
b
|
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..f00608a 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..d6dc872 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 kTimeUnitStyleToICU[] = { |
| 39 | UTMUTFMT_ABBREVIATED_STYLE, |
| 40 | UTMUTFMT_FULL_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 | fFormatter = NULL; |
| 51 | fInitStatus = B_BAD_VALUE; |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | fFormatter = new TimeUnitFormat(icuLocale, kTimeUnitStyleToICU[style], |
| 56 | icuStatus); |
44 | 57 | if (fFormatter == NULL) { |
45 | 58 | fInitStatus = B_NO_MEMORY; |
46 | 59 | return; |
… |
… |
BTimeUnitFormat::BTimeUnitFormat()
|
52 | 65 | |
53 | 66 | |
54 | 67 | BTimeUnitFormat::BTimeUnitFormat(const BLanguage& language, |
55 | | const BFormattingConventions& conventions) |
| 68 | const BFormattingConventions& conventions, |
| 69 | const time_unit_style style) |
56 | 70 | : Inherited(language, conventions) |
57 | 71 | { |
58 | 72 | Locale icuLocale(fLanguage.Code()); |
59 | 73 | UErrorCode icuStatus = U_ZERO_ERROR; |
60 | | fFormatter = new TimeUnitFormat(icuLocale, icuStatus); |
| 74 | if (style != B_TIME_UNIT_ABBREVIATED && style != B_TIME_UNIT_FULL) { |
| 75 | fFormatter = NULL; |
| 76 | fInitStatus = B_BAD_VALUE; |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | fFormatter = new TimeUnitFormat(icuLocale, kTimeUnitStyleToICU[style], |
| 81 | icuStatus); |
61 | 82 | if (fFormatter == NULL) { |
62 | 83 | fInitStatus = B_NO_MEMORY; |
63 | 84 | return; |
… |
… |
BTimeUnitFormat::~BTimeUnitFormat()
|
87 | 108 | |
88 | 109 | status_t |
89 | 110 | BTimeUnitFormat::Format(BString& buffer, const int32 value, |
90 | | const time_unit_element unit, time_unit_style style) const |
| 111 | const time_unit_element unit) const |
91 | 112 | { |
92 | | if (unit < 0 || unit > B_TIME_UNIT_LAST |
93 | | || (style != B_TIME_UNIT_ABBREVIATED && style != B_TIME_UNIT_FULL)) |
| 113 | if (unit < 0 || unit > B_TIME_UNIT_LAST) |
94 | 114 | return B_BAD_VALUE; |
95 | 115 | |
96 | 116 | if (fFormatter == NULL) |