Ticket #13606: 0001-Implement-extend-function-to-get-month-and-day-name-.patch

File 0001-Implement-extend-function-to-get-month-and-day-name-.patch, 4.3 KB (added by akshay, 7 years ago)
  • headers/os/locale/DateFormat.h

    From caf336bcc645d858cf74f34fadbacbbf727fb2e3 Mon Sep 17 00:00:00 2001
    From: Akshay Agarwal <agarwal.akshay.akshay8@gmail.com>
    Date: Tue, 18 Jul 2017 04:31:56 +0530
    Subject: [PATCH] Implement/extend function to get month and day name by
     specified width.
    
    * Add an enum BDateFormatWidth which includes the constants for date
    formatting width and create a map to the corresponding ICU unit.
    * Extend BDateFormat::GetMonthName() to take BDateFormatWidth as an
    argument and fill the string buffer with the month name as per the width.
    * Implement BDateFormat::GetDayName() similar to the extended
    BDateFormat::GetMonthName() to get day names by width.
    ---
     headers/os/locale/DateFormat.h | 17 +++++++++++++-
     src/kits/locale/DateFormat.cpp | 53 ++++++++++++++++++++++++++++++++++++++++--
     2 files changed, 67 insertions(+), 3 deletions(-)
    
    diff --git a/headers/os/locale/DateFormat.h b/headers/os/locale/DateFormat.h
    index 76a0567..72ad2c1 100644
    a b enum BWeekday {  
    3737};
    3838
    3939
     40// Short width is currently only supported for weekday names.
     41// In case of month names short defauts to abbreviated.
     42enum BDateFormatWidth {
     43    B_DATE_FORMAT_WIDE,     // e.g 'Sunday'
     44    B_DATE_FORMAT_ABBREVIATED,  // e.g 'Sun'
     45    B_DATE_FORMAT_SHORT,        // e.g 'Su'
     46    B_DATE_FORMAT_NARROW,       // e.g 'S'
     47
     48    B_DATE_FORMAT_WIDTH_COUNT = 4,
     49};
     50
     51
    4052class BDateFormat: public BFormat {
    4153public:
    4254                                BDateFormat(const BLocale* locale = NULL);
    public:  
    7183                                    ) const;
    7284
    7385            status_t            GetStartOfWeek(BWeekday* weekday) const;
    74             status_t            GetMonthName(int month, BString& outName);
     86            status_t            GetMonthName(int month, BString& outName,
     87                                    const BDateFormatWidth width) const;
     88            status_t            GetDayName(int day, BString& outName,
     89                                    const BDateFormatWidth width) const;
    7590
    7691                                // parsing
    7792
  • src/kits/locale/DateFormat.cpp

    diff --git a/src/kits/locale/DateFormat.cpp b/src/kits/locale/DateFormat.cpp
    index 2a386a5..a698d54 100644
    a b  
    2727#include <vector>
    2828
    2929
     30static const DateFormatSymbols::DtWidthType kDateFormatWidthToICU[] = {
     31    DateFormatSymbols::WIDE,
     32    DateFormatSymbols::ABBREVIATED,
     33    DateFormatSymbols::SHORT,
     34    DateFormatSymbols::NARROW,
     35};
     36
     37
    3038BDateFormat::BDateFormat(const BLocale* locale)
    3139    : BFormat(locale)
    3240{
    BDateFormat::GetStartOfWeek(BWeekday* startOfWeek) const  
    298306
    299307
    300308status_t
    301 BDateFormat::GetMonthName(int month, BString& outName)
     309BDateFormat::GetMonthName(int month, BString& outName,
     310    const BDateFormatWidth width) const
    302311{
     312    if (width < 0 || width >= B_DATE_FORMAT_WIDTH_COUNT)
     313        return B_BAD_VALUE;
     314
    303315    DateFormat* format = _CreateDateFormatter(B_LONG_DATE_FORMAT);
    304316
    305317    SimpleDateFormat* simpleFormat = dynamic_cast<SimpleDateFormat*>(format);
    BDateFormat::GetMonthName(int month, BString& outName)  
    311323    const DateFormatSymbols* symbols = simpleFormat->getDateFormatSymbols();
    312324
    313325    int32_t count;
    314     const UnicodeString* names = symbols->getMonths(count);
     326    const UnicodeString* names = symbols->getMonths(count,
     327        DateFormatSymbols::STANDALONE, kDateFormatWidthToICU[width]);
     328        // Use date formatting context as standalone.
    315329
    316330    if (month > count || month <= 0) {
    317331        delete simpleFormat;
    BDateFormat::GetMonthName(int month, BString& outName)  
    327341
    328342
    329343status_t
     344BDateFormat::GetDayName(int day, BString& outName,
     345    const BDateFormatWidth width) const
     346{
     347    if (width < 0 || width >= B_DATE_FORMAT_WIDTH_COUNT)
     348        return B_BAD_VALUE;
     349
     350    DateFormat* format = _CreateDateFormatter(B_LONG_DATE_FORMAT);
     351
     352    SimpleDateFormat* simpleFormat = dynamic_cast<SimpleDateFormat*>(format);
     353    if (simpleFormat == NULL) {
     354        delete format;
     355        return B_ERROR;
     356    }
     357
     358    const DateFormatSymbols* symbols = simpleFormat->getDateFormatSymbols();
     359
     360    int32_t count;
     361    const UnicodeString* names = symbols->getWeekdays(count,
     362        DateFormatSymbols::STANDALONE, kDateFormatWidthToICU[width]);
     363        // Use date formatting context as standalone.
     364
     365    if (day >= count || day <= 0) {
     366        delete simpleFormat;
     367        return B_BAD_DATA;
     368    }
     369
     370    BStringByteSink stringConverter(&outName);
     371    names[day].toUTF8(stringConverter);
     372
     373    delete simpleFormat;
     374    return B_OK;
     375}
     376
     377
     378status_t
    330379BDateFormat::Parse(BString source, BDateFormatStyle style, BDate& output)
    331380{
    332381    // FIXME currently this parses a date in any timezone (or the local one if