• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *******************************************************************************
3 * Copyright (C) 2007, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
6 *
7 * File DTPTNGEN.H
8 *
9 *******************************************************************************
10 */
11 
12 #ifndef __DTPTNGEN_H__
13 #define __DTPTNGEN_H__
14 
15 #include "unicode/datefmt.h"
16 #include "unicode/locid.h"
17 #include "unicode/udat.h"
18 #include "unicode/udatpg.h"
19 
20 U_NAMESPACE_BEGIN
21 
22 class Hashtable;
23 class FormatParser;
24 class DateTimeMatcher;
25 class DistanceInfo;
26 class PatternMap;
27 
28 /**
29  * This class provides flexible generation of date format patterns, like "yy-MM-dd".
30  * The user can build up the generator by adding successive patterns. Once that
31  * is done, a query can be made using a "skeleton", which is a pattern which just
32  * includes the desired fields and lengths. The generator will return the "best fit"
33  * pattern corresponding to that skeleton.
34  * <p>The main method people will use is getBestPattern(String skeleton),
35  * since normally this class is pre-built with data from a particular locale.
36  * However, generators can be built directly from other data as well.
37  * <p><i>Issue: may be useful to also have a function that returns the list of
38  * fields in a pattern, in order, since we have that internally.
39  * That would be useful for getting the UI order of field elements.</i>
40  * @draft ICU 3.8
41 **/
42 class U_I18N_API DateTimePatternGenerator : public UObject {
43 public:
44     /**
45      * Construct a flexible generator according to default locale.
46      * @param status  Output param set to success/failure code on exit,
47      *               which must not indicate a failure before the function call.
48      * @draft ICU 3.8
49      */
50     static DateTimePatternGenerator* U_EXPORT2 createInstance(UErrorCode& status);
51 
52     /**
53      * Construct a flexible generator according to data for a given locale.
54      * @param uLocale
55      * @param status  Output param set to success/failure code on exit,
56      *               which must not indicate a failure before the function call.
57      * @draft ICU 3.8
58      */
59     static DateTimePatternGenerator* U_EXPORT2 createInstance(const Locale& uLocale, UErrorCode& status);
60 
61     /**
62      * Create an empty generator, to be constructed with addPattern(...) etc.
63      * @param status  Output param set to success/failure code on exit,
64      *               which must not indicate a failure before the function call.
65      * @draft ICU 3.8
66      */
67      static DateTimePatternGenerator* U_EXPORT2 createEmptyInstance(UErrorCode& status);
68 
69     /**
70      * Destructor.
71      * @draft ICU 3.8
72      */
73     virtual ~DateTimePatternGenerator();
74 
75     /**
76      * Clone DateTimePatternGenerator object. Clients are responsible for
77      * deleting the DateTimePatternGenerator object cloned.
78      * @draft ICU 3.8
79      */
80     DateTimePatternGenerator* clone() const;
81 
82      /**
83       * Return true if another object is semantically equal to this one.
84       *
85       * @param other    the DateTimePatternGenerator object to be compared with.
86       * @return         true if other is semantically equal to this.
87       * @draft ICU 3.8
88       */
89     UBool operator==(const DateTimePatternGenerator& other) const;
90 
91     /**
92      * Return true if another object is semantically unequal to this one.
93      *
94      * @param other    the DateTimePatternGenerator object to be compared with.
95      * @return         true if other is semantically unequal to this.
96      * @draft ICU 3.8
97      */
98     UBool operator!=(const DateTimePatternGenerator& other) const;
99 
100     /**
101      * Utility to return a unique skeleton from a given pattern. For example,
102      * both "MMM-dd" and "dd/MMM" produce the skeleton "MMMdd".
103      *
104      * @param pattern   Input pattern, such as "dd/MMM"
105      * @param status  Output param set to success/failure code on exit,
106      *                  which must not indicate a failure before the function call.
107      * @return skeleton such as "MMMdd"
108      * @draft ICU 3.8
109      */
110     UnicodeString getSkeleton(const UnicodeString& pattern, UErrorCode& status);
111 
112     /**
113      * Utility to return a unique base skeleton from a given pattern. This is
114      * the same as the skeleton, except that differences in length are minimized
115      * so as to only preserve the difference between string and numeric form. So
116      * for example, both "MMM-dd" and "d/MMM" produce the skeleton "MMMd"
117      * (notice the single d).
118      *
119      * @param pattern  Input pattern, such as "dd/MMM"
120      * @param status  Output param set to success/failure code on exit,
121      *               which must not indicate a failure before the function call.
122      * @return base skeleton, such as "Md"
123      * @draft ICU 3.8
124      */
125     UnicodeString getBaseSkeleton(const UnicodeString& pattern, UErrorCode& status);
126 
127     /**
128      * Adds a pattern to the generator. If the pattern has the same skeleton as
129      * an existing pattern, and the override parameter is set, then the previous
130      * value is overriden. Otherwise, the previous value is retained. In either
131      * case, the conflicting status is set and previous vale is stored in
132      * conflicting pattern.
133      * <p>
134      * Note that single-field patterns (like "MMM") are automatically added, and
135      * don't need to be added explicitly!
136      *
137      * @param pattern   Input pattern, such as "dd/MMM"
138      * @param override  When existing values are to be overridden use true,
139      *                   otherwise use false.
140      * @param conflictingPattern  Previous pattern with the same skeleton.
141      * @param status  Output param set to success/failure code on exit,
142      *               which must not indicate a failure before the function call.
143      * @return conflicting status.  The value could be UDATPG_NO_CONFLICT,
144      *                             UDATPG_BASE_CONFLICT or UDATPG_CONFLICT.
145      * @draft ICU 3.8
146      */
147     UDateTimePatternConflict addPattern(const UnicodeString& pattern,
148                                         UBool override,
149                                         UnicodeString& conflictingPattern,
150                                         UErrorCode& status);
151 
152     /**
153      * An AppendItem format is a pattern used to append a field if there is no
154      * good match. For example, suppose that the input skeleton is "GyyyyMMMd",
155      * and there is no matching pattern internally, but there is a pattern
156      * matching "yyyyMMMd", say "d-MM-yyyy". Then that pattern is used, plus the
157      * G. The way these two are conjoined is by using the AppendItemFormat for G
158      * (era). So if that value is, say "{0}, {1}" then the final resulting
159      * pattern is "d-MM-yyyy, G".
160      * <p>
161      * There are actually three available variables: {0} is the pattern so far,
162      * {1} is the element we are adding, and {2} is the name of the element.
163      * <p>
164      * This reflects the way that the CLDR data is organized.
165      *
166      * @param field  such as UDATPG_ERA_FIELD.
167      * @param value  pattern, such as "{0}, {1}"
168      * @draft ICU 3.8
169      */
170     void setAppendItemFormat(UDateTimePatternField field, const UnicodeString& value);
171 
172     /**
173      * Getter corresponding to setAppendItemFormat. Values below 0 or at or
174      * above UDATPG_FIELD_COUNT are illegal arguments.
175      *
176      * @param  field  such as UDATPG_ERA_FIELD.
177      * @return append pattern for field
178      * @draft ICU 3.8
179      */
180     const UnicodeString& getAppendItemFormat(UDateTimePatternField field) const;
181 
182     /**
183      * Sets the names of field, eg "era" in English for ERA. These are only
184      * used if the corresponding AppendItemFormat is used, and if it contains a
185      * {2} variable.
186      * <p>
187      * This reflects the way that the CLDR data is organized.
188      *
189      * @param field   such as UDATPG_ERA_FIELD.
190      * @param value   name of the field
191      * @draft ICU 3.8
192      */
193     void setAppendItemName(UDateTimePatternField field, const UnicodeString& value);
194 
195     /**
196      * Getter corresponding to setAppendItemNames. Values below 0 or at or above
197      * UDATPG_FIELD_COUNT are illegal arguments.
198      *
199      * @param field  such as UDATPG_ERA_FIELD.
200      * @return name for field
201      * @draft ICU 3.8
202      */
203     const UnicodeString& getAppendItemName(UDateTimePatternField field) const;
204 
205     /**
206      * The date time format is a message format pattern used to compose date and
207      * time patterns. The default value is "{0} {1}", where {0} will be replaced
208      * by the date pattern and {1} will be replaced by the time pattern.
209      * <p>
210      * This is used when the input skeleton contains both date and time fields,
211      * but there is not a close match among the added patterns. For example,
212      * suppose that this object was created by adding "dd-MMM" and "hh:mm", and
213      * its datetimeFormat is the default "{0} {1}". Then if the input skeleton
214      * is "MMMdhmm", there is not an exact match, so the input skeleton is
215      * broken up into two components "MMMd" and "hmm". There are close matches
216      * for those two skeletons, so the result is put together with this pattern,
217      * resulting in "d-MMM h:mm".
218      *
219      * @param dateTimeFormat
220      *            message format pattern, here {0} will be replaced by the date
221      *            pattern and {1} will be replaced by the time pattern.
222      * @draft ICU 3.8
223      */
224     void setDateTimeFormat(const UnicodeString& dateTimeFormat);
225 
226     /**
227      * Getter corresponding to setDateTimeFormat.
228      * @return DateTimeFormat.
229      * @draft ICU 3.8
230      */
231     const UnicodeString& getDateTimeFormat() const;
232 
233     /**
234      * Return the best pattern matching the input skeleton. It is guaranteed to
235      * have all of the fields in the skeleton.
236      *
237      * @param skeleton
238      *            The skeleton is a pattern containing only the variable fields.
239      *            For example, "MMMdd" and "mmhh" are skeletons.
240      * @param status  Output param set to success/failure code on exit,
241      *               which must not indicate a failure before the function call.
242      * @return bestPattern
243      *            The best pattern found from the given skeleton.
244      * @draft ICU 3.8
245      */
246      UnicodeString getBestPattern(const UnicodeString& skeleton, UErrorCode& status);
247 
248 
249     /**
250      * Adjusts the field types (width and subtype) of a pattern to match what is
251      * in a skeleton. That is, if you supply a pattern like "d-M H:m", and a
252      * skeleton of "MMMMddhhmm", then the input pattern is adjusted to be
253      * "dd-MMMM hh:mm". This is used internally to get the best match for the
254      * input skeleton, but can also be used externally.
255      *
256      * @param pattern Input pattern
257      * @param skeleton
258      *            The skeleton is a pattern containing only the variable fields.
259      *            For example, "MMMdd" and "mmhh" are skeletons.
260      * @param status  Output param set to success/failure code on exit,
261      *               which must not indicate a failure before the function call.
262      * @return pattern adjusted to match the skeleton fields widths and subtypes.
263      * @draft ICU 3.8
264      */
265      UnicodeString replaceFieldTypes(const UnicodeString& pattern,
266                                      const UnicodeString& skeleton,
267                                      UErrorCode& status);
268 
269     /**
270      * Return a list of all the skeletons (in canonical form) from this class.
271      *
272      * Call getPatternForSkeleton() to get the corresponding pattern.
273      *
274      * @param status  Output param set to success/failure code on exit,
275      *               which must not indicate a failure before the function call.
276      * @return StringEnumeration with the skeletons.
277      *         The caller must delete the object.
278      * @draft ICU 3.8
279      */
280      StringEnumeration* getSkeletons(UErrorCode& status) const;
281 
282      /**
283       * Get the pattern corresponding to a given skeleton.
284       * @param skeleton
285       * @return pattern corresponding to a given skeleton.
286       * @draft ICU 3.8
287       */
288      const UnicodeString& getPatternForSkeleton(const UnicodeString& skeleton) const;
289 
290     /**
291      * Return a list of all the base skeletons (in canonical form) from this class.
292      *
293      * @param status  Output param set to success/failure code on exit,
294      *               which must not indicate a failure before the function call.
295      * @return a StringEnumeration with the base skeletons.
296      *         The caller must delete the object.
297      * @draft ICU 3.8
298      */
299      StringEnumeration* getBaseSkeletons(UErrorCode& status) const;
300 
301      /**
302       * Return a list of redundant patterns are those which if removed, make no
303       * difference in the resulting getBestPattern values. This method returns a
304       * list of them, to help check the consistency of the patterns used to build
305       * this generator.
306       *
307       * @param status  Output param set to success/failure code on exit,
308       *               which must not indicate a failure before the function call.
309       * @return a StringEnumeration with the redundant pattern.
310       *         The caller must delete the object.
311       * @internal ICU 3.8
312       */
313      StringEnumeration* getRedundants(UErrorCode& status);
314 
315     /**
316      * The decimal value is used in formatting fractions of seconds. If the
317      * skeleton contains fractional seconds, then this is used with the
318      * fractional seconds. For example, suppose that the input pattern is
319      * "hhmmssSSSS", and the best matching pattern internally is "H:mm:ss", and
320      * the decimal string is ",". Then the resulting pattern is modified to be
321      * "H:mm:ss,SSSS"
322      *
323      * @param decimal
324      * @draft ICU 3.8
325      */
326     void setDecimal(const UnicodeString& decimal);
327 
328     /**
329      * Getter corresponding to setDecimal.
330      * @return UnicodeString corresponding to the decimal point
331      * @draft ICU 3.8
332      */
333     const UnicodeString& getDecimal() const;
334 
335     /**
336      * ICU "poor man's RTTI", returns a UClassID for the actual class.
337      *
338      * @draft ICU 3.8
339      */
340     virtual UClassID getDynamicClassID() const;
341 
342     /**
343      * ICU "poor man's RTTI", returns a UClassID for this class.
344      *
345      * @draft ICU 3.8
346      */
347     static UClassID U_EXPORT2 getStaticClassID(void);
348 
349 private:
350     /**
351      * Constructor.
352      * @draft ICU 3.8
353      */
354     DateTimePatternGenerator(UErrorCode & status);
355 
356     /**
357      * Constructor.
358      * @draft ICU 3.8
359      */
360     DateTimePatternGenerator(const Locale& locale, UErrorCode & status);
361 
362     /**
363      * Copy constructor.
364      * @param other DateTimePatternGenerator to copy
365      * @draft ICU 3.8
366      */
367     DateTimePatternGenerator(const DateTimePatternGenerator& other);
368 
369     /**
370      * Default assignment operator.
371      * @param other DateTimePatternGenerator to copy
372      * @draft ICU 3.8
373      */
374     DateTimePatternGenerator& operator=(const DateTimePatternGenerator& other);
375 
376     Locale pLocale;  // pattern locale
377     FormatParser *fp;
378     DateTimeMatcher* dtMatcher;
379     DistanceInfo *distanceInfo;
380     PatternMap *patternMap;
381     UnicodeString appendItemFormats[UDATPG_FIELD_COUNT];
382     UnicodeString appendItemNames[UDATPG_FIELD_COUNT];
383     UnicodeString dateTimeFormat;
384     UnicodeString decimal;
385     DateTimeMatcher *skipMatcher;
386     Hashtable *fAvailableFormatKeyHash;
387     UnicodeString hackPattern;
388     UErrorCode fStatus;
389     UnicodeString emptyString;
390 
391     void initData(const Locale &locale);
392     void addCanonicalItems();
393     void addICUPatterns(const Locale& locale, UErrorCode& status);
394     void hackTimes(const UnicodeString& hackPattern, UErrorCode& status);
395     void addCLDRData(const Locale& locale);
396     void initHashtable(UErrorCode& status);
397     void setDateTimeFromCalendar(const Locale& locale, UErrorCode& status);
398     void setDecimalSymbols(const Locale& locale, UErrorCode& status);
399     UDateTimePatternField getAppendFormatNumber(const char* field) const;
400     UDateTimePatternField getAppendNameNumber(const char* field) const;
401     void getAppendName(UDateTimePatternField field, UnicodeString& value);
402     int32_t getCanonicalIndex(const UnicodeString& field);
403     const UnicodeString* getBestRaw(DateTimeMatcher& source, int32_t includeMask, DistanceInfo* missingFields);
404     UnicodeString adjustFieldTypes(const UnicodeString& pattern, UBool fixFractionalSeconds);
405     UnicodeString getBestAppending(int32_t missingFields);
406     int32_t getTopBitNumber(int32_t foundMask);
407     void setAvailableFormat(const UnicodeString &key, UErrorCode& status);
408     UBool isAvailableFormatSet(const UnicodeString &key) const;
409     void copyHashtable(Hashtable *other);
410     UBool isCanonicalItem(const UnicodeString& item) const;
getStatus()411     UErrorCode getStatus() const {  return fStatus; } ;
412 } ;// end class DateTimePatternGenerator
413 
414 U_NAMESPACE_END
415 
416 #endif
417