]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/unicode/udat.h
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / udat.h
CommitLineData
b75a7d8f
A
1/*
2*******************************************************************************
3* Copyright (C) 1996-2003, International Business Machines Corporation and others. All Rights Reserved.
4*******************************************************************************
5*/
6
7#ifndef UDAT_H
8#define UDAT_H
9
10#include "unicode/utypes.h"
11
12#if !UCONFIG_NO_FORMATTING
13
14#include "unicode/ucal.h"
15#include "unicode/unum.h"
16/**
17 * \file
18 * \brief C API: DateFormat
19 *
20 * <h2> Date Format C API</h2>
21 *
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.
29 * <P>
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.
33 * <P>
34 * To format a date for the current Locale with default time and date style,
35 * use one of the static factory methods:
36 * <pre>
37 * \code
38 * UErrorCode status = U_ZERO_ERROR;
39 * UChar *myString;
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);
47 * }
48 * \endcode
49 * </pre>
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.
53 * <pre>
54 * \code
55 * UErrorCode status = U_ZERO_ERROR;
56 * int32_t i, myStrlen = 0;
57 * UChar* myString;
58 * char buffer[1024];
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) );
68 * free(myString);
69 * }
70 * }
71 * \endcode
72 * </pre>
73 * To get specific fields of a date, you can use UFieldPosition to
74 * get specific fields.
75 * <pre>
76 * \code
77 * UErrorCode status = U_ZERO_ERROR;
78 * UFieldPosition pos;
79 * UChar *myString;
80 * int32_t myStrlen = 0;
81 * char buffer[1024];
82 *
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);
90 * }
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]);
94 * \endcode
95 * </pre>
96 * To format a date for a different Locale, specify it in the call to
97 * udat_open()
98 * <pre>
99 * \code
100 * UDateFormat* df = udat_open(UDAT_SHORT, UDAT_SHORT, "fr_FR", NULL, -1, NULL, 0, &status);
101 * \endcode
102 * </pre>
103 * You can use a DateFormat API udat_parse() to parse.
104 * <pre>
105 * \code
106 * UErrorCode status = U_ZERO_ERROR;
107 * int32_t parsepos=0;
108 * UDate myDate = udat_parse(df, myString, u_strlen(myString), &parsepos, &status);
109 * \endcode
110 * </pre>
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
115 * <ul type=round>
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.
121 * </ul>
122 * You can also set the time zone on the format if you wish.
123 * <P>
124 * You can also use forms of the parse and format methods with Parse Position and
125 * UFieldPosition to allow you to
126 * <ul type=round>
127 * <li> Progressively parse through pieces of a string.
128 * <li> Align any particular field, or find out where it is for selection
129 * on the screen.
130 * </ul>
131 */
132
133/** A date formatter.
134 * For usage in C programs.
135 * @stable ICU 2.6
136 */
137typedef void* UDateFormat;
138
139/** The possible date/time format styles
140 * @stable ICU 2.6
141 */
142typedef enum UDateFormatStyle {
143 /** Full style */
144 UDAT_FULL,
145 /** Long style */
146 UDAT_LONG,
147 /** Medium style */
148 UDAT_MEDIUM,
149 /** Short style */
150 UDAT_SHORT,
151 /** Default style */
152 UDAT_DEFAULT = UDAT_MEDIUM,
153 /** No style */
154 UDAT_NONE = -1,
155 /** for internal API use only */
156 UDAT_IGNORE = -2
157
158} UDateFormatStyle;
159
160/**
161 * Open a new UDateFormat for formatting and parsing dates and times.
162 * A UDateFormat may be used to format dates in calls to \Ref{udat_format},
163 * and to parse dates in calls to \Ref{udat_parse}.
164 * @param timeStyle The style used to format times; one of UDAT_FULL_STYLE, UDAT_LONG_STYLE,
165 * UDAT_MEDIUM_STYLE, UDAT_SHORT_STYLE, or UDAT_DEFAULT_STYLE
166 * @param dateStyle The style used to format dates; one of UDAT_FULL_STYLE, UDAT_LONG_STYLE,
167 * UDAT_MEDIUM_STYLE, UDAT_SHORT_STYLE, or UDAT_DEFAULT_STYLE
168 * @param locale The locale specifying the formatting conventions
169 * @param tzID A timezone ID specifying the timezone to use. If 0, use
170 * the default timezone.
171 * @param tzIDLength The length of tzID, or -1 if null-terminated.
172 * @param pattern A pattern specifying the format to use.
173 * @param patternLength The number of characters in the pattern, or -1 if null-terminated.
174 * @param status A pointer to an UErrorCode to receive any errors
175 * @return A pointer to a UDateFormat to use for formatting dates and times, or 0 if
176 * an error occurred.
177 * @stable ICU 2.0
178 */
179U_CAPI UDateFormat* U_EXPORT2
180udat_open(UDateFormatStyle timeStyle,
181 UDateFormatStyle dateStyle,
182 const char *locale,
183 const UChar *tzID,
184 int32_t tzIDLength,
185 const UChar *pattern,
186 int32_t patternLength,
187 UErrorCode *status);
188
189
190/**
191* Close a UDateFormat.
192* Once closed, a UDateFormat may no longer be used.
193* @param format The formatter to close.
194* @stable ICU 2.0
195*/
196U_CAPI void U_EXPORT2
197udat_close(UDateFormat* format);
198
199/**
200 * Open a copy of a UDateFormat.
201 * This function performs a deep copy.
202 * @param fmt The format to copy
203 * @param status A pointer to an UErrorCode to receive any errors.
204 * @return A pointer to a UDateFormat identical to fmt.
205 * @stable ICU 2.0
206 */
207U_CAPI UDateFormat* U_EXPORT2
208udat_clone(const UDateFormat *fmt,
209 UErrorCode *status);
210
211/**
212* Format a date using an UDateFormat.
213* The date will be formatted using the conventions specified in \Ref{udat_open}
214* @param format The formatter to use
215* @param dateToFormat The date to format
216* @param result A pointer to a buffer to receive the formatted number.
217* @param resultLength The maximum size of result.
218* @param position A pointer to a UFieldPosition. On input, position->field
219* is read. On output, position->beginIndex and position->endIndex indicate
220* the beginning and ending indices of field number position->field, if such
221* a field exists. This parameter may be NULL, in which case no field
222* position data is returned.
223* @param status A pointer to an UErrorCode to receive any errors
224* @return The total buffer size needed; if greater than resultLength, the output was truncated.
225* @see udat_parse
226* @see UFieldPosition
227* @stable ICU 2.0
228*/
229U_CAPI int32_t U_EXPORT2
230udat_format( const UDateFormat* format,
231 UDate dateToFormat,
232 UChar* result,
233 int32_t resultLength,
234 UFieldPosition* position,
235 UErrorCode* status);
236
237/**
238* Parse a string into an date/time using a UDateFormat.
239* The date will be parsed using the conventions specified in \Ref{udat_open}
240* @param format The formatter to use.
241* @param text The text to parse.
242* @param textLength The length of text, or -1 if null-terminated.
243* @param parsePos If not 0, on input a pointer to an integer specifying the offset at which
244* to begin parsing. If not 0, on output the offset at which parsing ended.
245* @param status A pointer to an UErrorCode to receive any errors
246* @return The value of the parsed date/time
247* @see udat_format
248* @stable ICU 2.0
249*/
250U_CAPI UDate U_EXPORT2
251udat_parse( const UDateFormat* format,
252 const UChar* text,
253 int32_t textLength,
254 int32_t *parsePos,
255 UErrorCode *status);
256
257/**
258* Parse a string into an date/time using a UDateFormat.
259* The date will be parsed using the conventions specified in \Ref{udat_open}
260* @param format The formatter to use.
261* @param calendar The calendar in which to store the parsed data.
262* @param text The text to parse.
263* @param textLength The length of text, or -1 if null-terminated.
264* @param parsePos If not 0, on input a pointer to an integer specifying the offset at which
265* to begin parsing. If not 0, on output the offset at which parsing ended.
266* @param status A pointer to an UErrorCode to receive any errors
267* @see udat_format
268* @stable ICU 2.0
269*/
270U_CAPI void U_EXPORT2
271udat_parseCalendar(const UDateFormat* format,
272 UCalendar* calendar,
273 const UChar* text,
274 int32_t textLength,
275 int32_t *parsePos,
276 UErrorCode *status);
277
278/**
279* Determine if an UDateFormat will perform lenient parsing.
280* With lenient parsing, the parser may use heuristics to interpret inputs that do not
281* precisely match the pattern. With strict parsing, inputs must match the pattern.
282* @param fmt The formatter to query
283* @return TRUE if fmt is set to perform lenient parsing, FALSE otherwise.
284* @see udat_setLenient
285* @stable ICU 2.0
286*/
287U_CAPI UBool U_EXPORT2
288udat_isLenient(const UDateFormat* fmt);
289
290/**
291* Specify whether an UDateFormat will perform lenient parsing.
292* With lenient parsing, the parser may use heuristics to interpret inputs that do not
293* precisely match the pattern. With strict parsing, inputs must match the pattern.
294* @param fmt The formatter to set
295* @param isLenient TRUE if fmt should perform lenient parsing, FALSE otherwise.
296* @see dat_isLenient
297* @stable ICU 2.0
298*/
299U_CAPI void U_EXPORT2
300udat_setLenient( UDateFormat* fmt,
301 UBool isLenient);
302
303/**
304* Get the UCalendar associated with an UDateFormat.
305* A UDateFormat uses a UCalendar to convert a raw value to, for example,
306* the day of the week.
307* @param fmt The formatter to query.
308* @return A pointer to the UCalendar used by fmt.
309* @see udat_setCalendar
310* @stable ICU 2.0
311*/
312U_CAPI const UCalendar* U_EXPORT2
313udat_getCalendar(const UDateFormat* fmt);
314
315/**
316* Set the UCalendar associated with an UDateFormat.
317* A UDateFormat uses a UCalendar to convert a raw value to, for example,
318* the day of the week.
319* @param fmt The formatter to set.
320* @param calendarToSet A pointer to an UCalendar to be used by fmt.
321* @see udat_setCalendar
322* @stable ICU 2.0
323*/
324U_CAPI void U_EXPORT2
325udat_setCalendar( UDateFormat* fmt,
326 const UCalendar* calendarToSet);
327
328/**
329* Get the UNumberFormat associated with an UDateFormat.
330* A UDateFormat uses a UNumberFormat to format numbers within a date,
331* for example the day number.
332* @param fmt The formatter to query.
333* @return A pointer to the UNumberFormat used by fmt to format numbers.
334* @see udat_setNumberFormat
335* @stable ICU 2.0
336*/
337U_CAPI const UNumberFormat* U_EXPORT2
338udat_getNumberFormat(const UDateFormat* fmt);
339
340/**
341* Set the UNumberFormat associated with an UDateFormat.
342* A UDateFormat uses a UNumberFormat to format numbers within a date,
343* for example the day number.
344* @param fmt The formatter to set.
345* @param numberFormatToSet A pointer to the UNumberFormat to be used by fmt to format numbers.
346* @see udat_getNumberFormat
347* @stable ICU 2.0
348*/
349U_CAPI void U_EXPORT2
350udat_setNumberFormat( UDateFormat* fmt,
351 const UNumberFormat* numberFormatToSet);
352
353/**
354* Get a locale for which date/time formatting patterns are available.
355* A UDateFormat in a locale returned by this function will perform the correct
356* formatting and parsing for the locale.
357* @param index The index of the desired locale.
358* @return A locale for which date/time formatting patterns are available, or 0 if none.
359* @see udat_countAvailable
360* @stable ICU 2.0
361*/
362U_CAPI const char* U_EXPORT2
363udat_getAvailable(int32_t index);
364
365/**
366* Determine how many locales have date/time formatting patterns available.
367* This function is most useful as determining the loop ending condition for
368* calls to \Ref{udat_getAvailable}.
369* @return The number of locales for which date/time formatting patterns are available.
370* @see udat_getAvailable
371* @stable ICU 2.0
372*/
373U_CAPI int32_t U_EXPORT2
374udat_countAvailable(void);
375
376/**
377* Get the year relative to which all 2-digit years are interpreted.
378* For example, if the 2-digit start year is 2100, the year 99 will be
379* interpreted as 2199.
380* @param fmt The formatter to query.
381* @param status A pointer to an UErrorCode to receive any errors
382* @return The year relative to which all 2-digit years are interpreted.
383* @see udat_Set2DigitYearStart
384* @stable ICU 2.0
385*/
386U_CAPI UDate U_EXPORT2
387udat_get2DigitYearStart( const UDateFormat *fmt,
388 UErrorCode *status);
389
390/**
391* Set the year relative to which all 2-digit years will be interpreted.
392* For example, if the 2-digit start year is 2100, the year 99 will be
393* interpreted as 2199.
394* @param fmt The formatter to set.
395* @param d The year relative to which all 2-digit years will be interpreted.
396* @param status A pointer to an UErrorCode to receive any errors
397* @see udat_Set2DigitYearStart
398* @stable ICU 2.0
399*/
400U_CAPI void U_EXPORT2
401udat_set2DigitYearStart( UDateFormat *fmt,
402 UDate d,
403 UErrorCode *status);
404
405/**
406* Extract the pattern from a UDateFormat.
407* The pattern will follow the pattern syntax rules.
408* @param fmt The formatter to query.
409* @param localized TRUE if the pattern should be localized, FALSE otherwise.
410* @param result A pointer to a buffer to receive the pattern.
411* @param resultLength The maximum size of result.
412* @param status A pointer to an UErrorCode to receive any errors
413* @return The total buffer size needed; if greater than resultLength, the output was truncated.
414* @see udat_applyPattern
415* @stable ICU 2.0
416*/
417U_CAPI int32_t U_EXPORT2
418udat_toPattern( const UDateFormat *fmt,
419 UBool localized,
420 UChar *result,
421 int32_t resultLength,
422 UErrorCode *status);
423
424/**
425* Set the pattern used by an UDateFormat.
426* The pattern should follow the pattern syntax rules.
427* @param format The formatter to set.
428* @param localized TRUE if the pattern is localized, FALSE otherwise.
429* @param pattern The new pattern
430* @param patternLength The length of pattern, or -1 if null-terminated.
431* @see udat_toPattern
432* @stable ICU 2.0
433*/
434U_CAPI void U_EXPORT2
435udat_applyPattern( UDateFormat *format,
436 UBool localized,
437 const UChar *pattern,
438 int32_t patternLength);
439
440/**
441 * The possible types of date format symbols
442 * @stable ICU 2.6
443 */
444typedef enum UDateFormatSymbolType {
445 /** The era names, for example AD */
446 UDAT_ERAS,
447 /** The month names, for example February */
448 UDAT_MONTHS,
449 /** The short month names, for example Feb. */
450 UDAT_SHORT_MONTHS,
451 /** The weekday names, for example Monday */
452 UDAT_WEEKDAYS,
453 /** The short weekday names, for example Mon. */
454 UDAT_SHORT_WEEKDAYS,
455 /** The AM/PM names, for example AM */
456 UDAT_AM_PMS,
457 /** The localized characters */
458 UDAT_LOCALIZED_CHARS
459} UDateFormatSymbolType;
460
461struct UDateFormatSymbols;
462/** Date format symbols.
463 * For usage in C programs.
464 * @stable ICU 2.6
465 */
466typedef struct UDateFormatSymbols UDateFormatSymbols;
467
468/**
469* Get the symbols associated with an UDateFormat.
470* The symbols are what a UDateFormat uses to represent locale-specific data,
471* for example month or day names.
472* @param fmt The formatter to query.
473* @param type The type of symbols to get. One of UDAT_ERAS, UDAT_MONTHS, UDAT_SHORT_MONTHS,
474* UDAT_WEEKDAYS, UDAT_SHORT_WEEKDAYS, UDAT_AM_PMS, or UDAT_LOCALIZED_CHARS
475* @param index The desired symbol of type type.
476* @param result A pointer to a buffer to receive the pattern.
477* @param resultLength The maximum size of result.
478* @param status A pointer to an UErrorCode to receive any errors
479* @return The total buffer size needed; if greater than resultLength, the output was truncated.
480* @see udat_countSymbols
481* @see udat_setSymbols
482* @stable ICU 2.0
483*/
484U_CAPI int32_t U_EXPORT2
485udat_getSymbols(const UDateFormat *fmt,
486 UDateFormatSymbolType type,
487 int32_t index,
488 UChar *result,
489 int32_t resultLength,
490 UErrorCode *status);
491
492/**
493* Count the number of particular symbols for an UDateFormat.
494* This function is most useful as for detemining the loop termination condition
495* for calls to \Ref{udat_getSymbols}.
496* @param fmt The formatter to query.
497* @param type The type of symbols to count. One of UDAT_ERAS, UDAT_MONTHS, UDAT_SHORT_MONTHS,
498* UDAT_WEEKDAYS, UDAT_SHORT_WEEKDAYS, UDAT_AM_PMS, or UDAT_LOCALIZED_CHARS
499* @return The number of symbols of type type.
500* @see udat_getSymbols
501* @see udat_setSymbols
502* @stable ICU 2.0
503*/
504U_CAPI int32_t U_EXPORT2
505udat_countSymbols( const UDateFormat *fmt,
506 UDateFormatSymbolType type);
507
508/**
509* Set the symbols associated with an UDateFormat.
510* The symbols are what a UDateFormat uses to represent locale-specific data,
511* for example month or day names.
512* @param format The formatter to set
513* @param type The type of symbols to set. One of UDAT_ERAS, UDAT_MONTHS, UDAT_SHORT_MONTHS,
514* UDAT_WEEKDAYS, UDAT_SHORT_WEEKDAYS, UDAT_AM_PMS, or UDAT_LOCALIZED_CHARS
515* @param index The index of the symbol to set of type type.
516* @param value The new value
517* @param valueLength The length of value, or -1 if null-terminated
518* @param status A pointer to an UErrorCode to receive any errors
519* @see udat_getSymbols
520* @see udat_countSymbols
521* @stable ICU 2.0
522*/
523U_CAPI void U_EXPORT2
524udat_setSymbols( UDateFormat *format,
525 UDateFormatSymbolType type,
526 int32_t index,
527 UChar *value,
528 int32_t valueLength,
529 UErrorCode *status);
530
531/********************* Obsolete API ************************************/
532/**
533 * TODO: Remove after Aug 2002
534 */
535#ifdef U_USE_DEPRECATED_FORMAT_API
536#if ((U_ICU_VERSION_MAJOR_NUM != 2) || (U_ICU_VERSION_MINOR_NUM != 2))
537# error "ICU version has changed. Please redefine the macros under U_USE_DEPRECATED_FORMAT_API pre-processor definition"
538#else
539 static UDateFormat*
540 udat_openPattern(const UChar* pattern,int32_t patternLength,const char* locale,UErrorCode *status)
541 {
542 return udat_open(UDAT_IGNORE,UDAT_IGNORE,locale,NULL,0,pattern,patternLength,status);
543 }
544
545# define udat_open_2_2(timeStyle,dateStyle,locale,tzId,tzIdLength,status) udat_open(timeStyle,dateStyle,locale,tzId,tzIdLength,NULL,0,status)
546#endif
547#endif
548/********************* End **********************************************/
549
550#endif /* #if !UCONFIG_NO_FORMATTING */
551
552#endif