2 *******************************************************************************
4 * Copyright (C) 2007-2010, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
10 * tab size: 8 (not used)
13 * created on: 2007jul30
14 * created by: Markus W. Scherer
20 #include "unicode/utypes.h"
21 #include "unicode/uenum.h"
22 #include "unicode/localpointer.h"
26 * \brief C API: Wrapper for DateTimePatternGenerator (unicode/dtptngen.h).
28 * UDateTimePatternGenerator provides flexible generation of date format patterns,
29 * like "yy-MM-dd". The user can build up the generator by adding successive
30 * patterns. Once that is done, a query can be made using a "skeleton", which is
31 * a pattern which just includes the desired fields and lengths. The generator
32 * will return the "best fit" pattern corresponding to that skeleton.
33 * <p>The main method people will use is udatpg_getBestPattern, since normally
34 * UDateTimePatternGenerator is pre-built with data from a particular locale.
35 * However, generators can be built directly from other data as well.
36 * <p><i>Issue: may be useful to also have a function that returns the list of
37 * fields in a pattern, in order, since we have that internally.
38 * That would be useful for getting the UI order of field elements.</i>
42 * Opaque type for a date/time pattern generator object.
45 typedef void *UDateTimePatternGenerator
;
48 * Field number constants for udatpg_getAppendItemFormats() and similar functions.
49 * These constants are separate from UDateFormatField despite semantic overlap
50 * because some fields are merged for the date/time pattern generator.
53 typedef enum UDateTimePatternField
{
54 /** @stable ICU 3.8 */
56 /** @stable ICU 3.8 */
58 /** @stable ICU 3.8 */
60 /** @stable ICU 3.8 */
62 /** @stable ICU 3.8 */
63 UDATPG_WEEK_OF_YEAR_FIELD
,
64 /** @stable ICU 3.8 */
65 UDATPG_WEEK_OF_MONTH_FIELD
,
66 /** @stable ICU 3.8 */
68 /** @stable ICU 3.8 */
69 UDATPG_DAY_OF_YEAR_FIELD
,
70 /** @stable ICU 3.8 */
71 UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD
,
72 /** @stable ICU 3.8 */
74 /** @stable ICU 3.8 */
75 UDATPG_DAYPERIOD_FIELD
,
76 /** @stable ICU 3.8 */
78 /** @stable ICU 3.8 */
80 /** @stable ICU 3.8 */
82 /** @stable ICU 3.8 */
83 UDATPG_FRACTIONAL_SECOND_FIELD
,
84 /** @stable ICU 3.8 */
86 /** @stable ICU 3.8 */
88 } UDateTimePatternField
;
91 * Masks to control forcing the length of specified fields in the returned
92 * pattern to match those in the skeleton (when this would not happen
93 * otherwise). These may be combined to force the length of multiple fields.
94 * Used with udatpg_getBestPatternWithOptions, udatpg_replaceFieldTypesWithOptions.
97 typedef enum UDateTimePatternMatchOptions
{
98 /** @stable ICU 4.4 */
99 UDATPG_MATCH_NO_OPTIONS
= 0,
100 /** @stable ICU 4.4 */
101 UDATPG_MATCH_HOUR_FIELD_LENGTH
= 1 << UDATPG_HOUR_FIELD
,
102 /** @internal ICU 4.4 */
103 UDATPG_MATCH_MINUTE_FIELD_LENGTH
= 1 << UDATPG_MINUTE_FIELD
,
104 /** @internal ICU 4.4 */
105 UDATPG_MATCH_SECOND_FIELD_LENGTH
= 1 << UDATPG_SECOND_FIELD
,
106 /** @stable ICU 4.4 */
107 UDATPG_MATCH_ALL_FIELDS_LENGTH
= (1 << UDATPG_FIELD_COUNT
) - 1
108 } UDateTimePatternMatchOptions
;
111 * Status return values from udatpg_addPattern().
114 typedef enum UDateTimePatternConflict
{
115 /** @stable ICU 3.8 */
117 /** @stable ICU 3.8 */
118 UDATPG_BASE_CONFLICT
,
119 /** @stable ICU 3.8 */
121 /** @stable ICU 3.8 */
122 UDATPG_CONFLICT_COUNT
123 } UDateTimePatternConflict
;
126 * Open a generator according to a given locale.
128 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
129 * failure before the function call.
130 * @return a pointer to UDateTimePatternGenerator.
133 U_STABLE UDateTimePatternGenerator
* U_EXPORT2
134 udatpg_open(const char *locale
, UErrorCode
*pErrorCode
);
137 * Open an empty generator, to be constructed with udatpg_addPattern(...) etc.
138 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
139 * failure before the function call.
140 * @return a pointer to UDateTimePatternGenerator.
143 U_STABLE UDateTimePatternGenerator
* U_EXPORT2
144 udatpg_openEmpty(UErrorCode
*pErrorCode
);
148 * @param dtpg a pointer to UDateTimePatternGenerator.
151 U_STABLE
void U_EXPORT2
152 udatpg_close(UDateTimePatternGenerator
*dtpg
);
154 #if U_SHOW_CPLUSPLUS_API
159 * \class LocalUDateTimePatternGeneratorPointer
160 * "Smart pointer" class, closes a UDateTimePatternGenerator via udatpg_close().
161 * For most methods see the LocalPointerBase base class.
163 * @see LocalPointerBase
167 U_DEFINE_LOCAL_OPEN_POINTER(LocalUDateTimePatternGeneratorPointer
, UDateTimePatternGenerator
, udatpg_close
);
174 * Create a copy pf a generator.
175 * @param dtpg a pointer to UDateTimePatternGenerator to be copied.
176 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
177 * failure before the function call.
178 * @return a pointer to a new UDateTimePatternGenerator.
181 U_STABLE UDateTimePatternGenerator
* U_EXPORT2
182 udatpg_clone(const UDateTimePatternGenerator
*dtpg
, UErrorCode
*pErrorCode
);
185 * Get the best pattern matching the input skeleton. It is guaranteed to
186 * have all of the fields in the skeleton.
188 * Note that this function uses a non-const UDateTimePatternGenerator:
189 * It uses a stateful pattern parser which is set up for each generator object,
190 * rather than creating one for each function call.
191 * Consecutive calls to this function do not affect each other,
192 * but this function cannot be used concurrently on a single generator object.
194 * @param dtpg a pointer to UDateTimePatternGenerator.
196 * The skeleton is a pattern containing only the variable fields.
197 * For example, "MMMdd" and "mmhh" are skeletons.
198 * @param length the length of skeleton
200 * The best pattern found from the given skeleton.
201 * @param capacity the capacity of bestPattern.
202 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
203 * failure before the function call.
204 * @return the length of bestPattern.
207 U_STABLE
int32_t U_EXPORT2
208 udatpg_getBestPattern(UDateTimePatternGenerator
*dtpg
,
209 const UChar
*skeleton
, int32_t length
,
210 UChar
*bestPattern
, int32_t capacity
,
211 UErrorCode
*pErrorCode
);
214 * Get the best pattern matching the input skeleton. It is guaranteed to
215 * have all of the fields in the skeleton.
217 * Note that this function uses a non-const UDateTimePatternGenerator:
218 * It uses a stateful pattern parser which is set up for each generator object,
219 * rather than creating one for each function call.
220 * Consecutive calls to this function do not affect each other,
221 * but this function cannot be used concurrently on a single generator object.
223 * @param dtpg a pointer to UDateTimePatternGenerator.
225 * The skeleton is a pattern containing only the variable fields.
226 * For example, "MMMdd" and "mmhh" are skeletons.
227 * @param length the length of skeleton
229 * Options for forcing the length of specified fields in the
230 * returned pattern to match those in the skeleton (when this
231 * would not happen otherwise). For default behavior, use
232 * UDATPG_MATCH_NO_OPTIONS.
234 * The best pattern found from the given skeleton.
236 * the capacity of bestPattern.
238 * a pointer to the UErrorCode which must not indicate a
239 * failure before the function call.
240 * @return the length of bestPattern.
243 U_STABLE
int32_t U_EXPORT2
244 udatpg_getBestPatternWithOptions(UDateTimePatternGenerator
*dtpg
,
245 const UChar
*skeleton
, int32_t length
,
246 UDateTimePatternMatchOptions options
,
247 UChar
*bestPattern
, int32_t capacity
,
248 UErrorCode
*pErrorCode
);
251 * Get a unique skeleton from a given pattern. For example,
252 * both "MMM-dd" and "dd/MMM" produce the skeleton "MMMdd".
254 * Note that this function uses a non-const UDateTimePatternGenerator:
255 * It uses a stateful pattern parser which is set up for each generator object,
256 * rather than creating one for each function call.
257 * Consecutive calls to this function do not affect each other,
258 * but this function cannot be used concurrently on a single generator object.
260 * @param dtpg a pointer to UDateTimePatternGenerator.
261 * @param pattern input pattern, such as "dd/MMM".
262 * @param length the length of pattern.
263 * @param skeleton such as "MMMdd"
264 * @param capacity the capacity of skeleton.
265 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
266 * failure before the function call.
267 * @return the length of skeleton.
270 U_STABLE
int32_t U_EXPORT2
271 udatpg_getSkeleton(UDateTimePatternGenerator
*dtpg
,
272 const UChar
*pattern
, int32_t length
,
273 UChar
*skeleton
, int32_t capacity
,
274 UErrorCode
*pErrorCode
);
277 * Get a unique base skeleton from a given pattern. This is the same
278 * as the skeleton, except that differences in length are minimized so
279 * as to only preserve the difference between string and numeric form. So
280 * for example, both "MMM-dd" and "d/MMM" produce the skeleton "MMMd"
281 * (notice the single d).
283 * Note that this function uses a non-const UDateTimePatternGenerator:
284 * It uses a stateful pattern parser which is set up for each generator object,
285 * rather than creating one for each function call.
286 * Consecutive calls to this function do not affect each other,
287 * but this function cannot be used concurrently on a single generator object.
289 * @param dtpg a pointer to UDateTimePatternGenerator.
290 * @param pattern input pattern, such as "dd/MMM".
291 * @param length the length of pattern.
292 * @param baseSkeleton such as "Md"
293 * @param capacity the capacity of base skeleton.
294 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
295 * failure before the function call.
296 * @return the length of baseSkeleton.
299 U_STABLE
int32_t U_EXPORT2
300 udatpg_getBaseSkeleton(UDateTimePatternGenerator
*dtpg
,
301 const UChar
*pattern
, int32_t length
,
302 UChar
*baseSkeleton
, int32_t capacity
,
303 UErrorCode
*pErrorCode
);
306 * Adds a pattern to the generator. If the pattern has the same skeleton as
307 * an existing pattern, and the override parameter is set, then the previous
308 * value is overriden. Otherwise, the previous value is retained. In either
309 * case, the conflicting status is set and previous vale is stored in
310 * conflicting pattern.
312 * Note that single-field patterns (like "MMM") are automatically added, and
313 * don't need to be added explicitly!
315 * @param dtpg a pointer to UDateTimePatternGenerator.
316 * @param pattern input pattern, such as "dd/MMM"
317 * @param patternLength the length of pattern.
318 * @param override When existing values are to be overridden use true,
319 * otherwise use false.
320 * @param conflictingPattern Previous pattern with the same skeleton.
321 * @param capacity the capacity of conflictingPattern.
322 * @param pLength a pointer to the length of conflictingPattern.
323 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
324 * failure before the function call.
325 * @return conflicting status. The value could be UDATPG_NO_CONFLICT,
326 * UDATPG_BASE_CONFLICT or UDATPG_CONFLICT.
329 U_STABLE UDateTimePatternConflict U_EXPORT2
330 udatpg_addPattern(UDateTimePatternGenerator
*dtpg
,
331 const UChar
*pattern
, int32_t patternLength
,
333 UChar
*conflictingPattern
, int32_t capacity
, int32_t *pLength
,
334 UErrorCode
*pErrorCode
);
337 * An AppendItem format is a pattern used to append a field if there is no
338 * good match. For example, suppose that the input skeleton is "GyyyyMMMd",
339 * and there is no matching pattern internally, but there is a pattern
340 * matching "yyyyMMMd", say "d-MM-yyyy". Then that pattern is used, plus the
341 * G. The way these two are conjoined is by using the AppendItemFormat for G
342 * (era). So if that value is, say "{0}, {1}" then the final resulting
343 * pattern is "d-MM-yyyy, G".
345 * There are actually three available variables: {0} is the pattern so far,
346 * {1} is the element we are adding, and {2} is the name of the element.
348 * This reflects the way that the CLDR data is organized.
350 * @param dtpg a pointer to UDateTimePatternGenerator.
351 * @param field UDateTimePatternField, such as UDATPG_ERA_FIELD
352 * @param value pattern, such as "{0}, {1}"
353 * @param length the length of value.
356 U_STABLE
void U_EXPORT2
357 udatpg_setAppendItemFormat(UDateTimePatternGenerator
*dtpg
,
358 UDateTimePatternField field
,
359 const UChar
*value
, int32_t length
);
362 * Getter corresponding to setAppendItemFormat. Values below 0 or at or
363 * above UDATPG_FIELD_COUNT are illegal arguments.
365 * @param dtpg A pointer to UDateTimePatternGenerator.
366 * @param field UDateTimePatternField, such as UDATPG_ERA_FIELD
367 * @param pLength A pointer that will receive the length of appendItemFormat.
368 * @return appendItemFormat for field.
371 U_STABLE
const UChar
* U_EXPORT2
372 udatpg_getAppendItemFormat(const UDateTimePatternGenerator
*dtpg
,
373 UDateTimePatternField field
,
377 * Set the name of field, eg "era" in English for ERA. These are only
378 * used if the corresponding AppendItemFormat is used, and if it contains a
381 * This reflects the way that the CLDR data is organized.
383 * @param dtpg a pointer to UDateTimePatternGenerator.
384 * @param field UDateTimePatternField
385 * @param value name for the field.
386 * @param length the length of value.
389 U_STABLE
void U_EXPORT2
390 udatpg_setAppendItemName(UDateTimePatternGenerator
*dtpg
,
391 UDateTimePatternField field
,
392 const UChar
*value
, int32_t length
);
395 * Getter corresponding to setAppendItemNames. Values below 0 or at or above
396 * UDATPG_FIELD_COUNT are illegal arguments.
398 * @param dtpg a pointer to UDateTimePatternGenerator.
399 * @param field UDateTimePatternField, such as UDATPG_ERA_FIELD
400 * @param pLength A pointer that will receive the length of the name for field.
401 * @return name for field
404 U_STABLE
const UChar
* U_EXPORT2
405 udatpg_getAppendItemName(const UDateTimePatternGenerator
*dtpg
,
406 UDateTimePatternField field
,
410 * The date time format is a message format pattern used to compose date and
411 * time patterns. The default value is "{0} {1}", where {0} will be replaced
412 * by the date pattern and {1} will be replaced by the time pattern.
414 * This is used when the input skeleton contains both date and time fields,
415 * but there is not a close match among the added patterns. For example,
416 * suppose that this object was created by adding "dd-MMM" and "hh:mm", and
417 * its datetimeFormat is the default "{0} {1}". Then if the input skeleton
418 * is "MMMdhmm", there is not an exact match, so the input skeleton is
419 * broken up into two components "MMMd" and "hmm". There are close matches
420 * for those two skeletons, so the result is put together with this pattern,
421 * resulting in "d-MMM h:mm".
423 * @param dtpg a pointer to UDateTimePatternGenerator.
425 * message format pattern, here {0} will be replaced by the date
426 * pattern and {1} will be replaced by the time pattern.
427 * @param length the length of dtFormat.
430 U_STABLE
void U_EXPORT2
431 udatpg_setDateTimeFormat(const UDateTimePatternGenerator
*dtpg
,
432 const UChar
*dtFormat
, int32_t length
);
435 * Getter corresponding to setDateTimeFormat.
436 * @param dtpg a pointer to UDateTimePatternGenerator.
437 * @param pLength A pointer that will receive the length of the format
438 * @return dateTimeFormat.
441 U_STABLE
const UChar
* U_EXPORT2
442 udatpg_getDateTimeFormat(const UDateTimePatternGenerator
*dtpg
,
446 * The decimal value is used in formatting fractions of seconds. If the
447 * skeleton contains fractional seconds, then this is used with the
448 * fractional seconds. For example, suppose that the input pattern is
449 * "hhmmssSSSS", and the best matching pattern internally is "H:mm:ss", and
450 * the decimal string is ",". Then the resulting pattern is modified to be
453 * @param dtpg a pointer to UDateTimePatternGenerator.
455 * @param length the length of decimal.
458 U_STABLE
void U_EXPORT2
459 udatpg_setDecimal(UDateTimePatternGenerator
*dtpg
,
460 const UChar
*decimal
, int32_t length
);
463 * Getter corresponding to setDecimal.
465 * @param dtpg a pointer to UDateTimePatternGenerator.
466 * @param pLength A pointer that will receive the length of the decimal string.
467 * @return corresponding to the decimal point.
470 U_STABLE
const UChar
* U_EXPORT2
471 udatpg_getDecimal(const UDateTimePatternGenerator
*dtpg
,
475 * Adjusts the field types (width and subtype) of a pattern to match what is
476 * in a skeleton. That is, if you supply a pattern like "d-M H:m", and a
477 * skeleton of "MMMMddhhmm", then the input pattern is adjusted to be
478 * "dd-MMMM hh:mm". This is used internally to get the best match for the
479 * input skeleton, but can also be used externally.
481 * Note that this function uses a non-const UDateTimePatternGenerator:
482 * It uses a stateful pattern parser which is set up for each generator object,
483 * rather than creating one for each function call.
484 * Consecutive calls to this function do not affect each other,
485 * but this function cannot be used concurrently on a single generator object.
487 * @param dtpg a pointer to UDateTimePatternGenerator.
488 * @param pattern Input pattern
489 * @param patternLength the length of input pattern.
491 * @param skeletonLength the length of input skeleton.
492 * @param dest pattern adjusted to match the skeleton fields widths and subtypes.
493 * @param destCapacity the capacity of dest.
494 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
495 * failure before the function call.
496 * @return the length of dest.
499 U_STABLE
int32_t U_EXPORT2
500 udatpg_replaceFieldTypes(UDateTimePatternGenerator
*dtpg
,
501 const UChar
*pattern
, int32_t patternLength
,
502 const UChar
*skeleton
, int32_t skeletonLength
,
503 UChar
*dest
, int32_t destCapacity
,
504 UErrorCode
*pErrorCode
);
507 * Adjusts the field types (width and subtype) of a pattern to match what is
508 * in a skeleton. That is, if you supply a pattern like "d-M H:m", and a
509 * skeleton of "MMMMddhhmm", then the input pattern is adjusted to be
510 * "dd-MMMM hh:mm". This is used internally to get the best match for the
511 * input skeleton, but can also be used externally.
513 * Note that this function uses a non-const UDateTimePatternGenerator:
514 * It uses a stateful pattern parser which is set up for each generator object,
515 * rather than creating one for each function call.
516 * Consecutive calls to this function do not affect each other,
517 * but this function cannot be used concurrently on a single generator object.
519 * @param dtpg a pointer to UDateTimePatternGenerator.
520 * @param pattern Input pattern
521 * @param patternLength the length of input pattern.
523 * @param skeletonLength the length of input skeleton.
525 * Options controlling whether the length of specified fields in the
526 * pattern are adjusted to match those in the skeleton (when this
527 * would not happen otherwise). For default behavior, use
528 * UDATPG_MATCH_NO_OPTIONS.
529 * @param dest pattern adjusted to match the skeleton fields widths and subtypes.
530 * @param destCapacity the capacity of dest.
531 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
532 * failure before the function call.
533 * @return the length of dest.
536 U_STABLE
int32_t U_EXPORT2
537 udatpg_replaceFieldTypesWithOptions(UDateTimePatternGenerator
*dtpg
,
538 const UChar
*pattern
, int32_t patternLength
,
539 const UChar
*skeleton
, int32_t skeletonLength
,
540 UDateTimePatternMatchOptions options
,
541 UChar
*dest
, int32_t destCapacity
,
542 UErrorCode
*pErrorCode
);
545 * Return a UEnumeration list of all the skeletons in canonical form.
546 * Call udatpg_getPatternForSkeleton() to get the corresponding pattern.
548 * @param dtpg a pointer to UDateTimePatternGenerator.
549 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
550 * failure before the function call
551 * @return a UEnumeration list of all the skeletons
552 * The caller must close the object.
555 U_STABLE UEnumeration
* U_EXPORT2
556 udatpg_openSkeletons(const UDateTimePatternGenerator
*dtpg
, UErrorCode
*pErrorCode
);
559 * Return a UEnumeration list of all the base skeletons in canonical form.
561 * @param dtpg a pointer to UDateTimePatternGenerator.
562 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
563 * failure before the function call.
564 * @return a UEnumeration list of all the base skeletons
565 * The caller must close the object.
568 U_STABLE UEnumeration
* U_EXPORT2
569 udatpg_openBaseSkeletons(const UDateTimePatternGenerator
*dtpg
, UErrorCode
*pErrorCode
);
572 * Get the pattern corresponding to a given skeleton.
574 * @param dtpg a pointer to UDateTimePatternGenerator.
576 * @param skeletonLength pointer to the length of skeleton.
577 * @param pLength pointer to the length of return pattern.
578 * @return pattern corresponding to a given skeleton.
581 U_STABLE
const UChar
* U_EXPORT2
582 udatpg_getPatternForSkeleton(const UDateTimePatternGenerator
*dtpg
,
583 const UChar
*skeleton
, int32_t skeletonLength
,