2 *******************************************************************************
3 * Copyright (C) 2008-2014, Google, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 *******************************************************************************
11 #include "unicode/utypes.h"
15 * \brief C++ API: Format and parse duration in single time unit
19 #if !UCONFIG_NO_FORMATTING
20 #ifndef U_HIDE_DEPRECATED_API
22 #include "unicode/unistr.h"
23 #include "unicode/tmunit.h"
24 #include "unicode/tmutamt.h"
25 #include "unicode/measfmt.h"
26 #include "unicode/numfmt.h"
27 #include "unicode/plurrule.h"
31 * Constants for various styles.
32 * There are 2 styles: full name and abbreviated name.
33 * For example, for English, the full name for hour duration is "3 hours",
34 * and the abbreviated name is "3 hrs".
35 * @deprecated ICU 53 Use MeasureFormat and UMeasureFormatWidth instead.
37 enum UTimeUnitFormatStyle
{
38 /** @deprecated ICU 53 */
40 /** @deprecated ICU 53 */
41 UTMUTFMT_ABBREVIATED_STYLE
,
42 /** @deprecated ICU 53 */
43 UTMUTFMT_FORMAT_STYLE_COUNT
45 typedef enum UTimeUnitFormatStyle UTimeUnitFormatStyle
; /**< @deprecated ICU 53 */
54 * Format or parse a TimeUnitAmount, using plural rules for the units where available.
59 * // create time unit amount instance - a combination of Number and time unit
60 * UErrorCode status = U_ZERO_ERROR;
61 * TimeUnitAmount* source = new TimeUnitAmount(2, TimeUnit::UTIMEUNIT_YEAR, status);
62 * // create time unit format instance
63 * TimeUnitFormat* format = new TimeUnitFormat(Locale("en"), status);
64 * // format a time unit amount
65 * UnicodeString formatted;
66 * Formattable formattable;
67 * if (U_SUCCESS(status)) {
68 * formattable.adoptObject(source);
69 * formatted = ((Format*)format)->format(formattable, formatted, status);
71 * ((Format*)format)->parseObject(formatted, result, status);
72 * if (U_SUCCESS(status)) {
73 * assert (result == formattable);
81 * @deprecated ICU 53 Use the MeasureFormat class instead.
83 class U_I18N_API TimeUnitFormat
: public MeasureFormat
{
87 * Create TimeUnitFormat with default locale, and full name style.
88 * Use setLocale and/or setFormat to modify.
91 TimeUnitFormat(UErrorCode
& status
);
94 * Create TimeUnitFormat given locale, and full name style.
97 TimeUnitFormat(const Locale
& locale
, UErrorCode
& status
);
100 * Create TimeUnitFormat given locale and style.
103 TimeUnitFormat(const Locale
& locale
, UTimeUnitFormatStyle style
, UErrorCode
& status
);
109 TimeUnitFormat(const TimeUnitFormat
&);
115 virtual ~TimeUnitFormat();
118 * Clone this Format object polymorphically. The caller owns the result and
119 * should delete it when done.
120 * @return A copy of the object.
123 virtual Format
* clone(void) const;
126 * Assignment operator
129 TimeUnitFormat
& operator=(const TimeUnitFormat
& other
);
132 * Return true if the given Format objects are not semantically equal.
133 * Objects of different subclasses are considered unequal.
134 * @param other the object to be compared with.
135 * @return true if the given Format objects are not semantically equal.
138 UBool
operator!=(const Format
& other
) const;
141 * Set the locale used for formatting or parsing.
142 * @param locale the locale to be set
143 * @param status output param set to success/failure code on exit
146 void setLocale(const Locale
& locale
, UErrorCode
& status
);
150 * Set the number format used for formatting or parsing.
151 * @param format the number formatter to be set
152 * @param status output param set to success/failure code on exit
155 void setNumberFormat(const NumberFormat
& format
, UErrorCode
& status
);
158 * Parse a TimeUnitAmount.
159 * @see Format#parseObject(const UnicodeString&, Formattable&, ParsePosition&) const;
162 virtual void parseObject(const UnicodeString
& source
,
164 ParsePosition
& pos
) const;
167 * Return the class ID for this class. This is useful only for comparing to
168 * a return value from getDynamicClassID(). For example:
170 * . Base* polymorphic_pointer = createPolymorphicObject();
171 * . if (polymorphic_pointer->getDynamicClassID() ==
172 * . erived::getStaticClassID()) ...
174 * @return The class ID for all objects of this class.
177 static UClassID U_EXPORT2
getStaticClassID(void);
180 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
181 * method is to implement a simple version of RTTI, since not all C++
182 * compilers support genuine RTTI. Polymorphic operator==() and clone()
183 * methods call this method.
185 * @return The class ID for this object. All objects of a
186 * given class have the same class ID. Objects of
187 * other classes have different class IDs.
190 virtual UClassID
getDynamicClassID(void) const;
193 Hashtable
* fTimeUnitToCountToPatterns
[TimeUnit::UTIMEUNIT_FIELD_COUNT
];
194 UTimeUnitFormatStyle fStyle
;
196 void create(UTimeUnitFormatStyle style
, UErrorCode
& status
);
198 // it might actually be simpler to make them Decimal Formats later.
199 // initialize all private data members
200 void setup(UErrorCode
& status
);
202 // initialize data member without fill in data for fTimeUnitToCountToPattern
203 void initDataMembers(UErrorCode
& status
);
205 // initialize fTimeUnitToCountToPatterns from current locale's resource.
206 void readFromCurrentLocale(UTimeUnitFormatStyle style
, const char* key
, const UVector
& pluralCounts
,
209 // check completeness of fTimeUnitToCountToPatterns against all time units,
210 // and all plural rules, fill in fallback as necessary.
211 void checkConsistency(UTimeUnitFormatStyle style
, const char* key
, UErrorCode
& status
);
213 // fill in fTimeUnitToCountToPatterns from locale fall-back chain
214 void searchInLocaleChain(UTimeUnitFormatStyle style
, const char* key
, const char* localeName
,
215 TimeUnit::UTimeUnitFields field
, const UnicodeString
&,
216 const char*, Hashtable
*, UErrorCode
&);
218 // initialize hash table
219 Hashtable
* initHash(UErrorCode
& status
);
222 void deleteHash(Hashtable
* htable
);
225 void copyHash(const Hashtable
* source
, Hashtable
* target
, UErrorCode
& status
);
226 // get time unit name, such as "year", from time unit field enum, such as
228 static const char* getTimeUnitName(TimeUnit::UTimeUnitFields field
, UErrorCode
& status
);
233 TimeUnitFormat::operator!=(const Format
& other
) const {
234 return !operator==(other
);
239 #endif /* U_HIDE_DEPRECATED_API */
240 #endif /* #if !UCONFIG_NO_FORMATTING */
242 #endif // __TMUTFMT_H__