2 *******************************************************************************
4 * Copyright (C) 2007-2015, 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 icu::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 #ifndef U_HIDE_INTERNAL_API
103 /** @internal ICU 4.4 */
104 UDATPG_MATCH_MINUTE_FIELD_LENGTH
= 1 << UDATPG_MINUTE_FIELD
,
105 /** @internal ICU 4.4 */
106 UDATPG_MATCH_SECOND_FIELD_LENGTH
= 1 << UDATPG_SECOND_FIELD
,
107 #endif /* U_HIDE_INTERNAL_API */
108 /** @stable ICU 4.4 */
109 UDATPG_MATCH_ALL_FIELDS_LENGTH
= (1 << UDATPG_FIELD_COUNT
) - 1,
110 /** @internal, Apple-specific for now */
111 UADATPG_FORCE_12_HOUR_CYCLE
= 1 << 29,
112 /** @internal, Apple-specific for now */
113 UADATPG_FORCE_24_HOUR_CYCLE
= 1 << 30,
114 /** @internal, Apple-specific for now */
115 UADATPG_FORCE_HOUR_CYCLE_MASK
= 3 << 29,
116 } UDateTimePatternMatchOptions
;
119 * Status return values from udatpg_addPattern().
122 typedef enum UDateTimePatternConflict
{
123 /** @stable ICU 3.8 */
125 /** @stable ICU 3.8 */
126 UDATPG_BASE_CONFLICT
,
127 /** @stable ICU 3.8 */
129 /** @stable ICU 3.8 */
130 UDATPG_CONFLICT_COUNT
131 } UDateTimePatternConflict
;
134 * Open a generator according to a given locale.
136 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
137 * failure before the function call.
138 * @return a pointer to UDateTimePatternGenerator.
141 U_STABLE UDateTimePatternGenerator
* U_EXPORT2
142 udatpg_open(const char *locale
, UErrorCode
*pErrorCode
);
145 * Open an empty generator, to be constructed with udatpg_addPattern(...) etc.
146 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
147 * failure before the function call.
148 * @return a pointer to UDateTimePatternGenerator.
151 U_STABLE UDateTimePatternGenerator
* U_EXPORT2
152 udatpg_openEmpty(UErrorCode
*pErrorCode
);
156 * @param dtpg a pointer to UDateTimePatternGenerator.
159 U_STABLE
void U_EXPORT2
160 udatpg_close(UDateTimePatternGenerator
*dtpg
);
162 #if U_SHOW_CPLUSPLUS_API
167 * \class LocalUDateTimePatternGeneratorPointer
168 * "Smart pointer" class, closes a UDateTimePatternGenerator via udatpg_close().
169 * For most methods see the LocalPointerBase base class.
171 * @see LocalPointerBase
175 U_DEFINE_LOCAL_OPEN_POINTER(LocalUDateTimePatternGeneratorPointer
, UDateTimePatternGenerator
, udatpg_close
);
182 * Create a copy pf a generator.
183 * @param dtpg a pointer to UDateTimePatternGenerator to be copied.
184 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
185 * failure before the function call.
186 * @return a pointer to a new UDateTimePatternGenerator.
189 U_STABLE UDateTimePatternGenerator
* U_EXPORT2
190 udatpg_clone(const UDateTimePatternGenerator
*dtpg
, UErrorCode
*pErrorCode
);
193 * Get the best pattern matching the input skeleton. It is guaranteed to
194 * have all of the fields in the skeleton.
196 * Note that this function uses a non-const UDateTimePatternGenerator:
197 * It uses a stateful pattern parser which is set up for each generator object,
198 * rather than creating one for each function call.
199 * Consecutive calls to this function do not affect each other,
200 * but this function cannot be used concurrently on a single generator object.
202 * @param dtpg a pointer to UDateTimePatternGenerator.
204 * The skeleton is a pattern containing only the variable fields.
205 * For example, "MMMdd" and "mmhh" are skeletons.
206 * @param length the length of skeleton
208 * The best pattern found from the given skeleton.
209 * @param capacity the capacity of bestPattern.
210 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
211 * failure before the function call.
212 * @return the length of bestPattern.
215 U_STABLE
int32_t U_EXPORT2
216 udatpg_getBestPattern(UDateTimePatternGenerator
*dtpg
,
217 const UChar
*skeleton
, int32_t length
,
218 UChar
*bestPattern
, int32_t capacity
,
219 UErrorCode
*pErrorCode
);
222 * Get the best pattern matching the input skeleton. It is guaranteed to
223 * have all of the fields in the skeleton.
225 * Note that this function uses a non-const UDateTimePatternGenerator:
226 * It uses a stateful pattern parser which is set up for each generator object,
227 * rather than creating one for each function call.
228 * Consecutive calls to this function do not affect each other,
229 * but this function cannot be used concurrently on a single generator object.
231 * @param dtpg a pointer to UDateTimePatternGenerator.
233 * The skeleton is a pattern containing only the variable fields.
234 * For example, "MMMdd" and "mmhh" are skeletons.
235 * @param length the length of skeleton
237 * Options for forcing the length of specified fields in the
238 * returned pattern to match those in the skeleton (when this
239 * would not happen otherwise). For default behavior, use
240 * UDATPG_MATCH_NO_OPTIONS.
242 * The best pattern found from the given skeleton.
244 * the capacity of bestPattern.
246 * a pointer to the UErrorCode which must not indicate a
247 * failure before the function call.
248 * @return the length of bestPattern.
251 U_STABLE
int32_t U_EXPORT2
252 udatpg_getBestPatternWithOptions(UDateTimePatternGenerator
*dtpg
,
253 const UChar
*skeleton
, int32_t length
,
254 UDateTimePatternMatchOptions options
,
255 UChar
*bestPattern
, int32_t capacity
,
256 UErrorCode
*pErrorCode
);
259 * Get a unique skeleton from a given pattern. For example,
260 * both "MMM-dd" and "dd/MMM" produce the skeleton "MMMdd".
262 * Note that this function uses a non-const UDateTimePatternGenerator:
263 * It uses a stateful pattern parser which is set up for each generator object,
264 * rather than creating one for each function call.
265 * Consecutive calls to this function do not affect each other,
266 * but this function cannot be used concurrently on a single generator object.
268 * @param dtpg a pointer to UDateTimePatternGenerator.
269 * @param pattern input pattern, such as "dd/MMM".
270 * @param length the length of pattern.
271 * @param skeleton such as "MMMdd"
272 * @param capacity the capacity of skeleton.
273 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
274 * failure before the function call.
275 * @return the length of skeleton.
278 U_STABLE
int32_t U_EXPORT2
279 udatpg_getSkeleton(UDateTimePatternGenerator
*dtpg
,
280 const UChar
*pattern
, int32_t length
,
281 UChar
*skeleton
, int32_t capacity
,
282 UErrorCode
*pErrorCode
);
285 * Get a unique base skeleton from a given pattern. This is the same
286 * as the skeleton, except that differences in length are minimized so
287 * as to only preserve the difference between string and numeric form. So
288 * for example, both "MMM-dd" and "d/MMM" produce the skeleton "MMMd"
289 * (notice the single d).
291 * Note that this function uses a non-const UDateTimePatternGenerator:
292 * It uses a stateful pattern parser which is set up for each generator object,
293 * rather than creating one for each function call.
294 * Consecutive calls to this function do not affect each other,
295 * but this function cannot be used concurrently on a single generator object.
297 * @param dtpg a pointer to UDateTimePatternGenerator.
298 * @param pattern input pattern, such as "dd/MMM".
299 * @param length the length of pattern.
300 * @param baseSkeleton such as "Md"
301 * @param capacity the capacity of base skeleton.
302 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
303 * failure before the function call.
304 * @return the length of baseSkeleton.
307 U_STABLE
int32_t U_EXPORT2
308 udatpg_getBaseSkeleton(UDateTimePatternGenerator
*dtpg
,
309 const UChar
*pattern
, int32_t length
,
310 UChar
*baseSkeleton
, int32_t capacity
,
311 UErrorCode
*pErrorCode
);
314 * Adds a pattern to the generator. If the pattern has the same skeleton as
315 * an existing pattern, and the override parameter is set, then the previous
316 * value is overriden. Otherwise, the previous value is retained. In either
317 * case, the conflicting status is set and previous vale is stored in
318 * conflicting pattern.
320 * Note that single-field patterns (like "MMM") are automatically added, and
321 * don't need to be added explicitly!
323 * @param dtpg a pointer to UDateTimePatternGenerator.
324 * @param pattern input pattern, such as "dd/MMM"
325 * @param patternLength the length of pattern.
326 * @param override When existing values are to be overridden use true,
327 * otherwise use false.
328 * @param conflictingPattern Previous pattern with the same skeleton.
329 * @param capacity the capacity of conflictingPattern.
330 * @param pLength a pointer to the length of conflictingPattern.
331 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
332 * failure before the function call.
333 * @return conflicting status. The value could be UDATPG_NO_CONFLICT,
334 * UDATPG_BASE_CONFLICT or UDATPG_CONFLICT.
337 U_STABLE UDateTimePatternConflict U_EXPORT2
338 udatpg_addPattern(UDateTimePatternGenerator
*dtpg
,
339 const UChar
*pattern
, int32_t patternLength
,
341 UChar
*conflictingPattern
, int32_t capacity
, int32_t *pLength
,
342 UErrorCode
*pErrorCode
);
345 * An AppendItem format is a pattern used to append a field if there is no
346 * good match. For example, suppose that the input skeleton is "GyyyyMMMd",
347 * and there is no matching pattern internally, but there is a pattern
348 * matching "yyyyMMMd", say "d-MM-yyyy". Then that pattern is used, plus the
349 * G. The way these two are conjoined is by using the AppendItemFormat for G
350 * (era). So if that value is, say "{0}, {1}" then the final resulting
351 * pattern is "d-MM-yyyy, G".
353 * There are actually three available variables: {0} is the pattern so far,
354 * {1} is the element we are adding, and {2} is the name of the element.
356 * This reflects the way that the CLDR data is organized.
358 * @param dtpg a pointer to UDateTimePatternGenerator.
359 * @param field UDateTimePatternField, such as UDATPG_ERA_FIELD
360 * @param value pattern, such as "{0}, {1}"
361 * @param length the length of value.
364 U_STABLE
void U_EXPORT2
365 udatpg_setAppendItemFormat(UDateTimePatternGenerator
*dtpg
,
366 UDateTimePatternField field
,
367 const UChar
*value
, int32_t length
);
370 * Getter corresponding to setAppendItemFormat. Values below 0 or at or
371 * above UDATPG_FIELD_COUNT are illegal arguments.
373 * @param dtpg A pointer to UDateTimePatternGenerator.
374 * @param field UDateTimePatternField, such as UDATPG_ERA_FIELD
375 * @param pLength A pointer that will receive the length of appendItemFormat.
376 * @return appendItemFormat for field.
379 U_STABLE
const UChar
* U_EXPORT2
380 udatpg_getAppendItemFormat(const UDateTimePatternGenerator
*dtpg
,
381 UDateTimePatternField field
,
385 * Set the name of field, eg "era" in English for ERA. These are only
386 * used if the corresponding AppendItemFormat is used, and if it contains a
389 * This reflects the way that the CLDR data is organized.
391 * @param dtpg a pointer to UDateTimePatternGenerator.
392 * @param field UDateTimePatternField
393 * @param value name for the field.
394 * @param length the length of value.
397 U_STABLE
void U_EXPORT2
398 udatpg_setAppendItemName(UDateTimePatternGenerator
*dtpg
,
399 UDateTimePatternField field
,
400 const UChar
*value
, int32_t length
);
403 * Getter corresponding to setAppendItemNames. Values below 0 or at or above
404 * UDATPG_FIELD_COUNT are illegal arguments.
406 * @param dtpg a pointer to UDateTimePatternGenerator.
407 * @param field UDateTimePatternField, such as UDATPG_ERA_FIELD
408 * @param pLength A pointer that will receive the length of the name for field.
409 * @return name for field
412 U_STABLE
const UChar
* U_EXPORT2
413 udatpg_getAppendItemName(const UDateTimePatternGenerator
*dtpg
,
414 UDateTimePatternField field
,
418 * The DateTimeFormat is a message format pattern used to compose date and
419 * time patterns. The default pattern in the root locale is "{1} {0}", where
420 * {1} will be replaced by the date pattern and {0} will be replaced by the
421 * time pattern; however, other locales may specify patterns such as
422 * "{1}, {0}" or "{1} 'at' {0}", etc.
424 * This is used when the input skeleton contains both date and time fields,
425 * but there is not a close match among the added patterns. For example,
426 * suppose that this object was created by adding "dd-MMM" and "hh:mm", and
427 * its DateTimeFormat is the default "{1} {0}". Then if the input skeleton
428 * is "MMMdhmm", there is not an exact match, so the input skeleton is
429 * broken up into two components "MMMd" and "hmm". There are close matches
430 * for those two skeletons, so the result is put together with this pattern,
431 * resulting in "d-MMM h:mm".
433 * @param dtpg a pointer to UDateTimePatternGenerator.
435 * message format pattern, here {1} will be replaced by the date
436 * pattern and {0} will be replaced by the time pattern.
437 * @param length the length of dtFormat.
440 U_STABLE
void U_EXPORT2
441 udatpg_setDateTimeFormat(const UDateTimePatternGenerator
*dtpg
,
442 const UChar
*dtFormat
, int32_t length
);
445 * Getter corresponding to setDateTimeFormat.
446 * @param dtpg a pointer to UDateTimePatternGenerator.
447 * @param pLength A pointer that will receive the length of the format
448 * @return dateTimeFormat.
451 U_STABLE
const UChar
* U_EXPORT2
452 udatpg_getDateTimeFormat(const UDateTimePatternGenerator
*dtpg
,
456 * The decimal value is used in formatting fractions of seconds. If the
457 * skeleton contains fractional seconds, then this is used with the
458 * fractional seconds. For example, suppose that the input pattern is
459 * "hhmmssSSSS", and the best matching pattern internally is "H:mm:ss", and
460 * the decimal string is ",". Then the resulting pattern is modified to be
463 * @param dtpg a pointer to UDateTimePatternGenerator.
465 * @param length the length of decimal.
468 U_STABLE
void U_EXPORT2
469 udatpg_setDecimal(UDateTimePatternGenerator
*dtpg
,
470 const UChar
*decimal
, int32_t length
);
473 * Getter corresponding to setDecimal.
475 * @param dtpg a pointer to UDateTimePatternGenerator.
476 * @param pLength A pointer that will receive the length of the decimal string.
477 * @return corresponding to the decimal point.
480 U_STABLE
const UChar
* U_EXPORT2
481 udatpg_getDecimal(const UDateTimePatternGenerator
*dtpg
,
485 * Adjusts the field types (width and subtype) of a pattern to match what is
486 * in a skeleton. That is, if you supply a pattern like "d-M H:m", and a
487 * skeleton of "MMMMddhhmm", then the input pattern is adjusted to be
488 * "dd-MMMM hh:mm". This is used internally to get the best match for the
489 * input skeleton, but can also be used externally.
491 * Note that this function uses a non-const UDateTimePatternGenerator:
492 * It uses a stateful pattern parser which is set up for each generator object,
493 * rather than creating one for each function call.
494 * Consecutive calls to this function do not affect each other,
495 * but this function cannot be used concurrently on a single generator object.
497 * @param dtpg a pointer to UDateTimePatternGenerator.
498 * @param pattern Input pattern
499 * @param patternLength the length of input pattern.
501 * @param skeletonLength the length of input skeleton.
502 * @param dest pattern adjusted to match the skeleton fields widths and subtypes.
503 * @param destCapacity the capacity of dest.
504 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
505 * failure before the function call.
506 * @return the length of dest.
509 U_STABLE
int32_t U_EXPORT2
510 udatpg_replaceFieldTypes(UDateTimePatternGenerator
*dtpg
,
511 const UChar
*pattern
, int32_t patternLength
,
512 const UChar
*skeleton
, int32_t skeletonLength
,
513 UChar
*dest
, int32_t destCapacity
,
514 UErrorCode
*pErrorCode
);
517 * Adjusts the field types (width and subtype) of a pattern to match what is
518 * in a skeleton. That is, if you supply a pattern like "d-M H:m", and a
519 * skeleton of "MMMMddhhmm", then the input pattern is adjusted to be
520 * "dd-MMMM hh:mm". This is used internally to get the best match for the
521 * input skeleton, but can also be used externally.
523 * Note that this function uses a non-const UDateTimePatternGenerator:
524 * It uses a stateful pattern parser which is set up for each generator object,
525 * rather than creating one for each function call.
526 * Consecutive calls to this function do not affect each other,
527 * but this function cannot be used concurrently on a single generator object.
529 * @param dtpg a pointer to UDateTimePatternGenerator.
530 * @param pattern Input pattern
531 * @param patternLength the length of input pattern.
533 * @param skeletonLength the length of input skeleton.
535 * Options controlling whether the length of specified fields in the
536 * pattern are adjusted to match those in the skeleton (when this
537 * would not happen otherwise). For default behavior, use
538 * UDATPG_MATCH_NO_OPTIONS.
539 * @param dest pattern adjusted to match the skeleton fields widths and subtypes.
540 * @param destCapacity the capacity of dest.
541 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
542 * failure before the function call.
543 * @return the length of dest.
546 U_STABLE
int32_t U_EXPORT2
547 udatpg_replaceFieldTypesWithOptions(UDateTimePatternGenerator
*dtpg
,
548 const UChar
*pattern
, int32_t patternLength
,
549 const UChar
*skeleton
, int32_t skeletonLength
,
550 UDateTimePatternMatchOptions options
,
551 UChar
*dest
, int32_t destCapacity
,
552 UErrorCode
*pErrorCode
);
555 * Return a UEnumeration list of all the skeletons in canonical form.
556 * Call udatpg_getPatternForSkeleton() to get the corresponding pattern.
558 * @param dtpg a pointer to UDateTimePatternGenerator.
559 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
560 * failure before the function call
561 * @return a UEnumeration list of all the skeletons
562 * The caller must close the object.
565 U_STABLE UEnumeration
* U_EXPORT2
566 udatpg_openSkeletons(const UDateTimePatternGenerator
*dtpg
, UErrorCode
*pErrorCode
);
569 * Return a UEnumeration list of all the base skeletons in canonical form.
571 * @param dtpg a pointer to UDateTimePatternGenerator.
572 * @param pErrorCode a pointer to the UErrorCode which must not indicate a
573 * failure before the function call.
574 * @return a UEnumeration list of all the base skeletons
575 * The caller must close the object.
578 U_STABLE UEnumeration
* U_EXPORT2
579 udatpg_openBaseSkeletons(const UDateTimePatternGenerator
*dtpg
, UErrorCode
*pErrorCode
);
582 * Get the pattern corresponding to a given skeleton.
584 * @param dtpg a pointer to UDateTimePatternGenerator.
586 * @param skeletonLength pointer to the length of skeleton.
587 * @param pLength pointer to the length of return pattern.
588 * @return pattern corresponding to a given skeleton.
591 U_STABLE
const UChar
* U_EXPORT2
592 udatpg_getPatternForSkeleton(const UDateTimePatternGenerator
*dtpg
,
593 const UChar
*skeleton
, int32_t skeletonLength
,
597 * Remap a pattern per the options (Apple-specific for now).
598 * Currently this will only remap the time to force an alternate time
599 * cycle (12-hour instead of 24-hour or vice versa), handling updating
600 * the pattern characters, insertion/removal of AM/PM marker, etc. in
601 * a locale-appropriate way. It calls udatpg_getBestPatternWithOptions
602 * as part of updating the time format.
604 * @param dtpg a pointer to UDateTimePatternGenerator.
606 * The pattern to remap.
607 * @param patternLength
608 * The length of the pattern (may be -1 to indicate that it
609 * is zero-terminated).
611 * Options for forcing the hour cycle and for forcing the
612 * length of specified fields in the
613 * returned pattern to match those in the skeleton (when this
614 * would not happen otherwise). For default behavior, use
615 * UDATPG_MATCH_NO_OPTIONS.
617 * The remapped pattern.
618 * @param newPatternCapacity
619 * The capacity of newPattern.
621 * A pointer to the UErrorCode. If at entry it indicates a
622 * failure, the call will return immediately.
624 * The length of newPattern. If this is greater than
625 * newPatternCapacity an error will be set and the contents of
626 * newPattern are undefined.
629 U_INTERNAL
int32_t U_EXPORT2
630 uadatpg_remapPatternWithOptions(UDateTimePatternGenerator
*dtpg
,
631 const UChar
*pattern
, int32_t patternLength
,
632 UDateTimePatternMatchOptions options
,
633 UChar
*newPattern
, int32_t newPatternCapacity
,
634 UErrorCode
*pErrorCode
);