2 *******************************************************************************
3 * Copyright (C) 2007-2015, 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;
120 * Utility to return a unique skeleton from a given pattern. For example,
121 * both "MMM-dd" and "dd/MMM" produce the skeleton "MMMdd".
123 * @param pattern Input pattern, such as "dd/MMM"
124 * @param status Output param set to success/failure code on exit,
125 * which must not indicate a failure before the function call.
126 * @return skeleton such as "MMMdd"
129 UnicodeString
getSkeleton(const UnicodeString
& pattern
, UErrorCode
& status
);
132 * Utility to return a unique base skeleton from a given pattern. This is
133 * the same as the skeleton, except that differences in length are minimized
134 * so as to only preserve the difference between string and numeric form. So
135 * for example, both "MMM-dd" and "d/MMM" produce the skeleton "MMMd"
136 * (notice the single d).
138 * @param pattern Input pattern, such as "dd/MMM"
139 * @param status Output param set to success/failure code on exit,
140 * which must not indicate a failure before the function call.
141 * @return base skeleton, such as "Md"
144 UnicodeString
getBaseSkeleton(const UnicodeString
& pattern
, UErrorCode
& status
);
147 * Adds a pattern to the generator. If the pattern has the same skeleton as
148 * an existing pattern, and the override parameter is set, then the previous
149 * value is overriden. Otherwise, the previous value is retained. In either
150 * case, the conflicting status is set and previous vale is stored in
151 * conflicting pattern.
153 * Note that single-field patterns (like "MMM") are automatically added, and
154 * don't need to be added explicitly!
156 * @param pattern Input pattern, such as "dd/MMM"
157 * @param override When existing values are to be overridden use true,
158 * otherwise use false.
159 * @param conflictingPattern Previous pattern with the same skeleton.
160 * @param status Output param set to success/failure code on exit,
161 * which must not indicate a failure before the function call.
162 * @return conflicting status. The value could be UDATPG_NO_CONFLICT,
163 * UDATPG_BASE_CONFLICT or UDATPG_CONFLICT.
166 * <h4>Sample code</h4>
167 * \snippet samples/dtptngsample/dtptngsample.cpp getBestPatternExample1
168 * \snippet samples/dtptngsample/dtptngsample.cpp addPatternExample
171 UDateTimePatternConflict
addPattern(const UnicodeString
& pattern
,
173 UnicodeString
& conflictingPattern
,
177 * An AppendItem format is a pattern used to append a field if there is no
178 * good match. For example, suppose that the input skeleton is "GyyyyMMMd",
179 * and there is no matching pattern internally, but there is a pattern
180 * matching "yyyyMMMd", say "d-MM-yyyy". Then that pattern is used, plus the
181 * G. The way these two are conjoined is by using the AppendItemFormat for G
182 * (era). So if that value is, say "{0}, {1}" then the final resulting
183 * pattern is "d-MM-yyyy, G".
185 * There are actually three available variables: {0} is the pattern so far,
186 * {1} is the element we are adding, and {2} is the name of the element.
188 * This reflects the way that the CLDR data is organized.
190 * @param field such as UDATPG_ERA_FIELD.
191 * @param value pattern, such as "{0}, {1}"
194 void setAppendItemFormat(UDateTimePatternField field
, const UnicodeString
& value
);
197 * Getter corresponding to setAppendItemFormat. Values below 0 or at or
198 * above UDATPG_FIELD_COUNT are illegal arguments.
200 * @param field such as UDATPG_ERA_FIELD.
201 * @return append pattern for field
204 const UnicodeString
& getAppendItemFormat(UDateTimePatternField field
) const;
207 * Sets the names of field, eg "era" in English for ERA. These are only
208 * used if the corresponding AppendItemFormat is used, and if it contains a
211 * This reflects the way that the CLDR data is organized.
213 * @param field such as UDATPG_ERA_FIELD.
214 * @param value name of the field
217 void setAppendItemName(UDateTimePatternField field
, const UnicodeString
& value
);
220 * Getter corresponding to setAppendItemNames. Values below 0 or at or above
221 * UDATPG_FIELD_COUNT are illegal arguments.
223 * @param field such as UDATPG_ERA_FIELD.
224 * @return name for field
227 const UnicodeString
& getAppendItemName(UDateTimePatternField field
) const;
230 * The DateTimeFormat is a message format pattern used to compose date and
231 * time patterns. The default pattern in the root locale is "{1} {0}", where
232 * {1} will be replaced by the date pattern and {0} will be replaced by the
233 * time pattern; however, other locales may specify patterns such as
234 * "{1}, {0}" or "{1} 'at' {0}", etc.
236 * This is used when the input skeleton contains both date and time fields,
237 * but there is not a close match among the added patterns. For example,
238 * suppose that this object was created by adding "dd-MMM" and "hh:mm", and
239 * its datetimeFormat is the default "{1} {0}". Then if the input skeleton
240 * is "MMMdhmm", there is not an exact match, so the input skeleton is
241 * broken up into two components "MMMd" and "hmm". There are close matches
242 * for those two skeletons, so the result is put together with this pattern,
243 * resulting in "d-MMM h:mm".
245 * @param dateTimeFormat
246 * message format pattern, here {1} will be replaced by the date
247 * pattern and {0} will be replaced by the time pattern.
250 void setDateTimeFormat(const UnicodeString
& dateTimeFormat
);
253 * Getter corresponding to setDateTimeFormat.
254 * @return DateTimeFormat.
257 const UnicodeString
& getDateTimeFormat() const;
260 * Return the best pattern matching the input skeleton. It is guaranteed to
261 * have all of the fields in the skeleton.
264 * The skeleton is a pattern containing only the variable fields.
265 * For example, "MMMdd" and "mmhh" are skeletons.
266 * @param status Output param set to success/failure code on exit,
267 * which must not indicate a failure before the function call.
268 * @return bestPattern
269 * The best pattern found from the given skeleton.
272 * <h4>Sample code</h4>
273 * \snippet samples/dtptngsample/dtptngsample.cpp getBestPatternExample1
274 * \snippet samples/dtptngsample/dtptngsample.cpp getBestPatternExample
277 UnicodeString
getBestPattern(const UnicodeString
& skeleton
, UErrorCode
& status
);
281 * Return the best pattern matching the input skeleton. It is guaranteed to
282 * have all of the fields in the skeleton.
285 * The skeleton is a pattern containing only the variable fields.
286 * For example, "MMMdd" and "mmhh" are skeletons.
288 * Options for forcing the length of specified fields in the
289 * returned pattern to match those in the skeleton (when this
290 * would not happen otherwise). For default behavior, use
291 * UDATPG_MATCH_NO_OPTIONS.
293 * Output param set to success/failure code on exit,
294 * which must not indicate a failure before the function call.
295 * @return bestPattern
296 * The best pattern found from the given skeleton.
299 UnicodeString
getBestPattern(const UnicodeString
& skeleton
,
300 UDateTimePatternMatchOptions options
,
305 * Adjusts the field types (width and subtype) of a pattern to match what is
306 * in a skeleton. That is, if you supply a pattern like "d-M H:m", and a
307 * skeleton of "MMMMddhhmm", then the input pattern is adjusted to be
308 * "dd-MMMM hh:mm". This is used internally to get the best match for the
309 * input skeleton, but can also be used externally.
311 * @param pattern Input pattern
313 * The skeleton is a pattern containing only the variable fields.
314 * For example, "MMMdd" and "mmhh" are skeletons.
315 * @param status Output param set to success/failure code on exit,
316 * which must not indicate a failure before the function call.
317 * @return pattern adjusted to match the skeleton fields widths and subtypes.
320 * <h4>Sample code</h4>
321 * \snippet samples/dtptngsample/dtptngsample.cpp getBestPatternExample1
322 * \snippet samples/dtptngsample/dtptngsample.cpp replaceFieldTypesExample
325 UnicodeString
replaceFieldTypes(const UnicodeString
& pattern
,
326 const UnicodeString
& skeleton
,
330 * Adjusts the field types (width and subtype) of a pattern to match what is
331 * in a skeleton. That is, if you supply a pattern like "d-M H:m", and a
332 * skeleton of "MMMMddhhmm", then the input pattern is adjusted to be
333 * "dd-MMMM hh:mm". This is used internally to get the best match for the
334 * input skeleton, but can also be used externally.
336 * @param pattern Input pattern
338 * The skeleton is a pattern containing only the variable fields.
339 * For example, "MMMdd" and "mmhh" are skeletons.
341 * Options controlling whether the length of specified fields in the
342 * pattern are adjusted to match those in the skeleton (when this
343 * would not happen otherwise). For default behavior, use
344 * UDATPG_MATCH_NO_OPTIONS.
346 * Output param set to success/failure code on exit,
347 * which must not indicate a failure before the function call.
348 * @return pattern adjusted to match the skeleton fields widths and subtypes.
351 UnicodeString
replaceFieldTypes(const UnicodeString
& pattern
,
352 const UnicodeString
& skeleton
,
353 UDateTimePatternMatchOptions options
,
357 * Return a list of all the skeletons (in canonical form) from this class.
359 * Call getPatternForSkeleton() to get the corresponding pattern.
361 * @param status Output param set to success/failure code on exit,
362 * which must not indicate a failure before the function call.
363 * @return StringEnumeration with the skeletons.
364 * The caller must delete the object.
367 StringEnumeration
* getSkeletons(UErrorCode
& status
) const;
370 * Get the pattern corresponding to a given skeleton.
372 * @return pattern corresponding to a given skeleton.
375 const UnicodeString
& getPatternForSkeleton(const UnicodeString
& skeleton
) const;
378 * Return a list of all the base skeletons (in canonical form) from this class.
380 * @param status Output param set to success/failure code on exit,
381 * which must not indicate a failure before the function call.
382 * @return a StringEnumeration with the base skeletons.
383 * The caller must delete the object.
386 StringEnumeration
* getBaseSkeletons(UErrorCode
& status
) const;
388 #ifndef U_HIDE_INTERNAL_API
390 * Return a list of redundant patterns are those which if removed, make no
391 * difference in the resulting getBestPattern values. This method returns a
392 * list of them, to help check the consistency of the patterns used to build
395 * @param status Output param set to success/failure code on exit,
396 * which must not indicate a failure before the function call.
397 * @return a StringEnumeration with the redundant pattern.
398 * The caller must delete the object.
401 StringEnumeration
* getRedundants(UErrorCode
& status
);
402 #endif /* U_HIDE_INTERNAL_API */
405 * The decimal value is used in formatting fractions of seconds. If the
406 * skeleton contains fractional seconds, then this is used with the
407 * fractional seconds. For example, suppose that the input pattern is
408 * "hhmmssSSSS", and the best matching pattern internally is "H:mm:ss", and
409 * the decimal string is ",". Then the resulting pattern is modified to be
415 void setDecimal(const UnicodeString
& decimal
);
418 * Getter corresponding to setDecimal.
419 * @return UnicodeString corresponding to the decimal point
422 const UnicodeString
& getDecimal() const;
425 * ICU "poor man's RTTI", returns a UClassID for the actual class.
429 virtual UClassID
getDynamicClassID() const;
432 * ICU "poor man's RTTI", returns a UClassID for this class.
436 static UClassID U_EXPORT2
getStaticClassID(void);
443 DateTimePatternGenerator(UErrorCode
& status
);
449 DateTimePatternGenerator(const Locale
& locale
, UErrorCode
& status
);
453 * @param other DateTimePatternGenerator to copy
456 DateTimePatternGenerator(const DateTimePatternGenerator
& other
);
459 * Default assignment operator.
460 * @param other DateTimePatternGenerator to copy
463 DateTimePatternGenerator
& operator=(const DateTimePatternGenerator
& other
);
465 Locale pLocale
; // pattern locale
467 DateTimeMatcher
* dtMatcher
;
468 DistanceInfo
*distanceInfo
;
469 PatternMap
*patternMap
;
470 UnicodeString appendItemFormats
[UDATPG_FIELD_COUNT
];
471 UnicodeString appendItemNames
[UDATPG_FIELD_COUNT
];
472 UnicodeString dateTimeFormat
;
473 UnicodeString decimal
;
474 DateTimeMatcher
*skipMatcher
;
475 Hashtable
*fAvailableFormatKeyHash
;
476 UnicodeString hackPattern
;
477 UnicodeString emptyString
;
478 UChar fDefaultHourFormatChar
;
480 /* internal flags masks for adjustFieldTypes etc. */
483 kDTPGFixFractionalSeconds
= 1,
484 kDTPGSkeletonUsesCapJ
= 2
487 void initData(const Locale
&locale
, UErrorCode
&status
);
488 void addCanonicalItems();
489 void addICUPatterns(const Locale
& locale
, UErrorCode
& status
);
490 void hackTimes(const UnicodeString
& hackPattern
, UErrorCode
& status
);
491 void addCLDRData(const Locale
& locale
, UErrorCode
& status
);
492 UDateTimePatternConflict
addPatternWithSkeleton(const UnicodeString
& pattern
, const UnicodeString
* skeletonToUse
, UBool override
, UnicodeString
& conflictingPattern
, UErrorCode
& status
);
493 void initHashtable(UErrorCode
& status
);
494 void setDateTimeFromCalendar(const Locale
& locale
, UErrorCode
& status
);
495 void setDecimalSymbols(const Locale
& locale
, UErrorCode
& status
);
496 UDateTimePatternField
getAppendFormatNumber(const char* field
) const;
497 UDateTimePatternField
getAppendNameNumber(const char* field
) const;
498 void getAppendName(UDateTimePatternField field
, UnicodeString
& value
);
499 int32_t getCanonicalIndex(const UnicodeString
& field
);
500 const UnicodeString
* getBestRaw(DateTimeMatcher
& source
, int32_t includeMask
, DistanceInfo
* missingFields
, const PtnSkeleton
** specifiedSkeletonPtr
= 0);
501 UnicodeString
adjustFieldTypes(const UnicodeString
& pattern
, const PtnSkeleton
* specifiedSkeleton
, int32_t flags
, UDateTimePatternMatchOptions options
= UDATPG_MATCH_NO_OPTIONS
);
502 UnicodeString
getBestAppending(int32_t missingFields
, int32_t flags
, UDateTimePatternMatchOptions options
= UDATPG_MATCH_NO_OPTIONS
);
503 int32_t getTopBitNumber(int32_t foundMask
);
504 void setAvailableFormat(const UnicodeString
&key
, UErrorCode
& status
);
505 UBool
isAvailableFormatSet(const UnicodeString
&key
) const;
506 void copyHashtable(Hashtable
*other
, UErrorCode
&status
);
507 UBool
isCanonicalItem(const UnicodeString
& item
) const;
508 } ;// end class DateTimePatternGenerator