2 *******************************************************************************
3 * Copyright (C) 2010, Google, International Business Machines Corporation and *
4 * 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
21 #include "unicode/unistr.h"
22 #include "unicode/tmunit.h"
23 #include "unicode/tmutamt.h"
24 #include "unicode/measfmt.h"
25 #include "unicode/numfmt.h"
26 #include "unicode/plurrule.h"
40 * Format or parse a TimeUnitAmount, using plural rules for the units where available.
45 * // create time unit amount instance - a combination of Number and time unit
46 * UErrorCode status = U_ZERO_ERROR;
47 * TimeUnitAmount* source = new TimeUnitAmount(2, TimeUnit::UTIMEUNIT_YEAR, status);
48 * // create time unit format instance
49 * TimeUnitFormat* format = new TimeUnitFormat(Locale("en"), status);
50 * // format a time unit amount
51 * UnicodeString formatted;
52 * Formattable formattable;
53 * if (U_SUCCESS(status)) {
54 * formattable.adoptObject(source);
55 * formatted = ((Format*)format)->format(formattable, formatted, status);
57 * ((Format*)format)->parseObject(formatted, result, status);
58 * if (U_SUCCESS(status)) {
59 * assert (result == formattable);
69 class U_I18N_API TimeUnitFormat
: public MeasureFormat
{
73 * Constants for various styles.
74 * There are 2 styles: full name and abbreviated name.
75 * For example, for English, the full name for hour duration is "3 hours",
76 * and the abbreviated name is "3 hrs".
82 kTotal
= kAbbreviate
+ 1
86 * Create TimeUnitFormat with default locale, and full name style.
87 * Use setLocale and/or setFormat to modify.
90 TimeUnitFormat(UErrorCode
& status
);
93 * Create TimeUnitFormat given locale, and full name style.
96 TimeUnitFormat(const Locale
& locale
, UErrorCode
& status
);
99 * Create TimeUnitFormat given locale and style.
102 TimeUnitFormat(const Locale
& locale
, EStyle style
, UErrorCode
& status
);
108 TimeUnitFormat(const TimeUnitFormat
&);
114 virtual ~TimeUnitFormat();
117 * Clone this Format object polymorphically. The caller owns the result and
118 * should delete it when done.
119 * @return A copy of the object.
122 virtual Format
* clone(void) const;
125 * Assignment operator
128 TimeUnitFormat
& operator=(const TimeUnitFormat
& other
);
132 * Return true if the given Format objects are semantically equal. Objects
133 * of different subclasses are considered unequal.
134 * @param other the object to be compared with.
135 * @return true if the given Format objects are semantically equal.
138 virtual UBool
operator==(const Format
& other
) const;
141 * Return true if the given Format objects are not semantically equal.
142 * Objects of different subclasses are considered unequal.
143 * @param other the object to be compared with.
144 * @return true if the given Format objects are not semantically equal.
147 UBool
operator!=(const Format
& other
) const;
150 * Set the locale used for formatting or parsing.
151 * @param locale the locale to be set
152 * @param status output param set to success/failure code on exit
155 void setLocale(const Locale
& locale
, UErrorCode
& status
);
159 * Set the number format used for formatting or parsing.
160 * @param format the number formatter to be set
161 * @param status output param set to success/failure code on exit
164 void setNumberFormat(const NumberFormat
& format
, UErrorCode
& status
);
167 using MeasureFormat::format
;
170 * Format a TimeUnitAmount.
171 * If the formattable object is not a time unit amount object,
172 * or the number in time unit amount is not a double type or long type
173 * numeric, it returns a failing status: U_ILLEGAL_ARGUMENT_ERROR.
174 * @see Format#format(const Formattable&, UnicodeString&, FieldPosition&, UErrorCode&) const
177 virtual UnicodeString
& format(const Formattable
& obj
,
178 UnicodeString
& toAppendTo
,
180 UErrorCode
& status
) const;
183 * Parse a TimeUnitAmount.
184 * @see Format#parseObject(const UnicodeString&, Formattable&, ParsePosition&) const;
187 virtual void parseObject(const UnicodeString
& source
,
189 ParsePosition
& pos
) const;
192 * Return the class ID for this class. This is useful only for comparing to
193 * a return value from getDynamicClassID(). For example:
195 * . Base* polymorphic_pointer = createPolymorphicObject();
196 * . if (polymorphic_pointer->getDynamicClassID() ==
197 * . erived::getStaticClassID()) ...
199 * @return The class ID for all objects of this class.
202 static UClassID U_EXPORT2
getStaticClassID(void);
205 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
206 * method is to implement a simple version of RTTI, since not all C++
207 * compilers support genuine RTTI. Polymorphic operator==() and clone()
208 * methods call this method.
210 * @return The class ID for this object. All objects of a
211 * given class have the same class ID. Objects of
212 * other classes have different class IDs.
215 virtual UClassID
getDynamicClassID(void) const;
218 NumberFormat
* fNumberFormat
;
220 Hashtable
* fTimeUnitToCountToPatterns
[TimeUnit::UTIMEUNIT_FIELD_COUNT
];
221 PluralRules
* fPluralRules
;
224 void create(const Locale
& locale
, EStyle style
, UErrorCode
& status
);
226 // it might actually be simpler to make them Decimal Formats later.
227 // initialize all private data members
228 void setup(UErrorCode
& status
);
230 // initialize data member without fill in data for fTimeUnitToCountToPattern
231 void initDataMembers(UErrorCode
& status
);
233 // initialize fTimeUnitToCountToPatterns from current locale's resource.
234 void readFromCurrentLocale(EStyle style
, const char* key
, UErrorCode
& status
);
236 // check completeness of fTimeUnitToCountToPatterns against all time units,
237 // and all plural rules, fill in fallback as necessary.
238 void checkConsistency(EStyle style
, const char* key
, UErrorCode
& status
);
240 // fill in fTimeUnitToCountToPatterns from locale fall-back chain
241 void searchInLocaleChain(EStyle style
, const char* key
, const char* localeName
,
242 TimeUnit::UTimeUnitFields field
, const char*,
243 const char*, Hashtable
*, UErrorCode
&);
245 // initialize hash table
246 Hashtable
* initHash(UErrorCode
& status
);
249 void deleteHash(Hashtable
* htable
);
252 void copyHash(const Hashtable
* source
, Hashtable
* target
, UErrorCode
& status
);
253 // get time unit name, such as "year", from time unit field enum, such as
255 static const char* getTimeUnitName(TimeUnit::UTimeUnitFields field
, UErrorCode
& status
);
261 TimeUnitFormat::operator!=(const Format
& other
) const {
262 return !operator==(other
);
269 #endif /* #if !UCONFIG_NO_FORMATTING */
271 #endif // __TMUTFMT_H__