2 *******************************************************************************
3 * Copyright (C) 1996-2004, International Business Machines Corporation and others. All Rights Reserved.
4 *******************************************************************************
10 #include "unicode/utypes.h"
12 #if !UCONFIG_NO_FORMATTING
14 #include "unicode/ucal.h"
15 #include "unicode/unum.h"
18 * \brief C API: DateFormat
20 * <h2> Date Format C API</h2>
22 * Date Format C API consists of functions that convert dates and
23 * times from their internal representations to textual form and back again in a
24 * language-independent manner. Converting from the internal representation (milliseconds
25 * since midnight, January 1, 1970) to text is known as "formatting," and converting
26 * from text to millis is known as "parsing." We currently define only one concrete
27 * structure UDateFormat, which can handle pretty much all normal
28 * date formatting and parsing actions.
30 * Date Format helps you to format and parse dates for any locale. Your code can
31 * be completely independent of the locale conventions for months, days of the
32 * week, or even the calendar format: lunar vs. solar.
34 * To format a date for the current Locale with default time and date style,
35 * use one of the static factory methods:
38 * UErrorCode status = U_ZERO_ERROR;
40 * int32_t myStrlen = 0;
41 * UDateFormat* dfmt = udat_open(UDAT_DEFAULT, UDAT_DEFAULT, NULL, NULL, -1, NULL, -1, &status);
42 * myStrlen = udat_format(dfmt, myDate, NULL, myStrlen, NULL, &status);
43 * if (status==U_BUFFER_OVERFLOW_ERROR){
44 * status=U_ZERO_ERROR;
45 * myString=(UChar*)malloc(sizeof(UChar) * (myStrlen+1) );
46 * udat_format(dfmt, myDate, myString, myStrlen+1, NULL, &status);
50 * If you are formatting multiple numbers, it is more efficient to get the
51 * format and use it multiple times so that the system doesn't have to fetch the
52 * information about the local language and country conventions multiple times.
55 * UErrorCode status = U_ZERO_ERROR;
56 * int32_t i, myStrlen = 0;
59 * UDate myDateArr[] = { 0.0, 100000000.0, 2000000000.0 }; // test values
60 * UDateFormat* df = udat_open(UDAT_DEFAULT, UDAT_DEFAULT, NULL, NULL, -1, NULL, 0, &status);
61 * for (i = 0; i < 3; i++) {
62 * myStrlen = udat_format(df, myDateArr[i], NULL, myStrlen, NULL, &status);
63 * if(status == U_BUFFER_OVERFLOW_ERROR){
64 * status = U_ZERO_ERROR;
65 * myString = (UChar*)malloc(sizeof(UChar) * (myStrlen+1) );
66 * udat_format(df, myDateArr[i], myString, myStrlen+1, NULL, &status);
67 * printf("%s\n", u_austrcpy(buffer, myString) );
73 * To get specific fields of a date, you can use UFieldPosition to
74 * get specific fields.
77 * UErrorCode status = U_ZERO_ERROR;
80 * int32_t myStrlen = 0;
83 * pos.field = 1; // Same as the DateFormat::EField enum
84 * UDateFormat* dfmt = udat_open(UDAT_DEFAULT, UDAT_DEFAULT, NULL, -1, NULL, 0, &status);
85 * myStrlen = udat_format(dfmt, myDate, NULL, myStrlen, &pos, &status);
86 * if (status==U_BUFFER_OVERFLOW_ERROR){
87 * status=U_ZERO_ERROR;
88 * myString=(UChar*)malloc(sizeof(UChar) * (myStrlen+1) );
89 * udat_format(dfmt, myDate, myString, myStrlen+1, &pos, &status);
91 * printf("date format: %s\n", u_austrcpy(buffer, myString));
92 * buffer[pos.endIndex] = 0; // NULL terminate the string.
93 * printf("UFieldPosition position equals %s\n", &buffer[pos.beginIndex]);
96 * To format a date for a different Locale, specify it in the call to
100 * UDateFormat* df = udat_open(UDAT_SHORT, UDAT_SHORT, "fr_FR", NULL, -1, NULL, 0, &status);
103 * You can use a DateFormat API udat_parse() to parse.
106 * UErrorCode status = U_ZERO_ERROR;
107 * int32_t parsepos=0;
108 * UDate myDate = udat_parse(df, myString, u_strlen(myString), &parsepos, &status);
111 * You can pass in different options for the arguments for date and time style
112 * to control the length of the result; from SHORT to MEDIUM to LONG to FULL.
113 * The exact result depends on the locale, but generally:
114 * see UDateFormatStyle for more details
116 * <li> UDAT_SHORT is completely numeric, such as 12/13/52 or 3:30pm
117 * <li> UDAT_MEDIUM is longer, such as Jan 12, 1952
118 * <li> UDAT_LONG is longer, such as January 12, 1952 or 3:30:32pm
119 * <li> UDAT_FULL is pretty completely specified, such as
120 * Tuesday, April 12, 1952 AD or 3:30:42pm PST.
122 * You can also set the time zone on the format if you wish.
124 * You can also use forms of the parse and format methods with Parse Position and
125 * UFieldPosition to allow you to
127 * <li> Progressively parse through pieces of a string.
128 * <li> Align any particular field, or find out where it is for selection
133 /** A date formatter.
134 * For usage in C programs.
137 typedef void* UDateFormat
;
139 /** The possible date/time format styles
142 typedef enum UDateFormatStyle
{
152 UDAT_DEFAULT
= UDAT_MEDIUM
,
155 /** for internal API use only */
161 * FieldPosition and UFieldPosition selectors for format fields
162 * defined by DateFormat and UDateFormat.
165 typedef enum UDateFormatField
{
167 * FieldPosition and UFieldPosition selector for 'G' field alignment,
168 * corresponding to the UCAL_ERA field.
174 * FieldPosition and UFieldPosition selector for 'y' field alignment,
175 * corresponding to the UCAL_YEAR field.
181 * FieldPosition and UFieldPosition selector for 'M' field alignment,
182 * corresponding to the UCAL_MONTH field.
185 UDAT_MONTH_FIELD
= 2,
188 * FieldPosition and UFieldPosition selector for 'd' field alignment,
189 * corresponding to the UCAL_DATE field.
195 * FieldPosition and UFieldPosition selector for 'k' field alignment,
196 * corresponding to the UCAL_HOUR_OF_DAY field.
197 * UDAT_HOUR_OF_DAY1_FIELD is used for the one-based 24-hour clock.
198 * For example, 23:59 + 01:00 results in 24:59.
201 UDAT_HOUR_OF_DAY1_FIELD
= 4,
204 * FieldPosition and UFieldPosition selector for 'H' field alignment,
205 * corresponding to the UCAL_HOUR_OF_DAY field.
206 * UDAT_HOUR_OF_DAY0_FIELD is used for the zero-based 24-hour clock.
207 * For example, 23:59 + 01:00 results in 00:59.
210 UDAT_HOUR_OF_DAY0_FIELD
= 5,
213 * FieldPosition and UFieldPosition selector for 'm' field alignment,
214 * corresponding to the UCAL_MINUTE field.
217 UDAT_MINUTE_FIELD
= 6,
220 * FieldPosition and UFieldPosition selector for 's' field alignment,
221 * corresponding to the UCAL_SECOND field.
224 UDAT_SECOND_FIELD
= 7,
227 * FieldPosition and UFieldPosition selector for 'S' field alignment,
228 * corresponding to the UCAL_MILLISECOND field.
231 UDAT_FRACTIONAL_SECOND_FIELD
= 8,
234 * FieldPosition and UFieldPosition selector for 'E' field alignment,
235 * corresponding to the UCAL_DAY_OF_WEEK field.
238 UDAT_DAY_OF_WEEK_FIELD
= 9,
241 * FieldPosition and UFieldPosition selector for 'D' field alignment,
242 * corresponding to the UCAL_DAY_OF_YEAR field.
245 UDAT_DAY_OF_YEAR_FIELD
= 10,
248 * FieldPosition and UFieldPosition selector for 'F' field alignment,
249 * corresponding to the UCAL_DAY_OF_WEEK_IN_MONTH field.
252 UDAT_DAY_OF_WEEK_IN_MONTH_FIELD
= 11,
255 * FieldPosition and UFieldPosition selector for 'w' field alignment,
256 * corresponding to the UCAL_WEEK_OF_YEAR field.
259 UDAT_WEEK_OF_YEAR_FIELD
= 12,
262 * FieldPosition and UFieldPosition selector for 'W' field alignment,
263 * corresponding to the UCAL_WEEK_OF_MONTH field.
266 UDAT_WEEK_OF_MONTH_FIELD
= 13,
269 * FieldPosition and UFieldPosition selector for 'a' field alignment,
270 * corresponding to the UCAL_AM_PM field.
273 UDAT_AM_PM_FIELD
= 14,
276 * FieldPosition and UFieldPosition selector for 'h' field alignment,
277 * corresponding to the UCAL_HOUR field.
278 * UDAT_HOUR1_FIELD is used for the one-based 12-hour clock.
279 * For example, 11:30 PM + 1 hour results in 12:30 AM.
282 UDAT_HOUR1_FIELD
= 15,
285 * FieldPosition and UFieldPosition selector for 'K' field alignment,
286 * corresponding to the UCAL_HOUR field.
287 * UDAT_HOUR0_FIELD is used for the zero-based 12-hour clock.
288 * For example, 11:30 PM + 1 hour results in 00:30 AM.
291 UDAT_HOUR0_FIELD
= 16,
294 * FieldPosition and UFieldPosition selector for 'z' field alignment,
295 * corresponding to the UCAL_ZONE_OFFSET and
296 * UCAL_DST_OFFSET fields.
299 UDAT_TIMEZONE_FIELD
= 17,
302 * FieldPosition and UFieldPosition selector for 'Y' field alignment,
303 * corresponding to the UCAL_YEAR_WOY field.
306 UDAT_YEAR_WOY_FIELD
= 18,
309 * FieldPosition and UFieldPosition selector for 'e' field alignment,
310 * corresponding to the UCAL_DOW_LOCAL field.
313 UDAT_DOW_LOCAL_FIELD
= 19,
316 * FieldPosition and UFieldPosition selector for 'u' field alignment,
317 * corresponding to the UCAL_EXTENDED_YEAR field.
320 UDAT_EXTENDED_YEAR_FIELD
= 20,
323 * FieldPosition and UFieldPosition selector for 'g' field alignment,
324 * corresponding to the UCAL_JULIAN_DAY field.
327 UDAT_JULIAN_DAY_FIELD
= 21,
330 * FieldPosition and UFieldPosition selector for 'A' field alignment,
331 * corresponding to the UCAL_MILLISECONDS_IN_DAY field.
334 UDAT_MILLISECONDS_IN_DAY_FIELD
= 22,
337 * FieldPosition and UFieldPosition selector for 'Z' field alignment,
338 * corresponding to the UCAL_ZONE_OFFSET and
339 * UCAL_DST_OFFSET fields.
342 UDAT_TIMEZONE_RFC_FIELD
= 23,
345 * Number of FieldPosition and UFieldPosition selectors for
346 * DateFormat and UDateFormat.
347 * Valid selectors range from 0 to UDAT_FIELD_COUNT-1.
348 * This value is subject to change if new fields are defined
352 UDAT_FIELD_COUNT
= 24
357 * Open a new UDateFormat for formatting and parsing dates and times.
358 * A UDateFormat may be used to format dates in calls to {@link #udat_format },
359 * and to parse dates in calls to {@link #udat_parse }.
360 * @param timeStyle The style used to format times; one of UDAT_FULL, UDAT_LONG,
361 * UDAT_MEDIUM, UDAT_SHORT, or UDAT_DEFAULT
362 * @param dateStyle The style used to format dates; one of UDAT_FULL, UDAT_LONG,
363 * UDAT_MEDIUM, UDAT_SHORT, or UDAT_DEFAULT
364 * @param locale The locale specifying the formatting conventions
365 * @param tzID A timezone ID specifying the timezone to use. If 0, use
366 * the default timezone.
367 * @param tzIDLength The length of tzID, or -1 if null-terminated.
368 * @param pattern A pattern specifying the format to use.
369 * @param patternLength The number of characters in the pattern, or -1 if null-terminated.
370 * @param status A pointer to an UErrorCode to receive any errors
371 * @return A pointer to a UDateFormat to use for formatting dates and times, or 0 if
375 U_STABLE UDateFormat
* U_EXPORT2
376 udat_open(UDateFormatStyle timeStyle
,
377 UDateFormatStyle dateStyle
,
381 const UChar
*pattern
,
382 int32_t patternLength
,
387 * Close a UDateFormat.
388 * Once closed, a UDateFormat may no longer be used.
389 * @param format The formatter to close.
392 U_STABLE
void U_EXPORT2
393 udat_close(UDateFormat
* format
);
396 * Open a copy of a UDateFormat.
397 * This function performs a deep copy.
398 * @param fmt The format to copy
399 * @param status A pointer to an UErrorCode to receive any errors.
400 * @return A pointer to a UDateFormat identical to fmt.
403 U_STABLE UDateFormat
* U_EXPORT2
404 udat_clone(const UDateFormat
*fmt
,
408 * Format a date using an UDateFormat.
409 * The date will be formatted using the conventions specified in {@link #udat_open }
410 * @param format The formatter to use
411 * @param dateToFormat The date to format
412 * @param result A pointer to a buffer to receive the formatted number.
413 * @param resultLength The maximum size of result.
414 * @param position A pointer to a UFieldPosition. On input, position->field
415 * is read. On output, position->beginIndex and position->endIndex indicate
416 * the beginning and ending indices of field number position->field, if such
417 * a field exists. This parameter may be NULL, in which case no field
418 * position data is returned.
419 * @param status A pointer to an UErrorCode to receive any errors
420 * @return The total buffer size needed; if greater than resultLength, the output was truncated.
422 * @see UFieldPosition
425 U_STABLE
int32_t U_EXPORT2
426 udat_format( const UDateFormat
* format
,
429 int32_t resultLength
,
430 UFieldPosition
* position
,
434 * Parse a string into an date/time using a UDateFormat.
435 * The date will be parsed using the conventions specified in {@link #udat_open }
436 * @param format The formatter to use.
437 * @param text The text to parse.
438 * @param textLength The length of text, or -1 if null-terminated.
439 * @param parsePos If not 0, on input a pointer to an integer specifying the offset at which
440 * to begin parsing. If not 0, on output the offset at which parsing ended.
441 * @param status A pointer to an UErrorCode to receive any errors
442 * @return The value of the parsed date/time
446 U_STABLE UDate U_EXPORT2
447 udat_parse( const UDateFormat
* format
,
454 * Parse a string into an date/time using a UDateFormat.
455 * The date will be parsed using the conventions specified in {@link #udat_open }
456 * @param format The formatter to use.
457 * @param calendar The calendar in which to store the parsed data.
458 * @param text The text to parse.
459 * @param textLength The length of text, or -1 if null-terminated.
460 * @param parsePos If not 0, on input a pointer to an integer specifying the offset at which
461 * to begin parsing. If not 0, on output the offset at which parsing ended.
462 * @param status A pointer to an UErrorCode to receive any errors
466 U_STABLE
void U_EXPORT2
467 udat_parseCalendar(const UDateFormat
* format
,
475 * Determine if an UDateFormat will perform lenient parsing.
476 * With lenient parsing, the parser may use heuristics to interpret inputs that do not
477 * precisely match the pattern. With strict parsing, inputs must match the pattern.
478 * @param fmt The formatter to query
479 * @return TRUE if fmt is set to perform lenient parsing, FALSE otherwise.
480 * @see udat_setLenient
483 U_STABLE UBool U_EXPORT2
484 udat_isLenient(const UDateFormat
* fmt
);
487 * Specify whether an UDateFormat will perform lenient parsing.
488 * With lenient parsing, the parser may use heuristics to interpret inputs that do not
489 * precisely match the pattern. With strict parsing, inputs must match the pattern.
490 * @param fmt The formatter to set
491 * @param isLenient TRUE if fmt should perform lenient parsing, FALSE otherwise.
495 U_STABLE
void U_EXPORT2
496 udat_setLenient( UDateFormat
* fmt
,
500 * Get the UCalendar associated with an UDateFormat.
501 * A UDateFormat uses a UCalendar to convert a raw value to, for example,
502 * the day of the week.
503 * @param fmt The formatter to query.
504 * @return A pointer to the UCalendar used by fmt.
505 * @see udat_setCalendar
508 U_STABLE
const UCalendar
* U_EXPORT2
509 udat_getCalendar(const UDateFormat
* fmt
);
512 * Set the UCalendar associated with an UDateFormat.
513 * A UDateFormat uses a UCalendar to convert a raw value to, for example,
514 * the day of the week.
515 * @param fmt The formatter to set.
516 * @param calendarToSet A pointer to an UCalendar to be used by fmt.
517 * @see udat_setCalendar
520 U_STABLE
void U_EXPORT2
521 udat_setCalendar( UDateFormat
* fmt
,
522 const UCalendar
* calendarToSet
);
525 * Get the UNumberFormat associated with an UDateFormat.
526 * A UDateFormat uses a UNumberFormat to format numbers within a date,
527 * for example the day number.
528 * @param fmt The formatter to query.
529 * @return A pointer to the UNumberFormat used by fmt to format numbers.
530 * @see udat_setNumberFormat
533 U_STABLE
const UNumberFormat
* U_EXPORT2
534 udat_getNumberFormat(const UDateFormat
* fmt
);
537 * Set the UNumberFormat associated with an UDateFormat.
538 * A UDateFormat uses a UNumberFormat to format numbers within a date,
539 * for example the day number.
540 * @param fmt The formatter to set.
541 * @param numberFormatToSet A pointer to the UNumberFormat to be used by fmt to format numbers.
542 * @see udat_getNumberFormat
545 U_STABLE
void U_EXPORT2
546 udat_setNumberFormat( UDateFormat
* fmt
,
547 const UNumberFormat
* numberFormatToSet
);
550 * Get a locale for which date/time formatting patterns are available.
551 * A UDateFormat in a locale returned by this function will perform the correct
552 * formatting and parsing for the locale.
553 * @param index The index of the desired locale.
554 * @return A locale for which date/time formatting patterns are available, or 0 if none.
555 * @see udat_countAvailable
558 U_STABLE
const char* U_EXPORT2
559 udat_getAvailable(int32_t index
);
562 * Determine how many locales have date/time formatting patterns available.
563 * This function is most useful as determining the loop ending condition for
564 * calls to {@link #udat_getAvailable }.
565 * @return The number of locales for which date/time formatting patterns are available.
566 * @see udat_getAvailable
569 U_STABLE
int32_t U_EXPORT2
570 udat_countAvailable(void);
573 * Get the year relative to which all 2-digit years are interpreted.
574 * For example, if the 2-digit start year is 2100, the year 99 will be
575 * interpreted as 2199.
576 * @param fmt The formatter to query.
577 * @param status A pointer to an UErrorCode to receive any errors
578 * @return The year relative to which all 2-digit years are interpreted.
579 * @see udat_Set2DigitYearStart
582 U_STABLE UDate U_EXPORT2
583 udat_get2DigitYearStart( const UDateFormat
*fmt
,
587 * Set the year relative to which all 2-digit years will be interpreted.
588 * For example, if the 2-digit start year is 2100, the year 99 will be
589 * interpreted as 2199.
590 * @param fmt The formatter to set.
591 * @param d The year relative to which all 2-digit years will be interpreted.
592 * @param status A pointer to an UErrorCode to receive any errors
593 * @see udat_Set2DigitYearStart
596 U_STABLE
void U_EXPORT2
597 udat_set2DigitYearStart( UDateFormat
*fmt
,
602 * Extract the pattern from a UDateFormat.
603 * The pattern will follow the pattern syntax rules.
604 * @param fmt The formatter to query.
605 * @param localized TRUE if the pattern should be localized, FALSE otherwise.
606 * @param result A pointer to a buffer to receive the pattern.
607 * @param resultLength The maximum size of result.
608 * @param status A pointer to an UErrorCode to receive any errors
609 * @return The total buffer size needed; if greater than resultLength, the output was truncated.
610 * @see udat_applyPattern
613 U_STABLE
int32_t U_EXPORT2
614 udat_toPattern( const UDateFormat
*fmt
,
617 int32_t resultLength
,
621 * Set the pattern used by an UDateFormat.
622 * The pattern should follow the pattern syntax rules.
623 * @param format The formatter to set.
624 * @param localized TRUE if the pattern is localized, FALSE otherwise.
625 * @param pattern The new pattern
626 * @param patternLength The length of pattern, or -1 if null-terminated.
627 * @see udat_toPattern
630 U_STABLE
void U_EXPORT2
631 udat_applyPattern( UDateFormat
*format
,
633 const UChar
*pattern
,
634 int32_t patternLength
);
637 * The possible types of date format symbols
640 typedef enum UDateFormatSymbolType
{
641 /** The era names, for example AD */
643 /** The month names, for example February */
645 /** The short month names, for example Feb. */
647 /** The weekday names, for example Monday */
649 /** The short weekday names, for example Mon. */
651 /** The AM/PM names, for example AM */
653 /** The localized characters */
655 } UDateFormatSymbolType
;
657 struct UDateFormatSymbols
;
658 /** Date format symbols.
659 * For usage in C programs.
662 typedef struct UDateFormatSymbols UDateFormatSymbols
;
665 * Get the symbols associated with an UDateFormat.
666 * The symbols are what a UDateFormat uses to represent locale-specific data,
667 * for example month or day names.
668 * @param fmt The formatter to query.
669 * @param type The type of symbols to get. One of UDAT_ERAS, UDAT_MONTHS, UDAT_SHORT_MONTHS,
670 * UDAT_WEEKDAYS, UDAT_SHORT_WEEKDAYS, UDAT_AM_PMS, or UDAT_LOCALIZED_CHARS
671 * @param index The desired symbol of type type.
672 * @param result A pointer to a buffer to receive the pattern.
673 * @param resultLength The maximum size of result.
674 * @param status A pointer to an UErrorCode to receive any errors
675 * @return The total buffer size needed; if greater than resultLength, the output was truncated.
676 * @see udat_countSymbols
677 * @see udat_setSymbols
680 U_STABLE
int32_t U_EXPORT2
681 udat_getSymbols(const UDateFormat
*fmt
,
682 UDateFormatSymbolType type
,
685 int32_t resultLength
,
689 * Count the number of particular symbols for an UDateFormat.
690 * This function is most useful as for detemining the loop termination condition
691 * for calls to {@link #udat_getSymbols }.
692 * @param fmt The formatter to query.
693 * @param type The type of symbols to count. One of UDAT_ERAS, UDAT_MONTHS, UDAT_SHORT_MONTHS,
694 * UDAT_WEEKDAYS, UDAT_SHORT_WEEKDAYS, UDAT_AM_PMS, or UDAT_LOCALIZED_CHARS
695 * @return The number of symbols of type type.
696 * @see udat_getSymbols
697 * @see udat_setSymbols
700 U_STABLE
int32_t U_EXPORT2
701 udat_countSymbols( const UDateFormat
*fmt
,
702 UDateFormatSymbolType type
);
705 * Set the symbols associated with an UDateFormat.
706 * The symbols are what a UDateFormat uses to represent locale-specific data,
707 * for example month or day names.
708 * @param format The formatter to set
709 * @param type The type of symbols to set. One of UDAT_ERAS, UDAT_MONTHS, UDAT_SHORT_MONTHS,
710 * UDAT_WEEKDAYS, UDAT_SHORT_WEEKDAYS, UDAT_AM_PMS, or UDAT_LOCALIZED_CHARS
711 * @param index The index of the symbol to set of type type.
712 * @param value The new value
713 * @param valueLength The length of value, or -1 if null-terminated
714 * @param status A pointer to an UErrorCode to receive any errors
715 * @see udat_getSymbols
716 * @see udat_countSymbols
719 U_STABLE
void U_EXPORT2
720 udat_setSymbols( UDateFormat
*format
,
721 UDateFormatSymbolType type
,
728 * Get the locale for this date format object.
729 * You can choose between valid and actual locale.
730 * @param fmt The formatter to get the locale from
731 * @param type type of the locale we're looking for (valid or actual)
732 * @param status error code for the operation
733 * @return the locale name
734 * @draft ICU 2.8 likely to change in ICU 3.0, based on feedback
736 U_DRAFT
const char* U_EXPORT2
737 udat_getLocaleByType(const UDateFormat
*fmt
,
738 ULocDataLocaleType type
,
741 #endif /* #if !UCONFIG_NO_FORMATTING */