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 {
|
37 | 37 | }; |
38 | 38 | |
39 | 39 | |
| 40 | // Short width is currently only supported for weekday names. |
| 41 | // In case of month names short defauts to abbreviated. |
| 42 | enum 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 | |
40 | 52 | class BDateFormat: public BFormat { |
41 | 53 | public: |
42 | 54 | BDateFormat(const BLocale* locale = NULL); |
… |
… |
public:
|
71 | 83 | ) const; |
72 | 84 | |
73 | 85 | 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; |
75 | 90 | |
76 | 91 | // parsing |
77 | 92 | |
diff --git a/src/kits/locale/DateFormat.cpp b/src/kits/locale/DateFormat.cpp
index 2a386a5..a698d54 100644
a
|
b
|
|
27 | 27 | #include <vector> |
28 | 28 | |
29 | 29 | |
| 30 | static const DateFormatSymbols::DtWidthType kDateFormatWidthToICU[] = { |
| 31 | DateFormatSymbols::WIDE, |
| 32 | DateFormatSymbols::ABBREVIATED, |
| 33 | DateFormatSymbols::SHORT, |
| 34 | DateFormatSymbols::NARROW, |
| 35 | }; |
| 36 | |
| 37 | |
30 | 38 | BDateFormat::BDateFormat(const BLocale* locale) |
31 | 39 | : BFormat(locale) |
32 | 40 | { |
… |
… |
BDateFormat::GetStartOfWeek(BWeekday* startOfWeek) const
|
298 | 306 | |
299 | 307 | |
300 | 308 | status_t |
301 | | BDateFormat::GetMonthName(int month, BString& outName) |
| 309 | BDateFormat::GetMonthName(int month, BString& outName, |
| 310 | const BDateFormatWidth width) const |
302 | 311 | { |
| 312 | if (width < 0 || width >= B_DATE_FORMAT_WIDTH_COUNT) |
| 313 | return B_BAD_VALUE; |
| 314 | |
303 | 315 | DateFormat* format = _CreateDateFormatter(B_LONG_DATE_FORMAT); |
304 | 316 | |
305 | 317 | SimpleDateFormat* simpleFormat = dynamic_cast<SimpleDateFormat*>(format); |
… |
… |
BDateFormat::GetMonthName(int month, BString& outName)
|
311 | 323 | const DateFormatSymbols* symbols = simpleFormat->getDateFormatSymbols(); |
312 | 324 | |
313 | 325 | 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. |
315 | 329 | |
316 | 330 | if (month > count || month <= 0) { |
317 | 331 | delete simpleFormat; |
… |
… |
BDateFormat::GetMonthName(int month, BString& outName)
|
327 | 341 | |
328 | 342 | |
329 | 343 | status_t |
| 344 | BDateFormat::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 | |
| 378 | status_t |
330 | 379 | BDateFormat::Parse(BString source, BDateFormatStyle style, BDate& output) |
331 | 380 | { |
332 | 381 | // FIXME currently this parses a date in any timezone (or the local one if |