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