2 *******************************************************************************
3 * Copyright (C) 2007-2016, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
9 *******************************************************************************
12 #ifndef __DTPTNGEN_H__
13 #define __DTPTNGEN_H__
15 #include "unicode/datefmt.h"
16 #include "unicode/locid.h"
17 #include "unicode/udat.h"
18 #include "unicode/udatpg.h"
24 * \brief C++ API: Date/Time Pattern Generator
30 class DateTimeMatcher
;
34 class SharedDateTimePatternGenerator
;
37 * This class provides flexible generation of date format patterns, like "yy-MM-dd".
38 * The user can build up the generator by adding successive patterns. Once that
39 * is done, a query can be made using a "skeleton", which is a pattern which just
40 * includes the desired fields and lengths. The generator will return the "best fit"
41 * pattern corresponding to that skeleton.
42 * <p>The main method people will use is getBestPattern(String skeleton),
43 * since normally this class is pre-built with data from a particular locale.
44 * However, generators can be built directly from other data as well.
45 * <p><i>Issue: may be useful to also have a function that returns the list of
46 * fields in a pattern, in order, since we have that internally.
47 * That would be useful for getting the UI order of field elements.</i>
50 class U_I18N_API DateTimePatternGenerator
: public UObject
{
53 * Construct a flexible generator according to default locale.
54 * @param status Output param set to success/failure code on exit,
55 * which must not indicate a failure before the function call.
58 static DateTimePatternGenerator
* U_EXPORT2
createInstance(UErrorCode
& status
);
61 * Construct a flexible generator according to data for a given locale.
63 * @param status Output param set to success/failure code on exit,
64 * which must not indicate a failure before the function call.
67 static DateTimePatternGenerator
* U_EXPORT2
createInstance(const Locale
& uLocale
, UErrorCode
& status
);
69 #ifndef U_HIDE_INTERNAL_API
76 static DateTimePatternGenerator
* U_EXPORT2
internalMakeInstance(const Locale
& uLocale
, UErrorCode
& status
);
78 #endif /* U_HIDE_INTERNAL_API */
81 * Create an empty generator, to be constructed with addPattern(...) etc.
82 * @param status Output param set to success/failure code on exit,
83 * which must not indicate a failure before the function call.
86 static DateTimePatternGenerator
* U_EXPORT2
createEmptyInstance(UErrorCode
& status
);
92 virtual ~DateTimePatternGenerator();
95 * Clone DateTimePatternGenerator object. Clients are responsible for
96 * deleting the DateTimePatternGenerator object cloned.
99 DateTimePatternGenerator
* clone() const;
102 * Return true if another object is semantically equal to this one.
104 * @param other the DateTimePatternGenerator object to be compared with.
105 * @return true if other is semantically equal to this.
108 UBool
operator==(const DateTimePatternGenerator
& other
) const;
111 * Return true if another object is semantically unequal to this one.
113 * @param other the DateTimePatternGenerator object to be compared with.
114 * @return true if other is semantically unequal to this.
117 UBool
operator!=(const DateTimePatternGenerator
& other
) const;
119 #ifndef U_HIDE_DRAFT_API
121 * Utility to return a unique skeleton from a given pattern. For example,
122 * both "MMM-dd" and "dd/MMM" produce the skeleton "MMMdd".
124 * @param pattern Input pattern, such as "dd/MMM"
125 * @param status Output param set to success/failure code on exit,
126 * which must not indicate a failure before the function call.
127 * @return skeleton such as "MMMdd"
130 static UnicodeString
staticGetSkeleton(const UnicodeString
& pattern
, UErrorCode
& status
);
131 #endif /* U_HIDE_DRAFT_API */
134 * Utility to return a unique skeleton from a given pattern. For example,
135 * both "MMM-dd" and "dd/MMM" produce the skeleton "MMMdd".
136 * getSkeleton() works exactly like staticGetSkeleton().
137 * Use staticGetSkeleton() instead of getSkeleton().
139 * @param pattern Input pattern, such as "dd/MMM"
140 * @param status Output param set to success/failure code on exit,
141 * which must not indicate a failure before the function call.
142 * @return skeleton such as "MMMdd"
145 UnicodeString
getSkeleton(const UnicodeString
& pattern
, UErrorCode
& status
); /* {
146 The function is commented out because it is a stable API calling a draft API.
147 After staticGetSkeleton becomes stable, staticGetSkeleton can be used and
148 these comments and the definition of getSkeleton in dtptngen.cpp should be removed.
149 return staticGetSkeleton(pattern, status);
152 #ifndef U_HIDE_DRAFT_API
154 * Utility to return a unique base skeleton from a given pattern. This is
155 * the same as the skeleton, except that differences in length are minimized
156 * so as to only preserve the difference between string and numeric form. So
157 * for example, both "MMM-dd" and "d/MMM" produce the skeleton "MMMd"
158 * (notice the single d).
160 * @param pattern Input pattern, such as "dd/MMM"
161 * @param status Output param set to success/failure code on exit,
162 * which must not indicate a failure before the function call.
163 * @return base skeleton, such as "MMMd"
166 static UnicodeString
staticGetBaseSkeleton(const UnicodeString
& pattern
, UErrorCode
& status
);
167 #endif /* U_HIDE_DRAFT_API */
170 * Utility to return a unique base skeleton from a given pattern. This is
171 * the same as the skeleton, except that differences in length are minimized
172 * so as to only preserve the difference between string and numeric form. So
173 * for example, both "MMM-dd" and "d/MMM" produce the skeleton "MMMd"
174 * (notice the single d).
175 * getBaseSkeleton() works exactly like staticGetBaseSkeleton().
176 * Use staticGetBaseSkeleton() instead of getBaseSkeleton().
178 * @param pattern Input pattern, such as "dd/MMM"
179 * @param status Output param set to success/failure code on exit,
180 * which must not indicate a failure before the function call.
181 * @return base skeleton, such as "MMMd"
184 UnicodeString
getBaseSkeleton(const UnicodeString
& pattern
, UErrorCode
& status
); /* {
185 The function is commented out because it is a stable API calling a draft API.
186 After staticGetBaseSkeleton becomes stable, staticGetBaseSkeleton can be used and
187 these comments and the definition of getBaseSkeleton in dtptngen.cpp should be removed.
188 return staticGetBaseSkeleton(pattern, status);
192 * Adds a pattern to the generator. If the pattern has the same skeleton as
193 * an existing pattern, and the override parameter is set, then the previous
194 * value is overriden. Otherwise, the previous value is retained. In either
195 * case, the conflicting status is set and previous vale is stored in
196 * conflicting pattern.
198 * Note that single-field patterns (like "MMM") are automatically added, and
199 * don't need to be added explicitly!
201 * @param pattern Input pattern, such as "dd/MMM"
202 * @param override When existing values are to be overridden use true,
203 * otherwise use false.
204 * @param conflictingPattern Previous pattern with the same skeleton.
205 * @param status Output param set to success/failure code on exit,
206 * which must not indicate a failure before the function call.
207 * @return conflicting status. The value could be UDATPG_NO_CONFLICT,
208 * UDATPG_BASE_CONFLICT or UDATPG_CONFLICT.
211 * <h4>Sample code</h4>
212 * \snippet samples/dtptngsample/dtptngsample.cpp getBestPatternExample1
213 * \snippet samples/dtptngsample/dtptngsample.cpp addPatternExample
216 UDateTimePatternConflict
addPattern(const UnicodeString
& pattern
,
218 UnicodeString
& conflictingPattern
,
222 * An AppendItem format is a pattern used to append a field if there is no
223 * good match. For example, suppose that the input skeleton is "GyyyyMMMd",
224 * and there is no matching pattern internally, but there is a pattern
225 * matching "yyyyMMMd", say "d-MM-yyyy". Then that pattern is used, plus the
226 * G. The way these two are conjoined is by using the AppendItemFormat for G
227 * (era). So if that value is, say "{0}, {1}" then the final resulting
228 * pattern is "d-MM-yyyy, G".
230 * There are actually three available variables: {0} is the pattern so far,
231 * {1} is the element we are adding, and {2} is the name of the element.
233 * This reflects the way that the CLDR data is organized.
235 * @param field such as UDATPG_ERA_FIELD.
236 * @param value pattern, such as "{0}, {1}"
239 void setAppendItemFormat(UDateTimePatternField field
, const UnicodeString
& value
);
242 * Getter corresponding to setAppendItemFormat. Values below 0 or at or
243 * above UDATPG_FIELD_COUNT are illegal arguments.
245 * @param field such as UDATPG_ERA_FIELD.
246 * @return append pattern for field
249 const UnicodeString
& getAppendItemFormat(UDateTimePatternField field
) const;
252 * Sets the names of field, eg "era" in English for ERA. These are only
253 * used if the corresponding AppendItemFormat is used, and if it contains a
256 * This reflects the way that the CLDR data is organized.
258 * @param field such as UDATPG_ERA_FIELD.
259 * @param value name of the field
262 void setAppendItemName(UDateTimePatternField field
, const UnicodeString
& value
);
265 * Getter corresponding to setAppendItemNames. Values below 0 or at or above
266 * UDATPG_FIELD_COUNT are illegal arguments.
268 * @param field such as UDATPG_ERA_FIELD.
269 * @return name for field
272 const UnicodeString
& getAppendItemName(UDateTimePatternField field
) const;
275 * The DateTimeFormat is a message format pattern used to compose date and
276 * time patterns. The default pattern in the root locale is "{1} {0}", where
277 * {1} will be replaced by the date pattern and {0} will be replaced by the
278 * time pattern; however, other locales may specify patterns such as
279 * "{1}, {0}" or "{1} 'at' {0}", etc.
281 * This is used when the input skeleton contains both date and time fields,
282 * but there is not a close match among the added patterns. For example,
283 * suppose that this object was created by adding "dd-MMM" and "hh:mm", and
284 * its datetimeFormat is the default "{1} {0}". Then if the input skeleton
285 * is "MMMdhmm", there is not an exact match, so the input skeleton is
286 * broken up into two components "MMMd" and "hmm". There are close matches
287 * for those two skeletons, so the result is put together with this pattern,
288 * resulting in "d-MMM h:mm".
290 * @param dateTimeFormat
291 * message format pattern, here {1} will be replaced by the date
292 * pattern and {0} will be replaced by the time pattern.
295 void setDateTimeFormat(const UnicodeString
& dateTimeFormat
);
298 * Getter corresponding to setDateTimeFormat.
299 * @return DateTimeFormat.
302 const UnicodeString
& getDateTimeFormat() const;
305 * Return the best pattern matching the input skeleton. It is guaranteed to
306 * have all of the fields in the skeleton.
309 * The skeleton is a pattern containing only the variable fields.
310 * For example, "MMMdd" and "mmhh" are skeletons.
311 * @param status Output param set to success/failure code on exit,
312 * which must not indicate a failure before the function call.
313 * @return bestPattern
314 * The best pattern found from the given skeleton.
317 * <h4>Sample code</h4>
318 * \snippet samples/dtptngsample/dtptngsample.cpp getBestPatternExample1
319 * \snippet samples/dtptngsample/dtptngsample.cpp getBestPatternExample
322 UnicodeString
getBestPattern(const UnicodeString
& skeleton
, UErrorCode
& status
);
326 * Return the best pattern matching the input skeleton. It is guaranteed to
327 * have all of the fields in the skeleton.
330 * The skeleton is a pattern containing only the variable fields.
331 * For example, "MMMdd" and "mmhh" are skeletons.
333 * Options for forcing the length of specified fields in the
334 * returned pattern to match those in the skeleton (when this
335 * would not happen otherwise). For default behavior, use
336 * UDATPG_MATCH_NO_OPTIONS.
338 * Output param set to success/failure code on exit,
339 * which must not indicate a failure before the function call.
340 * @return bestPattern
341 * The best pattern found from the given skeleton.
344 UnicodeString
getBestPattern(const UnicodeString
& skeleton
,
345 UDateTimePatternMatchOptions options
,
350 * Adjusts the field types (width and subtype) of a pattern to match what is
351 * in a skeleton. That is, if you supply a pattern like "d-M H:m", and a
352 * skeleton of "MMMMddhhmm", then the input pattern is adjusted to be
353 * "dd-MMMM hh:mm". This is used internally to get the best match for the
354 * input skeleton, but can also be used externally.
356 * @param pattern Input pattern
358 * The skeleton is a pattern containing only the variable fields.
359 * For example, "MMMdd" and "mmhh" are skeletons.
360 * @param status Output param set to success/failure code on exit,
361 * which must not indicate a failure before the function call.
362 * @return pattern adjusted to match the skeleton fields widths and subtypes.
365 * <h4>Sample code</h4>
366 * \snippet samples/dtptngsample/dtptngsample.cpp getBestPatternExample1
367 * \snippet samples/dtptngsample/dtptngsample.cpp replaceFieldTypesExample
370 UnicodeString
replaceFieldTypes(const UnicodeString
& pattern
,
371 const UnicodeString
& skeleton
,
375 * Adjusts the field types (width and subtype) of a pattern to match what is
376 * in a skeleton. That is, if you supply a pattern like "d-M H:m", and a
377 * skeleton of "MMMMddhhmm", then the input pattern is adjusted to be
378 * "dd-MMMM hh:mm". This is used internally to get the best match for the
379 * input skeleton, but can also be used externally.
381 * @param pattern Input pattern
383 * The skeleton is a pattern containing only the variable fields.
384 * For example, "MMMdd" and "mmhh" are skeletons.
386 * Options controlling whether the length of specified fields in the
387 * pattern are adjusted to match those in the skeleton (when this
388 * would not happen otherwise). For default behavior, use
389 * UDATPG_MATCH_NO_OPTIONS.
391 * Output param set to success/failure code on exit,
392 * which must not indicate a failure before the function call.
393 * @return pattern adjusted to match the skeleton fields widths and subtypes.
396 UnicodeString
replaceFieldTypes(const UnicodeString
& pattern
,
397 const UnicodeString
& skeleton
,
398 UDateTimePatternMatchOptions options
,
402 * Return a list of all the skeletons (in canonical form) from this class.
404 * Call getPatternForSkeleton() to get the corresponding pattern.
406 * @param status Output param set to success/failure code on exit,
407 * which must not indicate a failure before the function call.
408 * @return StringEnumeration with the skeletons.
409 * The caller must delete the object.
412 StringEnumeration
* getSkeletons(UErrorCode
& status
) const;
415 * Get the pattern corresponding to a given skeleton.
417 * @return pattern corresponding to a given skeleton.
420 const UnicodeString
& getPatternForSkeleton(const UnicodeString
& skeleton
) const;
423 * Return a list of all the base skeletons (in canonical form) from this class.
425 * @param status Output param set to success/failure code on exit,
426 * which must not indicate a failure before the function call.
427 * @return a StringEnumeration with the base skeletons.
428 * The caller must delete the object.
431 StringEnumeration
* getBaseSkeletons(UErrorCode
& status
) const;
433 #ifndef U_HIDE_INTERNAL_API
435 * Return a list of redundant patterns are those which if removed, make no
436 * difference in the resulting getBestPattern values. This method returns a
437 * list of them, to help check the consistency of the patterns used to build
440 * @param status Output param set to success/failure code on exit,
441 * which must not indicate a failure before the function call.
442 * @return a StringEnumeration with the redundant pattern.
443 * The caller must delete the object.
446 StringEnumeration
* getRedundants(UErrorCode
& status
);
447 #endif /* U_HIDE_INTERNAL_API */
450 * The decimal value is used in formatting fractions of seconds. If the
451 * skeleton contains fractional seconds, then this is used with the
452 * fractional seconds. For example, suppose that the input pattern is
453 * "hhmmssSSSS", and the best matching pattern internally is "H:mm:ss", and
454 * the decimal string is ",". Then the resulting pattern is modified to be
460 void setDecimal(const UnicodeString
& decimal
);
463 * Getter corresponding to setDecimal.
464 * @return UnicodeString corresponding to the decimal point
467 const UnicodeString
& getDecimal() const;
470 * ICU "poor man's RTTI", returns a UClassID for the actual class.
474 virtual UClassID
getDynamicClassID() const;
477 * ICU "poor man's RTTI", returns a UClassID for this class.
481 static UClassID U_EXPORT2
getStaticClassID(void);
488 DateTimePatternGenerator(UErrorCode
& status
);
494 DateTimePatternGenerator(const Locale
& locale
, UErrorCode
& status
);
498 * @param other DateTimePatternGenerator to copy
501 DateTimePatternGenerator(const DateTimePatternGenerator
& other
);
504 * Default assignment operator.
505 * @param other DateTimePatternGenerator to copy
508 DateTimePatternGenerator
& operator=(const DateTimePatternGenerator
& other
);
510 Locale pLocale
; // pattern locale
512 DateTimeMatcher
* dtMatcher
;
513 DistanceInfo
*distanceInfo
;
514 PatternMap
*patternMap
;
515 UnicodeString appendItemFormats
[UDATPG_FIELD_COUNT
];
516 UnicodeString appendItemNames
[UDATPG_FIELD_COUNT
];
517 UnicodeString dateTimeFormat
;
518 UnicodeString decimal
;
519 DateTimeMatcher
*skipMatcher
;
520 Hashtable
*fAvailableFormatKeyHash
;
521 UnicodeString hackPattern
;
522 UnicodeString emptyString
;
523 UChar fDefaultHourFormatChar
;
525 int32_t fAllowedHourFormats
[7]; // Actually an array of AllowedHourFormat enum type, ending with UNKNOWN.
527 /* internal flags masks for adjustFieldTypes etc. */
530 kDTPGFixFractionalSeconds
= 1,
531 kDTPGSkeletonUsesCapJ
= 2,
532 kDTPGSkeletonUsesLowB
= 3,
533 kDTPGSkeletonUsesCapB
= 4
536 void initData(const Locale
&locale
, UErrorCode
&status
);
537 void addCanonicalItems();
538 void addICUPatterns(const Locale
& locale
, UErrorCode
& status
);
539 void hackTimes(const UnicodeString
& hackPattern
, UErrorCode
& status
);
540 void addCLDRData(const Locale
& locale
, UErrorCode
& status
);
541 UDateTimePatternConflict
addPatternWithSkeleton(const UnicodeString
& pattern
, const UnicodeString
* skeletonToUse
, UBool override
, UnicodeString
& conflictingPattern
, UErrorCode
& status
);
542 void initHashtable(UErrorCode
& status
);
543 void setDateTimeFromCalendar(const Locale
& locale
, UErrorCode
& status
);
544 void setDecimalSymbols(const Locale
& locale
, UErrorCode
& status
);
545 UDateTimePatternField
getAppendFormatNumber(const char* field
) const;
546 UDateTimePatternField
getAppendNameNumber(const char* field
) const;
547 void getAppendName(UDateTimePatternField field
, UnicodeString
& value
);
548 int32_t getCanonicalIndex(const UnicodeString
& field
);
549 const UnicodeString
* getBestRaw(DateTimeMatcher
& source
, int32_t includeMask
, DistanceInfo
* missingFields
, const PtnSkeleton
** specifiedSkeletonPtr
= 0);
550 UnicodeString
adjustFieldTypes(const UnicodeString
& pattern
, const PtnSkeleton
* specifiedSkeleton
, int32_t flags
, UDateTimePatternMatchOptions options
= UDATPG_MATCH_NO_OPTIONS
);
551 UnicodeString
getBestAppending(int32_t missingFields
, int32_t flags
, UDateTimePatternMatchOptions options
= UDATPG_MATCH_NO_OPTIONS
);
552 int32_t getTopBitNumber(int32_t foundMask
);
553 void setAvailableFormat(const UnicodeString
&key
, UErrorCode
& status
);
554 UBool
isAvailableFormatSet(const UnicodeString
&key
) const;
555 void copyHashtable(Hashtable
*other
, UErrorCode
&status
);
556 UBool
isCanonicalItem(const UnicodeString
& item
) const;
557 static void loadAllowedHourFormatsData(UErrorCode
&status
);
558 void getAllowedHourFormats(const Locale
&locale
, UErrorCode
&status
);
559 } ;// end class DateTimePatternGenerator