jpayne@69
|
1 // © 2016 and later: Unicode, Inc. and others.
|
jpayne@69
|
2 // License & terms of use: http://www.unicode.org/copyright.html
|
jpayne@69
|
3 /*
|
jpayne@69
|
4 *******************************************************************************
|
jpayne@69
|
5 * Copyright (C) 2008-2014, Google, International Business Machines Corporation
|
jpayne@69
|
6 * and others. All Rights Reserved.
|
jpayne@69
|
7 *******************************************************************************
|
jpayne@69
|
8 */
|
jpayne@69
|
9
|
jpayne@69
|
10 #ifndef __TMUTFMT_H__
|
jpayne@69
|
11 #define __TMUTFMT_H__
|
jpayne@69
|
12
|
jpayne@69
|
13 #include "unicode/utypes.h"
|
jpayne@69
|
14
|
jpayne@69
|
15 /**
|
jpayne@69
|
16 * \file
|
jpayne@69
|
17 * \brief C++ API: Format and parse duration in single time unit
|
jpayne@69
|
18 */
|
jpayne@69
|
19
|
jpayne@69
|
20
|
jpayne@69
|
21 #if U_SHOW_CPLUSPLUS_API
|
jpayne@69
|
22
|
jpayne@69
|
23 #if !UCONFIG_NO_FORMATTING
|
jpayne@69
|
24
|
jpayne@69
|
25 #include "unicode/unistr.h"
|
jpayne@69
|
26 #include "unicode/tmunit.h"
|
jpayne@69
|
27 #include "unicode/tmutamt.h"
|
jpayne@69
|
28 #include "unicode/measfmt.h"
|
jpayne@69
|
29 #include "unicode/numfmt.h"
|
jpayne@69
|
30 #include "unicode/plurrule.h"
|
jpayne@69
|
31
|
jpayne@69
|
32 #ifndef U_HIDE_DEPRECATED_API
|
jpayne@69
|
33
|
jpayne@69
|
34 /**
|
jpayne@69
|
35 * Constants for various styles.
|
jpayne@69
|
36 * There are 2 styles: full name and abbreviated name.
|
jpayne@69
|
37 * For example, for English, the full name for hour duration is "3 hours",
|
jpayne@69
|
38 * and the abbreviated name is "3 hrs".
|
jpayne@69
|
39 * @deprecated ICU 53 Use MeasureFormat and UMeasureFormatWidth instead.
|
jpayne@69
|
40 */
|
jpayne@69
|
41 enum UTimeUnitFormatStyle {
|
jpayne@69
|
42 /** @deprecated ICU 53 */
|
jpayne@69
|
43 UTMUTFMT_FULL_STYLE,
|
jpayne@69
|
44 /** @deprecated ICU 53 */
|
jpayne@69
|
45 UTMUTFMT_ABBREVIATED_STYLE,
|
jpayne@69
|
46 /** @deprecated ICU 53 */
|
jpayne@69
|
47 UTMUTFMT_FORMAT_STYLE_COUNT
|
jpayne@69
|
48 };
|
jpayne@69
|
49 typedef enum UTimeUnitFormatStyle UTimeUnitFormatStyle; /**< @deprecated ICU 53 */
|
jpayne@69
|
50
|
jpayne@69
|
51
|
jpayne@69
|
52 U_NAMESPACE_BEGIN
|
jpayne@69
|
53
|
jpayne@69
|
54 class Hashtable;
|
jpayne@69
|
55 class UVector;
|
jpayne@69
|
56
|
jpayne@69
|
57 struct TimeUnitFormatReadSink;
|
jpayne@69
|
58
|
jpayne@69
|
59 /**
|
jpayne@69
|
60 * Format or parse a TimeUnitAmount, using plural rules for the units where available.
|
jpayne@69
|
61 *
|
jpayne@69
|
62 * <P>
|
jpayne@69
|
63 * Code Sample:
|
jpayne@69
|
64 * <pre>
|
jpayne@69
|
65 * // create time unit amount instance - a combination of Number and time unit
|
jpayne@69
|
66 * UErrorCode status = U_ZERO_ERROR;
|
jpayne@69
|
67 * TimeUnitAmount* source = new TimeUnitAmount(2, TimeUnit::UTIMEUNIT_YEAR, status);
|
jpayne@69
|
68 * // create time unit format instance
|
jpayne@69
|
69 * TimeUnitFormat* format = new TimeUnitFormat(Locale("en"), status);
|
jpayne@69
|
70 * // format a time unit amount
|
jpayne@69
|
71 * UnicodeString formatted;
|
jpayne@69
|
72 * Formattable formattable;
|
jpayne@69
|
73 * if (U_SUCCESS(status)) {
|
jpayne@69
|
74 * formattable.adoptObject(source);
|
jpayne@69
|
75 * formatted = ((Format*)format)->format(formattable, formatted, status);
|
jpayne@69
|
76 * Formattable result;
|
jpayne@69
|
77 * ((Format*)format)->parseObject(formatted, result, status);
|
jpayne@69
|
78 * if (U_SUCCESS(status)) {
|
jpayne@69
|
79 * assert (result == formattable);
|
jpayne@69
|
80 * }
|
jpayne@69
|
81 * }
|
jpayne@69
|
82 * </pre>
|
jpayne@69
|
83 *
|
jpayne@69
|
84 * <P>
|
jpayne@69
|
85 * @see TimeUnitAmount
|
jpayne@69
|
86 * @see TimeUnitFormat
|
jpayne@69
|
87 * @deprecated ICU 53 Use the MeasureFormat class instead.
|
jpayne@69
|
88 */
|
jpayne@69
|
89 class U_I18N_API TimeUnitFormat: public MeasureFormat {
|
jpayne@69
|
90 public:
|
jpayne@69
|
91
|
jpayne@69
|
92 /**
|
jpayne@69
|
93 * Create TimeUnitFormat with default locale, and full name style.
|
jpayne@69
|
94 * Use setLocale and/or setFormat to modify.
|
jpayne@69
|
95 * @deprecated ICU 53
|
jpayne@69
|
96 */
|
jpayne@69
|
97 TimeUnitFormat(UErrorCode& status);
|
jpayne@69
|
98
|
jpayne@69
|
99 /**
|
jpayne@69
|
100 * Create TimeUnitFormat given locale, and full name style.
|
jpayne@69
|
101 * @deprecated ICU 53
|
jpayne@69
|
102 */
|
jpayne@69
|
103 TimeUnitFormat(const Locale& locale, UErrorCode& status);
|
jpayne@69
|
104
|
jpayne@69
|
105 /**
|
jpayne@69
|
106 * Create TimeUnitFormat given locale and style.
|
jpayne@69
|
107 * @deprecated ICU 53
|
jpayne@69
|
108 */
|
jpayne@69
|
109 TimeUnitFormat(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status);
|
jpayne@69
|
110
|
jpayne@69
|
111 /**
|
jpayne@69
|
112 * Copy constructor.
|
jpayne@69
|
113 * @deprecated ICU 53
|
jpayne@69
|
114 */
|
jpayne@69
|
115 TimeUnitFormat(const TimeUnitFormat&);
|
jpayne@69
|
116
|
jpayne@69
|
117 /**
|
jpayne@69
|
118 * deconstructor
|
jpayne@69
|
119 * @deprecated ICU 53
|
jpayne@69
|
120 */
|
jpayne@69
|
121 virtual ~TimeUnitFormat();
|
jpayne@69
|
122
|
jpayne@69
|
123 /**
|
jpayne@69
|
124 * Clone this Format object polymorphically. The caller owns the result and
|
jpayne@69
|
125 * should delete it when done.
|
jpayne@69
|
126 * @return A copy of the object.
|
jpayne@69
|
127 * @deprecated ICU 53
|
jpayne@69
|
128 */
|
jpayne@69
|
129 virtual TimeUnitFormat* clone() const;
|
jpayne@69
|
130
|
jpayne@69
|
131 /**
|
jpayne@69
|
132 * Assignment operator
|
jpayne@69
|
133 * @deprecated ICU 53
|
jpayne@69
|
134 */
|
jpayne@69
|
135 TimeUnitFormat& operator=(const TimeUnitFormat& other);
|
jpayne@69
|
136
|
jpayne@69
|
137 /**
|
jpayne@69
|
138 * Return true if the given Format objects are not semantically equal.
|
jpayne@69
|
139 * Objects of different subclasses are considered unequal.
|
jpayne@69
|
140 * @param other the object to be compared with.
|
jpayne@69
|
141 * @return true if the given Format objects are not semantically equal.
|
jpayne@69
|
142 * @deprecated ICU 53
|
jpayne@69
|
143 */
|
jpayne@69
|
144 UBool operator!=(const Format& other) const;
|
jpayne@69
|
145
|
jpayne@69
|
146 /**
|
jpayne@69
|
147 * Set the locale used for formatting or parsing.
|
jpayne@69
|
148 * @param locale the locale to be set
|
jpayne@69
|
149 * @param status output param set to success/failure code on exit
|
jpayne@69
|
150 * @deprecated ICU 53
|
jpayne@69
|
151 */
|
jpayne@69
|
152 void setLocale(const Locale& locale, UErrorCode& status);
|
jpayne@69
|
153
|
jpayne@69
|
154
|
jpayne@69
|
155 /**
|
jpayne@69
|
156 * Set the number format used for formatting or parsing.
|
jpayne@69
|
157 * @param format the number formatter to be set
|
jpayne@69
|
158 * @param status output param set to success/failure code on exit
|
jpayne@69
|
159 * @deprecated ICU 53
|
jpayne@69
|
160 */
|
jpayne@69
|
161 void setNumberFormat(const NumberFormat& format, UErrorCode& status);
|
jpayne@69
|
162
|
jpayne@69
|
163 /**
|
jpayne@69
|
164 * Parse a TimeUnitAmount.
|
jpayne@69
|
165 * @see Format#parseObject(const UnicodeString&, Formattable&, ParsePosition&) const;
|
jpayne@69
|
166 * @deprecated ICU 53
|
jpayne@69
|
167 */
|
jpayne@69
|
168 virtual void parseObject(const UnicodeString& source,
|
jpayne@69
|
169 Formattable& result,
|
jpayne@69
|
170 ParsePosition& pos) const;
|
jpayne@69
|
171
|
jpayne@69
|
172 /**
|
jpayne@69
|
173 * Return the class ID for this class. This is useful only for comparing to
|
jpayne@69
|
174 * a return value from getDynamicClassID(). For example:
|
jpayne@69
|
175 * <pre>
|
jpayne@69
|
176 * . Base* polymorphic_pointer = createPolymorphicObject();
|
jpayne@69
|
177 * . if (polymorphic_pointer->getDynamicClassID() ==
|
jpayne@69
|
178 * . erived::getStaticClassID()) ...
|
jpayne@69
|
179 * </pre>
|
jpayne@69
|
180 * @return The class ID for all objects of this class.
|
jpayne@69
|
181 * @deprecated ICU 53
|
jpayne@69
|
182 */
|
jpayne@69
|
183 static UClassID U_EXPORT2 getStaticClassID(void);
|
jpayne@69
|
184
|
jpayne@69
|
185 /**
|
jpayne@69
|
186 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
|
jpayne@69
|
187 * method is to implement a simple version of RTTI, since not all C++
|
jpayne@69
|
188 * compilers support genuine RTTI. Polymorphic operator==() and clone()
|
jpayne@69
|
189 * methods call this method.
|
jpayne@69
|
190 *
|
jpayne@69
|
191 * @return The class ID for this object. All objects of a
|
jpayne@69
|
192 * given class have the same class ID. Objects of
|
jpayne@69
|
193 * other classes have different class IDs.
|
jpayne@69
|
194 * @deprecated ICU 53
|
jpayne@69
|
195 */
|
jpayne@69
|
196 virtual UClassID getDynamicClassID(void) const;
|
jpayne@69
|
197
|
jpayne@69
|
198 private:
|
jpayne@69
|
199 Hashtable* fTimeUnitToCountToPatterns[TimeUnit::UTIMEUNIT_FIELD_COUNT];
|
jpayne@69
|
200 UTimeUnitFormatStyle fStyle;
|
jpayne@69
|
201
|
jpayne@69
|
202 void create(UTimeUnitFormatStyle style, UErrorCode& status);
|
jpayne@69
|
203
|
jpayne@69
|
204 // it might actually be simpler to make them Decimal Formats later.
|
jpayne@69
|
205 // initialize all private data members
|
jpayne@69
|
206 void setup(UErrorCode& status);
|
jpayne@69
|
207
|
jpayne@69
|
208 // initialize data member without fill in data for fTimeUnitToCountToPattern
|
jpayne@69
|
209 void initDataMembers(UErrorCode& status);
|
jpayne@69
|
210
|
jpayne@69
|
211 // initialize fTimeUnitToCountToPatterns from current locale's resource.
|
jpayne@69
|
212 void readFromCurrentLocale(UTimeUnitFormatStyle style, const char* key, const UVector& pluralCounts,
|
jpayne@69
|
213 UErrorCode& status);
|
jpayne@69
|
214
|
jpayne@69
|
215 // check completeness of fTimeUnitToCountToPatterns against all time units,
|
jpayne@69
|
216 // and all plural rules, fill in fallback as necessary.
|
jpayne@69
|
217 void checkConsistency(UTimeUnitFormatStyle style, const char* key, UErrorCode& status);
|
jpayne@69
|
218
|
jpayne@69
|
219 // fill in fTimeUnitToCountToPatterns from locale fall-back chain
|
jpayne@69
|
220 void searchInLocaleChain(UTimeUnitFormatStyle style, const char* key, const char* localeName,
|
jpayne@69
|
221 TimeUnit::UTimeUnitFields field, const UnicodeString&,
|
jpayne@69
|
222 const char*, Hashtable*, UErrorCode&);
|
jpayne@69
|
223
|
jpayne@69
|
224 // initialize hash table
|
jpayne@69
|
225 Hashtable* initHash(UErrorCode& status);
|
jpayne@69
|
226
|
jpayne@69
|
227 // delete hash table
|
jpayne@69
|
228 void deleteHash(Hashtable* htable);
|
jpayne@69
|
229
|
jpayne@69
|
230 // copy hash table
|
jpayne@69
|
231 void copyHash(const Hashtable* source, Hashtable* target, UErrorCode& status);
|
jpayne@69
|
232 // get time unit name, such as "year", from time unit field enum, such as
|
jpayne@69
|
233 // UTIMEUNIT_YEAR.
|
jpayne@69
|
234 static const char* getTimeUnitName(TimeUnit::UTimeUnitFields field, UErrorCode& status);
|
jpayne@69
|
235
|
jpayne@69
|
236 friend struct TimeUnitFormatReadSink;
|
jpayne@69
|
237 };
|
jpayne@69
|
238
|
jpayne@69
|
239 inline UBool
|
jpayne@69
|
240 TimeUnitFormat::operator!=(const Format& other) const {
|
jpayne@69
|
241 return !operator==(other);
|
jpayne@69
|
242 }
|
jpayne@69
|
243
|
jpayne@69
|
244 U_NAMESPACE_END
|
jpayne@69
|
245
|
jpayne@69
|
246 #endif /* U_HIDE_DEPRECATED_API */
|
jpayne@69
|
247 #endif /* #if !UCONFIG_NO_FORMATTING */
|
jpayne@69
|
248
|
jpayne@69
|
249 #endif /* U_SHOW_CPLUSPLUS_API */
|
jpayne@69
|
250
|
jpayne@69
|
251 #endif // __TMUTFMT_H__
|
jpayne@69
|
252 //eof
|