2 ********************************************************************************
3 * Copyright (C) 1997-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ********************************************************************************
9 * Modification History:
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 ********************************************************************************
22 #include "unicode/utypes.h"
24 #if !UCONFIG_NO_FORMATTING
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"
37 * DateFormat is an abstract class for a family of classes that convert dates and
38 * times from their internal representations to textual form and back again in a
39 * language-independent manner. Converting from the internal representation (milliseconds
40 * since midnight, January 1, 1970) to text is known as "formatting," and converting
41 * from text to millis is known as "parsing." We currently define only one concrete
42 * subclass of DateFormat: SimpleDateFormat, which can handle pretty much all normal
43 * date formatting and parsing actions.
45 * DateFormat helps you to format and parse dates for any locale. Your code can
46 * be completely independent of the locale conventions for months, days of the
47 * week, or even the calendar format: lunar vs. solar.
49 * To format a date for the current Locale, use one of the static factory
53 * DateFormat* dfmt = DateFormat::createDateInstance();
54 * UDate myDate = Calendar::getNow();
55 * UnicodeString myString;
56 * myString = dfmt->format( myDate, myString );
59 * If you are formatting multiple numbers, it is more efficient to get the
60 * format and use it multiple times so that the system doesn't have to fetch the
61 * information about the local language and country conventions multiple times.
64 * DateFormat* df = DateFormat::createDateInstance();
65 * UnicodeString myString;
66 * UDate myDateArr[] = { 0.0, 100000000.0, 2000000000.0 }; // test values
67 * for (int32_t i = 0; i < 3; ++i) {
69 * cout << df->format( myDateArr[i], myString ) << endl;
73 * To get specific fields of a date, you can use UFieldPosition to
74 * get specific fields.
77 * DateFormat* dfmt = DateFormat::createDateInstance();
78 * FieldPosition pos(DateFormat::YEAR_FIELD);
79 * UnicodeString myString;
80 * myString = dfmt->format( myDate, myString );
81 * cout << myString << endl;
82 * cout << pos.getBeginIndex() << "," << pos. getEndIndex() << endl;
85 * To format a date for a different Locale, specify it in the call to
86 * createDateInstance().
90 * DateFormat::createDateInstance( DateFormat::SHORT, Locale::getFrance());
93 * You can use a DateFormat to parse also.
96 * UErrorCode status = U_ZERO_ERROR;
97 * UDate myDate = df->parse(myString, status);
100 * Use createDateInstance() to produce the normal date format for that country.
101 * There are other static factory methods available. Use createTimeInstance()
102 * to produce the normal time format for that country. Use createDateTimeInstance()
103 * to produce a DateFormat that formats both date and time. You can pass in
104 * different options to these factory methods to control the length of the
105 * result; from SHORT to MEDIUM to LONG to FULL. The exact result depends on the
106 * locale, but generally:
108 * <li> SHORT is completely numeric, such as 12/13/52 or 3:30pm
109 * <li> MEDIUM is longer, such as Jan 12, 1952
110 * <li> LONG is longer, such as January 12, 1952 or 3:30:32pm
111 * <li> FULL is pretty completely specified, such as
112 * Tuesday, April 12, 1952 AD or 3:30:42pm PST.
114 * You can also set the time zone on the format if you wish. If you want even
115 * more control over the format or parsing, (or want to give your users more
116 * control), you can try casting the DateFormat you get from the factory methods
117 * to a SimpleDateFormat. This will work for the majority of countries; just
118 * remember to chck getDynamicClassID() before carrying out the cast.
120 * You can also use forms of the parse and format methods with ParsePosition and
121 * FieldPosition to allow you to
123 * <li> Progressively parse through pieces of a string.
124 * <li> Align any particular field, or find out where it is for selection
128 * <p><em>User subclasses are not supported.</em> While clients may write
129 * subclasses, such code will not necessarily work and will not be
130 * guaranteed to work stably from release to release.
132 class U_I18N_API DateFormat
: public Format
{
136 * Constants for various style patterns. These reflect the order of items in
137 * the DateTimePatterns resource. There are 4 time patterns, 4 date patterns,
138 * and then the date-time pattern. Each block of 4 values in the resource occurs
139 * in the order full, long, medium, short.
151 kDateOffset
= kShort
+ 1,
152 // kFull + kDateOffset = 4
153 // kLong + kDateOffset = 5
154 // kMedium + kDateOffset = 6
155 // kShort + kDateOffset = 7
164 * These constants are provided for backwards compatibility only.
165 * Please use the C++ style constants defined above.
172 DATE_OFFSET
= kDateOffset
,
174 DATE_TIME
= kDateTime
181 virtual ~DateFormat();
184 * Equality operator. Returns true if the two formats have the same behavior.
187 virtual UBool
operator==(const Format
&) const;
190 * Format an object to produce a string. This method handles Formattable
191 * objects with a UDate type. If a the Formattable object type is not a Date,
192 * then it returns a failing UErrorCode.
194 * @param obj The object to format. Must be a Date.
195 * @param appendTo Output parameter to receive result.
196 * Result is appended to existing contents.
197 * @param pos On input: an alignment field, if desired.
198 * On output: the offsets of the alignment field.
199 * @param status Output param filled with success/failure status.
200 * @return Reference to 'appendTo' parameter.
203 virtual UnicodeString
& format(const Formattable
& obj
,
204 UnicodeString
& appendTo
,
206 UErrorCode
& status
) const;
209 * Formats a date into a date/time string. This is an abstract method which
210 * concrete subclasses must implement.
212 * On input, the FieldPosition parameter may have its "field" member filled with
213 * an enum value specifying a field. On output, the FieldPosition will be filled
214 * in with the text offsets for that field.
215 * <P> For example, given a time text
216 * "1996.07.10 AD at 15:08:56 PDT", if the given fieldPosition.field is
217 * UDAT_YEAR_FIELD, the offsets fieldPosition.beginIndex and
218 * statfieldPositionus.getEndIndex will be set to 0 and 4, respectively.
220 * that if the same time field appears more than once in a pattern, the status will
221 * be set for the first occurence of that time field. For instance,
222 * formatting a UDate to the time string "1 PM PDT (Pacific Daylight Time)"
223 * using the pattern "h a z (zzzz)" and the alignment field
224 * DateFormat::TIMEZONE_FIELD, the offsets fieldPosition.beginIndex and
225 * fieldPosition.getEndIndex will be set to 5 and 8, respectively, for the first
226 * occurence of the timezone pattern character 'z'.
228 * @param cal Calendar set to the date and time to be formatted
229 * into a date/time string.
230 * @param appendTo Output parameter to receive result.
231 * Result is appended to existing contents.
232 * @param fieldPosition On input: an alignment field, if desired (see examples above)
233 * On output: the offsets of the alignment field (see examples above)
234 * @return Reference to 'appendTo' parameter.
237 virtual UnicodeString
& format( Calendar
& cal
,
238 UnicodeString
& appendTo
,
239 FieldPosition
& fieldPosition
) const = 0;
242 * Formats a UDate into a date/time string.
244 * On input, the FieldPosition parameter may have its "field" member filled with
245 * an enum value specifying a field. On output, the FieldPosition will be filled
246 * in with the text offsets for that field.
247 * <P> For example, given a time text
248 * "1996.07.10 AD at 15:08:56 PDT", if the given fieldPosition.field is
249 * UDAT_YEAR_FIELD, the offsets fieldPosition.beginIndex and
250 * statfieldPositionus.getEndIndex will be set to 0 and 4, respectively.
252 * that if the same time field appears more than once in a pattern, the status will
253 * be set for the first occurence of that time field. For instance,
254 * formatting a UDate to the time string "1 PM PDT (Pacific Daylight Time)"
255 * using the pattern "h a z (zzzz)" and the alignment field
256 * DateFormat::TIMEZONE_FIELD, the offsets fieldPosition.beginIndex and
257 * fieldPosition.getEndIndex will be set to 5 and 8, respectively, for the first
258 * occurence of the timezone pattern character 'z'.
260 * @param date UDate to be formatted into a date/time string.
261 * @param appendTo Output parameter to receive result.
262 * Result is appended to existing contents.
263 * @param fieldPosition On input: an alignment field, if desired (see examples above)
264 * On output: the offsets of the alignment field (see examples above)
265 * @return Reference to 'appendTo' parameter.
268 UnicodeString
& format( UDate date
,
269 UnicodeString
& appendTo
,
270 FieldPosition
& fieldPosition
) const;
273 * Formats a UDate into a date/time string. If there is a problem, you won't
274 * know, using this method. Use the overloaded format() method which takes a
275 * FieldPosition& to detect formatting problems.
277 * @param date The UDate value to be formatted into a string.
278 * @param appendTo Output parameter to receive result.
279 * Result is appended to existing contents.
280 * @return Reference to 'appendTo' parameter.
283 UnicodeString
& format(UDate date
, UnicodeString
& appendTo
) const;
286 * Redeclared Format method.
288 * @param obj The object to be formatted into a string.
289 * @param appendTo Output parameter to receive result.
290 * Result is appended to existing contents.
291 * @param status Output param filled with success/failure status.
292 * @return Reference to 'appendTo' parameter.
295 UnicodeString
& format(const Formattable
& obj
,
296 UnicodeString
& appendTo
,
297 UErrorCode
& status
) const;
300 * Parse a date/time string.
302 * @param text The string to be parsed into a UDate value.
303 * @param status Output param to be set to success/failure code. If
304 * 'text' cannot be parsed, it will be set to a failure
306 * @result The parsed UDate value, if successful.
309 virtual UDate
parse( const UnicodeString
& text
,
310 UErrorCode
& status
) const;
313 * Parse a date/time string beginning at the given parse position. For
314 * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
315 * that is equivalent to Date(837039928046).
317 * By default, parsing is lenient: If the input is not in the form used by
318 * this object's format method but can still be parsed as a date, then the
319 * parse succeeds. Clients may insist on strict adherence to the format by
320 * calling setLenient(false).
322 * @see DateFormat::setLenient(boolean)
324 * @param text The date/time string to be parsed
325 * @param cal a Calendar set to the date and time to be formatted
326 * into a date/time string.
327 * @param pos On input, the position at which to start parsing; on
328 * output, the position at which parsing terminated, or the
329 * start position if the parse failed.
330 * @return A valid UDate if the input could be parsed.
333 virtual void parse( const UnicodeString
& text
,
335 ParsePosition
& pos
) const = 0;
338 * Parse a date/time string beginning at the given parse position. For
339 * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
340 * that is equivalent to Date(837039928046).
342 * By default, parsing is lenient: If the input is not in the form used by
343 * this object's format method but can still be parsed as a date, then the
344 * parse succeeds. Clients may insist on strict adherence to the format by
345 * calling setLenient(false).
347 * @see DateFormat::setLenient(boolean)
349 * @param text The date/time string to be parsed
350 * @param pos On input, the position at which to start parsing; on
351 * output, the position at which parsing terminated, or the
352 * start position if the parse failed.
353 * @return A valid UDate if the input could be parsed.
356 UDate
parse( const UnicodeString
& text
,
357 ParsePosition
& pos
) const;
360 * Parse a string to produce an object. This methods handles parsing of
361 * date/time strings into Formattable objects with UDate types.
363 * Before calling, set parse_pos.index to the offset you want to start
364 * parsing at in the source. After calling, parse_pos.index is the end of
365 * the text you parsed. If error occurs, index is unchanged.
367 * When parsing, leading whitespace is discarded (with a successful parse),
368 * while trailing whitespace is left as is.
370 * See Format::parseObject() for more.
372 * @param source The string to be parsed into an object.
373 * @param result Formattable to be set to the parse result.
374 * If parse fails, return contents are undefined.
375 * @param parse_pos The position to start parsing at. Upon return
376 * this param is set to the position after the
377 * last character successfully parsed. If the
378 * source is not parsed successfully, this param
379 * will remain unchanged.
380 * @return A newly created Formattable* object, or NULL
381 * on failure. The caller owns this and should
382 * delete it when done.
385 virtual void parseObject(const UnicodeString
& source
,
387 ParsePosition
& parse_pos
) const;
390 * Create a default date/time formatter that uses the SHORT style for both
391 * the date and the time.
393 * @return A date/time formatter which the caller owns.
396 static DateFormat
* U_EXPORT2
createInstance(void);
399 * Creates a time formatter with the given formatting style for the given
402 * @param style The given formatting style. For example,
403 * SHORT for "h:mm a" in the US locale.
404 * @param aLocale The given locale.
405 * @return A time formatter which the caller owns.
408 static DateFormat
* U_EXPORT2
createTimeInstance(EStyle style
= kDefault
,
409 const Locale
& aLocale
= Locale::getDefault());
412 * Creates a date formatter with the given formatting style for the given
415 * @param style The given formatting style. For example,
416 * SHORT for "M/d/yy" in the US locale.
417 * @param aLocale The given locale.
418 * @return A date formatter which the caller owns.
421 static DateFormat
* U_EXPORT2
createDateInstance(EStyle style
= kDefault
,
422 const Locale
& aLocale
= Locale::getDefault());
425 * Creates a date/time formatter with the given formatting styles for the
428 * @param dateStyle The given formatting style for the date portion of the result.
429 * For example, SHORT for "M/d/yy" in the US locale.
430 * @param timeStyle The given formatting style for the time portion of the result.
431 * For example, SHORT for "h:mm a" in the US locale.
432 * @param aLocale The given locale.
433 * @return A date/time formatter which the caller owns.
436 static DateFormat
* U_EXPORT2
createDateTimeInstance(EStyle dateStyle
= kDefault
,
437 EStyle timeStyle
= kDefault
,
438 const Locale
& aLocale
= Locale::getDefault());
441 * Gets the set of locales for which DateFormats are installed.
442 * @param count Filled in with the number of locales in the list that is returned.
443 * @return the set of locales for which DateFormats are installed. The caller
444 * does NOT own this list and must not delete it.
447 static const Locale
* U_EXPORT2
getAvailableLocales(int32_t& count
);
450 * Returns true if the formatter is set for lenient parsing.
453 virtual UBool
isLenient(void) const;
456 * Specify whether or not date/time parsing is to be lenient. With lenient
457 * parsing, the parser may use heuristics to interpret inputs that do not
458 * precisely match this object's format. With strict parsing, inputs must
459 * match this object's format.
461 * @param lenient True specifies date/time interpretation to be lenient.
462 * @see Calendar::setLenient
465 virtual void setLenient(UBool lenient
);
468 * Gets the calendar associated with this date/time formatter.
469 * @return the calendar associated with this date/time formatter.
472 virtual const Calendar
* getCalendar(void) const;
475 * Set the calendar to be used by this date format. Initially, the default
476 * calendar for the specified or default locale is used. The caller should
477 * not delete the Calendar object after it is adopted by this call.
478 * Adopting a new calendar will change to the default symbols.
480 * @param calendarToAdopt Calendar object to be adopted.
483 virtual void adoptCalendar(Calendar
* calendarToAdopt
);
486 * Set the calendar to be used by this date format. Initially, the default
487 * calendar for the specified or default locale is used.
489 * @param newCalendar Calendar object to be set.
492 virtual void setCalendar(const Calendar
& newCalendar
);
496 * Gets the number formatter which this date/time formatter uses to format
497 * and parse the numeric portions of the pattern.
498 * @return the number formatter which this date/time formatter uses.
501 virtual const NumberFormat
* getNumberFormat(void) const;
504 * Allows you to set the number formatter. The caller should
505 * not delete the NumberFormat object after it is adopted by this call.
506 * @param formatToAdopt NumberFormat object to be adopted.
509 virtual void adoptNumberFormat(NumberFormat
* formatToAdopt
);
512 * Allows you to set the number formatter.
513 * @param newNumberFormat NumberFormat object to be set.
516 virtual void setNumberFormat(const NumberFormat
& newNumberFormat
);
519 * Returns a reference to the TimeZone used by this DateFormat's calendar.
520 * @return the time zone associated with the calendar of DateFormat.
523 virtual const TimeZone
& getTimeZone(void) const;
526 * Sets the time zone for the calendar of this DateFormat object. The caller
527 * no longer owns the TimeZone object and should not delete it after this call.
528 * @param zoneToAdopt the TimeZone to be adopted.
531 virtual void adoptTimeZone(TimeZone
* zoneToAdopt
);
534 * Sets the time zone for the calendar of this DateFormat object.
535 * @param zone the new time zone.
538 virtual void setTimeZone(const TimeZone
& zone
);
542 * Default constructor. Creates a DateFormat with no Calendar or NumberFormat
543 * associated with it. This constructor depends on the subclasses to fill in
544 * the calendar and numberFormat fields.
553 DateFormat(const DateFormat
&);
556 * Default assignment operator.
559 DateFormat
& operator=(const DateFormat
&);
562 * The calendar that DateFormat uses to produce the time field values needed
563 * to implement date/time formatting. Subclasses should generally initialize
564 * this to the default calendar for the locale associated with this DateFormat.
570 * The number formatter that DateFormat uses to format numbers in dates and
571 * times. Subclasses should generally initialize this to the default number
572 * format for the locale associated with this DateFormat.
575 NumberFormat
* fNumberFormat
;
579 * Gets the date/time formatter with the given formatting styles for the
581 * @param dateStyle the given date formatting style.
582 * @param timeStyle the given time formatting style.
583 * @param inLocale the given locale.
584 * @return a date/time formatter, or 0 on failure.
586 static DateFormat
* U_EXPORT2
create(EStyle timeStyle
, EStyle dateStyle
, const Locale
&);
590 * Field selector for FieldPosition for DateFormat fields.
591 * @obsolete ICU 3.4 use UDateFormatField instead, since this API will be
592 * removed in that release
596 // Obsolete; use UDateFormatField instead
597 kEraField
= UDAT_ERA_FIELD
,
598 kYearField
= UDAT_YEAR_FIELD
,
599 kMonthField
= UDAT_MONTH_FIELD
,
600 kDateField
= UDAT_DATE_FIELD
,
601 kHourOfDay1Field
= UDAT_HOUR_OF_DAY1_FIELD
,
602 kHourOfDay0Field
= UDAT_HOUR_OF_DAY0_FIELD
,
603 kMinuteField
= UDAT_MINUTE_FIELD
,
604 kSecondField
= UDAT_SECOND_FIELD
,
605 kMillisecondField
= UDAT_FRACTIONAL_SECOND_FIELD
,
606 kDayOfWeekField
= UDAT_DAY_OF_WEEK_FIELD
,
607 kDayOfYearField
= UDAT_DAY_OF_YEAR_FIELD
,
608 kDayOfWeekInMonthField
= UDAT_DAY_OF_WEEK_IN_MONTH_FIELD
,
609 kWeekOfYearField
= UDAT_WEEK_OF_YEAR_FIELD
,
610 kWeekOfMonthField
= UDAT_WEEK_OF_MONTH_FIELD
,
611 kAmPmField
= UDAT_AM_PM_FIELD
,
612 kHour1Field
= UDAT_HOUR1_FIELD
,
613 kHour0Field
= UDAT_HOUR0_FIELD
,
614 kTimezoneField
= UDAT_TIMEZONE_FIELD
,
615 kYearWOYField
= UDAT_YEAR_WOY_FIELD
,
616 kDOWLocalField
= UDAT_DOW_LOCAL_FIELD
,
617 kExtendedYearField
= UDAT_EXTENDED_YEAR_FIELD
,
618 kJulianDayField
= UDAT_JULIAN_DAY_FIELD
,
619 kMillisecondsInDayField
= UDAT_MILLISECONDS_IN_DAY_FIELD
,
621 // Obsolete; use UDateFormatField instead
622 ERA_FIELD
= UDAT_ERA_FIELD
,
623 YEAR_FIELD
= UDAT_YEAR_FIELD
,
624 MONTH_FIELD
= UDAT_MONTH_FIELD
,
625 DATE_FIELD
= UDAT_DATE_FIELD
,
626 HOUR_OF_DAY1_FIELD
= UDAT_HOUR_OF_DAY1_FIELD
,
627 HOUR_OF_DAY0_FIELD
= UDAT_HOUR_OF_DAY0_FIELD
,
628 MINUTE_FIELD
= UDAT_MINUTE_FIELD
,
629 SECOND_FIELD
= UDAT_SECOND_FIELD
,
630 MILLISECOND_FIELD
= UDAT_FRACTIONAL_SECOND_FIELD
,
631 DAY_OF_WEEK_FIELD
= UDAT_DAY_OF_WEEK_FIELD
,
632 DAY_OF_YEAR_FIELD
= UDAT_DAY_OF_YEAR_FIELD
,
633 DAY_OF_WEEK_IN_MONTH_FIELD
= UDAT_DAY_OF_WEEK_IN_MONTH_FIELD
,
634 WEEK_OF_YEAR_FIELD
= UDAT_WEEK_OF_YEAR_FIELD
,
635 WEEK_OF_MONTH_FIELD
= UDAT_WEEK_OF_MONTH_FIELD
,
636 AM_PM_FIELD
= UDAT_AM_PM_FIELD
,
637 HOUR1_FIELD
= UDAT_HOUR1_FIELD
,
638 HOUR0_FIELD
= UDAT_HOUR0_FIELD
,
639 TIMEZONE_FIELD
= UDAT_TIMEZONE_FIELD
643 inline UnicodeString
&
644 DateFormat::format(const Formattable
& obj
,
645 UnicodeString
& appendTo
,
646 UErrorCode
& status
) const {
647 return Format::format(obj
, appendTo
, status
);
651 #endif /* #if !UCONFIG_NO_FORMATTING */