]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
b75a7d8f | 3 | /* |
46f4442e | 4 | ******************************************************************************** |
2ca993e8 | 5 | * Copyright (C) 1997-2016, International Business Machines |
46f4442e A |
6 | * Corporation and others. All Rights Reserved. |
7 | ******************************************************************************** | |
8 | * | |
9 | * File DATEFMT.H | |
10 | * | |
11 | * Modification History: | |
12 | * | |
13 | * Date Name Description | |
14 | * 02/19/97 aliu Converted from java. | |
15 | * 04/01/97 aliu Added support for centuries. | |
16 | * 07/23/98 stephen JDK 1.2 sync | |
17 | * 11/15/99 weiv Added support for week of year/day of week formatting | |
18 | ******************************************************************************** | |
19 | */ | |
b75a7d8f A |
20 | |
21 | #ifndef DATEFMT_H | |
22 | #define DATEFMT_H | |
374ca955 | 23 | |
b75a7d8f A |
24 | #include "unicode/utypes.h" |
25 | ||
26 | #if !UCONFIG_NO_FORMATTING | |
27 | ||
374ca955 | 28 | #include "unicode/udat.h" |
b75a7d8f A |
29 | #include "unicode/calendar.h" |
30 | #include "unicode/numfmt.h" | |
31 | #include "unicode/format.h" | |
32 | #include "unicode/locid.h" | |
57a6839d A |
33 | #include "unicode/enumset.h" |
34 | #include "unicode/udisplaycontext.h" | |
b75a7d8f | 35 | |
73c04bcf | 36 | /** |
729e4ab9 | 37 | * \file |
73c04bcf A |
38 | * \brief C++ API: Abstract class for converting dates. |
39 | */ | |
40 | ||
f3c0d7a5 | 41 | #if U_SHOW_CPLUSPLUS_API |
b75a7d8f A |
42 | U_NAMESPACE_BEGIN |
43 | ||
44 | class TimeZone; | |
729e4ab9 | 45 | class DateTimePatternGenerator; |
b75a7d8f | 46 | |
57a6839d | 47 | // explicit template instantiation. see digitlst.h |
0f5d89e8 A |
48 | // (When building DLLs for Windows this is required.) |
49 | #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN | |
57a6839d A |
50 | template class U_I18N_API EnumSet<UDateFormatBooleanAttribute, |
51 | 0, | |
52 | UDAT_BOOLEAN_ATTRIBUTE_COUNT>; | |
53 | #endif | |
54 | ||
b75a7d8f A |
55 | /** |
56 | * DateFormat is an abstract class for a family of classes that convert dates and | |
57 | * times from their internal representations to textual form and back again in a | |
58 | * language-independent manner. Converting from the internal representation (milliseconds | |
59 | * since midnight, January 1, 1970) to text is known as "formatting," and converting | |
60 | * from text to millis is known as "parsing." We currently define only one concrete | |
61 | * subclass of DateFormat: SimpleDateFormat, which can handle pretty much all normal | |
62 | * date formatting and parsing actions. | |
63 | * <P> | |
64 | * DateFormat helps you to format and parse dates for any locale. Your code can | |
65 | * be completely independent of the locale conventions for months, days of the | |
66 | * week, or even the calendar format: lunar vs. solar. | |
67 | * <P> | |
68 | * To format a date for the current Locale, use one of the static factory | |
69 | * methods: | |
70 | * <pre> | |
71 | * \code | |
72 | * DateFormat* dfmt = DateFormat::createDateInstance(); | |
73 | * UDate myDate = Calendar::getNow(); | |
74 | * UnicodeString myString; | |
75 | * myString = dfmt->format( myDate, myString ); | |
76 | * \endcode | |
77 | * </pre> | |
78 | * If you are formatting multiple numbers, it is more efficient to get the | |
79 | * format and use it multiple times so that the system doesn't have to fetch the | |
80 | * information about the local language and country conventions multiple times. | |
81 | * <pre> | |
82 | * \code | |
83 | * DateFormat* df = DateFormat::createDateInstance(); | |
84 | * UnicodeString myString; | |
85 | * UDate myDateArr[] = { 0.0, 100000000.0, 2000000000.0 }; // test values | |
86 | * for (int32_t i = 0; i < 3; ++i) { | |
87 | * myString.remove(); | |
88 | * cout << df->format( myDateArr[i], myString ) << endl; | |
89 | * } | |
90 | * \endcode | |
91 | * </pre> | |
92 | * To get specific fields of a date, you can use UFieldPosition to | |
93 | * get specific fields. | |
94 | * <pre> | |
95 | * \code | |
96 | * DateFormat* dfmt = DateFormat::createDateInstance(); | |
97 | * FieldPosition pos(DateFormat::YEAR_FIELD); | |
98 | * UnicodeString myString; | |
99 | * myString = dfmt->format( myDate, myString ); | |
100 | * cout << myString << endl; | |
101 | * cout << pos.getBeginIndex() << "," << pos. getEndIndex() << endl; | |
102 | * \endcode | |
103 | * </pre> | |
104 | * To format a date for a different Locale, specify it in the call to | |
105 | * createDateInstance(). | |
106 | * <pre> | |
107 | * \code | |
108 | * DateFormat* df = | |
109 | * DateFormat::createDateInstance( DateFormat::SHORT, Locale::getFrance()); | |
110 | * \endcode | |
111 | * </pre> | |
112 | * You can use a DateFormat to parse also. | |
113 | * <pre> | |
114 | * \code | |
115 | * UErrorCode status = U_ZERO_ERROR; | |
116 | * UDate myDate = df->parse(myString, status); | |
117 | * \endcode | |
118 | * </pre> | |
119 | * Use createDateInstance() to produce the normal date format for that country. | |
120 | * There are other static factory methods available. Use createTimeInstance() | |
121 | * to produce the normal time format for that country. Use createDateTimeInstance() | |
122 | * to produce a DateFormat that formats both date and time. You can pass in | |
123 | * different options to these factory methods to control the length of the | |
124 | * result; from SHORT to MEDIUM to LONG to FULL. The exact result depends on the | |
125 | * locale, but generally: | |
126 | * <ul type=round> | |
127 | * <li> SHORT is completely numeric, such as 12/13/52 or 3:30pm | |
128 | * <li> MEDIUM is longer, such as Jan 12, 1952 | |
129 | * <li> LONG is longer, such as January 12, 1952 or 3:30:32pm | |
130 | * <li> FULL is pretty completely specified, such as | |
131 | * Tuesday, April 12, 1952 AD or 3:30:42pm PST. | |
132 | * </ul> | |
133 | * You can also set the time zone on the format if you wish. If you want even | |
134 | * more control over the format or parsing, (or want to give your users more | |
135 | * control), you can try casting the DateFormat you get from the factory methods | |
136 | * to a SimpleDateFormat. This will work for the majority of countries; just | |
137 | * remember to chck getDynamicClassID() before carrying out the cast. | |
138 | * <P> | |
139 | * You can also use forms of the parse and format methods with ParsePosition and | |
140 | * FieldPosition to allow you to | |
141 | * <ul type=round> | |
142 | * <li> Progressively parse through pieces of a string. | |
143 | * <li> Align any particular field, or find out where it is for selection | |
144 | * on the screen. | |
145 | * </ul> | |
374ca955 A |
146 | * |
147 | * <p><em>User subclasses are not supported.</em> While clients may write | |
148 | * subclasses, such code will not necessarily work and will not be | |
149 | * guaranteed to work stably from release to release. | |
b75a7d8f A |
150 | */ |
151 | class U_I18N_API DateFormat : public Format { | |
152 | public: | |
b75a7d8f A |
153 | |
154 | /** | |
155 | * Constants for various style patterns. These reflect the order of items in | |
156 | * the DateTimePatterns resource. There are 4 time patterns, 4 date patterns, | |
729e4ab9 A |
157 | * the default date-time pattern, and 4 date-time patterns. Each block of 4 values |
158 | * in the resource occurs in the order full, long, medium, short. | |
374ca955 | 159 | * @stable ICU 2.4 |
b75a7d8f A |
160 | */ |
161 | enum EStyle | |
162 | { | |
163 | kNone = -1, | |
164 | ||
165 | kFull = 0, | |
166 | kLong = 1, | |
167 | kMedium = 2, | |
168 | kShort = 3, | |
169 | ||
170 | kDateOffset = kShort + 1, | |
171 | // kFull + kDateOffset = 4 | |
172 | // kLong + kDateOffset = 5 | |
173 | // kMedium + kDateOffset = 6 | |
174 | // kShort + kDateOffset = 7 | |
175 | ||
176 | kDateTime = 8, | |
729e4ab9 A |
177 | // Default DateTime |
178 | ||
179 | kDateTimeOffset = kDateTime + 1, | |
180 | // kFull + kDateTimeOffset = 9 | |
181 | // kLong + kDateTimeOffset = 10 | |
182 | // kMedium + kDateTimeOffset = 11 | |
183 | // kShort + kDateTimeOffset = 12 | |
46f4442e A |
184 | |
185 | // relative dates | |
186 | kRelative = (1 << 7), | |
729e4ab9 | 187 | |
46f4442e | 188 | kFullRelative = (kFull | kRelative), |
729e4ab9 | 189 | |
46f4442e | 190 | kLongRelative = kLong | kRelative, |
729e4ab9 | 191 | |
46f4442e | 192 | kMediumRelative = kMedium | kRelative, |
729e4ab9 | 193 | |
46f4442e | 194 | kShortRelative = kShort | kRelative, |
729e4ab9 | 195 | |
b75a7d8f A |
196 | |
197 | kDefault = kMedium, | |
198 | ||
374ca955 A |
199 | |
200 | ||
b75a7d8f A |
201 | /** |
202 | * These constants are provided for backwards compatibility only. | |
203 | * Please use the C++ style constants defined above. | |
374ca955 | 204 | */ |
b75a7d8f A |
205 | FULL = kFull, |
206 | LONG = kLong, | |
207 | MEDIUM = kMedium, | |
208 | SHORT = kShort, | |
209 | DEFAULT = kDefault, | |
210 | DATE_OFFSET = kDateOffset, | |
211 | NONE = kNone, | |
212 | DATE_TIME = kDateTime | |
213 | }; | |
214 | ||
215 | /** | |
216 | * Destructor. | |
217 | * @stable ICU 2.0 | |
218 | */ | |
219 | virtual ~DateFormat(); | |
220 | ||
221 | /** | |
222 | * Equality operator. Returns true if the two formats have the same behavior. | |
223 | * @stable ICU 2.0 | |
224 | */ | |
225 | virtual UBool operator==(const Format&) const; | |
226 | ||
729e4ab9 A |
227 | |
228 | using Format::format; | |
229 | ||
b75a7d8f A |
230 | /** |
231 | * Format an object to produce a string. This method handles Formattable | |
232 | * objects with a UDate type. If a the Formattable object type is not a Date, | |
233 | * then it returns a failing UErrorCode. | |
234 | * | |
235 | * @param obj The object to format. Must be a Date. | |
236 | * @param appendTo Output parameter to receive result. | |
237 | * Result is appended to existing contents. | |
238 | * @param pos On input: an alignment field, if desired. | |
239 | * On output: the offsets of the alignment field. | |
240 | * @param status Output param filled with success/failure status. | |
241 | * @return Reference to 'appendTo' parameter. | |
242 | * @stable ICU 2.0 | |
243 | */ | |
244 | virtual UnicodeString& format(const Formattable& obj, | |
245 | UnicodeString& appendTo, | |
246 | FieldPosition& pos, | |
247 | UErrorCode& status) const; | |
248 | ||
729e4ab9 A |
249 | /** |
250 | * Format an object to produce a string. This method handles Formattable | |
251 | * objects with a UDate type. If a the Formattable object type is not a Date, | |
252 | * then it returns a failing UErrorCode. | |
253 | * | |
254 | * @param obj The object to format. Must be a Date. | |
255 | * @param appendTo Output parameter to receive result. | |
256 | * Result is appended to existing contents. | |
257 | * @param posIter On return, can be used to iterate over positions | |
258 | * of fields generated by this format call. Field values | |
259 | * are defined in UDateFormatField. Can be NULL. | |
260 | * @param status Output param filled with success/failure status. | |
261 | * @return Reference to 'appendTo' parameter. | |
262 | * @stable ICU 4.4 | |
263 | */ | |
264 | virtual UnicodeString& format(const Formattable& obj, | |
265 | UnicodeString& appendTo, | |
266 | FieldPositionIterator* posIter, | |
267 | UErrorCode& status) const; | |
b75a7d8f A |
268 | /** |
269 | * Formats a date into a date/time string. This is an abstract method which | |
270 | * concrete subclasses must implement. | |
271 | * <P> | |
272 | * On input, the FieldPosition parameter may have its "field" member filled with | |
273 | * an enum value specifying a field. On output, the FieldPosition will be filled | |
374ca955 | 274 | * in with the text offsets for that field. |
b75a7d8f A |
275 | * <P> For example, given a time text |
276 | * "1996.07.10 AD at 15:08:56 PDT", if the given fieldPosition.field is | |
374ca955 A |
277 | * UDAT_YEAR_FIELD, the offsets fieldPosition.beginIndex and |
278 | * statfieldPositionus.getEndIndex will be set to 0 and 4, respectively. | |
b75a7d8f A |
279 | * <P> Notice |
280 | * that if the same time field appears more than once in a pattern, the status will | |
281 | * be set for the first occurence of that time field. For instance, | |
282 | * formatting a UDate to the time string "1 PM PDT (Pacific Daylight Time)" | |
283 | * using the pattern "h a z (zzzz)" and the alignment field | |
284 | * DateFormat::TIMEZONE_FIELD, the offsets fieldPosition.beginIndex and | |
285 | * fieldPosition.getEndIndex will be set to 5 and 8, respectively, for the first | |
286 | * occurence of the timezone pattern character 'z'. | |
287 | * | |
288 | * @param cal Calendar set to the date and time to be formatted | |
729e4ab9 A |
289 | * into a date/time string. When the calendar type is |
290 | * different from the internal calendar held by this | |
291 | * DateFormat instance, the date and the time zone will | |
292 | * be inherited from the input calendar, but other calendar | |
293 | * field values will be calculated by the internal calendar. | |
b75a7d8f A |
294 | * @param appendTo Output parameter to receive result. |
295 | * Result is appended to existing contents. | |
296 | * @param fieldPosition On input: an alignment field, if desired (see examples above) | |
297 | * On output: the offsets of the alignment field (see examples above) | |
298 | * @return Reference to 'appendTo' parameter. | |
299 | * @stable ICU 2.1 | |
300 | */ | |
301 | virtual UnicodeString& format( Calendar& cal, | |
302 | UnicodeString& appendTo, | |
303 | FieldPosition& fieldPosition) const = 0; | |
304 | ||
729e4ab9 A |
305 | /** |
306 | * Formats a date into a date/time string. Subclasses should implement this method. | |
307 | * | |
308 | * @param cal Calendar set to the date and time to be formatted | |
309 | * into a date/time string. When the calendar type is | |
310 | * different from the internal calendar held by this | |
311 | * DateFormat instance, the date and the time zone will | |
312 | * be inherited from the input calendar, but other calendar | |
313 | * field values will be calculated by the internal calendar. | |
314 | * @param appendTo Output parameter to receive result. | |
315 | * Result is appended to existing contents. | |
316 | * @param posIter On return, can be used to iterate over positions | |
317 | * of fields generated by this format call. Field values | |
318 | * are defined in UDateFormatField. Can be NULL. | |
319 | * @param status error status. | |
320 | * @return Reference to 'appendTo' parameter. | |
321 | * @stable ICU 4.4 | |
322 | */ | |
323 | virtual UnicodeString& format(Calendar& cal, | |
324 | UnicodeString& appendTo, | |
325 | FieldPositionIterator* posIter, | |
326 | UErrorCode& status) const; | |
b75a7d8f A |
327 | /** |
328 | * Formats a UDate into a date/time string. | |
329 | * <P> | |
330 | * On input, the FieldPosition parameter may have its "field" member filled with | |
331 | * an enum value specifying a field. On output, the FieldPosition will be filled | |
374ca955 | 332 | * in with the text offsets for that field. |
b75a7d8f A |
333 | * <P> For example, given a time text |
334 | * "1996.07.10 AD at 15:08:56 PDT", if the given fieldPosition.field is | |
374ca955 A |
335 | * UDAT_YEAR_FIELD, the offsets fieldPosition.beginIndex and |
336 | * statfieldPositionus.getEndIndex will be set to 0 and 4, respectively. | |
b75a7d8f A |
337 | * <P> Notice |
338 | * that if the same time field appears more than once in a pattern, the status will | |
339 | * be set for the first occurence of that time field. For instance, | |
340 | * formatting a UDate to the time string "1 PM PDT (Pacific Daylight Time)" | |
341 | * using the pattern "h a z (zzzz)" and the alignment field | |
342 | * DateFormat::TIMEZONE_FIELD, the offsets fieldPosition.beginIndex and | |
343 | * fieldPosition.getEndIndex will be set to 5 and 8, respectively, for the first | |
344 | * occurence of the timezone pattern character 'z'. | |
345 | * | |
346 | * @param date UDate to be formatted into a date/time string. | |
347 | * @param appendTo Output parameter to receive result. | |
348 | * Result is appended to existing contents. | |
349 | * @param fieldPosition On input: an alignment field, if desired (see examples above) | |
350 | * On output: the offsets of the alignment field (see examples above) | |
351 | * @return Reference to 'appendTo' parameter. | |
352 | * @stable ICU 2.0 | |
353 | */ | |
354 | UnicodeString& format( UDate date, | |
355 | UnicodeString& appendTo, | |
356 | FieldPosition& fieldPosition) const; | |
357 | ||
729e4ab9 A |
358 | /** |
359 | * Formats a UDate into a date/time string. | |
360 | * | |
361 | * @param date UDate to be formatted into a date/time string. | |
362 | * @param appendTo Output parameter to receive result. | |
363 | * Result is appended to existing contents. | |
364 | * @param posIter On return, can be used to iterate over positions | |
365 | * of fields generated by this format call. Field values | |
366 | * are defined in UDateFormatField. Can be NULL. | |
367 | * @param status error status. | |
368 | * @return Reference to 'appendTo' parameter. | |
369 | * @stable ICU 4.4 | |
370 | */ | |
371 | UnicodeString& format(UDate date, | |
372 | UnicodeString& appendTo, | |
373 | FieldPositionIterator* posIter, | |
374 | UErrorCode& status) const; | |
b75a7d8f A |
375 | /** |
376 | * Formats a UDate into a date/time string. If there is a problem, you won't | |
377 | * know, using this method. Use the overloaded format() method which takes a | |
378 | * FieldPosition& to detect formatting problems. | |
379 | * | |
380 | * @param date The UDate value to be formatted into a string. | |
381 | * @param appendTo Output parameter to receive result. | |
382 | * Result is appended to existing contents. | |
383 | * @return Reference to 'appendTo' parameter. | |
384 | * @stable ICU 2.0 | |
385 | */ | |
386 | UnicodeString& format(UDate date, UnicodeString& appendTo) const; | |
387 | ||
b75a7d8f | 388 | /** |
4388f060 A |
389 | * Parse a date/time string. For example, a time text "07/10/96 4:5 PM, PDT" |
390 | * will be parsed into a UDate that is equivalent to Date(837039928046). | |
391 | * Parsing begins at the beginning of the string and proceeds as far as | |
392 | * possible. Assuming no parse errors were encountered, this function | |
393 | * doesn't return any information about how much of the string was consumed | |
394 | * by the parsing. If you need that information, use the version of | |
395 | * parse() that takes a ParsePosition. | |
396 | * <P> | |
397 | * By default, parsing is lenient: If the input is not in the form used by | |
398 | * this object's format method but can still be parsed as a date, then the | |
399 | * parse succeeds. Clients may insist on strict adherence to the format by | |
400 | * calling setLenient(false). | |
401 | * @see DateFormat::setLenient(boolean) | |
402 | * <P> | |
403 | * Note that the normal date formats associated with some calendars - such | |
404 | * as the Chinese lunar calendar - do not specify enough fields to enable | |
405 | * dates to be parsed unambiguously. In the case of the Chinese lunar | |
406 | * calendar, while the year within the current 60-year cycle is specified, | |
407 | * the number of such cycles since the start date of the calendar (in the | |
408 | * ERA field of the Calendar object) is not normally part of the format, | |
409 | * and parsing may assume the wrong era. For cases such as this it is | |
410 | * recommended that clients parse using the method | |
411 | * parse(const UnicodeString&, Calendar& cal, ParsePosition&) | |
412 | * with the Calendar passed in set to the current date, or to a date | |
413 | * within the era/cycle that should be assumed if absent in the format. | |
b75a7d8f | 414 | * |
4388f060 | 415 | * @param text The date/time string to be parsed into a UDate value. |
b75a7d8f A |
416 | * @param status Output param to be set to success/failure code. If |
417 | * 'text' cannot be parsed, it will be set to a failure | |
418 | * code. | |
4388f060 | 419 | * @return The parsed UDate value, if successful. |
b75a7d8f A |
420 | * @stable ICU 2.0 |
421 | */ | |
422 | virtual UDate parse( const UnicodeString& text, | |
423 | UErrorCode& status) const; | |
424 | ||
425 | /** | |
426 | * Parse a date/time string beginning at the given parse position. For | |
427 | * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date | |
428 | * that is equivalent to Date(837039928046). | |
429 | * <P> | |
430 | * By default, parsing is lenient: If the input is not in the form used by | |
431 | * this object's format method but can still be parsed as a date, then the | |
432 | * parse succeeds. Clients may insist on strict adherence to the format by | |
433 | * calling setLenient(false). | |
b75a7d8f A |
434 | * @see DateFormat::setLenient(boolean) |
435 | * | |
4388f060 A |
436 | * @param text The date/time string to be parsed. |
437 | * @param cal A Calendar set on input to the date and time to be used for | |
438 | * missing values in the date/time string being parsed, and set | |
439 | * on output to the parsed date/time. When the calendar type is | |
440 | * different from the internal calendar held by this DateFormat | |
441 | * instance, the internal calendar will be cloned to a work | |
442 | * calendar set to the same milliseconds and time zone as the | |
443 | * cal parameter, field values will be parsed based on the work | |
444 | * calendar, then the result (milliseconds and time zone) will | |
445 | * be set in this calendar. | |
b75a7d8f A |
446 | * @param pos On input, the position at which to start parsing; on |
447 | * output, the position at which parsing terminated, or the | |
448 | * start position if the parse failed. | |
b75a7d8f A |
449 | * @stable ICU 2.1 |
450 | */ | |
451 | virtual void parse( const UnicodeString& text, | |
452 | Calendar& cal, | |
453 | ParsePosition& pos) const = 0; | |
454 | ||
455 | /** | |
456 | * Parse a date/time string beginning at the given parse position. For | |
457 | * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date | |
458 | * that is equivalent to Date(837039928046). | |
459 | * <P> | |
460 | * By default, parsing is lenient: If the input is not in the form used by | |
461 | * this object's format method but can still be parsed as a date, then the | |
462 | * parse succeeds. Clients may insist on strict adherence to the format by | |
463 | * calling setLenient(false). | |
b75a7d8f | 464 | * @see DateFormat::setLenient(boolean) |
4388f060 A |
465 | * <P> |
466 | * Note that the normal date formats associated with some calendars - such | |
467 | * as the Chinese lunar calendar - do not specify enough fields to enable | |
468 | * dates to be parsed unambiguously. In the case of the Chinese lunar | |
469 | * calendar, while the year within the current 60-year cycle is specified, | |
470 | * the number of such cycles since the start date of the calendar (in the | |
471 | * ERA field of the Calendar object) is not normally part of the format, | |
472 | * and parsing may assume the wrong era. For cases such as this it is | |
473 | * recommended that clients parse using the method | |
474 | * parse(const UnicodeString&, Calendar& cal, ParsePosition&) | |
475 | * with the Calendar passed in set to the current date, or to a date | |
476 | * within the era/cycle that should be assumed if absent in the format. | |
b75a7d8f | 477 | * |
4388f060 | 478 | * @param text The date/time string to be parsed into a UDate value. |
b75a7d8f A |
479 | * @param pos On input, the position at which to start parsing; on |
480 | * output, the position at which parsing terminated, or the | |
481 | * start position if the parse failed. | |
482 | * @return A valid UDate if the input could be parsed. | |
483 | * @stable ICU 2.0 | |
484 | */ | |
485 | UDate parse( const UnicodeString& text, | |
486 | ParsePosition& pos) const; | |
487 | ||
488 | /** | |
489 | * Parse a string to produce an object. This methods handles parsing of | |
490 | * date/time strings into Formattable objects with UDate types. | |
491 | * <P> | |
492 | * Before calling, set parse_pos.index to the offset you want to start | |
493 | * parsing at in the source. After calling, parse_pos.index is the end of | |
494 | * the text you parsed. If error occurs, index is unchanged. | |
495 | * <P> | |
496 | * When parsing, leading whitespace is discarded (with a successful parse), | |
497 | * while trailing whitespace is left as is. | |
498 | * <P> | |
499 | * See Format::parseObject() for more. | |
500 | * | |
501 | * @param source The string to be parsed into an object. | |
502 | * @param result Formattable to be set to the parse result. | |
503 | * If parse fails, return contents are undefined. | |
504 | * @param parse_pos The position to start parsing at. Upon return | |
505 | * this param is set to the position after the | |
506 | * last character successfully parsed. If the | |
507 | * source is not parsed successfully, this param | |
508 | * will remain unchanged. | |
b75a7d8f A |
509 | * @stable ICU 2.0 |
510 | */ | |
511 | virtual void parseObject(const UnicodeString& source, | |
512 | Formattable& result, | |
513 | ParsePosition& parse_pos) const; | |
514 | ||
515 | /** | |
516 | * Create a default date/time formatter that uses the SHORT style for both | |
517 | * the date and the time. | |
518 | * | |
519 | * @return A date/time formatter which the caller owns. | |
520 | * @stable ICU 2.0 | |
521 | */ | |
374ca955 | 522 | static DateFormat* U_EXPORT2 createInstance(void); |
b75a7d8f A |
523 | |
524 | /** | |
525 | * Creates a time formatter with the given formatting style for the given | |
526 | * locale. | |
374ca955 | 527 | * |
b75a7d8f | 528 | * @param style The given formatting style. For example, |
729e4ab9 A |
529 | * SHORT for "h:mm a" in the US locale. Relative |
530 | * time styles are not currently supported. | |
b75a7d8f A |
531 | * @param aLocale The given locale. |
532 | * @return A time formatter which the caller owns. | |
533 | * @stable ICU 2.0 | |
534 | */ | |
374ca955 | 535 | static DateFormat* U_EXPORT2 createTimeInstance(EStyle style = kDefault, |
b75a7d8f A |
536 | const Locale& aLocale = Locale::getDefault()); |
537 | ||
538 | /** | |
539 | * Creates a date formatter with the given formatting style for the given | |
540 | * const locale. | |
374ca955 | 541 | * |
4388f060 A |
542 | * @param style The given formatting style. For example, SHORT for "M/d/yy" in the |
543 | * US locale. As currently implemented, relative date formatting only | |
544 | * affects a limited range of calendar days before or after the | |
545 | * current date, based on the CLDR <field type="day">/<relative> data: | |
546 | * For example, in English, "Yesterday", "Today", and "Tomorrow". | |
547 | * Outside of this range, dates are formatted using the corresponding | |
548 | * non-relative style. | |
b75a7d8f A |
549 | * @param aLocale The given locale. |
550 | * @return A date formatter which the caller owns. | |
551 | * @stable ICU 2.0 | |
552 | */ | |
374ca955 | 553 | static DateFormat* U_EXPORT2 createDateInstance(EStyle style = kDefault, |
b75a7d8f A |
554 | const Locale& aLocale = Locale::getDefault()); |
555 | ||
556 | /** | |
557 | * Creates a date/time formatter with the given formatting styles for the | |
558 | * given locale. | |
374ca955 | 559 | * |
b75a7d8f | 560 | * @param dateStyle The given formatting style for the date portion of the result. |
4388f060 A |
561 | * For example, SHORT for "M/d/yy" in the US locale. As currently |
562 | * implemented, relative date formatting only affects a limited range | |
563 | * of calendar days before or after the current date, based on the | |
564 | * CLDR <field type="day">/<relative> data: For example, in English, | |
565 | * "Yesterday", "Today", and "Tomorrow". Outside of this range, dates | |
566 | * are formatted using the corresponding non-relative style. | |
b75a7d8f | 567 | * @param timeStyle The given formatting style for the time portion of the result. |
729e4ab9 A |
568 | * For example, SHORT for "h:mm a" in the US locale. Relative |
569 | * time styles are not currently supported. | |
b75a7d8f A |
570 | * @param aLocale The given locale. |
571 | * @return A date/time formatter which the caller owns. | |
572 | * @stable ICU 2.0 | |
573 | */ | |
374ca955 | 574 | static DateFormat* U_EXPORT2 createDateTimeInstance(EStyle dateStyle = kDefault, |
b75a7d8f A |
575 | EStyle timeStyle = kDefault, |
576 | const Locale& aLocale = Locale::getDefault()); | |
577 | ||
2ca993e8 A |
578 | #ifndef U_HIDE_INTERNAL_API |
579 | /** | |
580 | * Returns the best pattern given a skeleton and locale. | |
581 | * @param locale the locale | |
582 | * @param skeleton the skeleton | |
583 | * @param status ICU error returned here | |
584 | * @return the best pattern. | |
585 | * @internal For ICU use only. | |
586 | */ | |
587 | static UnicodeString getBestPattern( | |
588 | const Locale &locale, | |
589 | const UnicodeString &skeleton, | |
590 | UErrorCode &status); | |
591 | #endif /* U_HIDE_INTERNAL_API */ | |
b331163b A |
592 | |
593 | /** | |
594 | * Creates a date/time formatter for the given skeleton and | |
595 | * default locale. | |
596 | * | |
597 | * @param skeleton The skeleton e.g "yMMMMd." Fields in the skeleton can | |
598 | * be in any order, and this method uses the locale to | |
599 | * map the skeleton to a pattern that includes locale | |
600 | * specific separators with the fields in the appropriate | |
601 | * order for that locale. | |
602 | * @param status Any error returned here. | |
603 | * @return A date/time formatter which the caller owns. | |
2ca993e8 | 604 | * @stable ICU 55 |
b331163b A |
605 | */ |
606 | static DateFormat* U_EXPORT2 createInstanceForSkeleton( | |
607 | const UnicodeString& skeleton, | |
608 | UErrorCode &status); | |
609 | ||
610 | /** | |
611 | * Creates a date/time formatter for the given skeleton and locale. | |
612 | * | |
613 | * @param skeleton The skeleton e.g "yMMMMd." Fields in the skeleton can | |
614 | * be in any order, and this method uses the locale to | |
615 | * map the skeleton to a pattern that includes locale | |
616 | * specific separators with the fields in the appropriate | |
617 | * order for that locale. | |
618 | * @param locale The given locale. | |
619 | * @param status Any error returned here. | |
620 | * @return A date/time formatter which the caller owns. | |
2ca993e8 | 621 | * @stable ICU 55 |
b331163b A |
622 | */ |
623 | static DateFormat* U_EXPORT2 createInstanceForSkeleton( | |
624 | const UnicodeString& skeleton, | |
625 | const Locale &locale, | |
626 | UErrorCode &status); | |
627 | ||
628 | /** | |
629 | * Creates a date/time formatter for the given skeleton and locale. | |
630 | * | |
631 | * @param calendarToAdopt the calendar returned DateFormat is to use. | |
632 | * @param skeleton The skeleton e.g "yMMMMd." Fields in the skeleton can | |
633 | * be in any order, and this method uses the locale to | |
634 | * map the skeleton to a pattern that includes locale | |
635 | * specific separators with the fields in the appropriate | |
636 | * order for that locale. | |
637 | * @param locale The given locale. | |
638 | * @param status Any error returned here. | |
639 | * @return A date/time formatter which the caller owns. | |
2ca993e8 | 640 | * @stable ICU 55 |
b331163b A |
641 | */ |
642 | static DateFormat* U_EXPORT2 createInstanceForSkeleton( | |
643 | Calendar *calendarToAdopt, | |
644 | const UnicodeString& skeleton, | |
645 | const Locale &locale, | |
646 | UErrorCode &status); | |
647 | ||
b331163b | 648 | |
b75a7d8f A |
649 | /** |
650 | * Gets the set of locales for which DateFormats are installed. | |
651 | * @param count Filled in with the number of locales in the list that is returned. | |
652 | * @return the set of locales for which DateFormats are installed. The caller | |
653 | * does NOT own this list and must not delete it. | |
654 | * @stable ICU 2.0 | |
655 | */ | |
374ca955 A |
656 | static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count); |
657 | ||
b75a7d8f | 658 | /** |
57a6839d A |
659 | * Returns whether both date/time parsing in the encapsulated Calendar object and DateFormat whitespace & |
660 | * numeric processing is lenient. | |
b75a7d8f A |
661 | * @stable ICU 2.0 |
662 | */ | |
663 | virtual UBool isLenient(void) const; | |
664 | ||
665 | /** | |
57a6839d A |
666 | * Specifies whether date/time parsing is to be lenient. With |
667 | * lenient parsing, the parser may use heuristics to interpret inputs that | |
668 | * do not precisely match this object's format. Without lenient parsing, | |
669 | * inputs must match this object's format more closely. | |
670 | * | |
671 | * Note: ICU 53 introduced finer grained control of leniency (and added | |
672 | * new control points) making the preferred method a combination of | |
673 | * setCalendarLenient() & setBooleanAttribute() calls. | |
674 | * This method supports prior functionality but may not support all | |
675 | * future leniency control & behavior of DateFormat. For control of pre 53 leniency, | |
676 | * Calendar and DateFormat whitespace & numeric tolerance, this method is safe to | |
677 | * use. However, mixing leniency control via this method and modification of the | |
678 | * newer attributes via setBooleanAttribute() may produce undesirable | |
679 | * results. | |
374ca955 | 680 | * |
b75a7d8f A |
681 | * @param lenient True specifies date/time interpretation to be lenient. |
682 | * @see Calendar::setLenient | |
57a6839d | 683 | * @stable ICU 2.0 |
b75a7d8f A |
684 | */ |
685 | virtual void setLenient(UBool lenient); | |
374ca955 | 686 | |
57a6839d | 687 | |
57a6839d A |
688 | /** |
689 | * Returns whether date/time parsing in the encapsulated Calendar object processing is lenient. | |
b331163b | 690 | * @stable ICU 53 |
57a6839d A |
691 | */ |
692 | virtual UBool isCalendarLenient(void) const; | |
693 | ||
694 | ||
57a6839d A |
695 | /** |
696 | * Specifies whether encapsulated Calendar date/time parsing is to be lenient. With | |
697 | * lenient parsing, the parser may use heuristics to interpret inputs that | |
698 | * do not precisely match this object's format. Without lenient parsing, | |
699 | * inputs must match this object's format more closely. | |
700 | * @param lenient when true, parsing is lenient | |
701 | * @see com.ibm.icu.util.Calendar#setLenient | |
b331163b | 702 | * @stable ICU 53 |
57a6839d A |
703 | */ |
704 | virtual void setCalendarLenient(UBool lenient); | |
705 | ||
706 | ||
b75a7d8f A |
707 | /** |
708 | * Gets the calendar associated with this date/time formatter. | |
57a6839d A |
709 | * The calendar is owned by the formatter and must not be modified. |
710 | * Also, the calendar does not reflect the results of a parse operation. | |
711 | * To parse to a calendar, use {@link #parse(const UnicodeString&, Calendar& cal, ParsePosition&) const parse(const UnicodeString&, Calendar& cal, ParsePosition&)} | |
b75a7d8f A |
712 | * @return the calendar associated with this date/time formatter. |
713 | * @stable ICU 2.0 | |
714 | */ | |
715 | virtual const Calendar* getCalendar(void) const; | |
374ca955 | 716 | |
b75a7d8f A |
717 | /** |
718 | * Set the calendar to be used by this date format. Initially, the default | |
719 | * calendar for the specified or default locale is used. The caller should | |
720 | * not delete the Calendar object after it is adopted by this call. | |
721 | * Adopting a new calendar will change to the default symbols. | |
722 | * | |
723 | * @param calendarToAdopt Calendar object to be adopted. | |
724 | * @stable ICU 2.0 | |
725 | */ | |
726 | virtual void adoptCalendar(Calendar* calendarToAdopt); | |
727 | ||
728 | /** | |
729 | * Set the calendar to be used by this date format. Initially, the default | |
730 | * calendar for the specified or default locale is used. | |
731 | * | |
732 | * @param newCalendar Calendar object to be set. | |
733 | * @stable ICU 2.0 | |
734 | */ | |
735 | virtual void setCalendar(const Calendar& newCalendar); | |
736 | ||
374ca955 | 737 | |
b75a7d8f A |
738 | /** |
739 | * Gets the number formatter which this date/time formatter uses to format | |
740 | * and parse the numeric portions of the pattern. | |
741 | * @return the number formatter which this date/time formatter uses. | |
742 | * @stable ICU 2.0 | |
743 | */ | |
744 | virtual const NumberFormat* getNumberFormat(void) const; | |
374ca955 | 745 | |
b75a7d8f A |
746 | /** |
747 | * Allows you to set the number formatter. The caller should | |
748 | * not delete the NumberFormat object after it is adopted by this call. | |
749 | * @param formatToAdopt NumberFormat object to be adopted. | |
750 | * @stable ICU 2.0 | |
751 | */ | |
752 | virtual void adoptNumberFormat(NumberFormat* formatToAdopt); | |
753 | ||
754 | /** | |
755 | * Allows you to set the number formatter. | |
756 | * @param newNumberFormat NumberFormat object to be set. | |
757 | * @stable ICU 2.0 | |
758 | */ | |
759 | virtual void setNumberFormat(const NumberFormat& newNumberFormat); | |
760 | ||
761 | /** | |
762 | * Returns a reference to the TimeZone used by this DateFormat's calendar. | |
763 | * @return the time zone associated with the calendar of DateFormat. | |
764 | * @stable ICU 2.0 | |
765 | */ | |
766 | virtual const TimeZone& getTimeZone(void) const; | |
374ca955 | 767 | |
b75a7d8f A |
768 | /** |
769 | * Sets the time zone for the calendar of this DateFormat object. The caller | |
770 | * no longer owns the TimeZone object and should not delete it after this call. | |
771 | * @param zoneToAdopt the TimeZone to be adopted. | |
772 | * @stable ICU 2.0 | |
773 | */ | |
774 | virtual void adoptTimeZone(TimeZone* zoneToAdopt); | |
775 | ||
776 | /** | |
777 | * Sets the time zone for the calendar of this DateFormat object. | |
778 | * @param zone the new time zone. | |
779 | * @stable ICU 2.0 | |
780 | */ | |
781 | virtual void setTimeZone(const TimeZone& zone); | |
782 | ||
57a6839d A |
783 | /** |
784 | * Set a particular UDisplayContext value in the formatter, such as | |
785 | * UDISPCTX_CAPITALIZATION_FOR_STANDALONE. | |
786 | * @param value The UDisplayContext value to set. | |
787 | * @param status Input/output status. If at entry this indicates a failure | |
788 | * status, the function will do nothing; otherwise this will be | |
789 | * updated with any new status from the function. | |
b331163b | 790 | * @stable ICU 53 |
57a6839d A |
791 | */ |
792 | virtual void setContext(UDisplayContext value, UErrorCode& status); | |
793 | ||
57a6839d A |
794 | /** |
795 | * Get the formatter's UDisplayContext value for the specified UDisplayContextType, | |
796 | * such as UDISPCTX_TYPE_CAPITALIZATION. | |
797 | * @param type The UDisplayContextType whose value to return | |
798 | * @param status Input/output status. If at entry this indicates a failure | |
799 | * status, the function will do nothing; otherwise this will be | |
800 | * updated with any new status from the function. | |
801 | * @return The UDisplayContextValue for the specified type. | |
b331163b | 802 | * @stable ICU 53 |
57a6839d A |
803 | */ |
804 | virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const; | |
805 | ||
57a6839d A |
806 | /** |
807 | * Sets an boolean attribute on this DateFormat. | |
808 | * May return U_UNSUPPORTED_ERROR if this instance does not support | |
809 | * the specified attribute. | |
810 | * @param attr the attribute to set | |
811 | * @param newvalue new value | |
812 | * @param status the error type | |
813 | * @return *this - for chaining (example: format.setAttribute(...).setAttribute(...) ) | |
b331163b | 814 | * @stable ICU 53 |
57a6839d A |
815 | */ |
816 | ||
817 | virtual DateFormat& U_EXPORT2 setBooleanAttribute(UDateFormatBooleanAttribute attr, | |
818 | UBool newvalue, | |
819 | UErrorCode &status); | |
820 | ||
57a6839d A |
821 | /** |
822 | * Returns a boolean from this DateFormat | |
823 | * May return U_UNSUPPORTED_ERROR if this instance does not support | |
824 | * the specified attribute. | |
825 | * @param attr the attribute to set | |
826 | * @param status the error type | |
827 | * @return the attribute value. Undefined if there is an error. | |
b331163b | 828 | * @stable ICU 53 |
57a6839d A |
829 | */ |
830 | virtual UBool U_EXPORT2 getBooleanAttribute(UDateFormatBooleanAttribute attr, UErrorCode &status) const; | |
831 | ||
b75a7d8f A |
832 | protected: |
833 | /** | |
834 | * Default constructor. Creates a DateFormat with no Calendar or NumberFormat | |
835 | * associated with it. This constructor depends on the subclasses to fill in | |
836 | * the calendar and numberFormat fields. | |
837 | * @stable ICU 2.0 | |
838 | */ | |
839 | DateFormat(); | |
840 | ||
841 | /** | |
842 | * Copy constructor. | |
843 | * @stable ICU 2.0 | |
844 | */ | |
845 | DateFormat(const DateFormat&); | |
846 | ||
847 | /** | |
848 | * Default assignment operator. | |
849 | * @stable ICU 2.0 | |
850 | */ | |
851 | DateFormat& operator=(const DateFormat&); | |
852 | ||
853 | /** | |
854 | * The calendar that DateFormat uses to produce the time field values needed | |
855 | * to implement date/time formatting. Subclasses should generally initialize | |
856 | * this to the default calendar for the locale associated with this DateFormat. | |
374ca955 | 857 | * @stable ICU 2.4 |
b75a7d8f A |
858 | */ |
859 | Calendar* fCalendar; | |
860 | ||
861 | /** | |
862 | * The number formatter that DateFormat uses to format numbers in dates and | |
863 | * times. Subclasses should generally initialize this to the default number | |
864 | * format for the locale associated with this DateFormat. | |
374ca955 | 865 | * @stable ICU 2.4 |
b75a7d8f A |
866 | */ |
867 | NumberFormat* fNumberFormat; | |
868 | ||
57a6839d | 869 | |
b75a7d8f | 870 | private: |
2ca993e8 | 871 | |
b75a7d8f A |
872 | /** |
873 | * Gets the date/time formatter with the given formatting styles for the | |
874 | * given locale. | |
875 | * @param dateStyle the given date formatting style. | |
876 | * @param timeStyle the given time formatting style. | |
877 | * @param inLocale the given locale. | |
878 | * @return a date/time formatter, or 0 on failure. | |
879 | */ | |
4388f060 | 880 | static DateFormat* U_EXPORT2 create(EStyle timeStyle, EStyle dateStyle, const Locale& inLocale); |
374ca955 | 881 | |
57a6839d A |
882 | |
883 | /** | |
884 | * enum set of active boolean attributes for this instance | |
885 | */ | |
886 | EnumSet<UDateFormatBooleanAttribute, 0, UDAT_BOOLEAN_ATTRIBUTE_COUNT> fBoolFlags; | |
887 | ||
888 | ||
889 | UDisplayContext fCapitalizationContext; | |
b331163b | 890 | friend class DateFmtKeyByStyle; |
57a6839d | 891 | |
374ca955 | 892 | public: |
4388f060 | 893 | #ifndef U_HIDE_OBSOLETE_API |
374ca955 A |
894 | /** |
895 | * Field selector for FieldPosition for DateFormat fields. | |
896 | * @obsolete ICU 3.4 use UDateFormatField instead, since this API will be | |
897 | * removed in that release | |
898 | */ | |
899 | enum EField | |
900 | { | |
901 | // Obsolete; use UDateFormatField instead | |
902 | kEraField = UDAT_ERA_FIELD, | |
903 | kYearField = UDAT_YEAR_FIELD, | |
904 | kMonthField = UDAT_MONTH_FIELD, | |
905 | kDateField = UDAT_DATE_FIELD, | |
906 | kHourOfDay1Field = UDAT_HOUR_OF_DAY1_FIELD, | |
907 | kHourOfDay0Field = UDAT_HOUR_OF_DAY0_FIELD, | |
908 | kMinuteField = UDAT_MINUTE_FIELD, | |
909 | kSecondField = UDAT_SECOND_FIELD, | |
910 | kMillisecondField = UDAT_FRACTIONAL_SECOND_FIELD, | |
911 | kDayOfWeekField = UDAT_DAY_OF_WEEK_FIELD, | |
912 | kDayOfYearField = UDAT_DAY_OF_YEAR_FIELD, | |
913 | kDayOfWeekInMonthField = UDAT_DAY_OF_WEEK_IN_MONTH_FIELD, | |
914 | kWeekOfYearField = UDAT_WEEK_OF_YEAR_FIELD, | |
915 | kWeekOfMonthField = UDAT_WEEK_OF_MONTH_FIELD, | |
916 | kAmPmField = UDAT_AM_PM_FIELD, | |
917 | kHour1Field = UDAT_HOUR1_FIELD, | |
918 | kHour0Field = UDAT_HOUR0_FIELD, | |
919 | kTimezoneField = UDAT_TIMEZONE_FIELD, | |
920 | kYearWOYField = UDAT_YEAR_WOY_FIELD, | |
921 | kDOWLocalField = UDAT_DOW_LOCAL_FIELD, | |
922 | kExtendedYearField = UDAT_EXTENDED_YEAR_FIELD, | |
923 | kJulianDayField = UDAT_JULIAN_DAY_FIELD, | |
924 | kMillisecondsInDayField = UDAT_MILLISECONDS_IN_DAY_FIELD, | |
925 | ||
926 | // Obsolete; use UDateFormatField instead | |
927 | ERA_FIELD = UDAT_ERA_FIELD, | |
928 | YEAR_FIELD = UDAT_YEAR_FIELD, | |
929 | MONTH_FIELD = UDAT_MONTH_FIELD, | |
930 | DATE_FIELD = UDAT_DATE_FIELD, | |
931 | HOUR_OF_DAY1_FIELD = UDAT_HOUR_OF_DAY1_FIELD, | |
932 | HOUR_OF_DAY0_FIELD = UDAT_HOUR_OF_DAY0_FIELD, | |
933 | MINUTE_FIELD = UDAT_MINUTE_FIELD, | |
934 | SECOND_FIELD = UDAT_SECOND_FIELD, | |
935 | MILLISECOND_FIELD = UDAT_FRACTIONAL_SECOND_FIELD, | |
936 | DAY_OF_WEEK_FIELD = UDAT_DAY_OF_WEEK_FIELD, | |
937 | DAY_OF_YEAR_FIELD = UDAT_DAY_OF_YEAR_FIELD, | |
938 | DAY_OF_WEEK_IN_MONTH_FIELD = UDAT_DAY_OF_WEEK_IN_MONTH_FIELD, | |
939 | WEEK_OF_YEAR_FIELD = UDAT_WEEK_OF_YEAR_FIELD, | |
940 | WEEK_OF_MONTH_FIELD = UDAT_WEEK_OF_MONTH_FIELD, | |
941 | AM_PM_FIELD = UDAT_AM_PM_FIELD, | |
942 | HOUR1_FIELD = UDAT_HOUR1_FIELD, | |
943 | HOUR0_FIELD = UDAT_HOUR0_FIELD, | |
944 | TIMEZONE_FIELD = UDAT_TIMEZONE_FIELD | |
945 | }; | |
4388f060 | 946 | #endif /* U_HIDE_OBSOLETE_API */ |
b75a7d8f A |
947 | }; |
948 | ||
b75a7d8f | 949 | U_NAMESPACE_END |
f3c0d7a5 | 950 | #endif // U_SHOW_CPLUSPLUS_API |
b75a7d8f A |
951 | |
952 | #endif /* #if !UCONFIG_NO_FORMATTING */ | |
953 | ||
954 | #endif // _DATEFMT | |
955 | //eof |