| 1 |
// © 2016 and later: Unicode, Inc. and others. |
| 2 |
// License & terms of use: http://www.unicode.org/copyright.html |
| 3 |
/* |
| 4 |
******************************************************************************* |
| 5 |
* Copyright (C) 2008-2014, Google, International Business Machines Corporation |
| 6 |
* and others. All Rights Reserved. |
| 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 |
|
| 21 |
#if U_SHOW_CPLUSPLUS_API |
| 22 |
|
| 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 |
|
| 32 |
#ifndef U_HIDE_DEPRECATED_API |
| 33 |
|
| 34 |
/** |
| 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". |
| 39 |
* @deprecated ICU 53 Use MeasureFormat and UMeasureFormatWidth instead. |
| 40 |
*/ |
| 41 |
enum UTimeUnitFormatStyle { |
| 42 |
/** @deprecated ICU 53 */ |
| 43 |
UTMUTFMT_FULL_STYLE, |
| 44 |
/** @deprecated ICU 53 */ |
| 45 |
UTMUTFMT_ABBREVIATED_STYLE, |
| 46 |
/** @deprecated ICU 53 */ |
| 47 |
UTMUTFMT_FORMAT_STYLE_COUNT |
| 48 |
}; |
| 49 |
typedef enum UTimeUnitFormatStyle UTimeUnitFormatStyle; /**< @deprecated ICU 53 */ |
| 50 |
|
| 51 |
|
| 52 |
U_NAMESPACE_BEGIN |
| 53 |
|
| 54 |
class Hashtable; |
| 55 |
class UVector; |
| 56 |
|
| 57 |
struct TimeUnitFormatReadSink; |
| 58 |
|
| 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 |
| 87 |
* @deprecated ICU 53 Use the MeasureFormat class instead. |
| 88 |
*/ |
| 89 |
class U_I18N_API TimeUnitFormat: public MeasureFormat { |
| 90 |
public: |
| 91 |
|
| 92 |
/** |
| 93 |
* Create TimeUnitFormat with default locale, and full name style. |
| 94 |
* Use setLocale and/or setFormat to modify. |
| 95 |
* @deprecated ICU 53 |
| 96 |
*/ |
| 97 |
TimeUnitFormat(UErrorCode& status); |
| 98 |
|
| 99 |
/** |
| 100 |
* Create TimeUnitFormat given locale, and full name style. |
| 101 |
* @deprecated ICU 53 |
| 102 |
*/ |
| 103 |
TimeUnitFormat(const Locale& locale, UErrorCode& status); |
| 104 |
|
| 105 |
/** |
| 106 |
* Create TimeUnitFormat given locale and style. |
| 107 |
* @deprecated ICU 53 |
| 108 |
*/ |
| 109 |
TimeUnitFormat(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status); |
| 110 |
|
| 111 |
/** |
| 112 |
* Copy constructor. |
| 113 |
* @deprecated ICU 53 |
| 114 |
*/ |
| 115 |
TimeUnitFormat(const TimeUnitFormat&); |
| 116 |
|
| 117 |
/** |
| 118 |
* deconstructor |
| 119 |
* @deprecated ICU 53 |
| 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. |
| 127 |
* @deprecated ICU 53 |
| 128 |
*/ |
| 129 |
virtual TimeUnitFormat* clone() const override; |
| 130 |
|
| 131 |
/** |
| 132 |
* Assignment operator |
| 133 |
* @deprecated ICU 53 |
| 134 |
*/ |
| 135 |
TimeUnitFormat& operator=(const TimeUnitFormat& other); |
| 136 |
|
| 137 |
/** |
| 138 |
* Set the locale used for formatting or parsing. |
| 139 |
* @param locale the locale to be set |
| 140 |
* @param status output param set to success/failure code on exit |
| 141 |
* @deprecated ICU 53 |
| 142 |
*/ |
| 143 |
void setLocale(const Locale& locale, UErrorCode& status); |
| 144 |
|
| 145 |
|
| 146 |
/** |
| 147 |
* Set the number format used for formatting or parsing. |
| 148 |
* @param format the number formatter to be set |
| 149 |
* @param status output param set to success/failure code on exit |
| 150 |
* @deprecated ICU 53 |
| 151 |
*/ |
| 152 |
void setNumberFormat(const NumberFormat& format, UErrorCode& status); |
| 153 |
|
| 154 |
/** |
| 155 |
* Parse a TimeUnitAmount. |
| 156 |
* @see Format#parseObject(const UnicodeString&, Formattable&, ParsePosition&) const; |
| 157 |
* @deprecated ICU 53 |
| 158 |
*/ |
| 159 |
virtual void parseObject(const UnicodeString& source, |
| 160 |
Formattable& result, |
| 161 |
ParsePosition& pos) const override; |
| 162 |
|
| 163 |
/** |
| 164 |
* Return the class ID for this class. This is useful only for comparing to |
| 165 |
* a return value from getDynamicClassID(). For example: |
| 166 |
* <pre> |
| 167 |
* . Base* polymorphic_pointer = createPolymorphicObject(); |
| 168 |
* . if (polymorphic_pointer->getDynamicClassID() == |
| 169 |
* . erived::getStaticClassID()) ... |
| 170 |
* </pre> |
| 171 |
* @return The class ID for all objects of this class. |
| 172 |
* @deprecated ICU 53 |
| 173 |
*/ |
| 174 |
static UClassID U_EXPORT2 getStaticClassID(); |
| 175 |
|
| 176 |
/** |
| 177 |
* Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This |
| 178 |
* method is to implement a simple version of RTTI, since not all C++ |
| 179 |
* compilers support genuine RTTI. Polymorphic operator==() and clone() |
| 180 |
* methods call this method. |
| 181 |
* |
| 182 |
* @return The class ID for this object. All objects of a |
| 183 |
* given class have the same class ID. Objects of |
| 184 |
* other classes have different class IDs. |
| 185 |
* @deprecated ICU 53 |
| 186 |
*/ |
| 187 |
virtual UClassID getDynamicClassID() const override; |
| 188 |
|
| 189 |
private: |
| 190 |
Hashtable* fTimeUnitToCountToPatterns[TimeUnit::UTIMEUNIT_FIELD_COUNT]; |
| 191 |
UTimeUnitFormatStyle fStyle; |
| 192 |
|
| 193 |
void create(UTimeUnitFormatStyle style, UErrorCode& status); |
| 194 |
|
| 195 |
// it might actually be simpler to make them Decimal Formats later. |
| 196 |
// initialize all private data members |
| 197 |
void setup(UErrorCode& status); |
| 198 |
|
| 199 |
// initialize data member without fill in data for fTimeUnitToCountToPattern |
| 200 |
void initDataMembers(UErrorCode& status); |
| 201 |
|
| 202 |
// initialize fTimeUnitToCountToPatterns from current locale's resource. |
| 203 |
void readFromCurrentLocale(UTimeUnitFormatStyle style, const char* key, const UVector& pluralCounts, |
| 204 |
UErrorCode& status); |
| 205 |
|
| 206 |
// check completeness of fTimeUnitToCountToPatterns against all time units, |
| 207 |
// and all plural rules, fill in fallback as necessary. |
| 208 |
void checkConsistency(UTimeUnitFormatStyle style, const char* key, UErrorCode& status); |
| 209 |
|
| 210 |
// fill in fTimeUnitToCountToPatterns from locale fall-back chain |
| 211 |
void searchInLocaleChain(UTimeUnitFormatStyle style, const char* key, const char* localeName, |
| 212 |
TimeUnit::UTimeUnitFields field, const UnicodeString&, |
| 213 |
const char*, Hashtable*, UErrorCode&); |
| 214 |
|
| 215 |
// initialize hash table |
| 216 |
Hashtable* initHash(UErrorCode& status); |
| 217 |
|
| 218 |
// delete hash table |
| 219 |
void deleteHash(Hashtable* htable); |
| 220 |
|
| 221 |
// copy hash table |
| 222 |
void copyHash(const Hashtable* source, Hashtable* target, UErrorCode& status); |
| 223 |
// get time unit name, such as "year", from time unit field enum, such as |
| 224 |
// UTIMEUNIT_YEAR. |
| 225 |
static const char* getTimeUnitName(TimeUnit::UTimeUnitFields field, UErrorCode& status); |
| 226 |
|
| 227 |
friend struct TimeUnitFormatReadSink; |
| 228 |
}; |
| 229 |
|
| 230 |
U_NAMESPACE_END |
| 231 |
|
| 232 |
#endif /* U_HIDE_DEPRECATED_API */ |
| 233 |
#endif /* #if !UCONFIG_NO_FORMATTING */ |
| 234 |
|
| 235 |
#endif /* U_SHOW_CPLUSPLUS_API */ |
| 236 |
|
| 237 |
#endif // __TMUTFMT_H__ |
| 238 |
//eof |