• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *******************************************************************************
3 *
4 *   Copyright (C) 2007, International Business Machines
5 *   Corporation and others.  All Rights Reserved.
6 *
7 *******************************************************************************
8 *   file name:  udatpg.h
9 *   encoding:   US-ASCII
10 *   tab size:   8 (not used)
11 *   indentation:4
12 *
13 *   created on: 2007jul30
14 *   created by: Markus W. Scherer
15 */
16 
17 #ifndef __UDATPG_H__
18 #define __UDATPG_H__
19 
20 #include "unicode/utypes.h"
21 #include "unicode/uenum.h"
22 
23 /**
24  * \file
25  * \brief C API: Wrapper for DateTimePatternGenerator (unicode/dtptngen.h).
26  *
27  * UDateTimePatternGenerator provides flexible generation of date format patterns,
28  * like "yy-MM-dd". The user can build up the generator by adding successive
29  * patterns. Once that is done, a query can be made using a "skeleton", which is
30  * a pattern which just includes the desired fields and lengths. The generator
31  * will return the "best fit" pattern corresponding to that skeleton.
32  * <p>The main method people will use is udatpg_getBestPattern, since normally
33  * UDateTimePatternGenerator is pre-built with data from a particular locale.
34  * However, generators can be built directly from other data as well.
35  * <p><i>Issue: may be useful to also have a function that returns the list of
36  * fields in a pattern, in order, since we have that internally.
37  * That would be useful for getting the UI order of field elements.</i>
38  */
39 
40 /**
41  * Opaque type for a date/time pattern generator object.
42  * @draft ICU 3.8
43  */
44 typedef void *UDateTimePatternGenerator;
45 
46 #ifndef U_HIDE_DRAFT_API
47 
48 /**
49  * Field number constants for udatpg_getAppendItemFormats() and similar functions.
50  * These constants are separate from UDateFormatField despite semantic overlap
51  * because some fields are merged for the date/time pattern generator.
52  * @draft ICU 3.8
53  */
54 typedef enum UDateTimePatternField {
55     /** @draft ICU 3.8 */
56     UDATPG_ERA_FIELD,
57     /** @draft ICU 3.8 */
58     UDATPG_YEAR_FIELD,
59     /** @draft ICU 3.8 */
60     UDATPG_QUARTER_FIELD,
61     /** @draft ICU 3.8 */
62     UDATPG_MONTH_FIELD,
63     /** @draft ICU 3.8 */
64     UDATPG_WEEK_OF_YEAR_FIELD,
65     /** @draft ICU 3.8 */
66     UDATPG_WEEK_OF_MONTH_FIELD,
67     /** @draft ICU 3.8 */
68     UDATPG_WEEKDAY_FIELD,
69     /** @draft ICU 3.8 */
70     UDATPG_DAY_OF_YEAR_FIELD,
71     /** @draft ICU 3.8 */
72     UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD,
73     /** @draft ICU 3.8 */
74     UDATPG_DAY_FIELD,
75     /** @draft ICU 3.8 */
76     UDATPG_DAYPERIOD_FIELD,
77     /** @draft ICU 3.8 */
78     UDATPG_HOUR_FIELD,
79     /** @draft ICU 3.8 */
80     UDATPG_MINUTE_FIELD,
81     /** @draft ICU 3.8 */
82     UDATPG_SECOND_FIELD,
83     /** @draft ICU 3.8 */
84     UDATPG_FRACTIONAL_SECOND_FIELD,
85     /** @draft ICU 3.8 */
86     UDATPG_ZONE_FIELD,
87     /** @draft ICU 3.8 */
88     UDATPG_FIELD_COUNT
89 } UDateTimePatternField;
90 
91 /**
92  * Status return values from udatpg_addPattern().
93  * @draft ICU 3.8
94  */
95 typedef enum UDateTimePatternConflict {
96     /** @draft ICU 3.8 */
97     UDATPG_NO_CONFLICT,
98     /** @draft ICU 3.8 */
99     UDATPG_BASE_CONFLICT,
100     /** @draft ICU 3.8 */
101     UDATPG_CONFLICT,
102     /** @draft ICU 3.8 */
103     UDATPG_CONFLICT_COUNT
104 } UDateTimePatternConflict;
105 
106 #endif
107 
108 /**
109   * Open a generator according to a given locale.
110   * @param locale
111   * @param pErrorCode a pointer to the UErrorCode which must not indicate a
112   *                   failure before the function call.
113   * @return a pointer to UDateTimePatternGenerator.
114   * @draft ICU 3.8
115   */
116 U_DRAFT UDateTimePatternGenerator * U_EXPORT2
117 udatpg_open(const char *locale, UErrorCode *pErrorCode);
118 
119 /**
120   * Open an empty generator, to be constructed with udatpg_addPattern(...) etc.
121   * @param pErrorCode a pointer to the UErrorCode which must not indicate a
122   *                   failure before the function call.
123   * @return a pointer to UDateTimePatternGenerator.
124   * @draft ICU 3.8
125   */
126 U_DRAFT UDateTimePatternGenerator * U_EXPORT2
127 udatpg_openEmpty(UErrorCode *pErrorCode);
128 
129 /**
130   * Close a generator.
131   * @param dtpg a pointer to UDateTimePatternGenerator.
132   * @draft ICU 3.8
133   */
134 U_DRAFT void U_EXPORT2
135 udatpg_close(UDateTimePatternGenerator *dtpg);
136 
137 /**
138   * Create a copy pf a generator.
139   * @param dtpg a pointer to UDateTimePatternGenerator to be copied.
140   * @param pErrorCode a pointer to the UErrorCode which must not indicate a
141   *                   failure before the function call.
142   * @return a pointer to a new UDateTimePatternGenerator.
143   * @draft ICU 3.8
144  */
145 U_DRAFT UDateTimePatternGenerator * U_EXPORT2
146 udatpg_clone(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode);
147 
148 /**
149  * Get the best pattern matching the input skeleton. It is guaranteed to
150  * have all of the fields in the skeleton.
151  *
152  * Note that this function uses a non-const UDateTimePatternGenerator:
153  * It uses a stateful pattern parser which is set up for each generator object,
154  * rather than creating one for each function call.
155  * Consecutive calls to this function do not affect each other,
156  * but this function cannot be used concurrently on a single generator object.
157  *
158  * @param dtpg a pointer to UDateTimePatternGenerator.
159  * @param skeleton
160  *            The skeleton is a pattern containing only the variable fields.
161  *            For example, "MMMdd" and "mmhh" are skeletons.
162  * @param length the length of skeleton
163  * @param bestPattern
164  *            The best pattern found from the given skeleton.
165  * @param capacity the capacity of bestPattern.
166  * @param pErrorCode a pointer to the UErrorCode which must not indicate a
167  *                   failure before the function call.
168  * @return the length of bestPattern.
169  * @draft ICU 3.8
170  */
171 U_DRAFT int32_t U_EXPORT2
172 udatpg_getBestPattern(UDateTimePatternGenerator *dtpg,
173                       const UChar *skeleton, int32_t length,
174                       UChar *bestPattern, int32_t capacity,
175                       UErrorCode *pErrorCode);
176 
177 /**
178   * Get a unique skeleton from a given pattern. For example,
179   * both "MMM-dd" and "dd/MMM" produce the skeleton "MMMdd".
180   *
181   * Note that this function uses a non-const UDateTimePatternGenerator:
182   * It uses a stateful pattern parser which is set up for each generator object,
183   * rather than creating one for each function call.
184   * Consecutive calls to this function do not affect each other,
185   * but this function cannot be used concurrently on a single generator object.
186   *
187   * @param dtpg     a pointer to UDateTimePatternGenerator.
188   * @param pattern  input pattern, such as "dd/MMM".
189   * @param length   the length of pattern.
190   * @param skeleton such as "MMMdd"
191   * @param capacity the capacity of skeleton.
192   * @param pErrorCode a pointer to the UErrorCode which must not indicate a
193   *                  failure before the function call.
194   * @return the length of skeleton.
195   * @draft ICU 3.8
196   */
197 U_DRAFT int32_t U_EXPORT2
198 udatpg_getSkeleton(UDateTimePatternGenerator *dtpg,
199                    const UChar *pattern, int32_t length,
200                    UChar *skeleton, int32_t capacity,
201                    UErrorCode *pErrorCode);
202 
203 /**
204  * Get a unique base skeleton from a given pattern. This is the same
205  * as the skeleton, except that differences in length are minimized so
206  * as to only preserve the difference between string and numeric form. So
207  * for example, both "MMM-dd" and "d/MMM" produce the skeleton "MMMd"
208  * (notice the single d).
209  *
210  * Note that this function uses a non-const UDateTimePatternGenerator:
211  * It uses a stateful pattern parser which is set up for each generator object,
212  * rather than creating one for each function call.
213  * Consecutive calls to this function do not affect each other,
214  * but this function cannot be used concurrently on a single generator object.
215  *
216  * @param dtpg     a pointer to UDateTimePatternGenerator.
217  * @param pattern  input pattern, such as "dd/MMM".
218  * @param length   the length of pattern.
219  * @param baseSkeleton such as "Md"
220  * @param capacity the capacity of base skeleton.
221  * @param pErrorCode a pointer to the UErrorCode which must not indicate a
222  *                  failure before the function call.
223  * @return the length of baseSkeleton.
224  * @draft ICU 3.8
225  */
226 U_DRAFT int32_t U_EXPORT2
227 udatpg_getBaseSkeleton(UDateTimePatternGenerator *dtpg,
228                        const UChar *pattern, int32_t length,
229                        UChar *baseSkeleton, int32_t capacity,
230                        UErrorCode *pErrorCode);
231 
232 /**
233  * Adds a pattern to the generator. If the pattern has the same skeleton as
234  * an existing pattern, and the override parameter is set, then the previous
235  * value is overriden. Otherwise, the previous value is retained. In either
236  * case, the conflicting status is set and previous vale is stored in
237  * conflicting pattern.
238  * <p>
239  * Note that single-field patterns (like "MMM") are automatically added, and
240  * don't need to be added explicitly!
241  *
242  * @param dtpg     a pointer to UDateTimePatternGenerator.
243  * @param pattern  input pattern, such as "dd/MMM"
244  * @param patternLength the length of pattern.
245  * @param override  When existing values are to be overridden use true,
246  *                  otherwise use false.
247  * @param conflictingPattern  Previous pattern with the same skeleton.
248  * @param capacity the capacity of conflictingPattern.
249  * @param pLength a pointer to the length of conflictingPattern.
250  * @param pErrorCode a pointer to the UErrorCode which must not indicate a
251  *                  failure before the function call.
252  * @return conflicting status. The value could be UDATPG_NO_CONFLICT,
253  *                  UDATPG_BASE_CONFLICT or UDATPG_CONFLICT.
254  * @draft ICU 3.8
255  */
256 U_DRAFT UDateTimePatternConflict U_EXPORT2
257 udatpg_addPattern(UDateTimePatternGenerator *dtpg,
258                   const UChar *pattern, int32_t patternLength,
259                   UBool override,
260                   UChar *conflictingPattern, int32_t capacity, int32_t *pLength,
261                   UErrorCode *pErrorCode);
262 
263 /**
264   * An AppendItem format is a pattern used to append a field if there is no
265   * good match. For example, suppose that the input skeleton is "GyyyyMMMd",
266   * and there is no matching pattern internally, but there is a pattern
267   * matching "yyyyMMMd", say "d-MM-yyyy". Then that pattern is used, plus the
268   * G. The way these two are conjoined is by using the AppendItemFormat for G
269   * (era). So if that value is, say "{0}, {1}" then the final resulting
270   * pattern is "d-MM-yyyy, G".
271   * <p>
272   * There are actually three available variables: {0} is the pattern so far,
273   * {1} is the element we are adding, and {2} is the name of the element.
274   * <p>
275   * This reflects the way that the CLDR data is organized.
276   *
277   * @param dtpg   a pointer to UDateTimePatternGenerator.
278   * @param field  UDateTimePatternField, such as UDATPG_ERA_FIELD
279   * @param value  pattern, such as "{0}, {1}"
280   * @param length the length of value.
281   * @draft ICU 3.8
282   */
283 U_DRAFT void U_EXPORT2
284 udatpg_setAppendItemFormat(UDateTimePatternGenerator *dtpg,
285                            UDateTimePatternField field,
286                            const UChar *value, int32_t length);
287 
288 /**
289  * Getter corresponding to setAppendItemFormat. Values below 0 or at or
290  * above UDATPG_FIELD_COUNT are illegal arguments.
291  *
292  * @param dtpg   A pointer to UDateTimePatternGenerator.
293  * @param field  UDateTimePatternField, such as UDATPG_ERA_FIELD
294  * @param pLength A pointer that will receive the length of appendItemFormat.
295  * @return appendItemFormat for field.
296  * @draft ICU 3.8
297  */
298 U_DRAFT const UChar * U_EXPORT2
299 udatpg_getAppendItemFormat(const UDateTimePatternGenerator *dtpg,
300                            UDateTimePatternField field,
301                            int32_t *pLength);
302 
303 /**
304    * Set the name of field, eg "era" in English for ERA. These are only
305    * used if the corresponding AppendItemFormat is used, and if it contains a
306    * {2} variable.
307    * <p>
308    * This reflects the way that the CLDR data is organized.
309    *
310    * @param dtpg   a pointer to UDateTimePatternGenerator.
311    * @param field  UDateTimePatternField
312    * @param value  name for the field.
313    * @param length the length of value.
314    * @draft ICU 3.8
315    */
316 U_DRAFT void U_EXPORT2
317 udatpg_setAppendItemName(UDateTimePatternGenerator *dtpg,
318                          UDateTimePatternField field,
319                          const UChar *value, int32_t length);
320 
321 /**
322  * Getter corresponding to setAppendItemNames. Values below 0 or at or above
323  * UDATPG_FIELD_COUNT are illegal arguments.
324  *
325  * @param dtpg   a pointer to UDateTimePatternGenerator.
326  * @param field  UDateTimePatternField, such as UDATPG_ERA_FIELD
327  * @param pLength A pointer that will receive the length of the name for field.
328  * @return name for field
329  * @draft ICU 3.8
330  */
331 U_DRAFT const UChar * U_EXPORT2
332 udatpg_getAppendItemName(const UDateTimePatternGenerator *dtpg,
333                          UDateTimePatternField field,
334                          int32_t *pLength);
335 
336 /**
337  * The date time format is a message format pattern used to compose date and
338  * time patterns. The default value is "{0} {1}", where {0} will be replaced
339  * by the date pattern and {1} will be replaced by the time pattern.
340  * <p>
341  * This is used when the input skeleton contains both date and time fields,
342  * but there is not a close match among the added patterns. For example,
343  * suppose that this object was created by adding "dd-MMM" and "hh:mm", and
344  * its datetimeFormat is the default "{0} {1}". Then if the input skeleton
345  * is "MMMdhmm", there is not an exact match, so the input skeleton is
346  * broken up into two components "MMMd" and "hmm". There are close matches
347  * for those two skeletons, so the result is put together with this pattern,
348  * resulting in "d-MMM h:mm".
349  *
350  * @param dtpg a pointer to UDateTimePatternGenerator.
351  * @param dtFormat
352  *            message format pattern, here {0} will be replaced by the date
353  *            pattern and {1} will be replaced by the time pattern.
354  * @param length the length of dtFormat.
355  * @draft ICU 3.8
356  */
357 U_DRAFT void U_EXPORT2
358 udatpg_setDateTimeFormat(const UDateTimePatternGenerator *dtpg,
359                          const UChar *dtFormat, int32_t length);
360 
361 /**
362  * Getter corresponding to setDateTimeFormat.
363  * @param dtpg   a pointer to UDateTimePatternGenerator.
364  * @param pLength A pointer that will receive the length of the format
365  * @return dateTimeFormat.
366  * @draft ICU 3.8
367  */
368 U_DRAFT const UChar * U_EXPORT2
369 udatpg_getDateTimeFormat(const UDateTimePatternGenerator *dtpg,
370                          int32_t *pLength);
371 
372 /**
373  * The decimal value is used in formatting fractions of seconds. If the
374  * skeleton contains fractional seconds, then this is used with the
375  * fractional seconds. For example, suppose that the input pattern is
376  * "hhmmssSSSS", and the best matching pattern internally is "H:mm:ss", and
377  * the decimal string is ",". Then the resulting pattern is modified to be
378  * "H:mm:ss,SSSS"
379  *
380  * @param dtpg a pointer to UDateTimePatternGenerator.
381  * @param decimal
382  * @param length the length of decimal.
383  * @draft ICU 3.8
384  */
385 U_DRAFT void U_EXPORT2
386 udatpg_setDecimal(UDateTimePatternGenerator *dtpg,
387                   const UChar *decimal, int32_t length);
388 
389 /**
390  * Getter corresponding to setDecimal.
391  *
392  * @param dtpg a pointer to UDateTimePatternGenerator.
393  * @param pLength A pointer that will receive the length of the decimal string.
394  * @return corresponding to the decimal point.
395  * @draft ICU 3.8
396  */
397 U_DRAFT const UChar * U_EXPORT2
398 udatpg_getDecimal(const UDateTimePatternGenerator *dtpg,
399                   int32_t *pLength);
400 
401 /**
402  * Adjusts the field types (width and subtype) of a pattern to match what is
403  * in a skeleton. That is, if you supply a pattern like "d-M H:m", and a
404  * skeleton of "MMMMddhhmm", then the input pattern is adjusted to be
405  * "dd-MMMM hh:mm". This is used internally to get the best match for the
406  * input skeleton, but can also be used externally.
407  *
408  * Note that this function uses a non-const UDateTimePatternGenerator:
409  * It uses a stateful pattern parser which is set up for each generator object,
410  * rather than creating one for each function call.
411  * Consecutive calls to this function do not affect each other,
412  * but this function cannot be used concurrently on a single generator object.
413  *
414  * @param dtpg a pointer to UDateTimePatternGenerator.
415  * @param pattern Input pattern
416  * @param patternLength the length of input pattern.
417  * @param skeleton
418  * @param skeletonLength the length of input skeleton.
419  * @param dest  pattern adjusted to match the skeleton fields widths and subtypes.
420  * @param destCapacity the capacity of dest.
421  * @param pErrorCode a pointer to the UErrorCode which must not indicate a
422  *                  failure before the function call.
423  * @return the length of dest.
424  * @draft ICU 3.8
425  */
426 U_DRAFT int32_t U_EXPORT2
427 udatpg_replaceFieldTypes(UDateTimePatternGenerator *dtpg,
428                          const UChar *pattern, int32_t patternLength,
429                          const UChar *skeleton, int32_t skeletonLength,
430                          UChar *dest, int32_t destCapacity,
431                          UErrorCode *pErrorCode);
432 
433 /**
434  * Return a UEnumeration list of all the skeletons in canonical form.
435  * Call udatpg_getPatternForSkeleton() to get the corresponding pattern.
436  *
437  * @param dtpg a pointer to UDateTimePatternGenerator.
438  * @param pErrorCode a pointer to the UErrorCode which must not indicate a
439  *                  failure before the function call
440  * @return a UEnumeration list of all the skeletons
441  *         The caller must close the object.
442  * @draft ICU 3.8
443  */
444 U_DRAFT UEnumeration * U_EXPORT2
445 udatpg_openSkeletons(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode);
446 
447 /**
448  * Return a UEnumeration list of all the base skeletons in canonical form.
449  *
450  * @param dtpg a pointer to UDateTimePatternGenerator.
451  * @param pErrorCode a pointer to the UErrorCode which must not indicate a
452  *             failure before the function call.
453  * @return a UEnumeration list of all the base skeletons
454  *             The caller must close the object.
455  * @draft ICU 3.8
456  */
457 U_DRAFT UEnumeration * U_EXPORT2
458 udatpg_openBaseSkeletons(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode);
459 
460 /**
461  * Get the pattern corresponding to a given skeleton.
462  *
463  * @param dtpg a pointer to UDateTimePatternGenerator.
464  * @param skeleton
465  * @param skeletonLength pointer to the length of skeleton.
466  * @param pLength pointer to the length of return pattern.
467  * @return pattern corresponding to a given skeleton.
468  * @draft ICU 3.8
469  */
470 U_DRAFT const UChar * U_EXPORT2
471 udatpg_getPatternForSkeleton(const UDateTimePatternGenerator *dtpg,
472                              const UChar *skeleton, int32_t skeletonLength,
473                              int32_t *pLength);
474 
475 #endif
476