2 * Copyright (C) 1997-2009, International Business Machines Corporation and others. All Rights Reserved.
3 *******************************************************************************
7 * Modification History:
9 * Date Name Description
10 * 02/19/97 aliu Converted from java.
11 * 07/09/97 helena Make ParsePosition into a class.
12 * 07/21/98 stephen Added GMT_PLUS, GMT_MINUS
13 * Changed setTwoDigitStartDate to set2DigitYearStart
14 * Changed getTwoDigitStartDate to get2DigitYearStart
15 * Removed subParseLong
16 * Removed getZoneIndex (added in DateFormatSymbols)
17 * 06/14/99 stephen Removed fgTimeZoneDataSuffix
18 * 10/14/99 aliu Updated class doc to describe 2-digit year parsing
20 *******************************************************************************
26 #include "unicode/utypes.h"
30 * \brief C++ API: Format and parse dates in a language-independent manner.
33 #if !UCONFIG_NO_FORMATTING
35 #include "unicode/datefmt.h"
39 class DateFormatSymbols
;
45 * SimpleDateFormat is a concrete class for formatting and parsing dates in a
46 * language-independent manner. It allows for formatting (millis -> text),
47 * parsing (text -> millis), and normalization. Formats/Parses a date or time,
48 * which is the standard milliseconds since 24:00 GMT, Jan 1, 1970.
50 * Clients are encouraged to create a date-time formatter using DateFormat::getInstance(),
51 * getDateInstance(), getDateInstance(), or getDateTimeInstance() rather than
52 * explicitly constructing an instance of SimpleDateFormat. This way, the client
53 * is guaranteed to get an appropriate formatting pattern for whatever locale the
54 * program is running in. However, if the client needs something more unusual than
55 * the default patterns in the locales, he can construct a SimpleDateFormat directly
56 * and give it an appropriate pattern (or use one of the factory methods on DateFormat
57 * and modify the pattern after the fact with toPattern() and applyPattern().
59 * Date/Time format syntax:
61 * The date/time format is specified by means of a string time pattern. In this
62 * pattern, all ASCII letters are reserved as pattern letters, which are defined
66 * Symbol Meaning Presentation Example
67 * ------ ------- ------------ -------
68 * G era designator (Text) AD
69 * y year (Number) 1996
70 * Y year (week of year) (Number) 1997
71 * u extended year (Number) 4601
72 * Q Quarter (Text & Number) Q2 & 02
73 * M month in year (Text & Number) July & 07
74 * d day in month (Number) 10
75 * h hour in am/pm (1~12) (Number) 12
76 * H hour in day (0~23) (Number) 0
77 * m minute in hour (Number) 30
78 * s second in minute (Number) 55
79 * S fractional second (Number) 978
80 * E day of week (Text) Tuesday
81 * e day of week (local 1~7) (Text & Number) Tues & 2
82 * D day in year (Number) 189
83 * F day of week in month (Number) 2 (2nd Wed in July)
84 * w week in year (Number) 27
85 * W week in month (Number) 2
86 * a am/pm marker (Text) PM
87 * k hour in day (1~24) (Number) 24
88 * K hour in am/pm (0~11) (Number) 0
89 * z time zone (Time) Pacific Standard Time
90 * Z time zone (RFC 822) (Number) -0800
91 * v time zone (generic) (Text) Pacific Time
92 * V time zone (abreviation) (Text) PT
93 * VVVV time zone (location) (Text) United States (Los Angeles)
94 * g Julian day (Number) 2451334
95 * A milliseconds in day (Number) 69540000
96 * q stand alone quarter (Text & Number) Q2 & 02
97 * L stand alone month (Text & Number) July & 07
98 * c stand alone day of week (Text & Number) Tuesday & 2
99 * ' escape for text (Delimiter) 'Date='
100 * '' single quote (Literal) 'o''clock'
103 * The count of pattern letters determine the format.
105 * (Text): 4 or more, use full form, <4, use short or abbreviated form if it
106 * exists. (e.g., "EEEE" produces "Monday", "EEE" produces "Mon")
108 * (Number): the minimum number of digits. Shorter numbers are zero-padded to
109 * this amount (e.g. if "m" produces "6", "mm" produces "06"). Year is handled
110 * specially; that is, if the count of 'y' is 2, the Year will be truncated to 2 digits.
111 * (e.g., if "yyyy" produces "1997", "yy" produces "97".)
112 * Unlike other fields, fractional seconds are padded on the right with zero.
114 * (Text & Number): 3 or over, use text, otherwise use number. (e.g., "M" produces "1",
115 * "MM" produces "01", "MMM" produces "Jan", and "MMMM" produces "January".)
117 * Any characters in the pattern that are not in the ranges of ['a'..'z'] and
118 * ['A'..'Z'] will be treated as quoted text. For instance, characters
119 * like ':', '.', ' ', '#' and '@' will appear in the resulting time text
120 * even they are not embraced within single quotes.
122 * A pattern containing any invalid pattern letter will result in a failing
123 * UErrorCode result during formatting or parsing.
125 * Examples using the US locale:
128 * Format Pattern Result
129 * -------------- -------
130 * "yyyy.MM.dd G 'at' HH:mm:ss vvvv" ->> 1996.07.10 AD at 15:08:56 Pacific Time
131 * "EEE, MMM d, ''yy" ->> Wed, July 10, '96
132 * "h:mm a" ->> 12:08 PM
133 * "hh 'o''clock' a, zzzz" ->> 12 o'clock PM, Pacific Daylight Time
134 * "K:mm a, vvv" ->> 0:00 PM, PT
135 * "yyyyy.MMMMM.dd GGG hh:mm aaa" ->> 1996.July.10 AD 12:08 PM
141 * UErrorCode success = U_ZERO_ERROR;
142 * SimpleTimeZone* pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, "PST");
143 * pdt->setStartRule( Calendar::APRIL, 1, Calendar::SUNDAY, 2*60*60*1000);
144 * pdt->setEndRule( Calendar::OCTOBER, -1, Calendar::SUNDAY, 2*60*60*1000);
146 * // Format the current time.
147 * SimpleDateFormat* formatter
148 * = new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss a zzz", success );
149 * GregorianCalendar cal(success);
150 * UDate currentTime_1 = cal.getTime(success);
151 * FieldPosition fp(0);
152 * UnicodeString dateString;
153 * formatter->format( currentTime_1, dateString, fp );
154 * cout << "result: " << dateString << endl;
156 * // Parse the previous string back into a Date.
157 * ParsePosition pp(0);
158 * UDate currentTime_2 = formatter->parse(dateString, pp );
161 * In the above example, the time value "currentTime_2" obtained from parsing
162 * will be equal to currentTime_1. However, they may not be equal if the am/pm
163 * marker 'a' is left out from the format pattern while the "hour in am/pm"
164 * pattern symbol is used. This information loss can happen when formatting the
168 * When parsing a date string using the abbreviated year pattern ("y" or "yy"),
169 * SimpleDateFormat must interpret the abbreviated year
170 * relative to some century. It does this by adjusting dates to be
171 * within 80 years before and 20 years after the time the SimpleDateFormat
172 * instance is created. For example, using a pattern of "MM/dd/yy" and a
173 * SimpleDateFormat instance created on Jan 1, 1997, the string
174 * "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64"
175 * would be interpreted as May 4, 1964.
176 * During parsing, only strings consisting of exactly two digits, as defined by
177 * <code>Unicode::isDigit()</code>, will be parsed into the default century.
178 * Any other numeric string, such as a one digit string, a three or more digit
179 * string, or a two digit string that isn't all digits (for example, "-1"), is
180 * interpreted literally. So "01/02/3" or "01/02/003" are parsed, using the
181 * same pattern, as Jan 2, 3 AD. Likewise, "01/02/-3" is parsed as Jan 2, 4 BC.
184 * If the year pattern has more than two 'y' characters, the year is
185 * interpreted literally, regardless of the number of digits. So using the
186 * pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D.
189 * When numeric fields abut one another directly, with no intervening delimiter
190 * characters, they constitute a run of abutting numeric fields. Such runs are
191 * parsed specially. For example, the format "HHmmss" parses the input text
192 * "123456" to 12:34:56, parses the input text "12345" to 1:23:45, and fails to
193 * parse "1234". In other words, the leftmost field of the run is flexible,
194 * while the others keep a fixed width. If the parse fails anywhere in the run,
195 * then the leftmost field is shortened by one character, and the entire run is
196 * parsed again. This is repeated until either the parse succeeds or the
197 * leftmost field is one character in length. If the parse still fails at that
198 * point, the parse of the run fails.
201 * For time zones that have no names, SimpleDateFormat uses strings GMT+hours:minutes or
204 * The calendar defines what is the first day of the week, the first week of the
205 * year, whether hours are zero based or not (0 vs 12 or 24), and the timezone.
206 * There is one common number format to handle all the numbers; the digit count
207 * is handled programmatically according to the pattern.
209 * <p><em>User subclasses are not supported.</em> While clients may write
210 * subclasses, such code will not necessarily work and will not be
211 * guaranteed to work stably from release to release.
213 class U_I18N_API SimpleDateFormat
: public DateFormat
{
216 * Construct a SimpleDateFormat using the default pattern for the default
219 * [Note:] Not all locales support SimpleDateFormat; for full generality,
220 * use the factory methods in the DateFormat class.
221 * @param status Output param set to success/failure code.
224 SimpleDateFormat(UErrorCode
& status
);
227 * Construct a SimpleDateFormat using the given pattern and the default locale.
228 * The locale is used to obtain the symbols used in formatting (e.g., the
229 * names of the months), but not to provide the pattern.
231 * [Note:] Not all locales support SimpleDateFormat; for full generality,
232 * use the factory methods in the DateFormat class.
233 * @param pattern the pattern for the format.
234 * @param status Output param set to success/failure code.
237 SimpleDateFormat(const UnicodeString
& pattern
,
241 * Construct a SimpleDateFormat using the given pattern and locale.
242 * The locale is used to obtain the symbols used in formatting (e.g., the
243 * names of the months), but not to provide the pattern.
245 * [Note:] Not all locales support SimpleDateFormat; for full generality,
246 * use the factory methods in the DateFormat class.
247 * @param pattern the pattern for the format.
248 * @param locale the given locale.
249 * @param status Output param set to success/failure code.
252 SimpleDateFormat(const UnicodeString
& pattern
,
253 const Locale
& locale
,
257 * Construct a SimpleDateFormat using the given pattern and locale-specific
258 * symbol data. The formatter takes ownership of the DateFormatSymbols object;
259 * the caller is no longer responsible for deleting it.
260 * @param pattern the given pattern for the format.
261 * @param formatDataToAdopt the symbols to be adopted.
262 * @param status Output param set to success/faulure code.
265 SimpleDateFormat(const UnicodeString
& pattern
,
266 DateFormatSymbols
* formatDataToAdopt
,
270 * Construct a SimpleDateFormat using the given pattern and locale-specific
271 * symbol data. The DateFormatSymbols object is NOT adopted; the caller
272 * remains responsible for deleting it.
273 * @param pattern the given pattern for the format.
274 * @param formatData the formatting symbols to be use.
275 * @param status Output param set to success/faulure code.
278 SimpleDateFormat(const UnicodeString
& pattern
,
279 const DateFormatSymbols
& formatData
,
286 SimpleDateFormat(const SimpleDateFormat
&);
289 * Assignment operator.
292 SimpleDateFormat
& operator=(const SimpleDateFormat
&);
298 virtual ~SimpleDateFormat();
301 * Clone this Format object polymorphically. The caller owns the result and
302 * should delete it when done.
303 * @return A copy of the object.
306 virtual Format
* clone(void) const;
309 * Return true if the given Format objects are semantically equal. Objects
310 * of different subclasses are considered unequal.
311 * @param other the object to be compared with.
312 * @return true if the given Format objects are semantically equal.
315 virtual UBool
operator==(const Format
& other
) const;
318 * Format a date or time, which is the standard millis since 24:00 GMT, Jan
319 * 1, 1970. Overrides DateFormat pure virtual method.
321 * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->>
322 * 1996.07.10 AD at 15:08:56 PDT
324 * @param cal Calendar set to the date and time to be formatted
325 * into a date/time string.
326 * @param appendTo Output parameter to receive result.
327 * Result is appended to existing contents.
328 * @param pos The formatting position. On input: an alignment field,
329 * if desired. On output: the offsets of the alignment field.
330 * @return Reference to 'appendTo' parameter.
333 virtual UnicodeString
& format( Calendar
& cal
,
334 UnicodeString
& appendTo
,
335 FieldPosition
& pos
) const;
338 * Format a date or time, which is the standard millis since 24:00 GMT, Jan
339 * 1, 1970. Overrides DateFormat pure virtual method.
341 * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->>
342 * 1996.07.10 AD at 15:08:56 PDT
344 * @param obj A Formattable containing the date-time value to be formatted
345 * into a date-time string. If the type of the Formattable
346 * is a numeric type, it is treated as if it were an
348 * @param appendTo Output parameter to receive result.
349 * Result is appended to existing contents.
350 * @param pos The formatting position. On input: an alignment field,
351 * if desired. On output: the offsets of the alignment field.
352 * @param status Output param set to success/faulure code.
353 * @return Reference to 'appendTo' parameter.
356 virtual UnicodeString
& format( const Formattable
& obj
,
357 UnicodeString
& appendTo
,
359 UErrorCode
& status
) const;
362 * Redeclared DateFormat method.
363 * @param date the Date value to be formatted.
364 * @param appendTo Output parameter to receive result.
365 * Result is appended to existing contents.
366 * @param fieldPosition The formatting position. On input: an alignment field,
367 * if desired. On output: the offsets of the alignment field.
368 * @return Reference to 'appendTo' parameter.
371 UnicodeString
& format(UDate date
,
372 UnicodeString
& appendTo
,
373 FieldPosition
& fieldPosition
) const;
376 * Redeclared DateFormat method.
377 * @param obj Object to be formatted.
378 * @param appendTo Output parameter to receive result.
379 * Result is appended to existing contents.
380 * @param status Input/output success/failure code.
381 * @return Reference to 'appendTo' parameter.
384 UnicodeString
& format(const Formattable
& obj
,
385 UnicodeString
& appendTo
,
386 UErrorCode
& status
) const;
389 * Redeclared DateFormat method.
390 * @param date Date value to be formatted.
391 * @param appendTo Output parameter to receive result.
392 * Result is appended to existing contents.
393 * @return Reference to 'appendTo' parameter.
396 UnicodeString
& format(UDate date
, UnicodeString
& appendTo
) const;
399 * Parse a date/time string beginning at the given parse position. For
400 * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
401 * that is equivalent to Date(837039928046).
403 * By default, parsing is lenient: If the input is not in the form used by
404 * this object's format method but can still be parsed as a date, then the
405 * parse succeeds. Clients may insist on strict adherence to the format by
406 * calling setLenient(false).
408 * @param text The date/time string to be parsed
409 * @param cal a Calendar set to the date and time to be formatted
410 * into a date/time string.
411 * @param pos On input, the position at which to start parsing; on
412 * output, the position at which parsing terminated, or the
413 * start position if the parse failed.
414 * @return A valid UDate if the input could be parsed.
417 virtual void parse( const UnicodeString
& text
,
419 ParsePosition
& pos
) const;
422 * Parse a date/time string starting at the given parse position. For
423 * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
424 * that is equivalent to Date(837039928046).
426 * By default, parsing is lenient: If the input is not in the form used by
427 * this object's format method but can still be parsed as a date, then the
428 * parse succeeds. Clients may insist on strict adherence to the format by
429 * calling setLenient(false).
431 * @see DateFormat::setLenient(boolean)
433 * @param text The date/time string to be parsed
434 * @param pos On input, the position at which to start parsing; on
435 * output, the position at which parsing terminated, or the
436 * start position if the parse failed.
437 * @return A valid UDate if the input could be parsed.
440 UDate
parse( const UnicodeString
& text
,
441 ParsePosition
& pos
) const;
445 * Parse a date/time string. For example, a time text "07/10/96 4:5 PM, PDT"
446 * will be parsed into a UDate that is equivalent to Date(837039928046).
447 * Parsing begins at the beginning of the string and proceeds as far as
448 * possible. Assuming no parse errors were encountered, this function
449 * doesn't return any information about how much of the string was consumed
450 * by the parsing. If you need that information, use the version of
451 * parse() that takes a ParsePosition.
453 * @param text The date/time string to be parsed
454 * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with
455 * an error value if there was a parse error.
456 * @return A valid UDate if the input could be parsed.
459 virtual UDate
parse( const UnicodeString
& text
,
460 UErrorCode
& status
) const;
463 * Set the start UDate used to interpret two-digit year strings.
464 * When dates are parsed having 2-digit year strings, they are placed within
465 * a assumed range of 100 years starting on the two digit start date. For
466 * example, the string "24-Jan-17" may be in the year 1817, 1917, 2017, or
467 * some other year. SimpleDateFormat chooses a year so that the resultant
468 * date is on or after the two digit start date and within 100 years of the
469 * two digit start date.
471 * By default, the two digit start date is set to 80 years before the current
472 * time at which a SimpleDateFormat object is created.
473 * @param d start UDate used to interpret two-digit year strings.
474 * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with
475 * an error value if there was a parse error.
478 virtual void set2DigitYearStart(UDate d
, UErrorCode
& status
);
481 * Get the start UDate used to interpret two-digit year strings.
482 * When dates are parsed having 2-digit year strings, they are placed within
483 * a assumed range of 100 years starting on the two digit start date. For
484 * example, the string "24-Jan-17" may be in the year 1817, 1917, 2017, or
485 * some other year. SimpleDateFormat chooses a year so that the resultant
486 * date is on or after the two digit start date and within 100 years of the
487 * two digit start date.
489 * By default, the two digit start date is set to 80 years before the current
490 * time at which a SimpleDateFormat object is created.
491 * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with
492 * an error value if there was a parse error.
495 UDate
get2DigitYearStart(UErrorCode
& status
) const;
498 * Return a pattern string describing this date format.
499 * @param result Output param to receive the pattern.
500 * @return A reference to 'result'.
503 virtual UnicodeString
& toPattern(UnicodeString
& result
) const;
506 * Return a localized pattern string describing this date format.
507 * In most cases, this will return the same thing as toPattern(),
508 * but a locale can specify characters to use in pattern descriptions
509 * in place of the ones described in this class's class documentation.
510 * (Presumably, letters that would be more mnemonic in that locale's
511 * language.) This function would produce a pattern using those
514 * @param result Receives the localized pattern.
515 * @param status Output param set to success/failure code on
516 * exit. If the pattern is invalid, this will be
517 * set to a failure result.
518 * @return A reference to 'result'.
521 virtual UnicodeString
& toLocalizedPattern(UnicodeString
& result
,
522 UErrorCode
& status
) const;
525 * Apply the given unlocalized pattern string to this date format.
526 * (i.e., after this call, this formatter will format dates according to
529 * @param pattern The pattern to be applied.
532 virtual void applyPattern(const UnicodeString
& pattern
);
535 * Apply the given localized pattern string to this date format.
536 * (see toLocalizedPattern() for more information on localized patterns.)
538 * @param pattern The localized pattern to be applied.
539 * @param status Output param set to success/failure code on
540 * exit. If the pattern is invalid, this will be
541 * set to a failure result.
544 virtual void applyLocalizedPattern(const UnicodeString
& pattern
,
548 * Gets the date/time formatting symbols (this is an object carrying
549 * the various strings and other symbols used in formatting: e.g., month
550 * names and abbreviations, time zone names, AM/PM strings, etc.)
551 * @return a copy of the date-time formatting data associated
552 * with this date-time formatter.
555 virtual const DateFormatSymbols
* getDateFormatSymbols(void) const;
558 * Set the date/time formatting symbols. The caller no longer owns the
559 * DateFormatSymbols object and should not delete it after making this call.
560 * @param newFormatSymbols the given date-time formatting symbols to copy.
563 virtual void adoptDateFormatSymbols(DateFormatSymbols
* newFormatSymbols
);
566 * Set the date/time formatting data.
567 * @param newFormatSymbols the given date-time formatting symbols to copy.
570 virtual void setDateFormatSymbols(const DateFormatSymbols
& newFormatSymbols
);
573 * Return the class ID for this class. This is useful only for comparing to
574 * a return value from getDynamicClassID(). For example:
576 * . Base* polymorphic_pointer = createPolymorphicObject();
577 * . if (polymorphic_pointer->getDynamicClassID() ==
578 * . erived::getStaticClassID()) ...
580 * @return The class ID for all objects of this class.
583 static UClassID U_EXPORT2
getStaticClassID(void);
586 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
587 * method is to implement a simple version of RTTI, since not all C++
588 * compilers support genuine RTTI. Polymorphic operator==() and clone()
589 * methods call this method.
591 * @return The class ID for this object. All objects of a
592 * given class have the same class ID. Objects of
593 * other classes have different class IDs.
596 virtual UClassID
getDynamicClassID(void) const;
599 * Set the calendar to be used by this date format. Initially, the default
600 * calendar for the specified or default locale is used. The caller should
601 * not delete the Calendar object after it is adopted by this call.
602 * Adopting a new calendar will change to the default symbols.
604 * @param calendarToAdopt Calendar object to be adopted.
607 virtual void adoptCalendar(Calendar
* calendarToAdopt
);
610 * This is for ICU internal use only. Please do not use.
611 * Check whether the 'field' is smaller than all the fields covered in
612 * pattern, return TRUE if it is. The sequence of calendar field,
613 * from large to small is: ERA, YEAR, MONTH, DATE, AM_PM, HOUR, MINUTE,...
614 * @param field the calendar field need to check against
615 * @return TRUE if the 'field' is smaller than all the fields
616 * covered in pattern. FALSE otherwise.
619 UBool
isFieldUnitIgnored(UCalendarDateFields field
) const;
623 * This is for ICU internal use only. Please do not use.
624 * Check whether the 'field' is smaller than all the fields covered in
625 * pattern, return TRUE if it is. The sequence of calendar field,
626 * from large to small is: ERA, YEAR, MONTH, DATE, AM_PM, HOUR, MINUTE,...
627 * @param pattern the pattern to check against
628 * @param field the calendar field need to check against
629 * @return TRUE if the 'field' is smaller than all the fields
630 * covered in pattern. FALSE otherwise.
633 static UBool
isFieldUnitIgnored(const UnicodeString
& pattern
,
634 UCalendarDateFields field
);
639 * This is for ICU internal use only. Please do not use.
640 * Get the locale of this simple date formatter.
641 * It is used in DateIntervalFormat.
643 * @return locale in this simple date formatter
646 const Locale
& getSmpFmtLocale(void) const;
650 friend class DateFormat
;
652 void initializeDefaultCentury(void);
654 SimpleDateFormat(); // default constructor not implemented
657 * Used by the DateFormat factory methods to construct a SimpleDateFormat.
658 * @param timeStyle the time style.
659 * @param dateStyle the date style.
660 * @param locale the given locale.
661 * @param status Output param set to success/failure code on
664 SimpleDateFormat(EStyle timeStyle
, EStyle dateStyle
, const Locale
& locale
, UErrorCode
& status
);
667 * Construct a SimpleDateFormat for the given locale. If no resource data
668 * is available, create an object of last resort, using hard-coded strings.
669 * This is an internal method, called by DateFormat. It should never fail.
670 * @param locale the given locale.
671 * @param status Output param set to success/failure code on
674 SimpleDateFormat(const Locale
& locale
, UErrorCode
& status
); // Use default pattern
677 * Called by format() to format a single field.
679 * @param appendTo Output parameter to receive result.
680 * Result is appended to existing contents.
681 * @param ch The format character we encountered in the pattern.
682 * @param count Number of characters in the current pattern symbol (e.g.,
683 * "yyyy" in the pattern would result in a call to this function
684 * with ch equal to 'y' and count equal to 4)
685 * @param pos The FieldPosition being filled in by the format() call. If
686 * this function is formatting the field specfied by pos, it
687 * will fill in pos with the beginning and ending offsets of the
689 * @param status Receives a status code, which will be U_ZERO_ERROR if the operation
692 void subFormat( UnicodeString
&appendTo
,
697 UErrorCode
& status
) const; // in case of illegal argument
700 * Used by subFormat() to format a numeric value.
701 * Appends to toAppendTo a string representation of "value"
702 * having a number of digits between "minDigits" and
703 * "maxDigits". Uses the DateFormat's NumberFormat.
705 * @param appendTo Output parameter to receive result.
706 * Formatted number is appended to existing contents.
707 * @param value Value to format.
708 * @param minDigits Minimum number of digits the result should have
709 * @param maxDigits Maximum number of digits the result should have
711 void zeroPaddingNumber( UnicodeString
&appendTo
,
714 int32_t maxDigits
) const;
717 * Return true if the given format character, occuring count
718 * times, represents a numeric field.
720 static UBool
isNumeric(UChar formatChar
, int32_t count
);
723 * initializes fCalendar from parameters. Returns fCalendar as a convenience.
724 * @param adoptZone Zone to be adopted, or NULL for TimeZone::createDefault().
725 * @param locale Locale of the calendar
726 * @param status Error code
727 * @return the newly constructed fCalendar
729 Calendar
*initializeCalendar(TimeZone
* adoptZone
, const Locale
& locale
, UErrorCode
& status
);
732 * initializes fSymbols from parameters.
733 * @param locale Locale of the symbols
734 * @param calendar Alias to Calendar that will be used.
735 * @param status Error code
737 void initializeSymbols(const Locale
& locale
, Calendar
* calendar
, UErrorCode
& status
);
740 * Called by several of the constructors to load pattern data and formatting symbols
741 * out of a resource bundle and initialize the locale based on it.
742 * @param timeStyle The time style, as passed to DateFormat::createDateInstance().
743 * @param dateStyle The date style, as passed to DateFormat::createTimeInstance().
744 * @param locale The locale to load the patterns from.
745 * @param status Filled in with an error code if loading the data from the
748 void construct(EStyle timeStyle
, EStyle dateStyle
, const Locale
& locale
, UErrorCode
& status
);
751 * Called by construct() and the various constructors to set up the SimpleDateFormat's
752 * Calendar and NumberFormat objects.
753 * @param locale The locale for which we want a Calendar and a NumberFormat.
754 * @param statuc Filled in with an error code if creating either subobject fails.
756 void initialize(const Locale
& locale
, UErrorCode
& status
);
759 * Private code-size reduction function used by subParse.
760 * @param text the time text being parsed.
761 * @param start where to start parsing.
762 * @param field the date field being parsed.
763 * @param stringArray the string array to parsed.
764 * @param stringArrayCount the size of the array.
765 * @param cal a Calendar set to the date and time to be formatted
766 * into a date/time string.
767 * @return the new start position if matching succeeded; a negative number
768 * indicating matching failure, otherwise.
770 int32_t matchString(const UnicodeString
& text
, int32_t start
, UCalendarDateFields field
,
771 const UnicodeString
* stringArray
, int32_t stringArrayCount
, Calendar
& cal
) const;
774 * Private code-size reduction function used by subParse.
775 * @param text the time text being parsed.
776 * @param start where to start parsing.
777 * @param field the date field being parsed.
778 * @param stringArray the string array to parsed.
779 * @param stringArrayCount the size of the array.
780 * @param cal a Calendar set to the date and time to be formatted
781 * into a date/time string.
782 * @return the new start position if matching succeeded; a negative number
783 * indicating matching failure, otherwise.
785 int32_t matchQuarterString(const UnicodeString
& text
, int32_t start
, UCalendarDateFields field
,
786 const UnicodeString
* stringArray
, int32_t stringArrayCount
, Calendar
& cal
) const;
789 * Private function used by subParse to match literal pattern text.
791 * @param pattern the pattern string
792 * @param patternOffset the starting offset into the pattern text. On
793 * outupt will be set the offset of the first non-literal character in the pattern
794 * @param text the text being parsed
795 * @param textOffset the starting offset into the text. On output
796 * will be set to the offset of the character after the match
797 * @param lenient <code>TRUE</code> if the parse is lenient, <code>FALSE</code> otherwise.
799 * @return <code>TRUE</code> if the literal text could be matched, <code>FALSE</code> otherwise.
801 static UBool
matchLiterals(const UnicodeString
&pattern
, int32_t &patternOffset
,
802 const UnicodeString
&text
, int32_t &textOffset
, UBool lenient
);
805 * Private member function that converts the parsed date strings into
806 * timeFields. Returns -start (for ParsePosition) if failed.
807 * @param text the time text to be parsed.
808 * @param start where to start parsing.
809 * @param ch the pattern character for the date field text to be parsed.
810 * @param count the count of a pattern character.
811 * @param obeyCount if true then the count is strictly obeyed.
812 * @param ambiguousYear If true then the two-digit year == the default start year.
813 * @param cal a Calendar set to the date and time to be formatted
814 * into a date/time string.
815 * @return the new start position if matching succeeded; a negative number
816 * indicating matching failure, otherwise.
818 int32_t subParse(const UnicodeString
& text
, int32_t& start
, UChar ch
, int32_t count
,
819 UBool obeyCount
, UBool allowNegative
, UBool ambiguousYear
[], Calendar
& cal
) const;
821 void parseInt(const UnicodeString
& text
,
824 UBool allowNegative
) const;
826 void parseInt(const UnicodeString
& text
,
830 UBool allowNegative
) const;
833 * Translate a pattern, mapping each character in the from string to the
834 * corresponding character in the to string. Return an error if the original
835 * pattern contains an unmapped character, or if a quote is unmatched.
836 * Quoted (single quotes only) material is not translated.
837 * @param originalPattern the original pattern.
838 * @param translatedPattern Output param to receive the translited pattern.
839 * @param from the characters to be translited from.
840 * @param to the characters to be translited to.
841 * @param status Receives a status code, which will be U_ZERO_ERROR
842 * if the operation succeeds.
844 static void translatePattern(const UnicodeString
& originalPattern
,
845 UnicodeString
& translatedPattern
,
846 const UnicodeString
& from
,
847 const UnicodeString
& to
,
851 * Sets the starting date of the 100-year window that dates with 2-digit years
852 * are considered to fall within.
853 * @param startDate the start date
854 * @param status Receives a status code, which will be U_ZERO_ERROR
855 * if the operation succeeds.
857 void parseAmbiguousDatesAsAfter(UDate startDate
, UErrorCode
& status
);
860 * Private methods for formatting/parsing GMT string
862 void appendGMT(UnicodeString
&appendTo
, Calendar
& cal
, UErrorCode
& status
) const;
863 void formatGMTDefault(UnicodeString
&appendTo
, int32_t offset
) const;
864 int32_t parseGMT(const UnicodeString
&text
, ParsePosition
&pos
) const;
865 int32_t parseGMTDefault(const UnicodeString
&text
, ParsePosition
&pos
) const;
866 UBool
isDefaultGMTFormat() const;
868 void formatRFC822TZ(UnicodeString
&appendTo
, int32_t offset
) const;
871 * Initialize MessageFormat instances used for GMT formatting/parsing
873 void initGMTFormatters(UErrorCode
&status
);
876 * Used to map pattern characters to Calendar field identifiers.
878 static const UCalendarDateFields fgPatternIndexToCalendarField
[];
881 * Map index into pattern character string to DateFormat field number
883 static const UDateFormatField fgPatternIndexToDateFormatField
[];
886 * Used to map Calendar field to field level.
887 * The larger the level, the smaller the field unit.
888 * For example, UCAL_ERA level is 0, UCAL_YEAR level is 10,
889 * UCAL_MONTH level is 20.
891 static const int32_t fgCalendarFieldToLevel
[];
892 static const int32_t fgPatternCharToLevel
[];
895 * The formatting pattern for this formatter.
897 UnicodeString fPattern
;
900 * The original locale used (for reloading symbols)
905 * A pointer to an object containing the strings to use in formatting (e.g.,
906 * month and day names, AM and PM strings, time zone names, etc.)
908 DateFormatSymbols
* fSymbols
; // Owned
911 * If dates have ambiguous years, we map them into the century starting
912 * at defaultCenturyStart, which may be any date. If defaultCenturyStart is
913 * set to SYSTEM_DEFAULT_CENTURY, which it is by default, then the system
914 * values are used. The instance values defaultCenturyStart and
915 * defaultCenturyStartYear are only used if explicitly set by the user
916 * through the API method parseAmbiguousDatesAsAfter().
918 UDate fDefaultCenturyStart
;
921 * See documentation for defaultCenturyStart.
923 /*transient*/ int32_t fDefaultCenturyStartYear
;
931 ParsedTZType tztype
; // here to avoid api change
934 * MessageFormat instances used for localized GMT format
936 MessageFormat
**fGMTFormatters
;
938 UBool fHaveDefaultCentury
;
942 SimpleDateFormat::get2DigitYearStart(UErrorCode
& /*status*/) const
944 return fDefaultCenturyStart
;
947 inline UnicodeString
&
948 SimpleDateFormat::format(const Formattable
& obj
,
949 UnicodeString
& appendTo
,
950 UErrorCode
& status
) const {
951 // Don't use Format:: - use immediate base class only,
952 // in case immediate base modifies behavior later.
953 return DateFormat::format(obj
, appendTo
, status
);
956 inline UnicodeString
&
957 SimpleDateFormat::format(UDate date
,
958 UnicodeString
& appendTo
,
959 FieldPosition
& fieldPosition
) const {
960 // Don't use Format:: - use immediate base class only,
961 // in case immediate base modifies behavior later.
962 return DateFormat::format(date
, appendTo
, fieldPosition
);
965 inline UnicodeString
&
966 SimpleDateFormat::format(UDate date
, UnicodeString
& appendTo
) const {
967 return DateFormat::format(date
, appendTo
);
972 #endif /* #if !UCONFIG_NO_FORMATTING */