michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2008-2013, Google, International Business Machines Corporation michael@0: * and others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: */ michael@0: michael@0: #ifndef __TMUTFMT_H__ michael@0: #define __TMUTFMT_H__ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C++ API: Format and parse duration in single time unit michael@0: */ michael@0: michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/unistr.h" michael@0: #include "unicode/tmunit.h" michael@0: #include "unicode/tmutamt.h" michael@0: #include "unicode/measfmt.h" michael@0: #include "unicode/numfmt.h" michael@0: #include "unicode/plurrule.h" michael@0: michael@0: /** michael@0: * Constants for various styles. michael@0: * There are 2 styles: full name and abbreviated name. michael@0: * For example, for English, the full name for hour duration is "3 hours", michael@0: * and the abbreviated name is "3 hrs". michael@0: * @stable ICU 4.8 michael@0: */ michael@0: enum UTimeUnitFormatStyle { michael@0: /** @stable ICU 4.8 */ michael@0: UTMUTFMT_FULL_STYLE, michael@0: /** @stable ICU 4.8 */ michael@0: UTMUTFMT_ABBREVIATED_STYLE, michael@0: /** @stable ICU 4.8 */ michael@0: UTMUTFMT_FORMAT_STYLE_COUNT michael@0: }; michael@0: typedef enum UTimeUnitFormatStyle UTimeUnitFormatStyle; /**< @stable ICU 4.8 */ michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: class Hashtable; michael@0: class UVector; michael@0: michael@0: /** michael@0: * Format or parse a TimeUnitAmount, using plural rules for the units where available. michael@0: * michael@0: *
michael@0: * Code Sample: michael@0: *
michael@0: * // create time unit amount instance - a combination of Number and time unit michael@0: * UErrorCode status = U_ZERO_ERROR; michael@0: * TimeUnitAmount* source = new TimeUnitAmount(2, TimeUnit::UTIMEUNIT_YEAR, status); michael@0: * // create time unit format instance michael@0: * TimeUnitFormat* format = new TimeUnitFormat(Locale("en"), status); michael@0: * // format a time unit amount michael@0: * UnicodeString formatted; michael@0: * Formattable formattable; michael@0: * if (U_SUCCESS(status)) { michael@0: * formattable.adoptObject(source); michael@0: * formatted = ((Format*)format)->format(formattable, formatted, status); michael@0: * Formattable result; michael@0: * ((Format*)format)->parseObject(formatted, result, status); michael@0: * if (U_SUCCESS(status)) { michael@0: * assert (result == formattable); michael@0: * } michael@0: * } michael@0: *michael@0: * michael@0: *
michael@0: * @see TimeUnitAmount michael@0: * @see TimeUnitFormat michael@0: * @stable ICU 4.2 michael@0: */ michael@0: class U_I18N_API TimeUnitFormat: public MeasureFormat { michael@0: public: michael@0: michael@0: /** michael@0: * Create TimeUnitFormat with default locale, and full name style. michael@0: * Use setLocale and/or setFormat to modify. michael@0: * @stable ICU 4.2 michael@0: */ michael@0: TimeUnitFormat(UErrorCode& status); michael@0: michael@0: /** michael@0: * Create TimeUnitFormat given locale, and full name style. michael@0: * @stable ICU 4.2 michael@0: */ michael@0: TimeUnitFormat(const Locale& locale, UErrorCode& status); michael@0: michael@0: /** michael@0: * Create TimeUnitFormat given locale and style. michael@0: * @stable ICU 4.8 michael@0: */ michael@0: TimeUnitFormat(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status); michael@0: michael@0: /** michael@0: * Copy constructor. michael@0: * @stable ICU 4.2 michael@0: */ michael@0: TimeUnitFormat(const TimeUnitFormat&); michael@0: michael@0: /** michael@0: * deconstructor michael@0: * @stable ICU 4.2 michael@0: */ michael@0: virtual ~TimeUnitFormat(); michael@0: michael@0: /** michael@0: * Clone this Format object polymorphically. The caller owns the result and michael@0: * should delete it when done. michael@0: * @return A copy of the object. michael@0: * @stable ICU 4.2 michael@0: */ michael@0: virtual Format* clone(void) const; michael@0: michael@0: /** michael@0: * Assignment operator michael@0: * @stable ICU 4.2 michael@0: */ michael@0: TimeUnitFormat& operator=(const TimeUnitFormat& other); michael@0: michael@0: michael@0: /** michael@0: * Return true if the given Format objects are semantically equal. Objects michael@0: * of different subclasses are considered unequal. michael@0: * @param other the object to be compared with. michael@0: * @return true if the given Format objects are semantically equal. michael@0: * @stable ICU 4.2 michael@0: */ michael@0: virtual UBool operator==(const Format& other) const; michael@0: michael@0: /** michael@0: * Return true if the given Format objects are not semantically equal. michael@0: * Objects of different subclasses are considered unequal. michael@0: * @param other the object to be compared with. michael@0: * @return true if the given Format objects are not semantically equal. michael@0: * @stable ICU 4.2 michael@0: */ michael@0: UBool operator!=(const Format& other) const; michael@0: michael@0: /** michael@0: * Set the locale used for formatting or parsing. michael@0: * @param locale the locale to be set michael@0: * @param status output param set to success/failure code on exit michael@0: * @stable ICU 4.2 michael@0: */ michael@0: void setLocale(const Locale& locale, UErrorCode& status); michael@0: michael@0: michael@0: /** michael@0: * Set the number format used for formatting or parsing. michael@0: * @param format the number formatter to be set michael@0: * @param status output param set to success/failure code on exit michael@0: * @stable ICU 4.2 michael@0: */ michael@0: void setNumberFormat(const NumberFormat& format, UErrorCode& status); michael@0: michael@0: michael@0: using MeasureFormat::format; michael@0: michael@0: /** michael@0: * Format a TimeUnitAmount. michael@0: * If the formattable object is not a time unit amount object, michael@0: * or the number in time unit amount is not a double type or long type michael@0: * numeric, it returns a failing status: U_ILLEGAL_ARGUMENT_ERROR. michael@0: * @see Format#format(const Formattable&, UnicodeString&, FieldPosition&, UErrorCode&) const michael@0: * @stable ICU 4.2 michael@0: */ michael@0: virtual UnicodeString& format(const Formattable& obj, michael@0: UnicodeString& toAppendTo, michael@0: FieldPosition& pos, michael@0: UErrorCode& status) const; michael@0: michael@0: /** michael@0: * Parse a TimeUnitAmount. michael@0: * @see Format#parseObject(const UnicodeString&, Formattable&, ParsePosition&) const; michael@0: * @stable ICU 4.2 michael@0: */ michael@0: virtual void parseObject(const UnicodeString& source, michael@0: Formattable& result, michael@0: ParsePosition& pos) const; michael@0: michael@0: /** michael@0: * Return the class ID for this class. This is useful only for comparing to michael@0: * a return value from getDynamicClassID(). For example: michael@0: *
michael@0: * . Base* polymorphic_pointer = createPolymorphicObject(); michael@0: * . if (polymorphic_pointer->getDynamicClassID() == michael@0: * . erived::getStaticClassID()) ... michael@0: *michael@0: * @return The class ID for all objects of this class. michael@0: * @stable ICU 4.2 michael@0: */ michael@0: static UClassID U_EXPORT2 getStaticClassID(void); michael@0: michael@0: /** michael@0: * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This michael@0: * method is to implement a simple version of RTTI, since not all C++ michael@0: * compilers support genuine RTTI. Polymorphic operator==() and clone() michael@0: * methods call this method. michael@0: * michael@0: * @return The class ID for this object. All objects of a michael@0: * given class have the same class ID. Objects of michael@0: * other classes have different class IDs. michael@0: * @stable ICU 4.2 michael@0: */ michael@0: virtual UClassID getDynamicClassID(void) const; michael@0: michael@0: private: michael@0: NumberFormat* fNumberFormat; michael@0: Locale fLocale; michael@0: Hashtable* fTimeUnitToCountToPatterns[TimeUnit::UTIMEUNIT_FIELD_COUNT]; michael@0: PluralRules* fPluralRules; michael@0: UTimeUnitFormatStyle fStyle; michael@0: michael@0: void create(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status); michael@0: michael@0: // it might actually be simpler to make them Decimal Formats later. michael@0: // initialize all private data members michael@0: void setup(UErrorCode& status); michael@0: michael@0: // initialize data member without fill in data for fTimeUnitToCountToPattern michael@0: void initDataMembers(UErrorCode& status); michael@0: michael@0: // initialize fTimeUnitToCountToPatterns from current locale's resource. michael@0: void readFromCurrentLocale(UTimeUnitFormatStyle style, const char* key, const UVector& pluralCounts, michael@0: UErrorCode& status); michael@0: michael@0: // check completeness of fTimeUnitToCountToPatterns against all time units, michael@0: // and all plural rules, fill in fallback as necessary. michael@0: void checkConsistency(UTimeUnitFormatStyle style, const char* key, UErrorCode& status); michael@0: michael@0: // fill in fTimeUnitToCountToPatterns from locale fall-back chain michael@0: void searchInLocaleChain(UTimeUnitFormatStyle style, const char* key, const char* localeName, michael@0: TimeUnit::UTimeUnitFields field, const UnicodeString&, michael@0: const char*, Hashtable*, UErrorCode&); michael@0: michael@0: // initialize hash table michael@0: Hashtable* initHash(UErrorCode& status); michael@0: michael@0: // delete hash table michael@0: void deleteHash(Hashtable* htable); michael@0: michael@0: // copy hash table michael@0: void copyHash(const Hashtable* source, Hashtable* target, UErrorCode& status); michael@0: // get time unit name, such as "year", from time unit field enum, such as michael@0: // UTIMEUNIT_YEAR. michael@0: static const char* getTimeUnitName(TimeUnit::UTimeUnitFields field, UErrorCode& status); michael@0: michael@0: }; michael@0: michael@0: michael@0: michael@0: inline UBool michael@0: TimeUnitFormat::operator!=(const Format& other) const { michael@0: return !operator==(other); michael@0: } michael@0: michael@0: michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: michael@0: #endif // __TMUTFMT_H__ michael@0: //eof