Ticket #13508: 0001-Fix-style-formatting-issue-in-BTimeUnitFormat-and-up.patch

File 0001-Fix-style-formatting-issue-in-BTimeUnitFormat-and-up.patch, 7.2 KB (added by akshay, 7 years ago)
  • headers/os/locale/DurationFormat.h

    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 {  
    2828public:
    2929                                BDurationFormat(const BLanguage& language,
    3030                                    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);
    3435                                BDurationFormat(const BDurationFormat& other);
    3536    virtual                     ~BDurationFormat();
    3637
    public:  
    3940
    4041            status_t            Format(BString& buffer,
    4142                                    const bigtime_t startValue,
    42                                     const bigtime_t stopValue,
    43                                     time_unit_style style = B_TIME_UNIT_FULL
     43                                    const bigtime_t stopValue
    4444                                    ) const;
    4545
    4646private:
  • headers/os/locale/TimeUnitFormat.h

    diff --git a/headers/os/locale/TimeUnitFormat.h b/headers/os/locale/TimeUnitFormat.h
    index 447284b..0a34089 100644
    a b namespace U_ICU_NAMESPACE {  
    2121
    2222
    2323enum time_unit_style {
    24     B_TIME_UNIT_ABBREVIATED,    // e.g. '5 hrs.'
    2524    B_TIME_UNIT_FULL,           // e.g. '5 hours'
     25    B_TIME_UNIT_ABBREVIATED,    // e.g. '5 hrs.'
    2626};
    2727
    2828
    class BTimeUnitFormat : public BFormat {  
    4343    typedef BFormat             Inherited;
    4444
    4545public:
    46                                 BTimeUnitFormat();
     46                                BTimeUnitFormat(const time_unit_style style =
     47                                    B_TIME_UNIT_FULL);
    4748                                BTimeUnitFormat(const BLanguage& language,
    48                                     const BFormattingConventions& conventions);
     49                                    const BFormattingConventions& conventions,
     50                                    const time_unit_style style = B_TIME_UNIT_FULL);
    4951                                BTimeUnitFormat(const BTimeUnitFormat& other);
    5052    virtual                     ~BTimeUnitFormat();
    5153
    5254            status_t            Format(BString& buffer,
    5355                                    const int32 value,
    54                                     const time_unit_element unit,
    55                                     time_unit_style style = B_TIME_UNIT_FULL
     56                                    const time_unit_element unit
    5657                                    ) const;
    5758
    5859private:
  • src/kits/locale/DurationFormat.cpp

    diff --git a/src/kits/locale/DurationFormat.cpp b/src/kits/locale/DurationFormat.cpp
    index 9536480..8a07cdd 100644
    a b static const UCalendarDateFields skUnitMap[] = {  
    3535
    3636
    3737BDurationFormat::BDurationFormat(const BLanguage& language,
    38     const BFormattingConventions& conventions, const BString& separator)
     38        const BFormattingConventions& conventions,
     39    const BString& separator, const time_unit_style style)
    3940    :
    4041    Inherited(language, conventions),
    4142    fSeparator(separator),
    42     fTimeUnitFormat(language, conventions)
     43    fTimeUnitFormat(language, conventions, style)
    4344{
    4445    UErrorCode icuStatus = U_ZERO_ERROR;
    4546    fCalendar = new GregorianCalendar(icuStatus);
    BDurationFormat::BDurationFormat(const BLanguage& language,  
    5051}
    5152
    5253
    53 BDurationFormat::BDurationFormat(const BString& separator)
     54BDurationFormat::BDurationFormat(const BString& separator,
     55    const time_unit_style style)
    5456    :
    5557    Inherited(),
    5658    fSeparator(separator),
    57     fTimeUnitFormat()
     59    fTimeUnitFormat(style)
    5860{
    5961    UErrorCode icuStatus = U_ZERO_ERROR;
    6062    fCalendar = new GregorianCalendar(icuStatus);
    BDurationFormat::SetTimeZone(const BTimeZone* timeZone)  
    118120
    119121status_t
    120122BDurationFormat::Format(BString& buffer, const bigtime_t startValue,
    121     const bigtime_t stopValue, time_unit_style style) const
     123    const bigtime_t stopValue) const
    122124{
    123125    UErrorCode icuStatus = U_ZERO_ERROR;
    124126    fCalendar->setTime((UDate)startValue / 1000, icuStatus);
    BDurationFormat::Format(BString& buffer, const bigtime_t startValue,  
    139141            else
    140142                needSeparator = true;
    141143            status_t status = fTimeUnitFormat.Format(buffer, delta,
    142                 (time_unit_element)unit, style);
     144                (time_unit_element)unit);
    143145            if (status != B_OK)
    144146                return status;
    145147        }
  • src/kits/locale/TimeUnitFormat.cpp

    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[] = {  
    3434    TimeUnit::UTIMEUNIT_SECOND,
    3535};
    3636
     37//maps our unit style to the corresponding ICU unit
     38static const UTimeUnitFormatStyle skStyleMap[] = {
     39    UTMUTFMT_FULL_STYLE,
     40    UTMUTFMT_ABBREVIATED_STYLE,
     41};
     42
    3743
    38 BTimeUnitFormat::BTimeUnitFormat()
     44BTimeUnitFormat::BTimeUnitFormat(const time_unit_style style)
    3945    : Inherited()
    4046{
    4147    Locale icuLocale(fLanguage.Code());
    4248    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);
    4455    if (fFormatter == NULL) {
    4556        fInitStatus = B_NO_MEMORY;
    4657        return;
    BTimeUnitFormat::BTimeUnitFormat()  
    5263
    5364
    5465BTimeUnitFormat::BTimeUnitFormat(const BLanguage& language,
    55     const BFormattingConventions& conventions)
     66        const BFormattingConventions& conventions,
     67    const time_unit_style style)
    5668    : Inherited(language, conventions)
    5769{
    5870    Locale icuLocale(fLanguage.Code());
    5971    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);
    6178    if (fFormatter == NULL) {
    6279        fInitStatus = B_NO_MEMORY;
    6380        return;
    BTimeUnitFormat::~BTimeUnitFormat()  
    87104
    88105status_t
    89106BTimeUnitFormat::Format(BString& buffer, const int32 value,
    90     const time_unit_element unit, time_unit_style style) const
     107    const time_unit_element unit) const
    91108{
    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)
    94110        return B_BAD_VALUE;
    95111
    96112    if (fFormatter == NULL)