1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 1996-2015, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
10 #include "unicode/utypes.h"
12 #if !UCONFIG_NO_FORMATTING
14 #include "unicode/udat.h"
16 #include "unicode/uloc.h"
17 #include "unicode/datefmt.h"
18 #include "unicode/timezone.h"
19 #include "unicode/smpdtfmt.h"
20 #include "unicode/fieldpos.h"
21 #include "unicode/parsepos.h"
22 #include "unicode/calendar.h"
23 #include "unicode/numfmt.h"
24 #include "unicode/dtfmtsym.h"
25 #include "unicode/ustring.h"
26 #include "unicode/udisplaycontext.h"
27 #include "unicode/ufieldpositer.h"
35 * Verify that fmt is a SimpleDateFormat. Invalid error if not.
36 * @param fmt the UDateFormat, definitely a DateFormat, maybe something else
37 * @param status error code, will be set to failure if there is a familure or the fmt is NULL.
39 static void verifyIsSimpleDateFormat(const UDateFormat* fmt, UErrorCode *status) {
40 if(U_SUCCESS(*status) &&
41 dynamic_cast<const SimpleDateFormat*>(reinterpret_cast<const DateFormat*>(fmt))==NULL) {
42 *status = U_ILLEGAL_ARGUMENT_ERROR;
46 // This mirrors the correspondence between the
47 // SimpleDateFormat::fgPatternIndexToDateFormatField and
48 // SimpleDateFormat::fgPatternIndexToCalendarField arrays.
49 static UCalendarDateFields gDateFieldMapping[] = {
50 UCAL_ERA, // UDAT_ERA_FIELD = 0
51 UCAL_YEAR, // UDAT_YEAR_FIELD = 1
52 UCAL_MONTH, // UDAT_MONTH_FIELD = 2
53 UCAL_DATE, // UDAT_DATE_FIELD = 3
54 UCAL_HOUR_OF_DAY, // UDAT_HOUR_OF_DAY1_FIELD = 4
55 UCAL_HOUR_OF_DAY, // UDAT_HOUR_OF_DAY0_FIELD = 5
56 UCAL_MINUTE, // UDAT_MINUTE_FIELD = 6
57 UCAL_SECOND, // UDAT_SECOND_FIELD = 7
58 UCAL_MILLISECOND, // UDAT_FRACTIONAL_SECOND_FIELD = 8
59 UCAL_DAY_OF_WEEK, // UDAT_DAY_OF_WEEK_FIELD = 9
60 UCAL_DAY_OF_YEAR, // UDAT_DAY_OF_YEAR_FIELD = 10
61 UCAL_DAY_OF_WEEK_IN_MONTH, // UDAT_DAY_OF_WEEK_IN_MONTH_FIELD = 11
62 UCAL_WEEK_OF_YEAR, // UDAT_WEEK_OF_YEAR_FIELD = 12
63 UCAL_WEEK_OF_MONTH, // UDAT_WEEK_OF_MONTH_FIELD = 13
64 UCAL_AM_PM, // UDAT_AM_PM_FIELD = 14
65 UCAL_HOUR, // UDAT_HOUR1_FIELD = 15
66 UCAL_HOUR, // UDAT_HOUR0_FIELD = 16
67 UCAL_ZONE_OFFSET, // UDAT_TIMEZONE_FIELD = 17
68 UCAL_YEAR_WOY, // UDAT_YEAR_WOY_FIELD = 18
69 UCAL_DOW_LOCAL, // UDAT_DOW_LOCAL_FIELD = 19
70 UCAL_EXTENDED_YEAR, // UDAT_EXTENDED_YEAR_FIELD = 20
71 UCAL_JULIAN_DAY, // UDAT_JULIAN_DAY_FIELD = 21
72 UCAL_MILLISECONDS_IN_DAY, // UDAT_MILLISECONDS_IN_DAY_FIELD = 22
73 UCAL_ZONE_OFFSET, // UDAT_TIMEZONE_RFC_FIELD = 23 (also UCAL_DST_OFFSET)
74 UCAL_ZONE_OFFSET, // UDAT_TIMEZONE_GENERIC_FIELD = 24 (also UCAL_DST_OFFSET)
75 UCAL_DOW_LOCAL, // UDAT_STANDALONE_DAY_FIELD = 25
76 UCAL_MONTH, // UDAT_STANDALONE_MONTH_FIELD = 26
77 UCAL_MONTH, // UDAT_QUARTER_FIELD = 27
78 UCAL_MONTH, // UDAT_STANDALONE_QUARTER_FIELD = 28
79 UCAL_ZONE_OFFSET, // UDAT_TIMEZONE_SPECIAL_FIELD = 29 (also UCAL_DST_OFFSET)
80 UCAL_YEAR, // UDAT_YEAR_NAME_FIELD = 30
81 UCAL_ZONE_OFFSET, // UDAT_TIMEZONE_LOCALIZED_GMT_OFFSET_FIELD = 31 (also UCAL_DST_OFFSET)
82 UCAL_ZONE_OFFSET, // UDAT_TIMEZONE_ISO_FIELD = 32 (also UCAL_DST_OFFSET)
83 UCAL_ZONE_OFFSET, // UDAT_TIMEZONE_ISO_LOCAL_FIELD = 33 (also UCAL_DST_OFFSET)
84 UCAL_EXTENDED_YEAR, // UDAT_RELATED_YEAR_FIELD = 34 (not an exact match)
85 UCAL_FIELD_COUNT, // UDAT_FIELD_COUNT = 35
86 // UCAL_IS_LEAP_MONTH is not the target of a mapping
89 U_CAPI UCalendarDateFields U_EXPORT2
90 udat_toCalendarDateField(UDateFormatField field) {
91 return gDateFieldMapping[field];
94 /* For now- one opener. */
95 static UDateFormatOpener gOpener = NULL;
97 U_INTERNAL void U_EXPORT2
98 udat_registerOpener(UDateFormatOpener opener, UErrorCode *status)
100 if(U_FAILURE(*status)) return;
105 *status = U_ILLEGAL_ARGUMENT_ERROR;
110 U_INTERNAL UDateFormatOpener U_EXPORT2
111 udat_unregisterOpener(UDateFormatOpener opener, UErrorCode *status)
113 if(U_FAILURE(*status)) return NULL;
114 UDateFormatOpener oldOpener = NULL;
116 if(gOpener==NULL || gOpener!=opener) {
117 *status = U_ILLEGAL_ARGUMENT_ERROR;
128 U_CAPI UDateFormat* U_EXPORT2
129 udat_open(UDateFormatStyle timeStyle,
130 UDateFormatStyle dateStyle,
134 const UChar *pattern,
135 int32_t patternLength,
139 if(U_FAILURE(*status)) {
142 if(gOpener!=NULL) { // if it's registered
143 fmt = (DateFormat*) (*gOpener)(timeStyle,dateStyle,locale,tzID,tzIDLength,pattern,patternLength,status);
145 return (UDateFormat*)fmt;
146 } // else fall through.
148 if(timeStyle != UDAT_PATTERN) {
150 fmt = DateFormat::createDateTimeInstance((DateFormat::EStyle)dateStyle,
151 (DateFormat::EStyle)timeStyle);
154 fmt = DateFormat::createDateTimeInstance((DateFormat::EStyle)dateStyle,
155 (DateFormat::EStyle)timeStyle,
160 UnicodeString pat((UBool)(patternLength == -1), pattern, patternLength);
163 fmt = new SimpleDateFormat(pat, *status);
166 fmt = new SimpleDateFormat(pat, Locale(locale), *status);
171 *status = U_MEMORY_ALLOCATION_ERROR;
176 TimeZone *zone = TimeZone::createTimeZone(UnicodeString((UBool)(tzIDLength == -1), tzID, tzIDLength));
178 *status = U_MEMORY_ALLOCATION_ERROR;
182 fmt->adoptTimeZone(zone);
185 return (UDateFormat*)fmt;
189 U_CAPI void U_EXPORT2
190 udat_close(UDateFormat* format)
192 delete (DateFormat*)format;
195 U_CAPI UDateFormat* U_EXPORT2
196 udat_clone(const UDateFormat *fmt,
199 if(U_FAILURE(*status)) return 0;
201 Format *res = ((DateFormat*)fmt)->clone();
204 *status = U_MEMORY_ALLOCATION_ERROR;
208 return (UDateFormat*) res;
211 U_CAPI int32_t U_EXPORT2
212 udat_format( const UDateFormat* format,
215 int32_t resultLength,
216 UFieldPosition* position,
219 if(U_FAILURE(*status)) {
222 if (result == NULL ? resultLength != 0 : resultLength < 0) {
223 *status = U_ILLEGAL_ARGUMENT_ERROR;
228 if (result != NULL) {
229 // NULL destination for pure preflighting: empty dummy string
230 // otherwise, alias the destination buffer
231 res.setTo(result, 0, resultLength);
237 fp.setField(position->field);
239 ((DateFormat*)format)->format(dateToFormat, res, fp);
242 position->beginIndex = fp.getBeginIndex();
243 position->endIndex = fp.getEndIndex();
246 return res.extract(result, resultLength, *status);
249 U_CAPI int32_t U_EXPORT2
250 udat_formatCalendar(const UDateFormat* format,
253 int32_t resultLength,
254 UFieldPosition* position,
257 if(U_FAILURE(*status)) {
260 if (result == NULL ? resultLength != 0 : resultLength < 0) {
261 *status = U_ILLEGAL_ARGUMENT_ERROR;
266 if (result != NULL) {
267 // NULL destination for pure preflighting: empty dummy string
268 // otherwise, alias the destination buffer
269 res.setTo(result, 0, resultLength);
275 fp.setField(position->field);
277 ((DateFormat*)format)->format(*(Calendar*)calendar, res, fp);
280 position->beginIndex = fp.getBeginIndex();
281 position->endIndex = fp.getEndIndex();
284 return res.extract(result, resultLength, *status);
287 U_CAPI int32_t U_EXPORT2
288 udat_formatForFields( const UDateFormat* format,
291 int32_t resultLength,
292 UFieldPositionIterator* fpositer,
295 if(U_FAILURE(*status)) {
298 if (result == NULL ? resultLength != 0 : resultLength < 0) {
299 *status = U_ILLEGAL_ARGUMENT_ERROR;
304 if (result != NULL) {
305 // NULL destination for pure preflighting: empty dummy string
306 // otherwise, alias the destination buffer
307 res.setTo(result, 0, resultLength);
310 ((DateFormat*)format)->format(dateToFormat, res, (FieldPositionIterator*)fpositer, *status);
312 return res.extract(result, resultLength, *status);
315 U_CAPI int32_t U_EXPORT2
316 udat_formatCalendarForFields(const UDateFormat* format,
319 int32_t resultLength,
320 UFieldPositionIterator* fpositer,
323 if(U_FAILURE(*status)) {
326 if (result == NULL ? resultLength != 0 : resultLength < 0) {
327 *status = U_ILLEGAL_ARGUMENT_ERROR;
332 if (result != NULL) {
333 // NULL destination for pure preflighting: empty dummy string
334 // otherwise, alias the destination buffer
335 res.setTo(result, 0, resultLength);
338 ((DateFormat*)format)->format(*(Calendar*)calendar, res, (FieldPositionIterator*)fpositer, *status);
340 return res.extract(result, resultLength, *status);
343 U_CAPI UDate U_EXPORT2
344 udat_parse( const UDateFormat* format,
350 if(U_FAILURE(*status)) return (UDate)0;
352 const UnicodeString src((UBool)(textLength == -1), text, textLength);
354 int32_t stackParsePos = 0;
357 if(parsePos == NULL) {
358 parsePos = &stackParsePos;
361 pp.setIndex(*parsePos);
363 res = ((DateFormat*)format)->parse(src, pp);
365 if(pp.getErrorIndex() == -1)
366 *parsePos = pp.getIndex();
368 *parsePos = pp.getErrorIndex();
369 *status = U_PARSE_ERROR;
375 U_CAPI void U_EXPORT2
376 udat_parseCalendar(const UDateFormat* format,
383 if(U_FAILURE(*status)) return;
385 const UnicodeString src((UBool)(textLength == -1), text, textLength);
387 int32_t stackParsePos = 0;
389 if(parsePos == NULL) {
390 parsePos = &stackParsePos;
393 pp.setIndex(*parsePos);
395 ((DateFormat*)format)->parse(src, *(Calendar*)calendar, pp);
397 if(pp.getErrorIndex() == -1)
398 *parsePos = pp.getIndex();
400 *parsePos = pp.getErrorIndex();
401 *status = U_PARSE_ERROR;
405 U_CAPI UBool U_EXPORT2
406 udat_isLenient(const UDateFormat* fmt)
408 return ((DateFormat*)fmt)->isLenient();
411 U_CAPI void U_EXPORT2
412 udat_setLenient( UDateFormat* fmt,
415 ((DateFormat*)fmt)->setLenient(isLenient);
418 U_DRAFT UBool U_EXPORT2
419 udat_getBooleanAttribute(const UDateFormat* fmt,
420 UDateFormatBooleanAttribute attr,
423 if(U_FAILURE(*status)) return FALSE;
424 return ((DateFormat*)fmt)->getBooleanAttribute(attr, *status);
428 U_DRAFT void U_EXPORT2
429 udat_setBooleanAttribute(UDateFormat *fmt,
430 UDateFormatBooleanAttribute attr,
434 if(U_FAILURE(*status)) return;
435 ((DateFormat*)fmt)->setBooleanAttribute(attr, newValue, *status);
438 U_CAPI const UCalendar* U_EXPORT2
439 udat_getCalendar(const UDateFormat* fmt)
441 return (const UCalendar*) ((DateFormat*)fmt)->getCalendar();
444 U_CAPI void U_EXPORT2
445 udat_setCalendar(UDateFormat* fmt,
446 const UCalendar* calendarToSet)
448 ((DateFormat*)fmt)->setCalendar(*((Calendar*)calendarToSet));
451 U_DRAFT const UNumberFormat* U_EXPORT2
452 udat_getNumberFormatForField(const UDateFormat* fmt, UChar field)
454 UErrorCode status = U_ZERO_ERROR;
455 verifyIsSimpleDateFormat(fmt, &status);
456 if (U_FAILURE(status)) return (const UNumberFormat*) ((DateFormat*)fmt)->getNumberFormat();
457 return (const UNumberFormat*) ((SimpleDateFormat*)fmt)->getNumberFormatForField(field);
460 U_CAPI const UNumberFormat* U_EXPORT2
461 udat_getNumberFormat(const UDateFormat* fmt)
463 return (const UNumberFormat*) ((DateFormat*)fmt)->getNumberFormat();
466 U_DRAFT void U_EXPORT2
467 udat_adoptNumberFormatForFields( UDateFormat* fmt,
469 UNumberFormat* numberFormatToSet,
472 verifyIsSimpleDateFormat(fmt, status);
473 if (U_FAILURE(*status)) return;
476 UnicodeString overrideFields(fields);
477 ((SimpleDateFormat*)fmt)->adoptNumberFormat(overrideFields, (NumberFormat*)numberFormatToSet, *status);
481 U_CAPI void U_EXPORT2
482 udat_setNumberFormat(UDateFormat* fmt,
483 const UNumberFormat* numberFormatToSet)
485 ((DateFormat*)fmt)->setNumberFormat(*((NumberFormat*)numberFormatToSet));
488 U_DRAFT void U_EXPORT2
489 udat_adoptNumberFormat( UDateFormat* fmt,
490 UNumberFormat* numberFormatToAdopt)
492 ((DateFormat*)fmt)->adoptNumberFormat((NumberFormat*)numberFormatToAdopt);
495 U_CAPI const char* U_EXPORT2
496 udat_getAvailable(int32_t index)
498 return uloc_getAvailable(index);
501 U_CAPI int32_t U_EXPORT2
502 udat_countAvailable()
504 return uloc_countAvailable();
507 U_CAPI UDate U_EXPORT2
508 udat_get2DigitYearStart( const UDateFormat *fmt,
511 verifyIsSimpleDateFormat(fmt, status);
512 if(U_FAILURE(*status)) return (UDate)0;
513 return ((SimpleDateFormat*)fmt)->get2DigitYearStart(*status);
516 U_CAPI void U_EXPORT2
517 udat_set2DigitYearStart( UDateFormat *fmt,
521 verifyIsSimpleDateFormat(fmt, status);
522 if(U_FAILURE(*status)) return;
523 ((SimpleDateFormat*)fmt)->set2DigitYearStart(d, *status);
526 U_CAPI int32_t U_EXPORT2
527 udat_toPattern( const UDateFormat *fmt,
530 int32_t resultLength,
533 if(U_FAILURE(*status)) {
536 if (result == NULL ? resultLength != 0 : resultLength < 0) {
537 *status = U_ILLEGAL_ARGUMENT_ERROR;
542 if (result != NULL) {
543 // NULL destination for pure preflighting: empty dummy string
544 // otherwise, alias the destination buffer
545 res.setTo(result, 0, resultLength);
548 const DateFormat *df=reinterpret_cast<const DateFormat *>(fmt);
549 const SimpleDateFormat *sdtfmt=dynamic_cast<const SimpleDateFormat *>(df);
550 const RelativeDateFormat *reldtfmt;
553 sdtfmt->toLocalizedPattern(res, *status);
555 sdtfmt->toPattern(res);
556 } else if (!localized && (reldtfmt=dynamic_cast<const RelativeDateFormat *>(df))!=NULL) {
557 reldtfmt->toPattern(res, *status);
559 *status = U_ILLEGAL_ARGUMENT_ERROR;
563 return res.extract(result, resultLength, *status);
566 // TODO: should this take an UErrorCode?
567 // A: Yes. Of course.
568 U_CAPI void U_EXPORT2
569 udat_applyPattern( UDateFormat *format,
571 const UChar *pattern,
572 int32_t patternLength)
574 const UnicodeString pat((UBool)(patternLength == -1), pattern, patternLength);
575 UErrorCode status = U_ZERO_ERROR;
577 verifyIsSimpleDateFormat(format, &status);
578 if(U_FAILURE(status)) {
583 ((SimpleDateFormat*)format)->applyLocalizedPattern(pat, status);
585 ((SimpleDateFormat*)format)->applyPattern(pat);
588 U_CAPI int32_t U_EXPORT2
589 udat_getSymbols(const UDateFormat *fmt,
590 UDateFormatSymbolType type,
593 int32_t resultLength,
596 const DateFormatSymbols *syms;
597 const SimpleDateFormat* sdtfmt;
598 const RelativeDateFormat* rdtfmt;
599 if ((sdtfmt = dynamic_cast<const SimpleDateFormat*>(reinterpret_cast<const DateFormat*>(fmt))) != NULL) {
600 syms = sdtfmt->getDateFormatSymbols();
601 } else if ((rdtfmt = dynamic_cast<const RelativeDateFormat*>(reinterpret_cast<const DateFormat*>(fmt))) != NULL) {
602 syms = rdtfmt->getDateFormatSymbols();
607 const UnicodeString *res = NULL;
611 res = syms->getEras(count);
615 res = syms->getEraNames(count);
619 res = syms->getMonths(count);
622 case UDAT_SHORT_MONTHS:
623 res = syms->getShortMonths(count);
627 res = syms->getWeekdays(count);
630 case UDAT_SHORT_WEEKDAYS:
631 res = syms->getShortWeekdays(count);
635 res = syms->getAmPmStrings(count);
638 case UDAT_LOCALIZED_CHARS:
641 if(!(result==NULL && resultLength==0)) {
642 // NULL destination for pure preflighting: empty dummy string
643 // otherwise, alias the destination buffer
644 res1.setTo(result, 0, resultLength);
646 syms->getLocalPatternChars(res1);
647 return res1.extract(result, resultLength, *status);
650 case UDAT_NARROW_MONTHS:
651 res = syms->getMonths(count, DateFormatSymbols::FORMAT, DateFormatSymbols::NARROW);
654 case UDAT_SHORTER_WEEKDAYS:
655 res = syms->getWeekdays(count, DateFormatSymbols::FORMAT, DateFormatSymbols::SHORT);
658 case UDAT_NARROW_WEEKDAYS:
659 res = syms->getWeekdays(count, DateFormatSymbols::FORMAT, DateFormatSymbols::NARROW);
662 case UDAT_STANDALONE_MONTHS:
663 res = syms->getMonths(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE);
666 case UDAT_STANDALONE_SHORT_MONTHS:
667 res = syms->getMonths(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED);
670 case UDAT_STANDALONE_NARROW_MONTHS:
671 res = syms->getMonths(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::NARROW);
674 case UDAT_STANDALONE_WEEKDAYS:
675 res = syms->getWeekdays(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE);
678 case UDAT_STANDALONE_SHORT_WEEKDAYS:
679 res = syms->getWeekdays(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED);
682 case UDAT_STANDALONE_SHORTER_WEEKDAYS:
683 res = syms->getWeekdays(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::SHORT);
686 case UDAT_STANDALONE_NARROW_WEEKDAYS:
687 res = syms->getWeekdays(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::NARROW);
691 res = syms->getQuarters(count, DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE);
694 case UDAT_SHORT_QUARTERS:
695 res = syms->getQuarters(count, DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED);
698 case UDAT_STANDALONE_QUARTERS:
699 res = syms->getQuarters(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE);
702 case UDAT_STANDALONE_SHORT_QUARTERS:
703 res = syms->getQuarters(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED);
706 case UDAT_CYCLIC_YEARS_WIDE:
707 res = syms->getYearNames(count, DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE);
710 case UDAT_CYCLIC_YEARS_ABBREVIATED:
711 res = syms->getYearNames(count, DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED);
714 case UDAT_CYCLIC_YEARS_NARROW:
715 res = syms->getYearNames(count, DateFormatSymbols::FORMAT, DateFormatSymbols::NARROW);
718 case UDAT_ZODIAC_NAMES_WIDE:
719 res = syms->getZodiacNames(count, DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE);
722 case UDAT_ZODIAC_NAMES_ABBREVIATED:
723 res = syms->getZodiacNames(count, DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED);
726 case UDAT_ZODIAC_NAMES_NARROW:
727 res = syms->getZodiacNames(count, DateFormatSymbols::FORMAT, DateFormatSymbols::NARROW);
733 return res[index].extract(result, resultLength, *status);
738 // TODO: also needs an errorCode.
739 U_CAPI int32_t U_EXPORT2
740 udat_countSymbols( const UDateFormat *fmt,
741 UDateFormatSymbolType type)
743 const DateFormatSymbols *syms;
744 const SimpleDateFormat* sdtfmt;
745 const RelativeDateFormat* rdtfmt;
746 if ((sdtfmt = dynamic_cast<const SimpleDateFormat*>(reinterpret_cast<const DateFormat*>(fmt))) != NULL) {
747 syms = sdtfmt->getDateFormatSymbols();
748 } else if ((rdtfmt = dynamic_cast<const RelativeDateFormat*>(reinterpret_cast<const DateFormat*>(fmt))) != NULL) {
749 syms = rdtfmt->getDateFormatSymbols();
757 syms->getEras(count);
761 syms->getMonths(count);
764 case UDAT_SHORT_MONTHS:
765 syms->getShortMonths(count);
769 syms->getWeekdays(count);
772 case UDAT_SHORT_WEEKDAYS:
773 syms->getShortWeekdays(count);
777 syms->getAmPmStrings(count);
780 case UDAT_LOCALIZED_CHARS:
785 syms->getEraNames(count);
788 case UDAT_NARROW_MONTHS:
789 syms->getMonths(count, DateFormatSymbols::FORMAT, DateFormatSymbols::NARROW);
792 case UDAT_SHORTER_WEEKDAYS:
793 syms->getWeekdays(count, DateFormatSymbols::FORMAT, DateFormatSymbols::SHORT);
796 case UDAT_NARROW_WEEKDAYS:
797 syms->getWeekdays(count, DateFormatSymbols::FORMAT, DateFormatSymbols::NARROW);
800 case UDAT_STANDALONE_MONTHS:
801 syms->getMonths(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE);
804 case UDAT_STANDALONE_SHORT_MONTHS:
805 syms->getMonths(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED);
808 case UDAT_STANDALONE_NARROW_MONTHS:
809 syms->getMonths(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::NARROW);
812 case UDAT_STANDALONE_WEEKDAYS:
813 syms->getWeekdays(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE);
816 case UDAT_STANDALONE_SHORT_WEEKDAYS:
817 syms->getWeekdays(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED);
820 case UDAT_STANDALONE_SHORTER_WEEKDAYS:
821 syms->getWeekdays(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::SHORT);
824 case UDAT_STANDALONE_NARROW_WEEKDAYS:
825 syms->getWeekdays(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::NARROW);
829 syms->getQuarters(count, DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE);
832 case UDAT_SHORT_QUARTERS:
833 syms->getQuarters(count, DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED);
836 case UDAT_STANDALONE_QUARTERS:
837 syms->getQuarters(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE);
840 case UDAT_STANDALONE_SHORT_QUARTERS:
841 syms->getQuarters(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED);
844 case UDAT_CYCLIC_YEARS_WIDE:
845 syms->getYearNames(count, DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE);
848 case UDAT_CYCLIC_YEARS_ABBREVIATED:
849 syms->getYearNames(count, DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED);
852 case UDAT_CYCLIC_YEARS_NARROW:
853 syms->getYearNames(count, DateFormatSymbols::FORMAT, DateFormatSymbols::NARROW);
856 case UDAT_ZODIAC_NAMES_WIDE:
857 syms->getZodiacNames(count, DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE);
860 case UDAT_ZODIAC_NAMES_ABBREVIATED:
861 syms->getZodiacNames(count, DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED);
864 case UDAT_ZODIAC_NAMES_NARROW:
865 syms->getZodiacNames(count, DateFormatSymbols::FORMAT, DateFormatSymbols::NARROW);
876 * This DateFormatSymbolsSingleSetter class is a friend of DateFormatSymbols
877 * solely for the purpose of avoiding to clone the array of strings
878 * just to modify one of them and then setting all of them back.
879 * For example, the old code looked like this:
881 * res = syms->getMonths(count);
882 * array = new UnicodeString[count];
884 * *status = U_MEMORY_ALLOCATION_ERROR;
887 * uprv_arrayCopy(res, array, count);
889 * array[index] = val;
890 * syms->setMonths(array, count);
893 * Even worse, the old code actually cloned the entire DateFormatSymbols object,
894 * cloned one value array, changed one value, and then made the SimpleDateFormat
895 * replace its DateFormatSymbols object with the new one.
899 class DateFormatSymbolsSingleSetter /* not : public UObject because all methods are static */ {
902 setSymbol(UnicodeString *array, int32_t count, int32_t index,
903 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
907 errorCode=U_INDEX_OUTOFBOUNDS_ERROR;
908 } else if(value==NULL) {
909 errorCode=U_ILLEGAL_ARGUMENT_ERROR;
911 array[index].setTo(value, valueLength);
917 setEra(DateFormatSymbols *syms, int32_t index,
918 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
920 setSymbol(syms->fEras, syms->fErasCount, index, value, valueLength, errorCode);
924 setEraName(DateFormatSymbols *syms, int32_t index,
925 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
927 setSymbol(syms->fEraNames, syms->fEraNamesCount, index, value, valueLength, errorCode);
931 setMonth(DateFormatSymbols *syms, int32_t index,
932 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
934 setSymbol(syms->fMonths, syms->fMonthsCount, index, value, valueLength, errorCode);
938 setShortMonth(DateFormatSymbols *syms, int32_t index,
939 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
941 setSymbol(syms->fShortMonths, syms->fShortMonthsCount, index, value, valueLength, errorCode);
945 setNarrowMonth(DateFormatSymbols *syms, int32_t index,
946 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
948 setSymbol(syms->fNarrowMonths, syms->fNarrowMonthsCount, index, value, valueLength, errorCode);
952 setStandaloneMonth(DateFormatSymbols *syms, int32_t index,
953 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
955 setSymbol(syms->fStandaloneMonths, syms->fStandaloneMonthsCount, index, value, valueLength, errorCode);
959 setStandaloneShortMonth(DateFormatSymbols *syms, int32_t index,
960 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
962 setSymbol(syms->fStandaloneShortMonths, syms->fStandaloneShortMonthsCount, index, value, valueLength, errorCode);
966 setStandaloneNarrowMonth(DateFormatSymbols *syms, int32_t index,
967 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
969 setSymbol(syms->fStandaloneNarrowMonths, syms->fStandaloneNarrowMonthsCount, index, value, valueLength, errorCode);
973 setWeekday(DateFormatSymbols *syms, int32_t index,
974 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
976 setSymbol(syms->fWeekdays, syms->fWeekdaysCount, index, value, valueLength, errorCode);
980 setShortWeekday(DateFormatSymbols *syms, int32_t index,
981 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
983 setSymbol(syms->fShortWeekdays, syms->fShortWeekdaysCount, index, value, valueLength, errorCode);
987 setShorterWeekday(DateFormatSymbols *syms, int32_t index,
988 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
990 setSymbol(syms->fShorterWeekdays, syms->fShorterWeekdaysCount, index, value, valueLength, errorCode);
994 setNarrowWeekday(DateFormatSymbols *syms, int32_t index,
995 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
997 setSymbol(syms->fNarrowWeekdays, syms->fNarrowWeekdaysCount, index, value, valueLength, errorCode);
1001 setStandaloneWeekday(DateFormatSymbols *syms, int32_t index,
1002 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
1004 setSymbol(syms->fStandaloneWeekdays, syms->fStandaloneWeekdaysCount, index, value, valueLength, errorCode);
1008 setStandaloneShortWeekday(DateFormatSymbols *syms, int32_t index,
1009 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
1011 setSymbol(syms->fStandaloneShortWeekdays, syms->fStandaloneShortWeekdaysCount, index, value, valueLength, errorCode);
1015 setStandaloneShorterWeekday(DateFormatSymbols *syms, int32_t index,
1016 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
1018 setSymbol(syms->fStandaloneShorterWeekdays, syms->fStandaloneShorterWeekdaysCount, index, value, valueLength, errorCode);
1022 setStandaloneNarrowWeekday(DateFormatSymbols *syms, int32_t index,
1023 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
1025 setSymbol(syms->fStandaloneNarrowWeekdays, syms->fStandaloneNarrowWeekdaysCount, index, value, valueLength, errorCode);
1029 setQuarter(DateFormatSymbols *syms, int32_t index,
1030 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
1032 setSymbol(syms->fQuarters, syms->fQuartersCount, index, value, valueLength, errorCode);
1036 setShortQuarter(DateFormatSymbols *syms, int32_t index,
1037 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
1039 setSymbol(syms->fShortQuarters, syms->fShortQuartersCount, index, value, valueLength, errorCode);
1043 setStandaloneQuarter(DateFormatSymbols *syms, int32_t index,
1044 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
1046 setSymbol(syms->fStandaloneQuarters, syms->fStandaloneQuartersCount, index, value, valueLength, errorCode);
1050 setStandaloneShortQuarter(DateFormatSymbols *syms, int32_t index,
1051 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
1053 setSymbol(syms->fStandaloneShortQuarters, syms->fStandaloneShortQuartersCount, index, value, valueLength, errorCode);
1057 setShortYearNames(DateFormatSymbols *syms, int32_t index,
1058 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
1060 setSymbol(syms->fShortYearNames, syms->fShortYearNamesCount, index, value, valueLength, errorCode);
1064 setShortZodiacNames(DateFormatSymbols *syms, int32_t index,
1065 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
1067 setSymbol(syms->fShortZodiacNames, syms->fShortZodiacNamesCount, index, value, valueLength, errorCode);
1071 setAmPm(DateFormatSymbols *syms, int32_t index,
1072 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
1074 setSymbol(syms->fAmPms, syms->fAmPmsCount, index, value, valueLength, errorCode);
1078 setLocalPatternChars(DateFormatSymbols *syms,
1079 const UChar *value, int32_t valueLength, UErrorCode &errorCode)
1081 setSymbol(&syms->fLocalPatternChars, 1, 0, value, valueLength, errorCode);
1087 U_CAPI void U_EXPORT2
1088 udat_setSymbols( UDateFormat *format,
1089 UDateFormatSymbolType type,
1092 int32_t valueLength,
1095 verifyIsSimpleDateFormat(format, status);
1096 if(U_FAILURE(*status)) return;
1098 DateFormatSymbols *syms = (DateFormatSymbols *)((SimpleDateFormat *)format)->getDateFormatSymbols();
1102 DateFormatSymbolsSingleSetter::setEra(syms, index, value, valueLength, *status);
1105 case UDAT_ERA_NAMES:
1106 DateFormatSymbolsSingleSetter::setEraName(syms, index, value, valueLength, *status);
1110 DateFormatSymbolsSingleSetter::setMonth(syms, index, value, valueLength, *status);
1113 case UDAT_SHORT_MONTHS:
1114 DateFormatSymbolsSingleSetter::setShortMonth(syms, index, value, valueLength, *status);
1117 case UDAT_NARROW_MONTHS:
1118 DateFormatSymbolsSingleSetter::setNarrowMonth(syms, index, value, valueLength, *status);
1121 case UDAT_STANDALONE_MONTHS:
1122 DateFormatSymbolsSingleSetter::setStandaloneMonth(syms, index, value, valueLength, *status);
1125 case UDAT_STANDALONE_SHORT_MONTHS:
1126 DateFormatSymbolsSingleSetter::setStandaloneShortMonth(syms, index, value, valueLength, *status);
1129 case UDAT_STANDALONE_NARROW_MONTHS:
1130 DateFormatSymbolsSingleSetter::setStandaloneNarrowMonth(syms, index, value, valueLength, *status);
1134 DateFormatSymbolsSingleSetter::setWeekday(syms, index, value, valueLength, *status);
1137 case UDAT_SHORT_WEEKDAYS:
1138 DateFormatSymbolsSingleSetter::setShortWeekday(syms, index, value, valueLength, *status);
1141 case UDAT_SHORTER_WEEKDAYS:
1142 DateFormatSymbolsSingleSetter::setShorterWeekday(syms, index, value, valueLength, *status);
1145 case UDAT_NARROW_WEEKDAYS:
1146 DateFormatSymbolsSingleSetter::setNarrowWeekday(syms, index, value, valueLength, *status);
1149 case UDAT_STANDALONE_WEEKDAYS:
1150 DateFormatSymbolsSingleSetter::setStandaloneWeekday(syms, index, value, valueLength, *status);
1153 case UDAT_STANDALONE_SHORT_WEEKDAYS:
1154 DateFormatSymbolsSingleSetter::setStandaloneShortWeekday(syms, index, value, valueLength, *status);
1157 case UDAT_STANDALONE_SHORTER_WEEKDAYS:
1158 DateFormatSymbolsSingleSetter::setStandaloneShorterWeekday(syms, index, value, valueLength, *status);
1161 case UDAT_STANDALONE_NARROW_WEEKDAYS:
1162 DateFormatSymbolsSingleSetter::setStandaloneNarrowWeekday(syms, index, value, valueLength, *status);
1166 DateFormatSymbolsSingleSetter::setQuarter(syms, index, value, valueLength, *status);
1169 case UDAT_SHORT_QUARTERS:
1170 DateFormatSymbolsSingleSetter::setShortQuarter(syms, index, value, valueLength, *status);
1173 case UDAT_STANDALONE_QUARTERS:
1174 DateFormatSymbolsSingleSetter::setStandaloneQuarter(syms, index, value, valueLength, *status);
1177 case UDAT_STANDALONE_SHORT_QUARTERS:
1178 DateFormatSymbolsSingleSetter::setStandaloneShortQuarter(syms, index, value, valueLength, *status);
1181 case UDAT_CYCLIC_YEARS_ABBREVIATED:
1182 DateFormatSymbolsSingleSetter::setShortYearNames(syms, index, value, valueLength, *status);
1185 case UDAT_ZODIAC_NAMES_ABBREVIATED:
1186 DateFormatSymbolsSingleSetter::setShortZodiacNames(syms, index, value, valueLength, *status);
1190 DateFormatSymbolsSingleSetter::setAmPm(syms, index, value, valueLength, *status);
1193 case UDAT_LOCALIZED_CHARS:
1194 DateFormatSymbolsSingleSetter::setLocalPatternChars(syms, value, valueLength, *status);
1198 *status = U_UNSUPPORTED_ERROR;
1204 U_CAPI const char* U_EXPORT2
1205 udat_getLocaleByType(const UDateFormat *fmt,
1206 ULocDataLocaleType type,
1210 if (U_SUCCESS(*status)) {
1211 *status = U_ILLEGAL_ARGUMENT_ERROR;
1215 return ((Format*)fmt)->getLocaleID(type, *status);
1218 U_CAPI void U_EXPORT2
1219 udat_setContext(UDateFormat* fmt, UDisplayContext value, UErrorCode* status)
1221 if (U_FAILURE(*status)) {
1224 ((DateFormat*)fmt)->setContext(value, *status);
1228 U_CAPI UDisplayContext U_EXPORT2
1229 udat_getContext(const UDateFormat* fmt, UDisplayContextType type, UErrorCode* status)
1231 if (U_FAILURE(*status)) {
1232 return (UDisplayContext)0;
1234 return ((const DateFormat*)fmt)->getContext(type, *status);
1239 * Verify that fmt is a RelativeDateFormat. Invalid error if not.
1240 * @param fmt the UDateFormat, definitely a DateFormat, maybe something else
1241 * @param status error code, will be set to failure if there is a familure or the fmt is NULL.
1243 static void verifyIsRelativeDateFormat(const UDateFormat* fmt, UErrorCode *status) {
1244 if(U_SUCCESS(*status) &&
1245 dynamic_cast<const RelativeDateFormat*>(reinterpret_cast<const DateFormat*>(fmt))==NULL) {
1246 *status = U_ILLEGAL_ARGUMENT_ERROR;
1251 U_CAPI int32_t U_EXPORT2
1252 udat_toPatternRelativeDate(const UDateFormat *fmt,
1254 int32_t resultLength,
1257 verifyIsRelativeDateFormat(fmt, status);
1258 if(U_FAILURE(*status)) {
1261 if (result == NULL ? resultLength != 0 : resultLength < 0) {
1262 *status = U_ILLEGAL_ARGUMENT_ERROR;
1266 UnicodeString datePattern;
1267 if (result != NULL) {
1268 // NULL destination for pure preflighting: empty dummy string
1269 // otherwise, alias the destination buffer
1270 datePattern.setTo(result, 0, resultLength);
1272 ((RelativeDateFormat*)fmt)->toPatternDate(datePattern, *status);
1273 return datePattern.extract(result, resultLength, *status);
1276 U_CAPI int32_t U_EXPORT2
1277 udat_toPatternRelativeTime(const UDateFormat *fmt,
1279 int32_t resultLength,
1282 verifyIsRelativeDateFormat(fmt, status);
1283 if(U_FAILURE(*status)) {
1286 if (result == NULL ? resultLength != 0 : resultLength < 0) {
1287 *status = U_ILLEGAL_ARGUMENT_ERROR;
1291 UnicodeString timePattern;
1292 if (result != NULL) {
1293 // NULL destination for pure preflighting: empty dummy string
1294 // otherwise, alias the destination buffer
1295 timePattern.setTo(result, 0, resultLength);
1297 ((RelativeDateFormat*)fmt)->toPatternTime(timePattern, *status);
1298 return timePattern.extract(result, resultLength, *status);
1301 U_CAPI void U_EXPORT2
1302 udat_applyPatternRelative(UDateFormat *format,
1303 const UChar *datePattern,
1304 int32_t datePatternLength,
1305 const UChar *timePattern,
1306 int32_t timePatternLength,
1309 verifyIsRelativeDateFormat(format, status);
1310 if(U_FAILURE(*status)) return;
1311 const UnicodeString datePat((UBool)(datePatternLength == -1), datePattern, datePatternLength);
1312 const UnicodeString timePat((UBool)(timePatternLength == -1), timePattern, timePatternLength);
1313 ((RelativeDateFormat*)format)->applyPatterns(datePat, timePat, *status);
1316 #endif /* #if !UCONFIG_NO_FORMATTING */