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