1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2007-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
11 *******************************************************************************
14 #ifndef __DTPTNGEN_H__
15 #define __DTPTNGEN_H__
17 #include "unicode/datefmt.h"
18 #include "unicode/locid.h"
19 #include "unicode/udat.h"
20 #include "unicode/udatpg.h"
21 #include "unicode/unistr.h"
23 #if U_SHOW_CPLUSPLUS_API
28 * \brief C++ API: Date/Time Pattern Generator
35 class DateTimeMatcher
;
39 class SharedDateTimePatternGenerator
;
42 * This class provides flexible generation of date format patterns, like "yy-MM-dd".
43 * The user can build up the generator by adding successive patterns. Once that
44 * is done, a query can be made using a "skeleton", which is a pattern which just
45 * includes the desired fields and lengths. The generator will return the "best fit"
46 * pattern corresponding to that skeleton.
47 * <p>The main method people will use is getBestPattern(String skeleton),
48 * since normally this class is pre-built with data from a particular locale.
49 * However, generators can be built directly from other data as well.
50 * <p><i>Issue: may be useful to also have a function that returns the list of
51 * fields in a pattern, in order, since we have that internally.
52 * That would be useful for getting the UI order of field elements.</i>
55 class U_I18N_API DateTimePatternGenerator
: public UObject
{
58 * Construct a flexible generator according to default locale.
59 * @param status Output param set to success/failure code on exit,
60 * which must not indicate a failure before the function call.
63 static DateTimePatternGenerator
* U_EXPORT2
createInstance(UErrorCode
& status
);
66 * Construct a flexible generator according to data for a given locale.
68 * @param status Output param set to success/failure code on exit,
69 * which must not indicate a failure before the function call.
72 static DateTimePatternGenerator
* U_EXPORT2
createInstance(const Locale
& uLocale
, UErrorCode
& status
);
74 #ifndef U_HIDE_INTERNAL_API
81 static DateTimePatternGenerator
* U_EXPORT2
internalMakeInstance(const Locale
& uLocale
, UErrorCode
& status
);
83 #endif /* U_HIDE_INTERNAL_API */
86 * Create an empty generator, to be constructed with addPattern(...) etc.
87 * @param status Output param set to success/failure code on exit,
88 * which must not indicate a failure before the function call.
91 static DateTimePatternGenerator
* U_EXPORT2
createEmptyInstance(UErrorCode
& status
);
97 virtual ~DateTimePatternGenerator();
100 * Clone DateTimePatternGenerator object. Clients are responsible for
101 * deleting the DateTimePatternGenerator object cloned.
104 DateTimePatternGenerator
* clone() const;
107 * Return true if another object is semantically equal to this one.
109 * @param other the DateTimePatternGenerator object to be compared with.
110 * @return true if other is semantically equal to this.
113 UBool
operator==(const DateTimePatternGenerator
& other
) const;
116 * Return true if another object is semantically unequal to this one.
118 * @param other the DateTimePatternGenerator object to be compared with.
119 * @return true if other is semantically unequal to this.
122 UBool
operator!=(const DateTimePatternGenerator
& other
) const;
125 * Utility to return a unique skeleton from a given pattern. For example,
126 * both "MMM-dd" and "dd/MMM" produce the skeleton "MMMdd".
128 * @param pattern Input pattern, such as "dd/MMM"
129 * @param status Output param set to success/failure code on exit,
130 * which must not indicate a failure before the function call.
131 * @return skeleton such as "MMMdd"
134 static UnicodeString
staticGetSkeleton(const UnicodeString
& pattern
, UErrorCode
& status
);
137 * Utility to return a unique skeleton from a given pattern. For example,
138 * both "MMM-dd" and "dd/MMM" produce the skeleton "MMMdd".
139 * getSkeleton() works exactly like staticGetSkeleton().
140 * Use staticGetSkeleton() instead of getSkeleton().
142 * @param pattern Input pattern, such as "dd/MMM"
143 * @param status Output param set to success/failure code on exit,
144 * which must not indicate a failure before the function call.
145 * @return skeleton such as "MMMdd"
148 UnicodeString
getSkeleton(const UnicodeString
& pattern
, UErrorCode
& status
); /* {
149 The function is commented out because it is a stable API calling a draft API.
150 After staticGetSkeleton becomes stable, staticGetSkeleton can be used and
151 these comments and the definition of getSkeleton in dtptngen.cpp should be removed.
152 return staticGetSkeleton(pattern, status);
156 * Utility to return a unique base skeleton from a given pattern. This is
157 * the same as the skeleton, except that differences in length are minimized
158 * so as to only preserve the difference between string and numeric form. So
159 * for example, both "MMM-dd" and "d/MMM" produce the skeleton "MMMd"
160 * (notice the single d).
162 * @param pattern Input pattern, such as "dd/MMM"
163 * @param status Output param set to success/failure code on exit,
164 * which must not indicate a failure before the function call.
165 * @return base skeleton, such as "MMMd"
168 static UnicodeString
staticGetBaseSkeleton(const UnicodeString
& pattern
, UErrorCode
& status
);
171 * Utility to return a unique base skeleton from a given pattern. This is
172 * the same as the skeleton, except that differences in length are minimized
173 * so as to only preserve the difference between string and numeric form. So
174 * for example, both "MMM-dd" and "d/MMM" produce the skeleton "MMMd"
175 * (notice the single d).
176 * getBaseSkeleton() works exactly like staticGetBaseSkeleton().
177 * Use staticGetBaseSkeleton() instead of getBaseSkeleton().
179 * @param pattern Input pattern, such as "dd/MMM"
180 * @param status Output param set to success/failure code on exit,
181 * which must not indicate a failure before the function call.
182 * @return base skeleton, such as "MMMd"
185 UnicodeString
getBaseSkeleton(const UnicodeString
& pattern
, UErrorCode
& status
); /* {
186 The function is commented out because it is a stable API calling a draft API.
187 After staticGetBaseSkeleton becomes stable, staticGetBaseSkeleton can be used and
188 these comments and the definition of getBaseSkeleton in dtptngen.cpp should be removed.
189 return staticGetBaseSkeleton(pattern, status);
193 * Adds a pattern to the generator. If the pattern has the same skeleton as
194 * an existing pattern, and the override parameter is set, then the previous
195 * value is overriden. Otherwise, the previous value is retained. In either
196 * case, the conflicting status is set and previous vale is stored in
197 * conflicting pattern.
199 * Note that single-field patterns (like "MMM") are automatically added, and
200 * don't need to be added explicitly!
202 * @param pattern Input pattern, such as "dd/MMM"
203 * @param override When existing values are to be overridden use true,
204 * otherwise use false.
205 * @param conflictingPattern Previous pattern with the same skeleton.
206 * @param status Output param set to success/failure code on exit,
207 * which must not indicate a failure before the function call.
208 * @return conflicting status. The value could be UDATPG_NO_CONFLICT,
209 * UDATPG_BASE_CONFLICT or UDATPG_CONFLICT.
212 * <h4>Sample code</h4>
213 * \snippet samples/dtptngsample/dtptngsample.cpp getBestPatternExample1
214 * \snippet samples/dtptngsample/dtptngsample.cpp addPatternExample
217 UDateTimePatternConflict
addPattern(const UnicodeString
& pattern
,
219 UnicodeString
& conflictingPattern
,
223 * An AppendItem format is a pattern used to append a field if there is no
224 * good match. For example, suppose that the input skeleton is "GyyyyMMMd",
225 * and there is no matching pattern internally, but there is a pattern
226 * matching "yyyyMMMd", say "d-MM-yyyy". Then that pattern is used, plus the
227 * G. The way these two are conjoined is by using the AppendItemFormat for G
228 * (era). So if that value is, say "{0}, {1}" then the final resulting
229 * pattern is "d-MM-yyyy, G".
231 * There are actually three available variables: {0} is the pattern so far,
232 * {1} is the element we are adding, and {2} is the name of the element.
234 * This reflects the way that the CLDR data is organized.
236 * @param field such as UDATPG_ERA_FIELD.
237 * @param value pattern, such as "{0}, {1}"
240 void setAppendItemFormat(UDateTimePatternField field
, const UnicodeString
& value
);
243 * Getter corresponding to setAppendItemFormat. Values below 0 or at or
244 * above UDATPG_FIELD_COUNT are illegal arguments.
246 * @param field such as UDATPG_ERA_FIELD.
247 * @return append pattern for field
250 const UnicodeString
& getAppendItemFormat(UDateTimePatternField field
) const;
253 * Sets the names of field, eg "era" in English for ERA. These are only
254 * used if the corresponding AppendItemFormat is used, and if it contains a
257 * This reflects the way that the CLDR data is organized.
259 * @param field such as UDATPG_ERA_FIELD.
260 * @param value name of the field
263 void setAppendItemName(UDateTimePatternField field
, const UnicodeString
& value
);
266 * Getter corresponding to setAppendItemNames. Values below 0 or at or above
267 * UDATPG_FIELD_COUNT are illegal arguments. Note: The more general method
268 * for getting date/time field display names is getFieldDisplayName.
270 * @param field such as UDATPG_ERA_FIELD.
271 * @return name for field
272 * @see getFieldDisplayName
275 const UnicodeString
& getAppendItemName(UDateTimePatternField field
) const;
277 #ifndef U_HIDE_DRAFT_API
279 * The general interface to get a display name for a particular date/time field,
280 * in one of several possible display widths.
282 * @param field The desired UDateTimePatternField, such as UDATPG_ERA_FIELD.
283 * @param width The desired UDateTimePGDisplayWidth, such as UDATPG_ABBREVIATED.
284 * @return. The display name for field
287 UnicodeString
getFieldDisplayName(UDateTimePatternField field
, UDateTimePGDisplayWidth width
) const;
288 #endif // U_HIDE_DRAFT_API
291 * The DateTimeFormat is a message format pattern used to compose date and
292 * time patterns. The default pattern in the root locale is "{1} {0}", where
293 * {1} will be replaced by the date pattern and {0} will be replaced by the
294 * time pattern; however, other locales may specify patterns such as
295 * "{1}, {0}" or "{1} 'at' {0}", etc.
297 * This is used when the input skeleton contains both date and time fields,
298 * but there is not a close match among the added patterns. For example,
299 * suppose that this object was created by adding "dd-MMM" and "hh:mm", and
300 * its datetimeFormat is the default "{1} {0}". Then if the input skeleton
301 * is "MMMdhmm", there is not an exact match, so the input skeleton is
302 * broken up into two components "MMMd" and "hmm". There are close matches
303 * for those two skeletons, so the result is put together with this pattern,
304 * resulting in "d-MMM h:mm".
306 * @param dateTimeFormat
307 * message format pattern, here {1} will be replaced by the date
308 * pattern and {0} will be replaced by the time pattern.
311 void setDateTimeFormat(const UnicodeString
& dateTimeFormat
);
314 * Getter corresponding to setDateTimeFormat.
315 * @return DateTimeFormat.
318 const UnicodeString
& getDateTimeFormat() const;
321 * Return the best pattern matching the input skeleton. It is guaranteed to
322 * have all of the fields in the skeleton.
325 * The skeleton is a pattern containing only the variable fields.
326 * For example, "MMMdd" and "mmhh" are skeletons.
327 * @param status Output param set to success/failure code on exit,
328 * which must not indicate a failure before the function call.
329 * @return bestPattern
330 * The best pattern found from the given skeleton.
333 * <h4>Sample code</h4>
334 * \snippet samples/dtptngsample/dtptngsample.cpp getBestPatternExample1
335 * \snippet samples/dtptngsample/dtptngsample.cpp getBestPatternExample
338 UnicodeString
getBestPattern(const UnicodeString
& skeleton
, UErrorCode
& status
);
342 * Return the best pattern matching the input skeleton. It is guaranteed to
343 * have all of the fields in the skeleton.
346 * The skeleton is a pattern containing only the variable fields.
347 * For example, "MMMdd" and "mmhh" are skeletons.
349 * Options for forcing the length of specified fields in the
350 * returned pattern to match those in the skeleton (when this
351 * would not happen otherwise). For default behavior, use
352 * UDATPG_MATCH_NO_OPTIONS.
354 * Output param set to success/failure code on exit,
355 * which must not indicate a failure before the function call.
356 * @return bestPattern
357 * The best pattern found from the given skeleton.
360 UnicodeString
getBestPattern(const UnicodeString
& skeleton
,
361 UDateTimePatternMatchOptions options
,
366 * Adjusts the field types (width and subtype) of a pattern to match what is
367 * in a skeleton. That is, if you supply a pattern like "d-M H:m", and a
368 * skeleton of "MMMMddhhmm", then the input pattern is adjusted to be
369 * "dd-MMMM hh:mm". This is used internally to get the best match for the
370 * input skeleton, but can also be used externally.
372 * @param pattern Input pattern
374 * The skeleton is a pattern containing only the variable fields.
375 * For example, "MMMdd" and "mmhh" are skeletons.
376 * @param status Output param set to success/failure code on exit,
377 * which must not indicate a failure before the function call.
378 * @return pattern adjusted to match the skeleton fields widths and subtypes.
381 * <h4>Sample code</h4>
382 * \snippet samples/dtptngsample/dtptngsample.cpp getBestPatternExample1
383 * \snippet samples/dtptngsample/dtptngsample.cpp replaceFieldTypesExample
386 UnicodeString
replaceFieldTypes(const UnicodeString
& pattern
,
387 const UnicodeString
& skeleton
,
391 * Adjusts the field types (width and subtype) of a pattern to match what is
392 * in a skeleton. That is, if you supply a pattern like "d-M H:m", and a
393 * skeleton of "MMMMddhhmm", then the input pattern is adjusted to be
394 * "dd-MMMM hh:mm". This is used internally to get the best match for the
395 * input skeleton, but can also be used externally.
397 * @param pattern Input pattern
399 * The skeleton is a pattern containing only the variable fields.
400 * For example, "MMMdd" and "mmhh" are skeletons.
402 * Options controlling whether the length of specified fields in the
403 * pattern are adjusted to match those in the skeleton (when this
404 * would not happen otherwise). For default behavior, use
405 * UDATPG_MATCH_NO_OPTIONS.
407 * Output param set to success/failure code on exit,
408 * which must not indicate a failure before the function call.
409 * @return pattern adjusted to match the skeleton fields widths and subtypes.
412 UnicodeString
replaceFieldTypes(const UnicodeString
& pattern
,
413 const UnicodeString
& skeleton
,
414 UDateTimePatternMatchOptions options
,
418 * Return a list of all the skeletons (in canonical form) from this class.
420 * Call getPatternForSkeleton() to get the corresponding pattern.
422 * @param status Output param set to success/failure code on exit,
423 * which must not indicate a failure before the function call.
424 * @return StringEnumeration with the skeletons.
425 * The caller must delete the object.
428 StringEnumeration
* getSkeletons(UErrorCode
& status
) const;
431 * Get the pattern corresponding to a given skeleton.
433 * @return pattern corresponding to a given skeleton.
436 const UnicodeString
& getPatternForSkeleton(const UnicodeString
& skeleton
) const;
439 * Return a list of all the base skeletons (in canonical form) from this class.
441 * @param status Output param set to success/failure code on exit,
442 * which must not indicate a failure before the function call.
443 * @return a StringEnumeration with the base skeletons.
444 * The caller must delete the object.
447 StringEnumeration
* getBaseSkeletons(UErrorCode
& status
) const;
449 #ifndef U_HIDE_INTERNAL_API
451 * Return a list of redundant patterns are those which if removed, make no
452 * difference in the resulting getBestPattern values. This method returns a
453 * list of them, to help check the consistency of the patterns used to build
456 * @param status Output param set to success/failure code on exit,
457 * which must not indicate a failure before the function call.
458 * @return a StringEnumeration with the redundant pattern.
459 * The caller must delete the object.
462 StringEnumeration
* getRedundants(UErrorCode
& status
);
463 #endif /* U_HIDE_INTERNAL_API */
466 * The decimal value is used in formatting fractions of seconds. If the
467 * skeleton contains fractional seconds, then this is used with the
468 * fractional seconds. For example, suppose that the input pattern is
469 * "hhmmssSSSS", and the best matching pattern internally is "H:mm:ss", and
470 * the decimal string is ",". Then the resulting pattern is modified to be
476 void setDecimal(const UnicodeString
& decimal
);
479 * Getter corresponding to setDecimal.
480 * @return UnicodeString corresponding to the decimal point
483 const UnicodeString
& getDecimal() const;
486 * ICU "poor man's RTTI", returns a UClassID for the actual class.
490 virtual UClassID
getDynamicClassID() const;
493 * ICU "poor man's RTTI", returns a UClassID for this class.
497 static UClassID U_EXPORT2
getStaticClassID(void);
504 DateTimePatternGenerator(UErrorCode
& status
);
510 DateTimePatternGenerator(const Locale
& locale
, UErrorCode
& status
);
514 * @param other DateTimePatternGenerator to copy
517 DateTimePatternGenerator(const DateTimePatternGenerator
& other
);
520 * Default assignment operator.
521 * @param other DateTimePatternGenerator to copy
524 DateTimePatternGenerator
& operator=(const DateTimePatternGenerator
& other
);
526 // TODO(ticket:13619): re-enable when UDATPG_NARROW no longer in draft mode.
527 // static const int32_t UDATPG_WIDTH_COUNT = UDATPG_NARROW + 1;
529 Locale pLocale
; // pattern locale
531 DateTimeMatcher
* dtMatcher
;
532 DistanceInfo
*distanceInfo
;
533 PatternMap
*patternMap
;
534 UnicodeString appendItemFormats
[UDATPG_FIELD_COUNT
];
535 // TODO(ticket:13619): [3] -> UDATPG_WIDTH_COUNT
536 UnicodeString fieldDisplayNames
[UDATPG_FIELD_COUNT
][3];
537 UnicodeString dateTimeFormat
;
538 UnicodeString decimal
;
539 DateTimeMatcher
*skipMatcher
;
540 Hashtable
*fAvailableFormatKeyHash
;
541 UnicodeString emptyString
;
542 char16_t fDefaultHourFormatChar
;
544 int32_t fAllowedHourFormats
[7]; // Actually an array of AllowedHourFormat enum type, ending with UNKNOWN.
546 /* internal flags masks for adjustFieldTypes etc. */
549 kDTPGFixFractionalSeconds
= 1,
550 kDTPGSkeletonUsesCapJ
= 2
551 // with #13183, no longer need flags for b, B
554 void initData(const Locale
&locale
, UErrorCode
&status
);
555 void addCanonicalItems(UErrorCode
&status
);
556 void addICUPatterns(const Locale
& locale
, UErrorCode
& status
);
557 void hackTimes(const UnicodeString
& hackPattern
, UErrorCode
& status
);
558 void getCalendarTypeToUse(const Locale
& locale
, CharString
& destination
, UErrorCode
& err
);
559 void consumeShortTimePattern(const UnicodeString
& shortTimePattern
, UErrorCode
& status
);
560 void addCLDRData(const Locale
& locale
, UErrorCode
& status
);
561 UDateTimePatternConflict
addPatternWithSkeleton(const UnicodeString
& pattern
, const UnicodeString
* skeletonToUse
, UBool override
, UnicodeString
& conflictingPattern
, UErrorCode
& status
);
562 void initHashtable(UErrorCode
& status
);
563 void setDateTimeFromCalendar(const Locale
& locale
, UErrorCode
& status
);
564 void setDecimalSymbols(const Locale
& locale
, UErrorCode
& status
);
565 UDateTimePatternField
getAppendFormatNumber(const char* field
) const;
566 #ifndef U_HIDE_DRAFT_API
567 UDateTimePatternField
getFieldAndWidthIndices(const char* key
, UDateTimePGDisplayWidth
* widthP
) const;
568 void setFieldDisplayName(UDateTimePatternField field
, UDateTimePGDisplayWidth width
, const UnicodeString
& value
);
569 UnicodeString
& getMutableFieldDisplayName(UDateTimePatternField field
, UDateTimePGDisplayWidth width
);
570 #endif // U_HIDE_DRAFT_API
571 void getAppendName(UDateTimePatternField field
, UnicodeString
& value
);
572 UnicodeString
mapSkeletonMetacharacters(const UnicodeString
& patternForm
, int32_t* flags
, UDateTimePatternMatchOptions options
, UErrorCode
& status
);
573 int32_t getCanonicalIndex(const UnicodeString
& field
);
574 const UnicodeString
* getBestRaw(DateTimeMatcher
& source
, int32_t includeMask
, DistanceInfo
* missingFields
, const PtnSkeleton
** specifiedSkeletonPtr
= 0);
575 UnicodeString
adjustFieldTypes(const UnicodeString
& pattern
, const PtnSkeleton
* specifiedSkeleton
, int32_t flags
, UDateTimePatternMatchOptions options
= UDATPG_MATCH_NO_OPTIONS
);
576 UnicodeString
getBestAppending(int32_t missingFields
, int32_t flags
, UDateTimePatternMatchOptions options
= UDATPG_MATCH_NO_OPTIONS
);
577 int32_t getTopBitNumber(int32_t foundMask
);
578 void setAvailableFormat(const UnicodeString
&key
, UErrorCode
& status
);
579 UBool
isAvailableFormatSet(const UnicodeString
&key
) const;
580 void copyHashtable(Hashtable
*other
, UErrorCode
&status
);
581 UBool
isCanonicalItem(const UnicodeString
& item
) const;
582 static void U_CALLCONV
loadAllowedHourFormatsData(UErrorCode
&status
);
583 void getAllowedHourFormats(const Locale
&locale
, UErrorCode
&status
);
585 struct AppendItemFormatsSink
;
586 struct AppendItemNamesSink
;
587 struct AvailableFormatsSink
;
588 } ;// end class DateTimePatternGenerator
591 #endif // U_SHOW_CPLUSPLUS_API