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
, UBool skipICUData
= FALSE
);
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;
278 * The general interface to get a display name for a particular date/time field,
279 * in one of several possible display widths.
281 * @param field The desired UDateTimePatternField, such as UDATPG_ERA_FIELD.
282 * @param width The desired UDateTimePGDisplayWidth, such as UDATPG_ABBREVIATED.
283 * @return. The display name for field
286 UnicodeString
getFieldDisplayName(UDateTimePatternField field
, UDateTimePGDisplayWidth width
) const;
289 * The DateTimeFormat is a message format pattern used to compose date and
290 * time patterns. The default pattern in the root locale is "{1} {0}", where
291 * {1} will be replaced by the date pattern and {0} will be replaced by the
292 * time pattern; however, other locales may specify patterns such as
293 * "{1}, {0}" or "{1} 'at' {0}", etc.
295 * This is used when the input skeleton contains both date and time fields,
296 * but there is not a close match among the added patterns. For example,
297 * suppose that this object was created by adding "dd-MMM" and "hh:mm", and
298 * its datetimeFormat is the default "{1} {0}". Then if the input skeleton
299 * is "MMMdhmm", there is not an exact match, so the input skeleton is
300 * broken up into two components "MMMd" and "hmm". There are close matches
301 * for those two skeletons, so the result is put together with this pattern,
302 * resulting in "d-MMM h:mm".
304 * @param dateTimeFormat
305 * message format pattern, here {1} will be replaced by the date
306 * pattern and {0} will be replaced by the time pattern.
309 void setDateTimeFormat(const UnicodeString
& dateTimeFormat
);
312 * Getter corresponding to setDateTimeFormat.
313 * @return DateTimeFormat.
316 const UnicodeString
& getDateTimeFormat() const;
319 * Return the best pattern matching the input skeleton. It is guaranteed to
320 * have all of the fields in the skeleton.
323 * The skeleton is a pattern containing only the variable fields.
324 * For example, "MMMdd" and "mmhh" are skeletons.
325 * @param status Output param set to success/failure code on exit,
326 * which must not indicate a failure before the function call.
327 * @return bestPattern
328 * The best pattern found from the given skeleton.
331 * <h4>Sample code</h4>
332 * \snippet samples/dtptngsample/dtptngsample.cpp getBestPatternExample1
333 * \snippet samples/dtptngsample/dtptngsample.cpp getBestPatternExample
336 UnicodeString
getBestPattern(const UnicodeString
& skeleton
, UErrorCode
& status
);
340 * Return the best pattern matching the input skeleton. It is guaranteed to
341 * have all of the fields in the skeleton.
344 * The skeleton is a pattern containing only the variable fields.
345 * For example, "MMMdd" and "mmhh" are skeletons.
347 * Options for forcing the length of specified fields in the
348 * returned pattern to match those in the skeleton (when this
349 * would not happen otherwise). For default behavior, use
350 * UDATPG_MATCH_NO_OPTIONS.
352 * Output param set to success/failure code on exit,
353 * which must not indicate a failure before the function call.
354 * @return bestPattern
355 * The best pattern found from the given skeleton.
358 UnicodeString
getBestPattern(const UnicodeString
& skeleton
,
359 UDateTimePatternMatchOptions options
,
364 * Adjusts the field types (width and subtype) of a pattern to match what is
365 * in a skeleton. That is, if you supply a pattern like "d-M H:m", and a
366 * skeleton of "MMMMddhhmm", then the input pattern is adjusted to be
367 * "dd-MMMM hh:mm". This is used internally to get the best match for the
368 * input skeleton, but can also be used externally.
370 * @param pattern Input pattern
372 * The skeleton is a pattern containing only the variable fields.
373 * For example, "MMMdd" and "mmhh" are skeletons.
374 * @param status Output param set to success/failure code on exit,
375 * which must not indicate a failure before the function call.
376 * @return pattern adjusted to match the skeleton fields widths and subtypes.
379 * <h4>Sample code</h4>
380 * \snippet samples/dtptngsample/dtptngsample.cpp getBestPatternExample1
381 * \snippet samples/dtptngsample/dtptngsample.cpp replaceFieldTypesExample
384 UnicodeString
replaceFieldTypes(const UnicodeString
& pattern
,
385 const UnicodeString
& skeleton
,
389 * Adjusts the field types (width and subtype) of a pattern to match what is
390 * in a skeleton. That is, if you supply a pattern like "d-M H:m", and a
391 * skeleton of "MMMMddhhmm", then the input pattern is adjusted to be
392 * "dd-MMMM hh:mm". This is used internally to get the best match for the
393 * input skeleton, but can also be used externally.
395 * @param pattern Input pattern
397 * The skeleton is a pattern containing only the variable fields.
398 * For example, "MMMdd" and "mmhh" are skeletons.
400 * Options controlling whether the length of specified fields in the
401 * pattern are adjusted to match those in the skeleton (when this
402 * would not happen otherwise). For default behavior, use
403 * UDATPG_MATCH_NO_OPTIONS.
405 * Output param set to success/failure code on exit,
406 * which must not indicate a failure before the function call.
407 * @return pattern adjusted to match the skeleton fields widths and subtypes.
410 UnicodeString
replaceFieldTypes(const UnicodeString
& pattern
,
411 const UnicodeString
& skeleton
,
412 UDateTimePatternMatchOptions options
,
416 * Return a list of all the skeletons (in canonical form) from this class.
418 * Call getPatternForSkeleton() to get the corresponding pattern.
420 * @param status Output param set to success/failure code on exit,
421 * which must not indicate a failure before the function call.
422 * @return StringEnumeration with the skeletons.
423 * The caller must delete the object.
426 StringEnumeration
* getSkeletons(UErrorCode
& status
) const;
429 * Get the pattern corresponding to a given skeleton.
431 * @return pattern corresponding to a given skeleton.
434 const UnicodeString
& getPatternForSkeleton(const UnicodeString
& skeleton
) const;
437 * Return a list of all the base skeletons (in canonical form) from this class.
439 * @param status Output param set to success/failure code on exit,
440 * which must not indicate a failure before the function call.
441 * @return a StringEnumeration with the base skeletons.
442 * The caller must delete the object.
445 StringEnumeration
* getBaseSkeletons(UErrorCode
& status
) const;
447 #ifndef U_HIDE_INTERNAL_API
449 * Return a list of redundant patterns are those which if removed, make no
450 * difference in the resulting getBestPattern values. This method returns a
451 * list of them, to help check the consistency of the patterns used to build
454 * @param status Output param set to success/failure code on exit,
455 * which must not indicate a failure before the function call.
456 * @return a StringEnumeration with the redundant pattern.
457 * The caller must delete the object.
460 StringEnumeration
* getRedundants(UErrorCode
& status
);
461 #endif /* U_HIDE_INTERNAL_API */
464 * The decimal value is used in formatting fractions of seconds. If the
465 * skeleton contains fractional seconds, then this is used with the
466 * fractional seconds. For example, suppose that the input pattern is
467 * "hhmmssSSSS", and the best matching pattern internally is "H:mm:ss", and
468 * the decimal string is ",". Then the resulting pattern is modified to be
474 void setDecimal(const UnicodeString
& decimal
);
477 * Getter corresponding to setDecimal.
478 * @return UnicodeString corresponding to the decimal point
481 const UnicodeString
& getDecimal() const;
484 * ICU "poor man's RTTI", returns a UClassID for the actual class.
488 virtual UClassID
getDynamicClassID() const;
491 * ICU "poor man's RTTI", returns a UClassID for this class.
495 static UClassID U_EXPORT2
getStaticClassID(void);
501 DateTimePatternGenerator(UErrorCode
& status
);
506 DateTimePatternGenerator(const Locale
& locale
, UErrorCode
& status
, UBool skipICUData
= FALSE
);
510 * @param other DateTimePatternGenerator to copy
512 DateTimePatternGenerator(const DateTimePatternGenerator
& other
);
515 * Default assignment operator.
516 * @param other DateTimePatternGenerator to copy
518 DateTimePatternGenerator
& operator=(const DateTimePatternGenerator
& other
);
520 // TODO(ticket:13619): re-enable when UDATPG_NARROW no longer in draft mode.
521 // static const int32_t UDATPG_WIDTH_COUNT = UDATPG_NARROW + 1;
523 Locale pLocale
; // pattern locale
525 DateTimeMatcher
* dtMatcher
;
526 DistanceInfo
*distanceInfo
;
527 PatternMap
*patternMap
;
528 UnicodeString appendItemFormats
[UDATPG_FIELD_COUNT
];
529 // TODO(ticket:13619): [3] -> UDATPG_WIDTH_COUNT
530 UnicodeString fieldDisplayNames
[UDATPG_FIELD_COUNT
][3];
531 UnicodeString dateTimeFormat
;
532 UnicodeString decimal
;
533 DateTimeMatcher
*skipMatcher
;
534 Hashtable
*fAvailableFormatKeyHash
;
535 UnicodeString emptyString
;
536 char16_t fDefaultHourFormatChar
;
538 int32_t fAllowedHourFormats
[7]; // Actually an array of AllowedHourFormat enum type, ending with UNKNOWN.
540 // Internal error code used for recording/reporting errors that occur during methods that do not
541 // have a UErrorCode parameter. For example: the Copy Constructor, or the ::clone() method.
542 // When this is set to an error the object is in an invalid state.
543 UErrorCode internalErrorCode
;
545 /* internal flags masks for adjustFieldTypes etc. */
548 kDTPGFixFractionalSeconds
= 1,
549 kDTPGSkeletonUsesCapJ
= 2
550 // with #13183, no longer need flags for b, B
553 void initData(const Locale
&locale
, UErrorCode
&status
, UBool skipICUData
= FALSE
);
554 void addCanonicalItems(UErrorCode
&status
);
555 void addICUPatterns(const Locale
& locale
, UErrorCode
& status
);
556 void hackTimes(const UnicodeString
& hackPattern
, UErrorCode
& status
);
557 void getCalendarTypeToUse(const Locale
& locale
, CharString
& destination
, UErrorCode
& err
);
558 void consumeShortTimePattern(const UnicodeString
& shortTimePattern
, UErrorCode
& status
);
559 void addCLDRData(const Locale
& locale
, UErrorCode
& status
);
560 UDateTimePatternConflict
addPatternWithSkeleton(const UnicodeString
& pattern
, const UnicodeString
* skeletonToUse
, UBool override
, UnicodeString
& conflictingPattern
, UErrorCode
& status
);
561 void initHashtable(UErrorCode
& status
);
562 void setDateTimeFromCalendar(const Locale
& locale
, UErrorCode
& status
);
563 void setDecimalSymbols(const Locale
& locale
, UErrorCode
& status
);
564 UDateTimePatternField
getAppendFormatNumber(const char* field
) const;
565 #ifndef U_HIDE_DRAFT_API
566 // The following three have to be U_HIDE_DRAFT_API (though private) because UDateTimePGDisplayWidth is
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
, UErrorCode
& status
, 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
, UErrorCode
& status
, UDateTimePatternMatchOptions options
= UDATPG_MATCH_NO_OPTIONS
);
577 int32_t getTopBitNumber(int32_t foundMask
) const;
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