2 *******************************************************************************
3 * Copyright (C) 1996-2012, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_FORMATTING
12 #include "unicode/udat.h"
14 #include "unicode/uloc.h"
15 #include "unicode/datefmt.h"
16 #include "unicode/timezone.h"
17 #include "unicode/smpdtfmt.h"
18 #include "unicode/fieldpos.h"
19 #include "unicode/parsepos.h"
20 #include "unicode/calendar.h"
21 #include "unicode/numfmt.h"
22 #include "unicode/dtfmtsym.h"
23 #include "unicode/ustring.h"
31 * Verify that fmt is a SimpleDateFormat. Invalid error if not.
32 * @param fmt the UDateFormat, definitely a DateFormat, maybe something else
33 * @param status error code, will be set to failure if there is a familure or the fmt is NULL.
35 static void verifyIsSimpleDateFormat(const UDateFormat
* fmt
, UErrorCode
*status
) {
36 if(U_SUCCESS(*status
) &&
37 dynamic_cast<const SimpleDateFormat
*>(reinterpret_cast<const DateFormat
*>(fmt
))==NULL
) {
38 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
42 // This mirrors the correspondence between the
43 // SimpleDateFormat::fgPatternIndexToDateFormatField and
44 // SimpleDateFormat::fgPatternIndexToCalendarField arrays.
45 static UCalendarDateFields gDateFieldMapping
[] = {
46 UCAL_ERA
, // UDAT_ERA_FIELD = 0
47 UCAL_YEAR
, // UDAT_YEAR_FIELD = 1
48 UCAL_MONTH
, // UDAT_MONTH_FIELD = 2
49 UCAL_DATE
, // UDAT_DATE_FIELD = 3
50 UCAL_HOUR_OF_DAY
, // UDAT_HOUR_OF_DAY1_FIELD = 4
51 UCAL_HOUR_OF_DAY
, // UDAT_HOUR_OF_DAY0_FIELD = 5
52 UCAL_MINUTE
, // UDAT_MINUTE_FIELD = 6
53 UCAL_SECOND
, // UDAT_SECOND_FIELD = 7
54 UCAL_MILLISECOND
, // UDAT_FRACTIONAL_SECOND_FIELD = 8
55 UCAL_DAY_OF_WEEK
, // UDAT_DAY_OF_WEEK_FIELD = 9
56 UCAL_DAY_OF_YEAR
, // UDAT_DAY_OF_YEAR_FIELD = 10
57 UCAL_DAY_OF_WEEK_IN_MONTH
, // UDAT_DAY_OF_WEEK_IN_MONTH_FIELD = 11
58 UCAL_WEEK_OF_YEAR
, // UDAT_WEEK_OF_YEAR_FIELD = 12
59 UCAL_WEEK_OF_MONTH
, // UDAT_WEEK_OF_MONTH_FIELD = 13
60 UCAL_AM_PM
, // UDAT_AM_PM_FIELD = 14
61 UCAL_HOUR
, // UDAT_HOUR1_FIELD = 15
62 UCAL_HOUR
, // UDAT_HOUR0_FIELD = 16
63 UCAL_ZONE_OFFSET
, // UDAT_TIMEZONE_FIELD = 17
64 UCAL_YEAR_WOY
, // UDAT_YEAR_WOY_FIELD = 18
65 UCAL_DOW_LOCAL
, // UDAT_DOW_LOCAL_FIELD = 19
66 UCAL_EXTENDED_YEAR
, // UDAT_EXTENDED_YEAR_FIELD = 20
67 UCAL_JULIAN_DAY
, // UDAT_JULIAN_DAY_FIELD = 21
68 UCAL_MILLISECONDS_IN_DAY
, // UDAT_MILLISECONDS_IN_DAY_FIELD = 22
69 UCAL_ZONE_OFFSET
, // UDAT_TIMEZONE_RFC_FIELD = 23
70 // UCAL_DST_OFFSET also
71 UCAL_ZONE_OFFSET
, // UDAT_TIMEZONE_GENERIC_FIELD = 24
72 UCAL_DOW_LOCAL
, // UDAT_STANDALONE_DAY_FIELD = 25
73 UCAL_MONTH
, // UDAT_STANDALONE_MONTH_FIELD = 26
74 UCAL_MONTH
, // UDAT_QUARTER_FIELD = 27
75 UCAL_MONTH
, // UDAT_STANDALONE_QUARTER_FIELD = 28
76 UCAL_ZONE_OFFSET
, // UDAT_TIMEZONE_SPECIAL_FIELD = 29
77 UCAL_YEAR
, // UDAT_YEAR_NAME_FIELD = 30
78 UCAL_FIELD_COUNT
, // UDAT_FIELD_COUNT = 31
79 // UCAL_IS_LEAP_MONTH is not the target of a mapping
82 U_CAPI UCalendarDateFields U_EXPORT2
83 udat_toCalendarDateField(UDateFormatField field
) {
84 return gDateFieldMapping
[field
];
87 /* For now- one opener. */
88 static UDateFormatOpener gOpener
= NULL
;
90 U_INTERNAL
void U_EXPORT2
91 udat_registerOpener(UDateFormatOpener opener
, UErrorCode
*status
)
93 if(U_FAILURE(*status
)) return;
98 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
103 U_INTERNAL UDateFormatOpener U_EXPORT2
104 udat_unregisterOpener(UDateFormatOpener opener
, UErrorCode
*status
)
106 if(U_FAILURE(*status
)) return NULL
;
107 UDateFormatOpener oldOpener
= NULL
;
109 if(gOpener
==NULL
|| gOpener
!=opener
) {
110 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
121 U_CAPI UDateFormat
* U_EXPORT2
122 udat_open(UDateFormatStyle timeStyle
,
123 UDateFormatStyle dateStyle
,
127 const UChar
*pattern
,
128 int32_t patternLength
,
132 if(U_FAILURE(*status
)) {
135 if(gOpener
!=NULL
) { // if it's registered
136 fmt
= (DateFormat
*) (*gOpener
)(timeStyle
,dateStyle
,locale
,tzID
,tzIDLength
,pattern
,patternLength
,status
);
138 return (UDateFormat
*)fmt
;
139 } // else fall through.
141 if(timeStyle
!= UDAT_IGNORE
) {
143 fmt
= DateFormat::createDateTimeInstance((DateFormat::EStyle
)dateStyle
,
144 (DateFormat::EStyle
)timeStyle
);
147 fmt
= DateFormat::createDateTimeInstance((DateFormat::EStyle
)dateStyle
,
148 (DateFormat::EStyle
)timeStyle
,
153 UnicodeString
pat((UBool
)(patternLength
== -1), pattern
, patternLength
);
156 fmt
= new SimpleDateFormat(pat
, *status
);
159 fmt
= new SimpleDateFormat(pat
, Locale(locale
), *status
);
164 *status
= U_MEMORY_ALLOCATION_ERROR
;
169 TimeZone
*zone
= TimeZone::createTimeZone(UnicodeString((UBool
)(tzIDLength
== -1), tzID
, tzIDLength
));
171 *status
= U_MEMORY_ALLOCATION_ERROR
;
175 fmt
->adoptTimeZone(zone
);
178 return (UDateFormat
*)fmt
;
182 U_CAPI
void U_EXPORT2
183 udat_close(UDateFormat
* format
)
185 delete (DateFormat
*)format
;
188 U_CAPI UDateFormat
* U_EXPORT2
189 udat_clone(const UDateFormat
*fmt
,
192 if(U_FAILURE(*status
)) return 0;
194 Format
*res
= ((DateFormat
*)fmt
)->clone();
197 *status
= U_MEMORY_ALLOCATION_ERROR
;
201 return (UDateFormat
*) res
;
204 U_CAPI
int32_t U_EXPORT2
205 udat_format( const UDateFormat
* format
,
208 int32_t resultLength
,
209 UFieldPosition
* position
,
212 if(U_FAILURE(*status
)) return -1;
215 if(!(result
==NULL
&& resultLength
==0)) {
216 // NULL destination for pure preflighting: empty dummy string
217 // otherwise, alias the destination buffer
218 res
.setTo(result
, 0, resultLength
);
224 fp
.setField(position
->field
);
226 ((DateFormat
*)format
)->format(dateToFormat
, res
, fp
);
229 position
->beginIndex
= fp
.getBeginIndex();
230 position
->endIndex
= fp
.getEndIndex();
233 return res
.extract(result
, resultLength
, *status
);
236 U_CAPI UDate U_EXPORT2
237 udat_parse( const UDateFormat
* format
,
243 if(U_FAILURE(*status
)) return (UDate
)0;
245 const UnicodeString
src((UBool
)(textLength
== -1), text
, textLength
);
247 int32_t stackParsePos
= 0;
250 if(parsePos
== NULL
) {
251 parsePos
= &stackParsePos
;
254 pp
.setIndex(*parsePos
);
256 res
= ((DateFormat
*)format
)->parse(src
, pp
);
258 if(pp
.getErrorIndex() == -1)
259 *parsePos
= pp
.getIndex();
261 *parsePos
= pp
.getErrorIndex();
262 *status
= U_PARSE_ERROR
;
268 U_CAPI
void U_EXPORT2
269 udat_parseCalendar(const UDateFormat
* format
,
276 if(U_FAILURE(*status
)) return;
278 const UnicodeString
src((UBool
)(textLength
== -1), text
, textLength
);
282 pp
.setIndex(*parsePos
);
284 ((DateFormat
*)format
)->parse(src
, *(Calendar
*)calendar
, pp
);
287 if(pp
.getErrorIndex() == -1)
288 *parsePos
= pp
.getIndex();
290 *parsePos
= pp
.getErrorIndex();
291 *status
= U_PARSE_ERROR
;
296 U_CAPI UBool U_EXPORT2
297 udat_isLenient(const UDateFormat
* fmt
)
299 return ((DateFormat
*)fmt
)->isLenient();
302 U_CAPI
void U_EXPORT2
303 udat_setLenient( UDateFormat
* fmt
,
306 ((DateFormat
*)fmt
)->setLenient(isLenient
);
309 U_CAPI
const UCalendar
* U_EXPORT2
310 udat_getCalendar(const UDateFormat
* fmt
)
312 return (const UCalendar
*) ((DateFormat
*)fmt
)->getCalendar();
315 U_CAPI
void U_EXPORT2
316 udat_setCalendar(UDateFormat
* fmt
,
317 const UCalendar
* calendarToSet
)
319 ((DateFormat
*)fmt
)->setCalendar(*((Calendar
*)calendarToSet
));
322 U_CAPI
const UNumberFormat
* U_EXPORT2
323 udat_getNumberFormat(const UDateFormat
* fmt
)
325 return (const UNumberFormat
*) ((DateFormat
*)fmt
)->getNumberFormat();
328 U_CAPI
void U_EXPORT2
329 udat_setNumberFormat(UDateFormat
* fmt
,
330 const UNumberFormat
* numberFormatToSet
)
332 ((DateFormat
*)fmt
)->setNumberFormat(*((NumberFormat
*)numberFormatToSet
));
335 U_CAPI
const char* U_EXPORT2
336 udat_getAvailable(int32_t index
)
338 return uloc_getAvailable(index
);
341 U_CAPI
int32_t U_EXPORT2
342 udat_countAvailable()
344 return uloc_countAvailable();
347 U_CAPI UDate U_EXPORT2
348 udat_get2DigitYearStart( const UDateFormat
*fmt
,
351 verifyIsSimpleDateFormat(fmt
, status
);
352 if(U_FAILURE(*status
)) return (UDate
)0;
353 return ((SimpleDateFormat
*)fmt
)->get2DigitYearStart(*status
);
356 U_CAPI
void U_EXPORT2
357 udat_set2DigitYearStart( UDateFormat
*fmt
,
361 verifyIsSimpleDateFormat(fmt
, status
);
362 if(U_FAILURE(*status
)) return;
363 ((SimpleDateFormat
*)fmt
)->set2DigitYearStart(d
, *status
);
366 U_CAPI
int32_t U_EXPORT2
367 udat_toPattern( const UDateFormat
*fmt
,
370 int32_t resultLength
,
373 if(U_FAILURE(*status
)) return -1;
376 if(!(result
==NULL
&& resultLength
==0)) {
377 // NULL destination for pure preflighting: empty dummy string
378 // otherwise, alias the destination buffer
379 res
.setTo(result
, 0, resultLength
);
382 const DateFormat
*df
=reinterpret_cast<const DateFormat
*>(fmt
);
383 const SimpleDateFormat
*sdtfmt
=dynamic_cast<const SimpleDateFormat
*>(df
);
384 const RelativeDateFormat
*reldtfmt
;
387 sdtfmt
->toLocalizedPattern(res
, *status
);
389 sdtfmt
->toPattern(res
);
390 } else if (!localized
&& (reldtfmt
=dynamic_cast<const RelativeDateFormat
*>(df
))!=NULL
) {
391 reldtfmt
->toPattern(res
, *status
);
393 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
397 return res
.extract(result
, resultLength
, *status
);
400 // TODO: should this take an UErrorCode?
401 // A: Yes. Of course.
402 U_CAPI
void U_EXPORT2
403 udat_applyPattern( UDateFormat
*format
,
405 const UChar
*pattern
,
406 int32_t patternLength
)
408 const UnicodeString
pat((UBool
)(patternLength
== -1), pattern
, patternLength
);
409 UErrorCode status
= U_ZERO_ERROR
;
411 verifyIsSimpleDateFormat(format
, &status
);
412 if(U_FAILURE(status
)) {
417 ((SimpleDateFormat
*)format
)->applyLocalizedPattern(pat
, status
);
419 ((SimpleDateFormat
*)format
)->applyPattern(pat
);
422 U_CAPI
int32_t U_EXPORT2
423 udat_getSymbols(const UDateFormat
*fmt
,
424 UDateFormatSymbolType type
,
427 int32_t resultLength
,
430 const DateFormatSymbols
*syms
;
431 const SimpleDateFormat
* sdtfmt
;
432 const RelativeDateFormat
* rdtfmt
;
433 if ((sdtfmt
= dynamic_cast<const SimpleDateFormat
*>(reinterpret_cast<const DateFormat
*>(fmt
))) != NULL
) {
434 syms
= sdtfmt
->getDateFormatSymbols();
435 } else if ((rdtfmt
= dynamic_cast<const RelativeDateFormat
*>(reinterpret_cast<const DateFormat
*>(fmt
))) != NULL
) {
436 syms
= rdtfmt
->getDateFormatSymbols();
441 const UnicodeString
*res
= NULL
;
445 res
= syms
->getEras(count
);
449 res
= syms
->getEraNames(count
);
453 res
= syms
->getMonths(count
);
456 case UDAT_SHORT_MONTHS
:
457 res
= syms
->getShortMonths(count
);
461 res
= syms
->getWeekdays(count
);
464 case UDAT_SHORT_WEEKDAYS
:
465 res
= syms
->getShortWeekdays(count
);
469 res
= syms
->getAmPmStrings(count
);
472 case UDAT_LOCALIZED_CHARS
:
475 if(!(result
==NULL
&& resultLength
==0)) {
476 // NULL destination for pure preflighting: empty dummy string
477 // otherwise, alias the destination buffer
478 res1
.setTo(result
, 0, resultLength
);
480 syms
->getLocalPatternChars(res1
);
481 return res1
.extract(result
, resultLength
, *status
);
484 case UDAT_NARROW_MONTHS
:
485 res
= syms
->getMonths(count
, DateFormatSymbols::FORMAT
, DateFormatSymbols::NARROW
);
488 case UDAT_NARROW_WEEKDAYS
:
489 res
= syms
->getWeekdays(count
, DateFormatSymbols::FORMAT
, DateFormatSymbols::NARROW
);
492 case UDAT_STANDALONE_MONTHS
:
493 res
= syms
->getMonths(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::WIDE
);
496 case UDAT_STANDALONE_SHORT_MONTHS
:
497 res
= syms
->getMonths(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::ABBREVIATED
);
500 case UDAT_STANDALONE_NARROW_MONTHS
:
501 res
= syms
->getMonths(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::NARROW
);
504 case UDAT_STANDALONE_WEEKDAYS
:
505 res
= syms
->getWeekdays(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::WIDE
);
508 case UDAT_STANDALONE_SHORT_WEEKDAYS
:
509 res
= syms
->getWeekdays(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::ABBREVIATED
);
512 case UDAT_STANDALONE_NARROW_WEEKDAYS
:
513 res
= syms
->getWeekdays(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::NARROW
);
517 res
= syms
->getQuarters(count
, DateFormatSymbols::FORMAT
, DateFormatSymbols::WIDE
);
520 case UDAT_SHORT_QUARTERS
:
521 res
= syms
->getQuarters(count
, DateFormatSymbols::FORMAT
, DateFormatSymbols::ABBREVIATED
);
524 case UDAT_STANDALONE_QUARTERS
:
525 res
= syms
->getQuarters(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::WIDE
);
528 case UDAT_STANDALONE_SHORT_QUARTERS
:
529 res
= syms
->getQuarters(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::ABBREVIATED
);
535 return res
[index
].extract(result
, resultLength
, *status
);
540 // TODO: also needs an errorCode.
541 U_CAPI
int32_t U_EXPORT2
542 udat_countSymbols( const UDateFormat
*fmt
,
543 UDateFormatSymbolType type
)
545 const DateFormatSymbols
*syms
;
546 const SimpleDateFormat
* sdtfmt
;
547 const RelativeDateFormat
* rdtfmt
;
548 if ((sdtfmt
= dynamic_cast<const SimpleDateFormat
*>(reinterpret_cast<const DateFormat
*>(fmt
))) != NULL
) {
549 syms
= sdtfmt
->getDateFormatSymbols();
550 } else if ((rdtfmt
= dynamic_cast<const RelativeDateFormat
*>(reinterpret_cast<const DateFormat
*>(fmt
))) != NULL
) {
551 syms
= rdtfmt
->getDateFormatSymbols();
559 syms
->getEras(count
);
563 syms
->getMonths(count
);
566 case UDAT_SHORT_MONTHS
:
567 syms
->getShortMonths(count
);
571 syms
->getWeekdays(count
);
574 case UDAT_SHORT_WEEKDAYS
:
575 syms
->getShortWeekdays(count
);
579 syms
->getAmPmStrings(count
);
582 case UDAT_LOCALIZED_CHARS
:
587 syms
->getEraNames(count
);
590 case UDAT_NARROW_MONTHS
:
591 syms
->getMonths(count
, DateFormatSymbols::FORMAT
, DateFormatSymbols::NARROW
);
594 case UDAT_NARROW_WEEKDAYS
:
595 syms
->getWeekdays(count
, DateFormatSymbols::FORMAT
, DateFormatSymbols::NARROW
);
598 case UDAT_STANDALONE_MONTHS
:
599 syms
->getMonths(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::WIDE
);
602 case UDAT_STANDALONE_SHORT_MONTHS
:
603 syms
->getMonths(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::ABBREVIATED
);
606 case UDAT_STANDALONE_NARROW_MONTHS
:
607 syms
->getMonths(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::NARROW
);
610 case UDAT_STANDALONE_WEEKDAYS
:
611 syms
->getWeekdays(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::WIDE
);
614 case UDAT_STANDALONE_SHORT_WEEKDAYS
:
615 syms
->getWeekdays(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::ABBREVIATED
);
618 case UDAT_STANDALONE_NARROW_WEEKDAYS
:
619 syms
->getWeekdays(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::NARROW
);
623 syms
->getQuarters(count
, DateFormatSymbols::FORMAT
, DateFormatSymbols::WIDE
);
626 case UDAT_SHORT_QUARTERS
:
627 syms
->getQuarters(count
, DateFormatSymbols::FORMAT
, DateFormatSymbols::ABBREVIATED
);
630 case UDAT_STANDALONE_QUARTERS
:
631 syms
->getQuarters(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::WIDE
);
634 case UDAT_STANDALONE_SHORT_QUARTERS
:
635 syms
->getQuarters(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::ABBREVIATED
);
646 * This DateFormatSymbolsSingleSetter class is a friend of DateFormatSymbols
647 * solely for the purpose of avoiding to clone the array of strings
648 * just to modify one of them and then setting all of them back.
649 * For example, the old code looked like this:
651 * res = syms->getMonths(count);
652 * array = new UnicodeString[count];
654 * *status = U_MEMORY_ALLOCATION_ERROR;
657 * uprv_arrayCopy(res, array, count);
659 * array[index] = val;
660 * syms->setMonths(array, count);
663 * Even worse, the old code actually cloned the entire DateFormatSymbols object,
664 * cloned one value array, changed one value, and then made the SimpleDateFormat
665 * replace its DateFormatSymbols object with the new one.
669 class DateFormatSymbolsSingleSetter
/* not : public UObject because all methods are static */ {
672 setSymbol(UnicodeString
*array
, int32_t count
, int32_t index
,
673 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
677 errorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
678 } else if(value
==NULL
) {
679 errorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
681 array
[index
].setTo(value
, valueLength
);
687 setEra(DateFormatSymbols
*syms
, int32_t index
,
688 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
690 setSymbol(syms
->fEras
, syms
->fErasCount
, index
, value
, valueLength
, errorCode
);
694 setEraName(DateFormatSymbols
*syms
, int32_t index
,
695 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
697 setSymbol(syms
->fEraNames
, syms
->fEraNamesCount
, index
, value
, valueLength
, errorCode
);
701 setMonth(DateFormatSymbols
*syms
, int32_t index
,
702 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
704 setSymbol(syms
->fMonths
, syms
->fMonthsCount
, index
, value
, valueLength
, errorCode
);
708 setShortMonth(DateFormatSymbols
*syms
, int32_t index
,
709 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
711 setSymbol(syms
->fShortMonths
, syms
->fShortMonthsCount
, index
, value
, valueLength
, errorCode
);
715 setNarrowMonth(DateFormatSymbols
*syms
, int32_t index
,
716 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
718 setSymbol(syms
->fNarrowMonths
, syms
->fNarrowMonthsCount
, index
, value
, valueLength
, errorCode
);
722 setStandaloneMonth(DateFormatSymbols
*syms
, int32_t index
,
723 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
725 setSymbol(syms
->fStandaloneMonths
, syms
->fStandaloneMonthsCount
, index
, value
, valueLength
, errorCode
);
729 setStandaloneShortMonth(DateFormatSymbols
*syms
, int32_t index
,
730 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
732 setSymbol(syms
->fStandaloneShortMonths
, syms
->fStandaloneShortMonthsCount
, index
, value
, valueLength
, errorCode
);
736 setStandaloneNarrowMonth(DateFormatSymbols
*syms
, int32_t index
,
737 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
739 setSymbol(syms
->fStandaloneNarrowMonths
, syms
->fStandaloneNarrowMonthsCount
, index
, value
, valueLength
, errorCode
);
743 setWeekday(DateFormatSymbols
*syms
, int32_t index
,
744 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
746 setSymbol(syms
->fWeekdays
, syms
->fWeekdaysCount
, index
, value
, valueLength
, errorCode
);
750 setShortWeekday(DateFormatSymbols
*syms
, int32_t index
,
751 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
753 setSymbol(syms
->fShortWeekdays
, syms
->fShortWeekdaysCount
, index
, value
, valueLength
, errorCode
);
757 setNarrowWeekday(DateFormatSymbols
*syms
, int32_t index
,
758 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
760 setSymbol(syms
->fNarrowWeekdays
, syms
->fNarrowWeekdaysCount
, index
, value
, valueLength
, errorCode
);
764 setStandaloneWeekday(DateFormatSymbols
*syms
, int32_t index
,
765 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
767 setSymbol(syms
->fStandaloneWeekdays
, syms
->fStandaloneWeekdaysCount
, index
, value
, valueLength
, errorCode
);
771 setStandaloneShortWeekday(DateFormatSymbols
*syms
, int32_t index
,
772 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
774 setSymbol(syms
->fStandaloneShortWeekdays
, syms
->fStandaloneShortWeekdaysCount
, index
, value
, valueLength
, errorCode
);
778 setStandaloneNarrowWeekday(DateFormatSymbols
*syms
, int32_t index
,
779 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
781 setSymbol(syms
->fStandaloneNarrowWeekdays
, syms
->fStandaloneNarrowWeekdaysCount
, index
, value
, valueLength
, errorCode
);
785 setQuarter(DateFormatSymbols
*syms
, int32_t index
,
786 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
788 setSymbol(syms
->fQuarters
, syms
->fQuartersCount
, index
, value
, valueLength
, errorCode
);
792 setShortQuarter(DateFormatSymbols
*syms
, int32_t index
,
793 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
795 setSymbol(syms
->fShortQuarters
, syms
->fShortQuartersCount
, index
, value
, valueLength
, errorCode
);
799 setStandaloneQuarter(DateFormatSymbols
*syms
, int32_t index
,
800 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
802 setSymbol(syms
->fStandaloneQuarters
, syms
->fStandaloneQuartersCount
, index
, value
, valueLength
, errorCode
);
806 setStandaloneShortQuarter(DateFormatSymbols
*syms
, int32_t index
,
807 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
809 setSymbol(syms
->fStandaloneShortQuarters
, syms
->fStandaloneShortQuartersCount
, index
, value
, valueLength
, errorCode
);
813 setAmPm(DateFormatSymbols
*syms
, int32_t index
,
814 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
816 setSymbol(syms
->fAmPms
, syms
->fAmPmsCount
, index
, value
, valueLength
, errorCode
);
820 setLocalPatternChars(DateFormatSymbols
*syms
,
821 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
823 setSymbol(&syms
->fLocalPatternChars
, 1, 0, value
, valueLength
, errorCode
);
829 U_CAPI
void U_EXPORT2
830 udat_setSymbols( UDateFormat
*format
,
831 UDateFormatSymbolType type
,
837 verifyIsSimpleDateFormat(format
, status
);
838 if(U_FAILURE(*status
)) return;
840 DateFormatSymbols
*syms
= (DateFormatSymbols
*)((SimpleDateFormat
*)format
)->getDateFormatSymbols();
844 DateFormatSymbolsSingleSetter::setEra(syms
, index
, value
, valueLength
, *status
);
848 DateFormatSymbolsSingleSetter::setEraName(syms
, index
, value
, valueLength
, *status
);
852 DateFormatSymbolsSingleSetter::setMonth(syms
, index
, value
, valueLength
, *status
);
855 case UDAT_SHORT_MONTHS
:
856 DateFormatSymbolsSingleSetter::setShortMonth(syms
, index
, value
, valueLength
, *status
);
859 case UDAT_NARROW_MONTHS
:
860 DateFormatSymbolsSingleSetter::setNarrowMonth(syms
, index
, value
, valueLength
, *status
);
863 case UDAT_STANDALONE_MONTHS
:
864 DateFormatSymbolsSingleSetter::setStandaloneMonth(syms
, index
, value
, valueLength
, *status
);
867 case UDAT_STANDALONE_SHORT_MONTHS
:
868 DateFormatSymbolsSingleSetter::setStandaloneShortMonth(syms
, index
, value
, valueLength
, *status
);
871 case UDAT_STANDALONE_NARROW_MONTHS
:
872 DateFormatSymbolsSingleSetter::setStandaloneNarrowMonth(syms
, index
, value
, valueLength
, *status
);
876 DateFormatSymbolsSingleSetter::setWeekday(syms
, index
, value
, valueLength
, *status
);
879 case UDAT_SHORT_WEEKDAYS
:
880 DateFormatSymbolsSingleSetter::setShortWeekday(syms
, index
, value
, valueLength
, *status
);
883 case UDAT_NARROW_WEEKDAYS
:
884 DateFormatSymbolsSingleSetter::setNarrowWeekday(syms
, index
, value
, valueLength
, *status
);
887 case UDAT_STANDALONE_WEEKDAYS
:
888 DateFormatSymbolsSingleSetter::setStandaloneWeekday(syms
, index
, value
, valueLength
, *status
);
891 case UDAT_STANDALONE_SHORT_WEEKDAYS
:
892 DateFormatSymbolsSingleSetter::setStandaloneShortWeekday(syms
, index
, value
, valueLength
, *status
);
895 case UDAT_STANDALONE_NARROW_WEEKDAYS
:
896 DateFormatSymbolsSingleSetter::setStandaloneNarrowWeekday(syms
, index
, value
, valueLength
, *status
);
900 DateFormatSymbolsSingleSetter::setQuarter(syms
, index
, value
, valueLength
, *status
);
903 case UDAT_SHORT_QUARTERS
:
904 DateFormatSymbolsSingleSetter::setShortQuarter(syms
, index
, value
, valueLength
, *status
);
907 case UDAT_STANDALONE_QUARTERS
:
908 DateFormatSymbolsSingleSetter::setStandaloneQuarter(syms
, index
, value
, valueLength
, *status
);
911 case UDAT_STANDALONE_SHORT_QUARTERS
:
912 DateFormatSymbolsSingleSetter::setStandaloneShortQuarter(syms
, index
, value
, valueLength
, *status
);
916 DateFormatSymbolsSingleSetter::setAmPm(syms
, index
, value
, valueLength
, *status
);
919 case UDAT_LOCALIZED_CHARS
:
920 DateFormatSymbolsSingleSetter::setLocalPatternChars(syms
, value
, valueLength
, *status
);
924 *status
= U_UNSUPPORTED_ERROR
;
930 U_CAPI
const char* U_EXPORT2
931 udat_getLocaleByType(const UDateFormat
*fmt
,
932 ULocDataLocaleType type
,
936 if (U_SUCCESS(*status
)) {
937 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
941 return ((Format
*)fmt
)->getLocaleID(type
, *status
);
945 U_CAPI
void U_EXPORT2
946 udat_setDefaultContext(UDateFormat
* fmt
,
947 UDateFormatContextType type
, UDateFormatContextValue value
,
950 verifyIsSimpleDateFormat(fmt
, status
);
951 if (U_FAILURE(*status
)) {
954 ((SimpleDateFormat
*)fmt
)->setDefaultContext(type
, value
, *status
);
957 U_CAPI
int32_t U_EXPORT2
958 udat_getDefaultContext(UDateFormat
* fmt
,
959 UDateFormatContextType type
,
962 verifyIsSimpleDateFormat(fmt
, status
);
963 if (U_FAILURE(*status
)) {
966 return ((SimpleDateFormat
*)fmt
)->getDefaultContext(type
, *status
);
971 * Verify that fmt is a RelativeDateFormat. Invalid error if not.
972 * @param fmt the UDateFormat, definitely a DateFormat, maybe something else
973 * @param status error code, will be set to failure if there is a familure or the fmt is NULL.
975 static void verifyIsRelativeDateFormat(const UDateFormat
* fmt
, UErrorCode
*status
) {
976 if(U_SUCCESS(*status
) &&
977 dynamic_cast<const RelativeDateFormat
*>(reinterpret_cast<const DateFormat
*>(fmt
))==NULL
) {
978 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
983 U_CAPI
int32_t U_EXPORT2
984 udat_toPatternRelativeDate(const UDateFormat
*fmt
,
986 int32_t resultLength
,
989 verifyIsRelativeDateFormat(fmt
, status
);
990 if(U_FAILURE(*status
)) return -1;
992 UnicodeString datePattern
;
993 if(!(result
==NULL
&& resultLength
==0)) {
994 // NULL destination for pure preflighting: empty dummy string
995 // otherwise, alias the destination buffer
996 datePattern
.setTo(result
, 0, resultLength
);
998 ((RelativeDateFormat
*)fmt
)->toPatternDate(datePattern
, *status
);
999 return datePattern
.extract(result
, resultLength
, *status
);
1002 U_CAPI
int32_t U_EXPORT2
1003 udat_toPatternRelativeTime(const UDateFormat
*fmt
,
1005 int32_t resultLength
,
1008 verifyIsRelativeDateFormat(fmt
, status
);
1009 if(U_FAILURE(*status
)) return -1;
1011 UnicodeString timePattern
;
1012 if(!(result
==NULL
&& resultLength
==0)) {
1013 // NULL destination for pure preflighting: empty dummy string
1014 // otherwise, alias the destination buffer
1015 timePattern
.setTo(result
, 0, resultLength
);
1017 ((RelativeDateFormat
*)fmt
)->toPatternTime(timePattern
, *status
);
1018 return timePattern
.extract(result
, resultLength
, *status
);
1021 U_CAPI
void U_EXPORT2
1022 udat_applyPatternRelative(UDateFormat
*format
,
1023 const UChar
*datePattern
,
1024 int32_t datePatternLength
,
1025 const UChar
*timePattern
,
1026 int32_t timePatternLength
,
1029 verifyIsRelativeDateFormat(format
, status
);
1030 if(U_FAILURE(*status
)) return;
1031 const UnicodeString
datePat((UBool
)(datePatternLength
== -1), datePattern
, datePatternLength
);
1032 const UnicodeString
timePat((UBool
)(timePatternLength
== -1), timePattern
, timePatternLength
);
1033 ((RelativeDateFormat
*)format
)->applyPatterns(datePat
, timePat
, *status
);
1036 #endif /* #if !UCONFIG_NO_FORMATTING */