2 *******************************************************************************
3 * Copyright (C) 1996-2010, 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"
30 * Verify that fmt is a SimpleDateFormat. Invalid error if not.
31 * @param fmt the UDateFormat, definitely a DateFormat, maybe something else
32 * @param status error code, will be set to failure if there is a familure or the fmt is NULL.
34 static void verifyIsSimpleDateFormat(const UDateFormat
* fmt
, UErrorCode
*status
) {
35 if(U_SUCCESS(*status
) &&
36 dynamic_cast<const SimpleDateFormat
*>(reinterpret_cast<const DateFormat
*>(fmt
))==NULL
) {
37 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
41 // This mirrors the correspondence between the
42 // SimpleDateFormat::fgPatternIndexToDateFormatField and
43 // SimpleDateFormat::fgPatternIndexToCalendarField arrays.
44 static UCalendarDateFields gDateFieldMapping
[] = {
45 UCAL_ERA
, // UDAT_ERA_FIELD = 0
46 UCAL_YEAR
, // UDAT_YEAR_FIELD = 1
47 UCAL_MONTH
, // UDAT_MONTH_FIELD = 2
48 UCAL_DATE
, // UDAT_DATE_FIELD = 3
49 UCAL_HOUR_OF_DAY
, // UDAT_HOUR_OF_DAY1_FIELD = 4
50 UCAL_HOUR_OF_DAY
, // UDAT_HOUR_OF_DAY0_FIELD = 5
51 UCAL_MINUTE
, // UDAT_MINUTE_FIELD = 6
52 UCAL_SECOND
, // UDAT_SECOND_FIELD = 7
53 UCAL_MILLISECOND
, // UDAT_FRACTIONAL_SECOND_FIELD = 8
54 UCAL_DAY_OF_WEEK
, // UDAT_DAY_OF_WEEK_FIELD = 9
55 UCAL_DAY_OF_YEAR
, // UDAT_DAY_OF_YEAR_FIELD = 10
56 UCAL_DAY_OF_WEEK_IN_MONTH
, // UDAT_DAY_OF_WEEK_IN_MONTH_FIELD = 11
57 UCAL_WEEK_OF_YEAR
, // UDAT_WEEK_OF_YEAR_FIELD = 12
58 UCAL_WEEK_OF_MONTH
, // UDAT_WEEK_OF_MONTH_FIELD = 13
59 UCAL_AM_PM
, // UDAT_AM_PM_FIELD = 14
60 UCAL_HOUR
, // UDAT_HOUR1_FIELD = 15
61 UCAL_HOUR
, // UDAT_HOUR0_FIELD = 16
62 UCAL_ZONE_OFFSET
, // UDAT_TIMEZONE_FIELD = 17
63 UCAL_YEAR_WOY
, // UDAT_YEAR_WOY_FIELD = 18
64 UCAL_DOW_LOCAL
, // UDAT_DOW_LOCAL_FIELD = 19
65 UCAL_EXTENDED_YEAR
, // UDAT_EXTENDED_YEAR_FIELD = 20
66 UCAL_JULIAN_DAY
, // UDAT_JULIAN_DAY_FIELD = 21
67 UCAL_MILLISECONDS_IN_DAY
, // UDAT_MILLISECONDS_IN_DAY_FIELD = 22
68 UCAL_ZONE_OFFSET
, // UDAT_TIMEZONE_RFC_FIELD = 23
69 // UCAL_DST_OFFSET also
70 UCAL_ZONE_OFFSET
, // UDAT_TIMEZONE_GENERIC_FIELD = 24
71 UCAL_DOW_LOCAL
, // UDAT_STANDALONE_DAY_FIELD = 25
72 UCAL_MONTH
, // UDAT_STANDALONE_MONTH_FIELD = 26
73 UCAL_MONTH
, // UDAT_QUARTER_FIELD = 27
74 UCAL_MONTH
, // UDAT_STANDALONE_QUARTER_FIELD = 28
75 UCAL_ZONE_OFFSET
, // UDAT_TIMEZONE_SPECIAL_FIELD = 29
76 UCAL_FIELD_COUNT
, // UDAT_FIELD_COUNT = 30
77 // UCAL_IS_LEAP_MONTH is not the target of a mapping
80 U_CAPI UCalendarDateFields U_EXPORT2
81 udat_toCalendarDateField(UDateFormatField field
) {
82 return gDateFieldMapping
[field
];
85 U_CAPI UDateFormat
* U_EXPORT2
86 udat_open(UDateFormatStyle timeStyle
,
87 UDateFormatStyle dateStyle
,
92 int32_t patternLength
,
96 if(U_FAILURE(*status
)) {
99 if(timeStyle
!= UDAT_IGNORE
) {
101 fmt
= DateFormat::createDateTimeInstance((DateFormat::EStyle
)dateStyle
,
102 (DateFormat::EStyle
)timeStyle
);
105 fmt
= DateFormat::createDateTimeInstance((DateFormat::EStyle
)dateStyle
,
106 (DateFormat::EStyle
)timeStyle
,
111 UnicodeString
pat((UBool
)(patternLength
== -1), pattern
, patternLength
);
114 fmt
= new SimpleDateFormat(pat
, *status
);
117 fmt
= new SimpleDateFormat(pat
, Locale(locale
), *status
);
122 *status
= U_MEMORY_ALLOCATION_ERROR
;
127 TimeZone
*zone
= TimeZone::createTimeZone(UnicodeString((UBool
)(tzIDLength
== -1), tzID
, tzIDLength
));
129 *status
= U_MEMORY_ALLOCATION_ERROR
;
133 fmt
->adoptTimeZone(zone
);
136 return (UDateFormat
*)fmt
;
140 U_CAPI
void U_EXPORT2
141 udat_close(UDateFormat
* format
)
143 delete (DateFormat
*)format
;
146 U_CAPI UDateFormat
* U_EXPORT2
147 udat_clone(const UDateFormat
*fmt
,
150 if(U_FAILURE(*status
)) return 0;
152 Format
*res
= ((DateFormat
*)fmt
)->clone();
155 *status
= U_MEMORY_ALLOCATION_ERROR
;
159 return (UDateFormat
*) res
;
162 U_CAPI
int32_t U_EXPORT2
163 udat_format( const UDateFormat
* format
,
166 int32_t resultLength
,
167 UFieldPosition
* position
,
170 if(U_FAILURE(*status
)) return -1;
173 if(!(result
==NULL
&& resultLength
==0)) {
174 // NULL destination for pure preflighting: empty dummy string
175 // otherwise, alias the destination buffer
176 res
.setTo(result
, 0, resultLength
);
182 fp
.setField(position
->field
);
184 ((DateFormat
*)format
)->format(dateToFormat
, res
, fp
);
187 position
->beginIndex
= fp
.getBeginIndex();
188 position
->endIndex
= fp
.getEndIndex();
191 return res
.extract(result
, resultLength
, *status
);
194 U_CAPI UDate U_EXPORT2
195 udat_parse( const UDateFormat
* format
,
201 if(U_FAILURE(*status
)) return (UDate
)0;
203 const UnicodeString
src((UBool
)(textLength
== -1), text
, textLength
);
205 int32_t stackParsePos
= 0;
208 if(parsePos
== NULL
) {
209 parsePos
= &stackParsePos
;
212 pp
.setIndex(*parsePos
);
214 res
= ((DateFormat
*)format
)->parse(src
, pp
);
216 if(pp
.getErrorIndex() == -1)
217 *parsePos
= pp
.getIndex();
219 *parsePos
= pp
.getErrorIndex();
220 *status
= U_PARSE_ERROR
;
226 U_CAPI
void U_EXPORT2
227 udat_parseCalendar(const UDateFormat
* format
,
234 if(U_FAILURE(*status
)) return;
236 const UnicodeString
src((UBool
)(textLength
== -1), text
, textLength
);
240 pp
.setIndex(*parsePos
);
242 ((DateFormat
*)format
)->parse(src
, *(Calendar
*)calendar
, pp
);
245 if(pp
.getErrorIndex() == -1)
246 *parsePos
= pp
.getIndex();
248 *parsePos
= pp
.getErrorIndex();
249 *status
= U_PARSE_ERROR
;
254 U_CAPI UBool U_EXPORT2
255 udat_isLenient(const UDateFormat
* fmt
)
257 return ((DateFormat
*)fmt
)->isLenient();
260 U_CAPI
void U_EXPORT2
261 udat_setLenient( UDateFormat
* fmt
,
264 ((DateFormat
*)fmt
)->setLenient(isLenient
);
267 U_CAPI
const UCalendar
* U_EXPORT2
268 udat_getCalendar(const UDateFormat
* fmt
)
270 return (const UCalendar
*) ((DateFormat
*)fmt
)->getCalendar();
273 U_CAPI
void U_EXPORT2
274 udat_setCalendar(UDateFormat
* fmt
,
275 const UCalendar
* calendarToSet
)
277 ((DateFormat
*)fmt
)->setCalendar(*((Calendar
*)calendarToSet
));
280 U_CAPI
const UNumberFormat
* U_EXPORT2
281 udat_getNumberFormat(const UDateFormat
* fmt
)
283 return (const UNumberFormat
*) ((DateFormat
*)fmt
)->getNumberFormat();
286 U_CAPI
void U_EXPORT2
287 udat_setNumberFormat(UDateFormat
* fmt
,
288 const UNumberFormat
* numberFormatToSet
)
290 ((DateFormat
*)fmt
)->setNumberFormat(*((NumberFormat
*)numberFormatToSet
));
293 U_CAPI
const char* U_EXPORT2
294 udat_getAvailable(int32_t index
)
296 return uloc_getAvailable(index
);
299 U_CAPI
int32_t U_EXPORT2
300 udat_countAvailable()
302 return uloc_countAvailable();
305 U_CAPI UDate U_EXPORT2
306 udat_get2DigitYearStart( const UDateFormat
*fmt
,
309 verifyIsSimpleDateFormat(fmt
, status
);
310 if(U_FAILURE(*status
)) return (UDate
)0;
311 return ((SimpleDateFormat
*)fmt
)->get2DigitYearStart(*status
);
314 U_CAPI
void U_EXPORT2
315 udat_set2DigitYearStart( UDateFormat
*fmt
,
319 verifyIsSimpleDateFormat(fmt
, status
);
320 if(U_FAILURE(*status
)) return;
321 ((SimpleDateFormat
*)fmt
)->set2DigitYearStart(d
, *status
);
324 U_CAPI
int32_t U_EXPORT2
325 udat_toPattern( const UDateFormat
*fmt
,
328 int32_t resultLength
,
331 if(U_FAILURE(*status
)) return -1;
334 if(!(result
==NULL
&& resultLength
==0)) {
335 // NULL destination for pure preflighting: empty dummy string
336 // otherwise, alias the destination buffer
337 res
.setTo(result
, 0, resultLength
);
340 const DateFormat
*df
=reinterpret_cast<const DateFormat
*>(fmt
);
341 const SimpleDateFormat
*sdtfmt
=dynamic_cast<const SimpleDateFormat
*>(df
);
342 const RelativeDateFormat
*reldtfmt
;
345 sdtfmt
->toLocalizedPattern(res
, *status
);
347 sdtfmt
->toPattern(res
);
348 } else if (!localized
&& (reldtfmt
=dynamic_cast<const RelativeDateFormat
*>(df
))!=NULL
) {
349 reldtfmt
->toPattern(res
, *status
);
351 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
355 return res
.extract(result
, resultLength
, *status
);
358 // TODO: should this take an UErrorCode?
359 // A: Yes. Of course.
360 U_CAPI
void U_EXPORT2
361 udat_applyPattern( UDateFormat
*format
,
363 const UChar
*pattern
,
364 int32_t patternLength
)
366 const UnicodeString
pat((UBool
)(patternLength
== -1), pattern
, patternLength
);
367 UErrorCode status
= U_ZERO_ERROR
;
369 verifyIsSimpleDateFormat(format
, &status
);
370 if(U_FAILURE(status
)) {
375 ((SimpleDateFormat
*)format
)->applyLocalizedPattern(pat
, status
);
377 ((SimpleDateFormat
*)format
)->applyPattern(pat
);
380 U_CAPI
int32_t U_EXPORT2
381 udat_getSymbols(const UDateFormat
*fmt
,
382 UDateFormatSymbolType type
,
385 int32_t resultLength
,
388 const DateFormatSymbols
*syms
;
389 const SimpleDateFormat
* sdtfmt
;
390 const RelativeDateFormat
* rdtfmt
;
391 if ((sdtfmt
= dynamic_cast<const SimpleDateFormat
*>(reinterpret_cast<const DateFormat
*>(fmt
))) != NULL
) {
392 syms
= sdtfmt
->getDateFormatSymbols();
393 } else if ((rdtfmt
= dynamic_cast<const RelativeDateFormat
*>(reinterpret_cast<const DateFormat
*>(fmt
))) != NULL
) {
394 syms
= rdtfmt
->getDateFormatSymbols();
399 const UnicodeString
*res
= NULL
;
403 res
= syms
->getEras(count
);
407 res
= syms
->getEraNames(count
);
411 res
= syms
->getMonths(count
);
414 case UDAT_SHORT_MONTHS
:
415 res
= syms
->getShortMonths(count
);
419 res
= syms
->getWeekdays(count
);
422 case UDAT_SHORT_WEEKDAYS
:
423 res
= syms
->getShortWeekdays(count
);
427 res
= syms
->getAmPmStrings(count
);
430 case UDAT_LOCALIZED_CHARS
:
433 if(!(result
==NULL
&& resultLength
==0)) {
434 // NULL destination for pure preflighting: empty dummy string
435 // otherwise, alias the destination buffer
436 res1
.setTo(result
, 0, resultLength
);
438 syms
->getLocalPatternChars(res1
);
439 return res1
.extract(result
, resultLength
, *status
);
442 case UDAT_NARROW_MONTHS
:
443 res
= syms
->getMonths(count
, DateFormatSymbols::FORMAT
, DateFormatSymbols::NARROW
);
446 case UDAT_NARROW_WEEKDAYS
:
447 res
= syms
->getWeekdays(count
, DateFormatSymbols::FORMAT
, DateFormatSymbols::NARROW
);
450 case UDAT_STANDALONE_MONTHS
:
451 res
= syms
->getMonths(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::WIDE
);
454 case UDAT_STANDALONE_SHORT_MONTHS
:
455 res
= syms
->getMonths(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::ABBREVIATED
);
458 case UDAT_STANDALONE_NARROW_MONTHS
:
459 res
= syms
->getMonths(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::NARROW
);
462 case UDAT_STANDALONE_WEEKDAYS
:
463 res
= syms
->getWeekdays(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::WIDE
);
466 case UDAT_STANDALONE_SHORT_WEEKDAYS
:
467 res
= syms
->getWeekdays(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::ABBREVIATED
);
470 case UDAT_STANDALONE_NARROW_WEEKDAYS
:
471 res
= syms
->getWeekdays(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::NARROW
);
475 res
= syms
->getQuarters(count
, DateFormatSymbols::FORMAT
, DateFormatSymbols::WIDE
);
478 case UDAT_SHORT_QUARTERS
:
479 res
= syms
->getQuarters(count
, DateFormatSymbols::FORMAT
, DateFormatSymbols::ABBREVIATED
);
482 case UDAT_STANDALONE_QUARTERS
:
483 res
= syms
->getQuarters(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::WIDE
);
486 case UDAT_STANDALONE_SHORT_QUARTERS
:
487 res
= syms
->getQuarters(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::ABBREVIATED
);
493 return res
[index
].extract(result
, resultLength
, *status
);
498 // TODO: also needs an errorCode.
499 U_CAPI
int32_t U_EXPORT2
500 udat_countSymbols( const UDateFormat
*fmt
,
501 UDateFormatSymbolType type
)
503 const DateFormatSymbols
*syms
;
504 const SimpleDateFormat
* sdtfmt
;
505 const RelativeDateFormat
* rdtfmt
;
506 if ((sdtfmt
= dynamic_cast<const SimpleDateFormat
*>(reinterpret_cast<const DateFormat
*>(fmt
))) != NULL
) {
507 syms
= sdtfmt
->getDateFormatSymbols();
508 } else if ((rdtfmt
= dynamic_cast<const RelativeDateFormat
*>(reinterpret_cast<const DateFormat
*>(fmt
))) != NULL
) {
509 syms
= rdtfmt
->getDateFormatSymbols();
517 syms
->getEras(count
);
521 syms
->getMonths(count
);
524 case UDAT_SHORT_MONTHS
:
525 syms
->getShortMonths(count
);
529 syms
->getWeekdays(count
);
532 case UDAT_SHORT_WEEKDAYS
:
533 syms
->getShortWeekdays(count
);
537 syms
->getAmPmStrings(count
);
540 case UDAT_LOCALIZED_CHARS
:
545 syms
->getEraNames(count
);
548 case UDAT_NARROW_MONTHS
:
549 syms
->getMonths(count
, DateFormatSymbols::FORMAT
, DateFormatSymbols::NARROW
);
552 case UDAT_NARROW_WEEKDAYS
:
553 syms
->getWeekdays(count
, DateFormatSymbols::FORMAT
, DateFormatSymbols::NARROW
);
556 case UDAT_STANDALONE_MONTHS
:
557 syms
->getMonths(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::WIDE
);
560 case UDAT_STANDALONE_SHORT_MONTHS
:
561 syms
->getMonths(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::ABBREVIATED
);
564 case UDAT_STANDALONE_NARROW_MONTHS
:
565 syms
->getMonths(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::NARROW
);
568 case UDAT_STANDALONE_WEEKDAYS
:
569 syms
->getWeekdays(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::WIDE
);
572 case UDAT_STANDALONE_SHORT_WEEKDAYS
:
573 syms
->getWeekdays(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::ABBREVIATED
);
576 case UDAT_STANDALONE_NARROW_WEEKDAYS
:
577 syms
->getWeekdays(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::NARROW
);
581 syms
->getQuarters(count
, DateFormatSymbols::FORMAT
, DateFormatSymbols::WIDE
);
584 case UDAT_SHORT_QUARTERS
:
585 syms
->getQuarters(count
, DateFormatSymbols::FORMAT
, DateFormatSymbols::ABBREVIATED
);
588 case UDAT_STANDALONE_QUARTERS
:
589 syms
->getQuarters(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::WIDE
);
592 case UDAT_STANDALONE_SHORT_QUARTERS
:
593 syms
->getQuarters(count
, DateFormatSymbols::STANDALONE
, DateFormatSymbols::ABBREVIATED
);
604 * This DateFormatSymbolsSingleSetter class is a friend of DateFormatSymbols
605 * solely for the purpose of avoiding to clone the array of strings
606 * just to modify one of them and then setting all of them back.
607 * For example, the old code looked like this:
609 * res = syms->getMonths(count);
610 * array = new UnicodeString[count];
612 * *status = U_MEMORY_ALLOCATION_ERROR;
615 * uprv_arrayCopy(res, array, count);
617 * array[index] = val;
618 * syms->setMonths(array, count);
621 * Even worse, the old code actually cloned the entire DateFormatSymbols object,
622 * cloned one value array, changed one value, and then made the SimpleDateFormat
623 * replace its DateFormatSymbols object with the new one.
627 class DateFormatSymbolsSingleSetter
/* not : public UObject because all methods are static */ {
630 setSymbol(UnicodeString
*array
, int32_t count
, int32_t index
,
631 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
635 errorCode
=U_INDEX_OUTOFBOUNDS_ERROR
;
636 } else if(value
==NULL
) {
637 errorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
639 array
[index
].setTo(value
, valueLength
);
645 setEra(DateFormatSymbols
*syms
, int32_t index
,
646 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
648 setSymbol(syms
->fEras
, syms
->fErasCount
, index
, value
, valueLength
, errorCode
);
652 setEraName(DateFormatSymbols
*syms
, int32_t index
,
653 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
655 setSymbol(syms
->fEraNames
, syms
->fEraNamesCount
, index
, value
, valueLength
, errorCode
);
659 setMonth(DateFormatSymbols
*syms
, int32_t index
,
660 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
662 setSymbol(syms
->fMonths
, syms
->fMonthsCount
, index
, value
, valueLength
, errorCode
);
666 setShortMonth(DateFormatSymbols
*syms
, int32_t index
,
667 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
669 setSymbol(syms
->fShortMonths
, syms
->fShortMonthsCount
, index
, value
, valueLength
, errorCode
);
673 setNarrowMonth(DateFormatSymbols
*syms
, int32_t index
,
674 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
676 setSymbol(syms
->fNarrowMonths
, syms
->fNarrowMonthsCount
, index
, value
, valueLength
, errorCode
);
680 setStandaloneMonth(DateFormatSymbols
*syms
, int32_t index
,
681 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
683 setSymbol(syms
->fStandaloneMonths
, syms
->fStandaloneMonthsCount
, index
, value
, valueLength
, errorCode
);
687 setStandaloneShortMonth(DateFormatSymbols
*syms
, int32_t index
,
688 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
690 setSymbol(syms
->fStandaloneShortMonths
, syms
->fStandaloneShortMonthsCount
, index
, value
, valueLength
, errorCode
);
694 setStandaloneNarrowMonth(DateFormatSymbols
*syms
, int32_t index
,
695 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
697 setSymbol(syms
->fStandaloneNarrowMonths
, syms
->fStandaloneNarrowMonthsCount
, index
, value
, valueLength
, errorCode
);
701 setWeekday(DateFormatSymbols
*syms
, int32_t index
,
702 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
704 setSymbol(syms
->fWeekdays
, syms
->fWeekdaysCount
, index
, value
, valueLength
, errorCode
);
708 setShortWeekday(DateFormatSymbols
*syms
, int32_t index
,
709 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
711 setSymbol(syms
->fShortWeekdays
, syms
->fShortWeekdaysCount
, index
, value
, valueLength
, errorCode
);
715 setNarrowWeekday(DateFormatSymbols
*syms
, int32_t index
,
716 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
718 setSymbol(syms
->fNarrowWeekdays
, syms
->fNarrowWeekdaysCount
, index
, value
, valueLength
, errorCode
);
722 setStandaloneWeekday(DateFormatSymbols
*syms
, int32_t index
,
723 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
725 setSymbol(syms
->fStandaloneWeekdays
, syms
->fStandaloneWeekdaysCount
, index
, value
, valueLength
, errorCode
);
729 setStandaloneShortWeekday(DateFormatSymbols
*syms
, int32_t index
,
730 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
732 setSymbol(syms
->fStandaloneShortWeekdays
, syms
->fStandaloneShortWeekdaysCount
, index
, value
, valueLength
, errorCode
);
736 setStandaloneNarrowWeekday(DateFormatSymbols
*syms
, int32_t index
,
737 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
739 setSymbol(syms
->fStandaloneNarrowWeekdays
, syms
->fStandaloneNarrowWeekdaysCount
, index
, value
, valueLength
, errorCode
);
743 setQuarter(DateFormatSymbols
*syms
, int32_t index
,
744 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
746 setSymbol(syms
->fQuarters
, syms
->fQuartersCount
, index
, value
, valueLength
, errorCode
);
750 setShortQuarter(DateFormatSymbols
*syms
, int32_t index
,
751 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
753 setSymbol(syms
->fShortQuarters
, syms
->fShortQuartersCount
, index
, value
, valueLength
, errorCode
);
757 setStandaloneQuarter(DateFormatSymbols
*syms
, int32_t index
,
758 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
760 setSymbol(syms
->fStandaloneQuarters
, syms
->fStandaloneQuartersCount
, index
, value
, valueLength
, errorCode
);
764 setStandaloneShortQuarter(DateFormatSymbols
*syms
, int32_t index
,
765 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
767 setSymbol(syms
->fStandaloneShortQuarters
, syms
->fStandaloneShortQuartersCount
, index
, value
, valueLength
, errorCode
);
771 setAmPm(DateFormatSymbols
*syms
, int32_t index
,
772 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
774 setSymbol(syms
->fAmPms
, syms
->fAmPmsCount
, index
, value
, valueLength
, errorCode
);
778 setLocalPatternChars(DateFormatSymbols
*syms
,
779 const UChar
*value
, int32_t valueLength
, UErrorCode
&errorCode
)
781 setSymbol(&syms
->fLocalPatternChars
, 1, 0, value
, valueLength
, errorCode
);
787 U_CAPI
void U_EXPORT2
788 udat_setSymbols( UDateFormat
*format
,
789 UDateFormatSymbolType type
,
795 verifyIsSimpleDateFormat(format
, status
);
796 if(U_FAILURE(*status
)) return;
798 DateFormatSymbols
*syms
= (DateFormatSymbols
*)((SimpleDateFormat
*)format
)->getDateFormatSymbols();
802 DateFormatSymbolsSingleSetter::setEra(syms
, index
, value
, valueLength
, *status
);
806 DateFormatSymbolsSingleSetter::setEraName(syms
, index
, value
, valueLength
, *status
);
810 DateFormatSymbolsSingleSetter::setMonth(syms
, index
, value
, valueLength
, *status
);
813 case UDAT_SHORT_MONTHS
:
814 DateFormatSymbolsSingleSetter::setShortMonth(syms
, index
, value
, valueLength
, *status
);
817 case UDAT_NARROW_MONTHS
:
818 DateFormatSymbolsSingleSetter::setNarrowMonth(syms
, index
, value
, valueLength
, *status
);
821 case UDAT_STANDALONE_MONTHS
:
822 DateFormatSymbolsSingleSetter::setStandaloneMonth(syms
, index
, value
, valueLength
, *status
);
825 case UDAT_STANDALONE_SHORT_MONTHS
:
826 DateFormatSymbolsSingleSetter::setStandaloneShortMonth(syms
, index
, value
, valueLength
, *status
);
829 case UDAT_STANDALONE_NARROW_MONTHS
:
830 DateFormatSymbolsSingleSetter::setStandaloneNarrowMonth(syms
, index
, value
, valueLength
, *status
);
834 DateFormatSymbolsSingleSetter::setWeekday(syms
, index
, value
, valueLength
, *status
);
837 case UDAT_SHORT_WEEKDAYS
:
838 DateFormatSymbolsSingleSetter::setShortWeekday(syms
, index
, value
, valueLength
, *status
);
841 case UDAT_NARROW_WEEKDAYS
:
842 DateFormatSymbolsSingleSetter::setNarrowWeekday(syms
, index
, value
, valueLength
, *status
);
845 case UDAT_STANDALONE_WEEKDAYS
:
846 DateFormatSymbolsSingleSetter::setStandaloneWeekday(syms
, index
, value
, valueLength
, *status
);
849 case UDAT_STANDALONE_SHORT_WEEKDAYS
:
850 DateFormatSymbolsSingleSetter::setStandaloneShortWeekday(syms
, index
, value
, valueLength
, *status
);
853 case UDAT_STANDALONE_NARROW_WEEKDAYS
:
854 DateFormatSymbolsSingleSetter::setStandaloneNarrowWeekday(syms
, index
, value
, valueLength
, *status
);
858 DateFormatSymbolsSingleSetter::setQuarter(syms
, index
, value
, valueLength
, *status
);
861 case UDAT_SHORT_QUARTERS
:
862 DateFormatSymbolsSingleSetter::setShortQuarter(syms
, index
, value
, valueLength
, *status
);
865 case UDAT_STANDALONE_QUARTERS
:
866 DateFormatSymbolsSingleSetter::setStandaloneQuarter(syms
, index
, value
, valueLength
, *status
);
869 case UDAT_STANDALONE_SHORT_QUARTERS
:
870 DateFormatSymbolsSingleSetter::setStandaloneShortQuarter(syms
, index
, value
, valueLength
, *status
);
874 DateFormatSymbolsSingleSetter::setAmPm(syms
, index
, value
, valueLength
, *status
);
877 case UDAT_LOCALIZED_CHARS
:
878 DateFormatSymbolsSingleSetter::setLocalPatternChars(syms
, value
, valueLength
, *status
);
882 *status
= U_UNSUPPORTED_ERROR
;
888 U_CAPI
const char* U_EXPORT2
889 udat_getLocaleByType(const UDateFormat
*fmt
,
890 ULocDataLocaleType type
,
894 if (U_SUCCESS(*status
)) {
895 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
899 return ((Format
*)fmt
)->getLocaleID(type
, *status
);
903 * Verify that fmt is a RelativeDateFormat. Invalid error if not.
904 * @param fmt the UDateFormat, definitely a DateFormat, maybe something else
905 * @param status error code, will be set to failure if there is a familure or the fmt is NULL.
907 static void verifyIsRelativeDateFormat(const UDateFormat
* fmt
, UErrorCode
*status
) {
908 if(U_SUCCESS(*status
) &&
909 dynamic_cast<const RelativeDateFormat
*>(reinterpret_cast<const DateFormat
*>(fmt
))==NULL
) {
910 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
915 U_CAPI
int32_t U_EXPORT2
916 udat_toPatternRelativeDate(const UDateFormat
*fmt
,
918 int32_t resultLength
,
921 verifyIsRelativeDateFormat(fmt
, status
);
922 if(U_FAILURE(*status
)) return -1;
924 UnicodeString datePattern
;
925 if(!(result
==NULL
&& resultLength
==0)) {
926 // NULL destination for pure preflighting: empty dummy string
927 // otherwise, alias the destination buffer
928 datePattern
.setTo(result
, 0, resultLength
);
930 ((RelativeDateFormat
*)fmt
)->toPatternDate(datePattern
, *status
);
931 return datePattern
.extract(result
, resultLength
, *status
);
934 U_CAPI
int32_t U_EXPORT2
935 udat_toPatternRelativeTime(const UDateFormat
*fmt
,
937 int32_t resultLength
,
940 verifyIsRelativeDateFormat(fmt
, status
);
941 if(U_FAILURE(*status
)) return -1;
943 UnicodeString timePattern
;
944 if(!(result
==NULL
&& resultLength
==0)) {
945 // NULL destination for pure preflighting: empty dummy string
946 // otherwise, alias the destination buffer
947 timePattern
.setTo(result
, 0, resultLength
);
949 ((RelativeDateFormat
*)fmt
)->toPatternTime(timePattern
, *status
);
950 return timePattern
.extract(result
, resultLength
, *status
);
953 U_CAPI
void U_EXPORT2
954 udat_applyPatternRelative(UDateFormat
*format
,
955 const UChar
*datePattern
,
956 int32_t datePatternLength
,
957 const UChar
*timePattern
,
958 int32_t timePatternLength
,
961 verifyIsRelativeDateFormat(format
, status
);
962 if(U_FAILURE(*status
)) return;
963 const UnicodeString
datePat((UBool
)(datePatternLength
== -1), datePattern
, datePatternLength
);
964 const UnicodeString
timePat((UBool
)(timePatternLength
== -1), timePattern
, timePatternLength
);
965 ((RelativeDateFormat
*)format
)->applyPatterns(datePat
, timePat
, *status
);
968 #endif /* #if !UCONFIG_NO_FORMATTING */