2 * Copyright (C) 1997-2012, International Business Machines Corporation and
3 * others. All Rights Reserved.
4 *******************************************************************************
8 * Modification History:
10 * Date Name Description
11 * 02/19/97 aliu Converted from java.
12 * 07/09/97 helena Make ParsePosition into a class.
13 * 07/21/98 stephen Added GMT_PLUS, GMT_MINUS
14 * Changed setTwoDigitStartDate to set2DigitYearStart
15 * Changed getTwoDigitStartDate to get2DigitYearStart
16 * Removed subParseLong
17 * Removed getZoneIndex (added in DateFormatSymbols)
18 * 06/14/99 stephen Removed fgTimeZoneDataSuffix
19 * 10/14/99 aliu Updated class doc to describe 2-digit year parsing
21 *******************************************************************************
27 #include "unicode/utypes.h"
31 * \brief C++ API: Format and parse dates in a language-independent manner.
34 #if !UCONFIG_NO_FORMATTING
36 #include "unicode/datefmt.h"
40 class DateFormatSymbols
;
43 class FieldPositionHandler
;
48 * SimpleDateFormat is a concrete class for formatting and parsing dates in a
49 * language-independent manner. It allows for formatting (millis -> text),
50 * parsing (text -> millis), and normalization. Formats/Parses a date or time,
51 * which is the standard milliseconds since 24:00 GMT, Jan 1, 1970.
53 * Clients are encouraged to create a date-time formatter using DateFormat::getInstance(),
54 * getDateInstance(), getDateInstance(), or getDateTimeInstance() rather than
55 * explicitly constructing an instance of SimpleDateFormat. This way, the client
56 * is guaranteed to get an appropriate formatting pattern for whatever locale the
57 * program is running in. However, if the client needs something more unusual than
58 * the default patterns in the locales, he can construct a SimpleDateFormat directly
59 * and give it an appropriate pattern (or use one of the factory methods on DateFormat
60 * and modify the pattern after the fact with toPattern() and applyPattern().
62 * Date/Time format syntax:
64 * The date/time format is specified by means of a string time pattern. In this
65 * pattern, all ASCII letters are reserved as pattern letters, which are defined
69 * Symbol Meaning Presentation Example
70 * ------ ------- ------------ -------
71 * G era designator (Text) AD
72 * y year (Number) 1996
73 * Y year (week of year) (Number) 1997
74 * u extended year (Number) 4601
75 * U cyclic year name (Text,NumFallback) ren-chen (29)
76 * Q Quarter (Text & Number) Q2 & 02
77 * M month in year (Text & Number) July & 07
78 * d day in month (Number) 10
79 * h hour in am/pm (1~12) (Number) 12
80 * H hour in day (0~23) (Number) 0
81 * m minute in hour (Number) 30
82 * s second in minute (Number) 55
83 * S fractional second (Number) 978
84 * E day of week (Text) Tuesday
85 * e day of week (local 1~7) (Text & Number) Tues & 2
86 * D day in year (Number) 189
87 * F day of week in month (Number) 2 (2nd Wed in July)
88 * w week in year (Number) 27
89 * W week in month (Number) 2
90 * a am/pm marker (Text) PM
91 * k hour in day (1~24) (Number) 24
92 * K hour in am/pm (0~11) (Number) 0
93 * z time zone (Text) PST
94 * zzzz time zone (Text) Pacific Standard Time
95 * Z time zone (RFC 822) (Number) -0800
96 * ZZZZ time zone (RFC 822) (Text & Number) GMT-08:00
97 * ZZZZZ time zone (ISO 8601) (Text & Number) -08:00 & Z
98 * v time zone (generic) (Text) PT
99 * vvvv time zone (generic) (Text) Pacific Time
100 * V time zone (abreviation) (Text) PST
101 * VVVV time zone (location) (Text) United States Time (Los Angeles)
102 * g Julian day (Number) 2451334
103 * A milliseconds in day (Number) 69540000
104 * q stand alone quarter (Text & Number) Q2 & 02
105 * L stand alone month (Text & Number) July & 07
106 * c stand alone day of week (Text & Number) Tuesday & 2
107 * ' escape for text (Delimiter) 'Date='
108 * '' single quote (Literal) 'o''clock'
111 * The count of pattern letters determine the format.
113 * (Text): 4 or more, use full form, <4, use short or abbreviated form if it
114 * exists. (e.g., "EEEE" produces "Monday", "EEE" produces "Mon")
116 * (Number): the minimum number of digits. Shorter numbers are zero-padded to
117 * this amount (e.g. if "m" produces "6", "mm" produces "06"). Year is handled
118 * specially; that is, if the count of 'y' is 2, the Year will be truncated to 2 digits.
119 * (e.g., if "yyyy" produces "1997", "yy" produces "97".)
120 * Unlike other fields, fractional seconds are padded on the right with zero.
122 * (Text & Number): 3 or over, use text, otherwise use number. (e.g., "M" produces "1",
123 * "MM" produces "01", "MMM" produces "Jan", and "MMMM" produces "January".)
125 * (Text,NumFallback): Behaves like Text if there is supporting data, like
128 * Any characters in the pattern that are not in the ranges of ['a'..'z'] and
129 * ['A'..'Z'] will be treated as quoted text. For instance, characters
130 * like ':', '.', ' ', '#' and '@' will appear in the resulting time text
131 * even they are not embraced within single quotes.
133 * A pattern containing any invalid pattern letter will result in a failing
134 * UErrorCode result during formatting or parsing.
136 * Examples using the US locale:
139 * Format Pattern Result
140 * -------------- -------
141 * "yyyy.MM.dd G 'at' HH:mm:ss vvvv" ->> 1996.07.10 AD at 15:08:56 Pacific Time
142 * "EEE, MMM d, ''yy" ->> Wed, July 10, '96
143 * "h:mm a" ->> 12:08 PM
144 * "hh 'o''clock' a, zzzz" ->> 12 o'clock PM, Pacific Daylight Time
145 * "K:mm a, vvv" ->> 0:00 PM, PT
146 * "yyyyy.MMMMM.dd GGG hh:mm aaa" ->> 1996.July.10 AD 12:08 PM
152 * UErrorCode success = U_ZERO_ERROR;
153 * SimpleTimeZone* pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, "PST");
154 * pdt->setStartRule( Calendar::APRIL, 1, Calendar::SUNDAY, 2*60*60*1000);
155 * pdt->setEndRule( Calendar::OCTOBER, -1, Calendar::SUNDAY, 2*60*60*1000);
157 * // Format the current time.
158 * SimpleDateFormat* formatter
159 * = new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss a zzz", success );
160 * GregorianCalendar cal(success);
161 * UDate currentTime_1 = cal.getTime(success);
162 * FieldPosition fp(0);
163 * UnicodeString dateString;
164 * formatter->format( currentTime_1, dateString, fp );
165 * cout << "result: " << dateString << endl;
167 * // Parse the previous string back into a Date.
168 * ParsePosition pp(0);
169 * UDate currentTime_2 = formatter->parse(dateString, pp );
172 * In the above example, the time value "currentTime_2" obtained from parsing
173 * will be equal to currentTime_1. However, they may not be equal if the am/pm
174 * marker 'a' is left out from the format pattern while the "hour in am/pm"
175 * pattern symbol is used. This information loss can happen when formatting the
179 * When parsing a date string using the abbreviated year pattern ("y" or "yy"),
180 * SimpleDateFormat must interpret the abbreviated year
181 * relative to some century. It does this by adjusting dates to be
182 * within 80 years before and 20 years after the time the SimpleDateFormat
183 * instance is created. For example, using a pattern of "MM/dd/yy" and a
184 * SimpleDateFormat instance created on Jan 1, 1997, the string
185 * "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64"
186 * would be interpreted as May 4, 1964.
187 * During parsing, only strings consisting of exactly two digits, as defined by
188 * <code>Unicode::isDigit()</code>, will be parsed into the default century.
189 * Any other numeric string, such as a one digit string, a three or more digit
190 * string, or a two digit string that isn't all digits (for example, "-1"), is
191 * interpreted literally. So "01/02/3" or "01/02/003" are parsed (for the
192 * Gregorian calendar), using the same pattern, as Jan 2, 3 AD. Likewise (but
193 * only in lenient parse mode, the default) "01/02/-3" is parsed as Jan 2, 4 BC.
196 * If the year pattern has more than two 'y' characters, the year is
197 * interpreted literally, regardless of the number of digits. So using the
198 * pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D.
201 * When numeric fields abut one another directly, with no intervening delimiter
202 * characters, they constitute a run of abutting numeric fields. Such runs are
203 * parsed specially. For example, the format "HHmmss" parses the input text
204 * "123456" to 12:34:56, parses the input text "12345" to 1:23:45, and fails to
205 * parse "1234". In other words, the leftmost field of the run is flexible,
206 * while the others keep a fixed width. If the parse fails anywhere in the run,
207 * then the leftmost field is shortened by one character, and the entire run is
208 * parsed again. This is repeated until either the parse succeeds or the
209 * leftmost field is one character in length. If the parse still fails at that
210 * point, the parse of the run fails.
213 * For time zones that have no names, SimpleDateFormat uses strings GMT+hours:minutes or
216 * The calendar defines what is the first day of the week, the first week of the
217 * year, whether hours are zero based or not (0 vs 12 or 24), and the timezone.
218 * There is one common number format to handle all the numbers; the digit count
219 * is handled programmatically according to the pattern.
221 * <p><em>User subclasses are not supported.</em> While clients may write
222 * subclasses, such code will not necessarily work and will not be
223 * guaranteed to work stably from release to release.
225 class U_I18N_API SimpleDateFormat
: public DateFormat
{
228 * Construct a SimpleDateFormat using the default pattern for the default
231 * [Note:] Not all locales support SimpleDateFormat; for full generality,
232 * use the factory methods in the DateFormat class.
233 * @param status Output param set to success/failure code.
236 SimpleDateFormat(UErrorCode
& status
);
239 * Construct a SimpleDateFormat using the given pattern and the default locale.
240 * The locale is used to obtain the symbols used in formatting (e.g., the
241 * names of the months), but not to provide the pattern.
243 * [Note:] Not all locales support SimpleDateFormat; for full generality,
244 * use the factory methods in the DateFormat class.
245 * @param pattern the pattern for the format.
246 * @param status Output param set to success/failure code.
249 SimpleDateFormat(const UnicodeString
& pattern
,
253 * Construct a SimpleDateFormat using the given pattern, numbering system override, and the default locale.
254 * The locale is used to obtain the symbols used in formatting (e.g., the
255 * names of the months), but not to provide the pattern.
257 * A numbering system override is a string containing either the name of a known numbering system,
258 * or a set of field and numbering system pairs that specify which fields are to be formattied with
259 * the alternate numbering system. For example, to specify that all numeric fields in the specified
260 * date or time pattern are to be rendered using Thai digits, simply specify the numbering system override
261 * as "thai". To specify that just the year portion of the date be formatted using Hebrew numbering,
262 * use the override string "y=hebrew". Numbering system overrides can be combined using a semi-colon
263 * character in the override string, such as "d=decimal;M=arabic;y=hebrew", etc.
266 * [Note:] Not all locales support SimpleDateFormat; for full generality,
267 * use the factory methods in the DateFormat class.
268 * @param pattern the pattern for the format.
269 * @param override the override string.
270 * @param status Output param set to success/failure code.
273 SimpleDateFormat(const UnicodeString
& pattern
,
274 const UnicodeString
& override
,
278 * Construct a SimpleDateFormat using the given pattern and locale.
279 * The locale is used to obtain the symbols used in formatting (e.g., the
280 * names of the months), but not to provide the pattern.
282 * [Note:] Not all locales support SimpleDateFormat; for full generality,
283 * use the factory methods in the DateFormat class.
284 * @param pattern the pattern for the format.
285 * @param locale the given locale.
286 * @param status Output param set to success/failure code.
289 SimpleDateFormat(const UnicodeString
& pattern
,
290 const Locale
& locale
,
294 * Construct a SimpleDateFormat using the given pattern, numbering system override, and locale.
295 * The locale is used to obtain the symbols used in formatting (e.g., the
296 * names of the months), but not to provide the pattern.
298 * A numbering system override is a string containing either the name of a known numbering system,
299 * or a set of field and numbering system pairs that specify which fields are to be formattied with
300 * the alternate numbering system. For example, to specify that all numeric fields in the specified
301 * date or time pattern are to be rendered using Thai digits, simply specify the numbering system override
302 * as "thai". To specify that just the year portion of the date be formatted using Hebrew numbering,
303 * use the override string "y=hebrew". Numbering system overrides can be combined using a semi-colon
304 * character in the override string, such as "d=decimal;M=arabic;y=hebrew", etc.
306 * [Note:] Not all locales support SimpleDateFormat; for full generality,
307 * use the factory methods in the DateFormat class.
308 * @param pattern the pattern for the format.
309 * @param override the numbering system override.
310 * @param locale the given locale.
311 * @param status Output param set to success/failure code.
314 SimpleDateFormat(const UnicodeString
& pattern
,
315 const UnicodeString
& override
,
316 const Locale
& locale
,
320 * Construct a SimpleDateFormat using the given pattern and locale-specific
321 * symbol data. The formatter takes ownership of the DateFormatSymbols object;
322 * the caller is no longer responsible for deleting it.
323 * @param pattern the given pattern for the format.
324 * @param formatDataToAdopt the symbols to be adopted.
325 * @param status Output param set to success/faulure code.
328 SimpleDateFormat(const UnicodeString
& pattern
,
329 DateFormatSymbols
* formatDataToAdopt
,
333 * Construct a SimpleDateFormat using the given pattern and locale-specific
334 * symbol data. The DateFormatSymbols object is NOT adopted; the caller
335 * remains responsible for deleting it.
336 * @param pattern the given pattern for the format.
337 * @param formatData the formatting symbols to be use.
338 * @param status Output param set to success/faulure code.
341 SimpleDateFormat(const UnicodeString
& pattern
,
342 const DateFormatSymbols
& formatData
,
349 SimpleDateFormat(const SimpleDateFormat
&);
352 * Assignment operator.
355 SimpleDateFormat
& operator=(const SimpleDateFormat
&);
361 virtual ~SimpleDateFormat();
364 * Clone this Format object polymorphically. The caller owns the result and
365 * should delete it when done.
366 * @return A copy of the object.
369 virtual Format
* clone(void) const;
372 * Return true if the given Format objects are semantically equal. Objects
373 * of different subclasses are considered unequal.
374 * @param other the object to be compared with.
375 * @return true if the given Format objects are semantically equal.
378 virtual UBool
operator==(const Format
& other
) const;
381 using DateFormat::format
;
384 * Format a date or time, which is the standard millis since 24:00 GMT, Jan
385 * 1, 1970. Overrides DateFormat pure virtual method.
387 * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->>
388 * 1996.07.10 AD at 15:08:56 PDT
390 * @param cal Calendar set to the date and time to be formatted
391 * into a date/time string.
392 * @param appendTo Output parameter to receive result.
393 * Result is appended to existing contents.
394 * @param pos The formatting position. On input: an alignment field,
395 * if desired. On output: the offsets of the alignment field.
396 * @return Reference to 'appendTo' parameter.
399 virtual UnicodeString
& format( Calendar
& cal
,
400 UnicodeString
& appendTo
,
401 FieldPosition
& pos
) const;
403 /* Cannot use #ifndef U_HIDE_DRAFT_API for the following draft method since it is virtual */
405 * Format a date or time, which is the standard millis since 24:00 GMT, Jan
406 * 1, 1970. Overrides DateFormat pure virtual method.
408 * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->>
409 * 1996.07.10 AD at 15:08:56 PDT
411 * @param cal Calendar set to the date and time to be formatted
412 * into a date/time string.
413 * @param types Array of UDateFormatContextTypes for which the corresponding
414 * value specified in the next parameter should override the
415 * formatter's default value for this call (this does not
416 * change the default value).
417 * @param values Array of UDateFormatContextValues corresponding 1-1 to the
418 * UDateFormatContextTypes in the previous parameter.
419 * @param typesAndValuesCount Number of elements in the types and values
421 * @param appendTo Output parameter to receive result.
422 * Result is appended to existing contents.
423 * @param pos The formatting position. On input: an alignment field,
424 * if desired. On output: the offsets of the alignment field.
425 * @return Reference to 'appendTo' parameter.
428 virtual UnicodeString
& format( Calendar
& cal
,
429 const UDateFormatContextType
* types
,
430 const UDateFormatContextValue
* values
,
431 int32_t typesAndValuesCount
,
432 UnicodeString
& appendTo
,
433 FieldPosition
& pos
) const;
436 * Format a date or time, which is the standard millis since 24:00 GMT, Jan
437 * 1, 1970. Overrides DateFormat pure virtual method.
439 * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->>
440 * 1996.07.10 AD at 15:08:56 PDT
442 * @param cal Calendar set to the date and time to be formatted
443 * into a date/time string.
444 * @param appendTo Output parameter to receive result.
445 * Result is appended to existing contents.
446 * @param posIter On return, can be used to iterate over positions
447 * of fields generated by this format call. Field values
448 * are defined in UDateFormatField.
449 * @param status Input/output param set to success/failure code.
450 * @return Reference to 'appendTo' parameter.
453 virtual UnicodeString
& format( Calendar
& cal
,
454 UnicodeString
& appendTo
,
455 FieldPositionIterator
* posIter
,
456 UErrorCode
& status
) const;
459 * Format a date or time, which is the standard millis since 24:00 GMT, Jan
460 * 1, 1970. Overrides DateFormat pure virtual method.
462 * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->>
463 * 1996.07.10 AD at 15:08:56 PDT
465 * @param obj A Formattable containing the date-time value to be formatted
466 * into a date-time string. If the type of the Formattable
467 * is a numeric type, it is treated as if it were an
469 * @param appendTo Output parameter to receive result.
470 * Result is appended to existing contents.
471 * @param pos The formatting position. On input: an alignment field,
472 * if desired. On output: the offsets of the alignment field.
473 * @param status Input/output param set to success/failure code.
474 * @return Reference to 'appendTo' parameter.
477 virtual UnicodeString
& format( const Formattable
& obj
,
478 UnicodeString
& appendTo
,
480 UErrorCode
& status
) const;
483 * Format a date or time, which is the standard millis since 24:00 GMT, Jan
484 * 1, 1970. Overrides DateFormat pure virtual method.
486 * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->>
487 * 1996.07.10 AD at 15:08:56 PDT
489 * @param obj A Formattable containing the date-time value to be formatted
490 * into a date-time string. If the type of the Formattable
491 * is a numeric type, it is treated as if it were an
493 * @param appendTo Output parameter to receive result.
494 * Result is appended to existing contents.
495 * @param posIter On return, can be used to iterate over positions
496 * of fields generated by this format call. Field values
497 * are defined in UDateFormatField.
498 * @param status Input/output param set to success/failure code.
499 * @return Reference to 'appendTo' parameter.
502 virtual UnicodeString
& format( const Formattable
& obj
,
503 UnicodeString
& appendTo
,
504 FieldPositionIterator
* posIter
,
505 UErrorCode
& status
) const;
508 * Redeclared DateFormat method.
509 * @param date the Date value to be formatted.
510 * @param appendTo Output parameter to receive result.
511 * Result is appended to existing contents.
512 * @param fieldPosition The formatting position. On input: an alignment field,
513 * if desired. On output: the offsets of the alignment field.
514 * @return Reference to 'appendTo' parameter.
517 UnicodeString
& format(UDate date
,
518 UnicodeString
& appendTo
,
519 FieldPosition
& fieldPosition
) const;
522 * Redeclared DateFormat method.
523 * @param date the Date value to be formatted.
524 * @param appendTo Output parameter to receive result.
525 * Result is appended to existing contents.
526 * @param posIter On return, can be used to iterate over positions
527 * of fields generated by this format call. Field values
528 * are defined in UDateFormatField.
529 * @param status Input/output param set to success/failure code.
530 * @return Reference to 'appendTo' parameter.
533 UnicodeString
& format(UDate date
,
534 UnicodeString
& appendTo
,
535 FieldPositionIterator
* posIter
,
536 UErrorCode
& status
) const;
539 * Redeclared DateFormat method.
540 * @param obj Object to be formatted.
541 * @param appendTo Output parameter to receive result.
542 * Result is appended to existing contents.
543 * @param status Input/output success/failure code.
544 * @return Reference to 'appendTo' parameter.
547 UnicodeString
& format(const Formattable
& obj
,
548 UnicodeString
& appendTo
,
549 UErrorCode
& status
) const;
552 * Redeclared DateFormat method.
553 * @param date Date value to be formatted.
554 * @param appendTo Output parameter to receive result.
555 * Result is appended to existing contents.
556 * @return Reference to 'appendTo' parameter.
559 UnicodeString
& format(UDate date
, UnicodeString
& appendTo
) const;
562 * Parse a date/time string beginning at the given parse position. For
563 * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
564 * that is equivalent to Date(837039928046).
566 * By default, parsing is lenient: If the input is not in the form used by
567 * this object's format method but can still be parsed as a date, then the
568 * parse succeeds. Clients may insist on strict adherence to the format by
569 * calling setLenient(false).
570 * @see DateFormat::setLenient(boolean)
572 * @param text The date/time string to be parsed
573 * @param cal A Calendar set on input to the date and time to be used for
574 * missing values in the date/time string being parsed, and set
575 * on output to the parsed date/time. When the calendar type is
576 * different from the internal calendar held by this SimpleDateFormat
577 * instance, the internal calendar will be cloned to a work
578 * calendar set to the same milliseconds and time zone as the
579 * cal parameter, field values will be parsed based on the work
580 * calendar, then the result (milliseconds and time zone) will
581 * be set in this calendar.
582 * @param pos On input, the position at which to start parsing; on
583 * output, the position at which parsing terminated, or the
584 * start position if the parse failed.
585 * @return A valid UDate if the input could be parsed.
588 virtual void parse( const UnicodeString
& text
,
590 ParsePosition
& pos
) const;
593 * Parse a date/time string starting at the given parse position. For
594 * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
595 * that is equivalent to Date(837039928046).
597 * By default, parsing is lenient: If the input is not in the form used by
598 * this object's format method but can still be parsed as a date, then the
599 * parse succeeds. Clients may insist on strict adherence to the format by
600 * calling setLenient(false).
601 * @see DateFormat::setLenient(boolean)
603 * Note that the normal date formats associated with some calendars - such
604 * as the Chinese lunar calendar - do not specify enough fields to enable
605 * dates to be parsed unambiguously. In the case of the Chinese lunar
606 * calendar, while the year within the current 60-year cycle is specified,
607 * the number of such cycles since the start date of the calendar (in the
608 * ERA field of the Calendar object) is not normally part of the format,
609 * and parsing may assume the wrong era. For cases such as this it is
610 * recommended that clients parse using the method
611 * parse(const UnicodeString&, Calendar& cal, ParsePosition&)
612 * with the Calendar passed in set to the current date, or to a date
613 * within the era/cycle that should be assumed if absent in the format.
615 * @param text The date/time string to be parsed
616 * @param pos On input, the position at which to start parsing; on
617 * output, the position at which parsing terminated, or the
618 * start position if the parse failed.
619 * @return A valid UDate if the input could be parsed.
622 UDate
parse( const UnicodeString
& text
,
623 ParsePosition
& pos
) const;
627 * Parse a date/time string. For example, a time text "07/10/96 4:5 PM, PDT"
628 * will be parsed into a UDate that is equivalent to Date(837039928046).
629 * Parsing begins at the beginning of the string and proceeds as far as
630 * possible. Assuming no parse errors were encountered, this function
631 * doesn't return any information about how much of the string was consumed
632 * by the parsing. If you need that information, use the version of
633 * parse() that takes a ParsePosition.
635 * By default, parsing is lenient: If the input is not in the form used by
636 * this object's format method but can still be parsed as a date, then the
637 * parse succeeds. Clients may insist on strict adherence to the format by
638 * calling setLenient(false).
639 * @see DateFormat::setLenient(boolean)
641 * Note that the normal date formats associated with some calendars - such
642 * as the Chinese lunar calendar - do not specify enough fields to enable
643 * dates to be parsed unambiguously. In the case of the Chinese lunar
644 * calendar, while the year within the current 60-year cycle is specified,
645 * the number of such cycles since the start date of the calendar (in the
646 * ERA field of the Calendar object) is not normally part of the format,
647 * and parsing may assume the wrong era. For cases such as this it is
648 * recommended that clients parse using the method
649 * parse(const UnicodeString&, Calendar& cal, ParsePosition&)
650 * with the Calendar passed in set to the current date, or to a date
651 * within the era/cycle that should be assumed if absent in the format.
653 * @param text The date/time string to be parsed into a UDate value.
654 * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with
655 * an error value if there was a parse error.
656 * @return A valid UDate if the input could be parsed.
659 virtual UDate
parse( const UnicodeString
& text
,
660 UErrorCode
& status
) const;
663 * Set the start UDate used to interpret two-digit year strings.
664 * When dates are parsed having 2-digit year strings, they are placed within
665 * a assumed range of 100 years starting on the two digit start date. For
666 * example, the string "24-Jan-17" may be in the year 1817, 1917, 2017, or
667 * some other year. SimpleDateFormat chooses a year so that the resultant
668 * date is on or after the two digit start date and within 100 years of the
669 * two digit start date.
671 * By default, the two digit start date is set to 80 years before the current
672 * time at which a SimpleDateFormat object is created.
673 * @param d start UDate used to interpret two-digit year strings.
674 * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with
675 * an error value if there was a parse error.
678 virtual void set2DigitYearStart(UDate d
, UErrorCode
& status
);
681 * Get the start UDate used to interpret two-digit year strings.
682 * When dates are parsed having 2-digit year strings, they are placed within
683 * a assumed range of 100 years starting on the two digit start date. For
684 * example, the string "24-Jan-17" may be in the year 1817, 1917, 2017, or
685 * some other year. SimpleDateFormat chooses a year so that the resultant
686 * date is on or after the two digit start date and within 100 years of the
687 * two digit start date.
689 * By default, the two digit start date is set to 80 years before the current
690 * time at which a SimpleDateFormat object is created.
691 * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with
692 * an error value if there was a parse error.
695 UDate
get2DigitYearStart(UErrorCode
& status
) const;
698 * Return a pattern string describing this date format.
699 * @param result Output param to receive the pattern.
700 * @return A reference to 'result'.
703 virtual UnicodeString
& toPattern(UnicodeString
& result
) const;
706 * Return a localized pattern string describing this date format.
707 * In most cases, this will return the same thing as toPattern(),
708 * but a locale can specify characters to use in pattern descriptions
709 * in place of the ones described in this class's class documentation.
710 * (Presumably, letters that would be more mnemonic in that locale's
711 * language.) This function would produce a pattern using those
714 * @param result Receives the localized pattern.
715 * @param status Output param set to success/failure code on
716 * exit. If the pattern is invalid, this will be
717 * set to a failure result.
718 * @return A reference to 'result'.
721 virtual UnicodeString
& toLocalizedPattern(UnicodeString
& result
,
722 UErrorCode
& status
) const;
725 * Apply the given unlocalized pattern string to this date format.
726 * (i.e., after this call, this formatter will format dates according to
729 * @param pattern The pattern to be applied.
732 virtual void applyPattern(const UnicodeString
& pattern
);
735 * Apply the given localized pattern string to this date format.
736 * (see toLocalizedPattern() for more information on localized patterns.)
738 * @param pattern The localized pattern to be applied.
739 * @param status Output param set to success/failure code on
740 * exit. If the pattern is invalid, this will be
741 * set to a failure result.
744 virtual void applyLocalizedPattern(const UnicodeString
& pattern
,
748 * Gets the date/time formatting symbols (this is an object carrying
749 * the various strings and other symbols used in formatting: e.g., month
750 * names and abbreviations, time zone names, AM/PM strings, etc.)
751 * @return a copy of the date-time formatting data associated
752 * with this date-time formatter.
755 virtual const DateFormatSymbols
* getDateFormatSymbols(void) const;
758 * Set the date/time formatting symbols. The caller no longer owns the
759 * DateFormatSymbols object and should not delete it after making this call.
760 * @param newFormatSymbols the given date-time formatting symbols to copy.
763 virtual void adoptDateFormatSymbols(DateFormatSymbols
* newFormatSymbols
);
766 * Set the date/time formatting data.
767 * @param newFormatSymbols the given date-time formatting symbols to copy.
770 virtual void setDateFormatSymbols(const DateFormatSymbols
& newFormatSymbols
);
773 * Return the class ID for this class. This is useful only for comparing to
774 * a return value from getDynamicClassID(). For example:
776 * . Base* polymorphic_pointer = createPolymorphicObject();
777 * . if (polymorphic_pointer->getDynamicClassID() ==
778 * . erived::getStaticClassID()) ...
780 * @return The class ID for all objects of this class.
783 static UClassID U_EXPORT2
getStaticClassID(void);
786 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
787 * method is to implement a simple version of RTTI, since not all C++
788 * compilers support genuine RTTI. Polymorphic operator==() and clone()
789 * methods call this method.
791 * @return The class ID for this object. All objects of a
792 * given class have the same class ID. Objects of
793 * other classes have different class IDs.
796 virtual UClassID
getDynamicClassID(void) const;
799 * Set the calendar to be used by this date format. Initially, the default
800 * calendar for the specified or default locale is used. The caller should
801 * not delete the Calendar object after it is adopted by this call.
802 * Adopting a new calendar will change to the default symbols.
804 * @param calendarToAdopt Calendar object to be adopted.
807 virtual void adoptCalendar(Calendar
* calendarToAdopt
);
809 /* Cannot use #ifndef U_HIDE_DRAFT_API for the following draft method since it is virtual */
811 * Set the formatter's default value for a particular context type,
812 * such as UDAT_CAPITALIZATION.
813 * @param type The context type for which the default value should be set.
814 * @param value The default value to set for the specified context type.
815 * @param status Input/output status. If at entry this indicates a failure
816 * status, the function will do nothing; otherwise this will be
817 * updated with any new status from the function.
820 virtual void setDefaultContext(UDateFormatContextType type
, UDateFormatContextValue value
,
823 /* Cannot use #ifndef U_HIDE_DRAFT_API for the following draft method since it is virtual */
825 * Get the formatter's default value for a particular context type,
826 * such as UDAT_CAPITALIZATION.
827 * @param type The context type for which the default value should be obtained.
828 * @param status Input/output status. If at entry this indicates a failure
829 * status, the function will do nothing; otherwise this will be
830 * updated with any new status from the function.
831 * @return The current default value for the specified context type.
834 virtual int32_t getDefaultContext(UDateFormatContextType type
, UErrorCode
& status
) const;
836 #ifndef U_HIDE_INTERNAL_API
838 * Sets the TimeZoneFormat to be used by this date/time formatter.
839 * The caller should not delete the TimeZoneFormat object after
840 * it is adopted by this call.
841 * @param timeZoneFormatToAdopt The TimeZoneFormat object to be adopted.
842 * @internal ICU 49 technology preview
844 virtual void adoptTimeZoneFormat(TimeZoneFormat
* timeZoneFormatToAdopt
);
847 * Sets the TimeZoneFormat to be used by this date/time formatter.
848 * @param newTimeZoneFormat The TimeZoneFormat object to copy.
849 * @internal ICU 49 technology preview
851 virtual void setTimeZoneFormat(const TimeZoneFormat
& newTimeZoneFormat
);
854 * Gets the time zone format object associated with this date/time formatter.
855 * @return the time zone format associated with this date/time formatter.
856 * @internal ICU 49 technology preview
858 virtual const TimeZoneFormat
* getTimeZoneFormat(void) const;
859 #endif /* U_HIDE_INTERNAL_API */
861 #ifndef U_HIDE_INTERNAL_API
863 * This is for ICU internal use only. Please do not use.
864 * Check whether the 'field' is smaller than all the fields covered in
865 * pattern, return TRUE if it is. The sequence of calendar field,
866 * from large to small is: ERA, YEAR, MONTH, DATE, AM_PM, HOUR, MINUTE,...
867 * @param field the calendar field need to check against
868 * @return TRUE if the 'field' is smaller than all the fields
869 * covered in pattern. FALSE otherwise.
872 UBool
isFieldUnitIgnored(UCalendarDateFields field
) const;
876 * This is for ICU internal use only. Please do not use.
877 * Check whether the 'field' is smaller than all the fields covered in
878 * pattern, return TRUE if it is. The sequence of calendar field,
879 * from large to small is: ERA, YEAR, MONTH, DATE, AM_PM, HOUR, MINUTE,...
880 * @param pattern the pattern to check against
881 * @param field the calendar field need to check against
882 * @return TRUE if the 'field' is smaller than all the fields
883 * covered in pattern. FALSE otherwise.
886 static UBool
isFieldUnitIgnored(const UnicodeString
& pattern
,
887 UCalendarDateFields field
);
890 * This is for ICU internal use only. Please do not use.
891 * Get the locale of this simple date formatter.
892 * It is used in DateIntervalFormat.
894 * @return locale in this simple date formatter
897 const Locale
& getSmpFmtLocale(void) const;
898 #endif /* U_HIDE_INTERNAL_API */
901 friend class DateFormat
;
903 void initializeDefaultCentury(void);
905 SimpleDateFormat(); // default constructor not implemented
908 * Used by the DateFormat factory methods to construct a SimpleDateFormat.
909 * @param timeStyle the time style.
910 * @param dateStyle the date style.
911 * @param locale the given locale.
912 * @param status Output param set to success/failure code on
915 SimpleDateFormat(EStyle timeStyle
, EStyle dateStyle
, const Locale
& locale
, UErrorCode
& status
);
918 * Construct a SimpleDateFormat for the given locale. If no resource data
919 * is available, create an object of last resort, using hard-coded strings.
920 * This is an internal method, called by DateFormat. It should never fail.
921 * @param locale the given locale.
922 * @param status Output param set to success/failure code on
925 SimpleDateFormat(const Locale
& locale
, UErrorCode
& status
); // Use default pattern
928 * Hook called by format(... FieldPosition& ...) and format(...FieldPositionIterator&...)
930 UnicodeString
& _format(Calendar
& cal
, UDateFormatContextValue capitalizationContext
,
931 UnicodeString
& appendTo
, FieldPositionHandler
& handler
, UErrorCode
& status
) const;
934 * Called by format() to format a single field.
936 * @param appendTo Output parameter to receive result.
937 * Result is appended to existing contents.
938 * @param ch The format character we encountered in the pattern.
939 * @param count Number of characters in the current pattern symbol (e.g.,
940 * "yyyy" in the pattern would result in a call to this function
941 * with ch equal to 'y' and count equal to 4)
942 * @param capitalizationContext Capitalization context for this date format.
943 * @param fieldNum Zero-based numbering of current field within the overall format.
944 * @param handler Records information about field positions.
945 * @param cal Calendar to use
946 * @param status Receives a status code, which will be U_ZERO_ERROR if the operation
949 void subFormat(UnicodeString
&appendTo
,
952 UDateFormatContextValue capitalizationContext
,
954 FieldPositionHandler
& handler
,
956 UErrorCode
& status
) const; // in case of illegal argument
959 * Used by subFormat() to format a numeric value.
960 * Appends to toAppendTo a string representation of "value"
961 * having a number of digits between "minDigits" and
962 * "maxDigits". Uses the DateFormat's NumberFormat.
964 * @param currentNumberFormat
965 * @param appendTo Output parameter to receive result.
966 * Formatted number is appended to existing contents.
967 * @param value Value to format.
968 * @param minDigits Minimum number of digits the result should have
969 * @param maxDigits Maximum number of digits the result should have
971 void zeroPaddingNumber(NumberFormat
*currentNumberFormat
,
972 UnicodeString
&appendTo
,
975 int32_t maxDigits
) const;
978 * Return true if the given format character, occuring count
979 * times, represents a numeric field.
981 static UBool
isNumeric(UChar formatChar
, int32_t count
);
984 * initializes fCalendar from parameters. Returns fCalendar as a convenience.
985 * @param adoptZone Zone to be adopted, or NULL for TimeZone::createDefault().
986 * @param locale Locale of the calendar
987 * @param status Error code
988 * @return the newly constructed fCalendar
990 Calendar
*initializeCalendar(TimeZone
* adoptZone
, const Locale
& locale
, UErrorCode
& status
);
993 * initializes fSymbols from parameters.
994 * @param locale Locale of the symbols
995 * @param calendar Alias to Calendar that will be used.
996 * @param status Error code
998 void initializeSymbols(const Locale
& locale
, Calendar
* calendar
, UErrorCode
& status
);
1001 * Called by several of the constructors to load pattern data and formatting symbols
1002 * out of a resource bundle and initialize the locale based on it.
1003 * @param timeStyle The time style, as passed to DateFormat::createDateInstance().
1004 * @param dateStyle The date style, as passed to DateFormat::createTimeInstance().
1005 * @param locale The locale to load the patterns from.
1006 * @param status Filled in with an error code if loading the data from the
1009 void construct(EStyle timeStyle
, EStyle dateStyle
, const Locale
& locale
, UErrorCode
& status
);
1012 * Called by construct() and the various constructors to set up the SimpleDateFormat's
1013 * Calendar and NumberFormat objects.
1014 * @param locale The locale for which we want a Calendar and a NumberFormat.
1015 * @param status Filled in with an error code if creating either subobject fails.
1017 void initialize(const Locale
& locale
, UErrorCode
& status
);
1020 * Private code-size reduction function used by subParse.
1021 * @param text the time text being parsed.
1022 * @param start where to start parsing.
1023 * @param field the date field being parsed.
1024 * @param stringArray the string array to parsed.
1025 * @param stringArrayCount the size of the array.
1026 * @param monthPattern pointer to leap month pattern, or NULL if none.
1027 * @param cal a Calendar set to the date and time to be formatted
1028 * into a date/time string.
1029 * @return the new start position if matching succeeded; a negative number
1030 * indicating matching failure, otherwise.
1032 int32_t matchString(const UnicodeString
& text
, int32_t start
, UCalendarDateFields field
,
1033 const UnicodeString
* stringArray
, int32_t stringArrayCount
,
1034 const UnicodeString
* monthPattern
, Calendar
& cal
) const;
1037 * Private code-size reduction function used by subParse.
1038 * @param text the time text being parsed.
1039 * @param start where to start parsing.
1040 * @param field the date field being parsed.
1041 * @param stringArray the string array to parsed.
1042 * @param stringArrayCount the size of the array.
1043 * @param cal a Calendar set to the date and time to be formatted
1044 * into a date/time string.
1045 * @return the new start position if matching succeeded; a negative number
1046 * indicating matching failure, otherwise.
1048 int32_t matchQuarterString(const UnicodeString
& text
, int32_t start
, UCalendarDateFields field
,
1049 const UnicodeString
* stringArray
, int32_t stringArrayCount
, Calendar
& cal
) const;
1052 * Private function used by subParse to match literal pattern text.
1054 * @param pattern the pattern string
1055 * @param patternOffset the starting offset into the pattern text. On
1056 * outupt will be set the offset of the first non-literal character in the pattern
1057 * @param text the text being parsed
1058 * @param textOffset the starting offset into the text. On output
1059 * will be set to the offset of the character after the match
1060 * @param lenient <code>TRUE</code> if the parse is lenient, <code>FALSE</code> otherwise.
1062 * @return <code>TRUE</code> if the literal text could be matched, <code>FALSE</code> otherwise.
1064 static UBool
matchLiterals(const UnicodeString
&pattern
, int32_t &patternOffset
,
1065 const UnicodeString
&text
, int32_t &textOffset
, UBool lenient
);
1068 * Private member function that converts the parsed date strings into
1069 * timeFields. Returns -start (for ParsePosition) if failed.
1070 * @param text the time text to be parsed.
1071 * @param start where to start parsing.
1072 * @param ch the pattern character for the date field text to be parsed.
1073 * @param count the count of a pattern character.
1074 * @param obeyCount if true then the count is strictly obeyed.
1075 * @param allowNegative
1076 * @param ambiguousYear If true then the two-digit year == the default start year.
1077 * @param saveHebrewMonth Used to hang onto month until year is known.
1078 * @param cal a Calendar set to the date and time to be formatted
1079 * into a date/time string.
1081 * @param numericLeapMonthFormatter If non-null, used to parse numeric leap months.
1082 * @return the new start position if matching succeeded; a negative number
1083 * indicating matching failure, otherwise.
1085 int32_t subParse(const UnicodeString
& text
, int32_t& start
, UChar ch
, int32_t count
,
1086 UBool obeyCount
, UBool allowNegative
, UBool ambiguousYear
[], int32_t& saveHebrewMonth
, Calendar
& cal
,
1087 int32_t patLoc
, MessageFormat
* numericLeapMonthFormatter
) const;
1089 void parseInt(const UnicodeString
& text
,
1090 Formattable
& number
,
1092 UBool allowNegative
,
1093 NumberFormat
*fmt
) const;
1095 void parseInt(const UnicodeString
& text
,
1096 Formattable
& number
,
1099 UBool allowNegative
,
1100 NumberFormat
*fmt
) const;
1102 int32_t checkIntSuffix(const UnicodeString
& text
, int32_t start
,
1103 int32_t patLoc
, UBool isNegative
) const;
1106 * Translate a pattern, mapping each character in the from string to the
1107 * corresponding character in the to string. Return an error if the original
1108 * pattern contains an unmapped character, or if a quote is unmatched.
1109 * Quoted (single quotes only) material is not translated.
1110 * @param originalPattern the original pattern.
1111 * @param translatedPattern Output param to receive the translited pattern.
1112 * @param from the characters to be translited from.
1113 * @param to the characters to be translited to.
1114 * @param status Receives a status code, which will be U_ZERO_ERROR
1115 * if the operation succeeds.
1117 static void translatePattern(const UnicodeString
& originalPattern
,
1118 UnicodeString
& translatedPattern
,
1119 const UnicodeString
& from
,
1120 const UnicodeString
& to
,
1121 UErrorCode
& status
);
1124 * Sets the starting date of the 100-year window that dates with 2-digit years
1125 * are considered to fall within.
1126 * @param startDate the start date
1127 * @param status Receives a status code, which will be U_ZERO_ERROR
1128 * if the operation succeeds.
1130 void parseAmbiguousDatesAsAfter(UDate startDate
, UErrorCode
& status
);
1133 * Return the length matched by the given affix, or -1 if none.
1134 * Runs of white space in the affix, match runs of white space in
1136 * @param affix pattern string, taken as a literal
1137 * @param input input text
1138 * @param pos offset into input at which to begin matching
1139 * @return length of input that matches, or -1 if match failure
1141 int32_t compareSimpleAffix(const UnicodeString
& affix
,
1142 const UnicodeString
& input
,
1146 * Skip over a run of zero or more Pattern_White_Space characters at
1149 int32_t skipPatternWhiteSpace(const UnicodeString
& text
, int32_t pos
) const;
1152 * Skip over a run of zero or more isUWhiteSpace() characters at pos
1155 int32_t skipUWhiteSpace(const UnicodeString
& text
, int32_t pos
) const;
1158 * Initialize NumberFormat instances used for numbering system overrides.
1160 void initNumberFormatters(const Locale
&locale
,UErrorCode
&status
);
1163 * Get the numbering system to be used for a particular field.
1165 NumberFormat
* getNumberFormatByIndex(UDateFormatField index
) const;
1168 * Parse the given override string and set up structures for number formats
1170 void processOverrideString(const Locale
&locale
, const UnicodeString
&str
, int8_t type
, UErrorCode
&status
);
1173 * Used to map pattern characters to Calendar field identifiers.
1175 static const UCalendarDateFields fgPatternIndexToCalendarField
[];
1178 * Map index into pattern character string to DateFormat field number
1180 static const UDateFormatField fgPatternIndexToDateFormatField
[];
1183 * Lazy TimeZoneFormat instantiation, semantically const
1185 TimeZoneFormat
*tzFormat() const;
1188 * Used to map Calendar field to field level.
1189 * The larger the level, the smaller the field unit.
1190 * For example, UCAL_ERA level is 0, UCAL_YEAR level is 10,
1191 * UCAL_MONTH level is 20.
1193 static const int32_t fgCalendarFieldToLevel
[];
1194 static const int32_t fgPatternCharToLevel
[];
1197 * The formatting pattern for this formatter.
1199 UnicodeString fPattern
;
1202 * The numbering system override for dates.
1204 UnicodeString fDateOverride
;
1207 * The numbering system override for times.
1209 UnicodeString fTimeOverride
;
1213 * The original locale used (for reloading symbols)
1218 * A pointer to an object containing the strings to use in formatting (e.g.,
1219 * month and day names, AM and PM strings, time zone names, etc.)
1221 DateFormatSymbols
* fSymbols
; // Owned
1224 * The time zone formatter
1226 TimeZoneFormat
* fTimeZoneFormat
;
1229 * If dates have ambiguous years, we map them into the century starting
1230 * at defaultCenturyStart, which may be any date. If defaultCenturyStart is
1231 * set to SYSTEM_DEFAULT_CENTURY, which it is by default, then the system
1232 * values are used. The instance values defaultCenturyStart and
1233 * defaultCenturyStartYear are only used if explicitly set by the user
1234 * through the API method parseAmbiguousDatesAsAfter().
1236 UDate fDefaultCenturyStart
;
1239 * See documentation for defaultCenturyStart.
1241 /*transient*/ int32_t fDefaultCenturyStartYear
;
1243 int32_t tztype
; // here to avoid api change
1245 typedef struct NSOverride
{
1251 NumberFormat
**fNumberFormatters
;
1253 NSOverride
*fOverrideList
;
1255 UBool fHaveDefaultCentury
;
1257 UDateFormatContextValue fDefaultCapitalizationContext
;
1261 SimpleDateFormat::get2DigitYearStart(UErrorCode
& /*status*/) const
1263 return fDefaultCenturyStart
;
1266 inline UnicodeString
&
1267 SimpleDateFormat::format(const Formattable
& obj
,
1268 UnicodeString
& appendTo
,
1269 UErrorCode
& status
) const {
1270 // Don't use Format:: - use immediate base class only,
1271 // in case immediate base modifies behavior later.
1272 return DateFormat::format(obj
, appendTo
, status
);
1275 inline UnicodeString
&
1276 SimpleDateFormat::format(const Formattable
& obj
,
1277 UnicodeString
& appendTo
,
1279 UErrorCode
& status
) const
1281 // Don't use Format:: - use immediate base class only,
1282 // in case immediate base modifies behavior later.
1283 return DateFormat::format(obj
, appendTo
, pos
, status
);
1286 inline UnicodeString
&
1287 SimpleDateFormat::format(const Formattable
& obj
,
1288 UnicodeString
& appendTo
,
1289 FieldPositionIterator
* posIter
,
1290 UErrorCode
& status
) const
1292 // Don't use Format:: - use immediate base class only,
1293 // in case immediate base modifies behavior later.
1294 return DateFormat::format(obj
, appendTo
, posIter
, status
);
1297 inline UnicodeString
&
1298 SimpleDateFormat::format(UDate date
,
1299 UnicodeString
& appendTo
,
1300 FieldPosition
& fieldPosition
) const {
1301 // Don't use Format:: - use immediate base class only,
1302 // in case immediate base modifies behavior later.
1303 return DateFormat::format(date
, appendTo
, fieldPosition
);
1306 inline UnicodeString
&
1307 SimpleDateFormat::format(UDate date
,
1308 UnicodeString
& appendTo
,
1309 FieldPositionIterator
* posIter
,
1310 UErrorCode
& status
) const {
1311 // Don't use Format:: - use immediate base class only,
1312 // in case immediate base modifies behavior later.
1313 return DateFormat::format(date
, appendTo
, posIter
, status
);
1316 inline UnicodeString
&
1317 SimpleDateFormat::format(UDate date
, UnicodeString
& appendTo
) const {
1318 return DateFormat::format(date
, appendTo
);
1323 #endif /* #if !UCONFIG_NO_FORMATTING */