]> git.saurik.com Git - apple/icu.git/blame_incremental - icuSources/i18n/unicode/tmutfmt.h
ICU-59180.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / tmutfmt.h
... / ...
CommitLineData
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 !UCONFIG_NO_FORMATTING
22#ifndef U_HIDE_DEPRECATED_API
23
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"
30
31
32/**
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.
38 */
39enum UTimeUnitFormatStyle {
40 /** @deprecated ICU 53 */
41 UTMUTFMT_FULL_STYLE,
42 /** @deprecated ICU 53 */
43 UTMUTFMT_ABBREVIATED_STYLE,
44 /** @deprecated ICU 53 */
45 UTMUTFMT_FORMAT_STYLE_COUNT
46};
47typedef enum UTimeUnitFormatStyle UTimeUnitFormatStyle; /**< @deprecated ICU 53 */
48
49
50#if U_SHOW_CPLUSPLUS_API
51U_NAMESPACE_BEGIN
52
53class Hashtable;
54class UVector;
55
56struct TimeUnitFormatReadSink;
57
58/**
59 * Format or parse a TimeUnitAmount, using plural rules for the units where available.
60 *
61 * <P>
62 * Code Sample:
63 * <pre>
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);
75 * Formattable result;
76 * ((Format*)format)->parseObject(formatted, result, status);
77 * if (U_SUCCESS(status)) {
78 * assert (result == formattable);
79 * }
80 * }
81 * </pre>
82 *
83 * <P>
84 * @see TimeUnitAmount
85 * @see TimeUnitFormat
86 * @deprecated ICU 53 Use the MeasureFormat class instead.
87 */
88class U_I18N_API TimeUnitFormat: public MeasureFormat {
89public:
90
91 /**
92 * Create TimeUnitFormat with default locale, and full name style.
93 * Use setLocale and/or setFormat to modify.
94 * @deprecated ICU 53
95 */
96 TimeUnitFormat(UErrorCode& status);
97
98 /**
99 * Create TimeUnitFormat given locale, and full name style.
100 * @deprecated ICU 53
101 */
102 TimeUnitFormat(const Locale& locale, UErrorCode& status);
103
104 /**
105 * Create TimeUnitFormat given locale and style.
106 * @deprecated ICU 53
107 */
108 TimeUnitFormat(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status);
109
110 /**
111 * Copy constructor.
112 * @deprecated ICU 53
113 */
114 TimeUnitFormat(const TimeUnitFormat&);
115
116 /**
117 * deconstructor
118 * @deprecated ICU 53
119 */
120 virtual ~TimeUnitFormat();
121
122 /**
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.
126 * @deprecated ICU 53
127 */
128 virtual Format* clone(void) const;
129
130 /**
131 * Assignment operator
132 * @deprecated ICU 53
133 */
134 TimeUnitFormat& operator=(const TimeUnitFormat& other);
135
136 /**
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.
141 * @deprecated ICU 53
142 */
143 UBool operator!=(const Format& other) const;
144
145 /**
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
149 * @deprecated ICU 53
150 */
151 void setLocale(const Locale& locale, UErrorCode& status);
152
153
154 /**
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
158 * @deprecated ICU 53
159 */
160 void setNumberFormat(const NumberFormat& format, UErrorCode& status);
161
162 /**
163 * Parse a TimeUnitAmount.
164 * @see Format#parseObject(const UnicodeString&, Formattable&, ParsePosition&) const;
165 * @deprecated ICU 53
166 */
167 virtual void parseObject(const UnicodeString& source,
168 Formattable& result,
169 ParsePosition& pos) const;
170
171 /**
172 * Return the class ID for this class. This is useful only for comparing to
173 * a return value from getDynamicClassID(). For example:
174 * <pre>
175 * . Base* polymorphic_pointer = createPolymorphicObject();
176 * . if (polymorphic_pointer->getDynamicClassID() ==
177 * . erived::getStaticClassID()) ...
178 * </pre>
179 * @return The class ID for all objects of this class.
180 * @deprecated ICU 53
181 */
182 static UClassID U_EXPORT2 getStaticClassID(void);
183
184 /**
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.
189 *
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.
193 * @deprecated ICU 53
194 */
195 virtual UClassID getDynamicClassID(void) const;
196
197private:
198 Hashtable* fTimeUnitToCountToPatterns[TimeUnit::UTIMEUNIT_FIELD_COUNT];
199 UTimeUnitFormatStyle fStyle;
200
201 void create(UTimeUnitFormatStyle style, UErrorCode& status);
202
203 // it might actually be simpler to make them Decimal Formats later.
204 // initialize all private data members
205 void setup(UErrorCode& status);
206
207 // initialize data member without fill in data for fTimeUnitToCountToPattern
208 void initDataMembers(UErrorCode& status);
209
210 // initialize fTimeUnitToCountToPatterns from current locale's resource.
211 void readFromCurrentLocale(UTimeUnitFormatStyle style, const char* key, const UVector& pluralCounts,
212 UErrorCode& status);
213
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);
217
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&);
222
223 // initialize hash table
224 Hashtable* initHash(UErrorCode& status);
225
226 // delete hash table
227 void deleteHash(Hashtable* htable);
228
229 // copy hash table
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
232 // UTIMEUNIT_YEAR.
233 static const char* getTimeUnitName(TimeUnit::UTimeUnitFields field, UErrorCode& status);
234
235 friend struct TimeUnitFormatReadSink;
236};
237
238inline UBool
239TimeUnitFormat::operator!=(const Format& other) const {
240 return !operator==(other);
241}
242
243U_NAMESPACE_END
244#endif // U_SHOW_CPLUSPLUS_API
245
246#endif /* U_HIDE_DEPRECATED_API */
247#endif /* #if !UCONFIG_NO_FORMATTING */
248
249#endif // __TMUTFMT_H__
250//eof