1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2008-2014, Google, International Business Machines Corporation
6 * and others. All Rights Reserved.
7 *******************************************************************************
13 #include "unicode/utypes.h"
17 * \brief C++ API: Format and parse duration in single time unit
21 #if !UCONFIG_NO_FORMATTING
22 #ifndef U_HIDE_DEPRECATED_API
24 #include "unicode/unistr.h"
25 #include "unicode/tmunit.h"
26 #include "unicode/tmutamt.h"
27 #include "unicode/measfmt.h"
28 #include "unicode/numfmt.h"
29 #include "unicode/plurrule.h"
33 * Constants for various styles.
34 * There are 2 styles: full name and abbreviated name.
35 * For example, for English, the full name for hour duration is "3 hours",
36 * and the abbreviated name is "3 hrs".
37 * @deprecated ICU 53 Use MeasureFormat and UMeasureFormatWidth instead.
39 enum UTimeUnitFormatStyle
{
40 /** @deprecated ICU 53 */
42 /** @deprecated ICU 53 */
43 UTMUTFMT_ABBREVIATED_STYLE
,
44 /** @deprecated ICU 53 */
45 UTMUTFMT_FORMAT_STYLE_COUNT
47 typedef enum UTimeUnitFormatStyle UTimeUnitFormatStyle
; /**< @deprecated ICU 53 */
50 #if U_SHOW_CPLUSPLUS_API
56 struct TimeUnitFormatReadSink
;
59 * Format or parse a TimeUnitAmount, using plural rules for the units where available.
64 * // create time unit amount instance - a combination of Number and time unit
65 * UErrorCode status = U_ZERO_ERROR;
66 * TimeUnitAmount* source = new TimeUnitAmount(2, TimeUnit::UTIMEUNIT_YEAR, status);
67 * // create time unit format instance
68 * TimeUnitFormat* format = new TimeUnitFormat(Locale("en"), status);
69 * // format a time unit amount
70 * UnicodeString formatted;
71 * Formattable formattable;
72 * if (U_SUCCESS(status)) {
73 * formattable.adoptObject(source);
74 * formatted = ((Format*)format)->format(formattable, formatted, status);
76 * ((Format*)format)->parseObject(formatted, result, status);
77 * if (U_SUCCESS(status)) {
78 * assert (result == formattable);
86 * @deprecated ICU 53 Use the MeasureFormat class instead.
88 class U_I18N_API TimeUnitFormat
: public MeasureFormat
{
92 * Create TimeUnitFormat with default locale, and full name style.
93 * Use setLocale and/or setFormat to modify.
96 TimeUnitFormat(UErrorCode
& status
);
99 * Create TimeUnitFormat given locale, and full name style.
102 TimeUnitFormat(const Locale
& locale
, UErrorCode
& status
);
105 * Create TimeUnitFormat given locale and style.
108 TimeUnitFormat(const Locale
& locale
, UTimeUnitFormatStyle style
, UErrorCode
& status
);
114 TimeUnitFormat(const TimeUnitFormat
&);
120 virtual ~TimeUnitFormat();
123 * Clone this Format object polymorphically. The caller owns the result and
124 * should delete it when done.
125 * @return A copy of the object.
128 virtual Format
* clone(void) const;
131 * Assignment operator
134 TimeUnitFormat
& operator=(const TimeUnitFormat
& other
);
137 * Return true if the given Format objects are not semantically equal.
138 * Objects of different subclasses are considered unequal.
139 * @param other the object to be compared with.
140 * @return true if the given Format objects are not semantically equal.
143 UBool
operator!=(const Format
& other
) const;
146 * Set the locale used for formatting or parsing.
147 * @param locale the locale to be set
148 * @param status output param set to success/failure code on exit
151 void setLocale(const Locale
& locale
, UErrorCode
& status
);
155 * Set the number format used for formatting or parsing.
156 * @param format the number formatter to be set
157 * @param status output param set to success/failure code on exit
160 void setNumberFormat(const NumberFormat
& format
, UErrorCode
& status
);
163 * Parse a TimeUnitAmount.
164 * @see Format#parseObject(const UnicodeString&, Formattable&, ParsePosition&) const;
167 virtual void parseObject(const UnicodeString
& source
,
169 ParsePosition
& pos
) const;
172 * Return the class ID for this class. This is useful only for comparing to
173 * a return value from getDynamicClassID(). For example:
175 * . Base* polymorphic_pointer = createPolymorphicObject();
176 * . if (polymorphic_pointer->getDynamicClassID() ==
177 * . erived::getStaticClassID()) ...
179 * @return The class ID for all objects of this class.
182 static UClassID U_EXPORT2
getStaticClassID(void);
185 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
186 * method is to implement a simple version of RTTI, since not all C++
187 * compilers support genuine RTTI. Polymorphic operator==() and clone()
188 * methods call this method.
190 * @return The class ID for this object. All objects of a
191 * given class have the same class ID. Objects of
192 * other classes have different class IDs.
195 virtual UClassID
getDynamicClassID(void) const;
198 Hashtable
* fTimeUnitToCountToPatterns
[TimeUnit::UTIMEUNIT_FIELD_COUNT
];
199 UTimeUnitFormatStyle fStyle
;
201 void create(UTimeUnitFormatStyle style
, UErrorCode
& status
);
203 // it might actually be simpler to make them Decimal Formats later.
204 // initialize all private data members
205 void setup(UErrorCode
& status
);
207 // initialize data member without fill in data for fTimeUnitToCountToPattern
208 void initDataMembers(UErrorCode
& status
);
210 // initialize fTimeUnitToCountToPatterns from current locale's resource.
211 void readFromCurrentLocale(UTimeUnitFormatStyle style
, const char* key
, const UVector
& pluralCounts
,
214 // check completeness of fTimeUnitToCountToPatterns against all time units,
215 // and all plural rules, fill in fallback as necessary.
216 void checkConsistency(UTimeUnitFormatStyle style
, const char* key
, UErrorCode
& status
);
218 // fill in fTimeUnitToCountToPatterns from locale fall-back chain
219 void searchInLocaleChain(UTimeUnitFormatStyle style
, const char* key
, const char* localeName
,
220 TimeUnit::UTimeUnitFields field
, const UnicodeString
&,
221 const char*, Hashtable
*, UErrorCode
&);
223 // initialize hash table
224 Hashtable
* initHash(UErrorCode
& status
);
227 void deleteHash(Hashtable
* htable
);
230 void copyHash(const Hashtable
* source
, Hashtable
* target
, UErrorCode
& status
);
231 // get time unit name, such as "year", from time unit field enum, such as
233 static const char* getTimeUnitName(TimeUnit::UTimeUnitFields field
, UErrorCode
& status
);
235 friend struct TimeUnitFormatReadSink
;
239 TimeUnitFormat::operator!=(const Format
& other
) const {
240 return !operator==(other
);
244 #endif // U_SHOW_CPLUSPLUS_API
246 #endif /* U_HIDE_DEPRECATED_API */
247 #endif /* #if !UCONFIG_NO_FORMATTING */
249 #endif // __TMUTFMT_H__