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

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

    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 {  
    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..632cdcb 100644
    a b 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..f00608a 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..d6dc872 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 kTimeUnitStyleToICU[] = {
     39    UTMUTFMT_ABBREVIATED_STYLE,
     40    UTMUTFMT_FULL_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        fFormatter = NULL;
     51        fInitStatus = B_BAD_VALUE;
     52        return;
     53    }
     54
     55    fFormatter = new TimeUnitFormat(icuLocale, kTimeUnitStyleToICU[style],
     56        icuStatus);
    4457    if (fFormatter == NULL) {
    4558        fInitStatus = B_NO_MEMORY;
    4659        return;
    BTimeUnitFormat::BTimeUnitFormat()  
    5265
    5366
    5467BTimeUnitFormat::BTimeUnitFormat(const BLanguage& language,
    55     const BFormattingConventions& conventions)
     68        const BFormattingConventions& conventions,
     69    const time_unit_style style)
    5670    : Inherited(language, conventions)
    5771{
    5872    Locale icuLocale(fLanguage.Code());
    5973    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);
    6182    if (fFormatter == NULL) {
    6283        fInitStatus = B_NO_MEMORY;
    6384        return;
    BTimeUnitFormat::~BTimeUnitFormat()  
    87108
    88109status_t
    89110BTimeUnitFormat::Format(BString& buffer, const int32 value,
    90     const time_unit_element unit, time_unit_style style) const
     111    const time_unit_element unit) const
    91112{
    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)
    94114        return B_BAD_VALUE;
    95115
    96116    if (fFormatter == NULL)