2 * Copyright (C) 1997-2006, 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
;
44 * SimpleDateFormat is a concrete class for formatting and parsing dates in a
45 * language-independent manner. It allows for formatting (millis -> text),
46 * parsing (text -> millis), and normalization. Formats/Parses a date or time,
47 * which is the standard milliseconds since 24:00 GMT, Jan 1, 1970.
49 * Clients are encouraged to create a date-time formatter using DateFormat::getInstance(),
50 * getDateInstance(), getDateInstance(), or getDateTimeInstance() rather than
51 * explicitly constructing an instance of SimpleDateFormat. This way, the client
52 * is guaranteed to get an appropriate formatting pattern for whatever locale the
53 * program is running in. However, if the client needs something more unusual than
54 * the default patterns in the locales, he can construct a SimpleDateFormat directly
55 * and give it an appropriate pattern (or use one of the factory methods on DateFormat
56 * and modify the pattern after the fact with toPattern() and applyPattern().
58 * Date/Time format syntax:
60 * The date/time format is specified by means of a string time pattern. In this
61 * pattern, all ASCII letters are reserved as pattern letters, which are defined
65 * Symbol Meaning Presentation Example
66 * ------ ------- ------------ -------
67 * G era designator (Text) AD
68 * y year (Number) 1996
69 * Y year (week of year) (Number) 1997
70 * u extended year (Number) 4601
71 * M month in year (Text & Number) July & 07
72 * d day in month (Number) 10
73 * h hour in am/pm (1~12) (Number) 12
74 * H hour in day (0~23) (Number) 0
75 * m minute in hour (Number) 30
76 * s second in minute (Number) 55
77 * S fractional second (Number) 978
78 * E day of week (Text) Tuesday
79 * e day of week (local 1~7) (Number) 2
80 * D day in year (Number) 189
81 * F day of week in month (Number) 2 (2nd Wed in July)
82 * w week in year (Number) 27
83 * W week in month (Number) 2
84 * a am/pm marker (Text) PM
85 * k hour in day (1~24) (Number) 24
86 * K hour in am/pm (0~11) (Number) 0
87 * z time zone (Time) Pacific Standard Time
88 * Z time zone (RFC 822) (Number) -0800
89 * v time zone (generic) (Text) Pacific Time
90 * g Julian day (Number) 2451334
91 * A milliseconds in day (Number) 69540000
92 * ' escape for text (Delimiter) 'Date='
93 * '' single quote (Literal) 'o''clock'
96 * The count of pattern letters determine the format.
98 * (Text): 4 or more, use full form, <4, use short or abbreviated form if it
99 * exists. (e.g., "EEEE" produces "Monday", "EEE" produces "Mon")
101 * (Number): the minimum number of digits. Shorter numbers are zero-padded to
102 * this amount (e.g. if "m" produces "6", "mm" produces "06"). Year is handled
103 * specially; that is, if the count of 'y' is 2, the Year will be truncated to 2 digits.
104 * (e.g., if "yyyy" produces "1997", "yy" produces "97".)
105 * Unlike other fields, fractional seconds are padded on the right with zero.
107 * (Text & Number): 3 or over, use text, otherwise use number. (e.g., "M" produces "1",
108 * "MM" produces "01", "MMM" produces "Jan", and "MMMM" produces "January".)
110 * Any characters in the pattern that are not in the ranges of ['a'..'z'] and
111 * ['A'..'Z'] will be treated as quoted text. For instance, characters
112 * like ':', '.', ' ', '#' and '@' will appear in the resulting time text
113 * even they are not embraced within single quotes.
115 * A pattern containing any invalid pattern letter will result in a failing
116 * UErrorCode result during formatting or parsing.
118 * Examples using the US locale:
121 * Format Pattern Result
122 * -------------- -------
123 * "yyyy.MM.dd G 'at' HH:mm:ss vvvv" ->> 1996.07.10 AD at 15:08:56 Pacific Time
124 * "EEE, MMM d, ''yy" ->> Wed, July 10, '96
125 * "h:mm a" ->> 12:08 PM
126 * "hh 'o''clock' a, zzzz" ->> 12 o'clock PM, Pacific Daylight Time
127 * "K:mm a, vvv" ->> 0:00 PM, PT
128 * "yyyyy.MMMMM.dd GGG hh:mm aaa" ->> 1996.July.10 AD 12:08 PM
134 * UErrorCode success = U_ZERO_ERROR;
135 * SimpleTimeZone* pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, "PST");
136 * pdt->setStartRule( Calendar::APRIL, 1, Calendar::SUNDAY, 2*60*60*1000);
137 * pdt->setEndRule( Calendar::OCTOBER, -1, Calendar::SUNDAY, 2*60*60*1000);
139 * // Format the current time.
140 * SimpleDateFormat* formatter
141 * = new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss a zzz", success );
142 * GregorianCalendar cal(success);
143 * UDate currentTime_1 = cal.getTime(success);
144 * FieldPosition fp(0);
145 * UnicodeString dateString;
146 * formatter->format( currentTime_1, dateString, fp );
147 * cout << "result: " << dateString << endl;
149 * // Parse the previous string back into a Date.
150 * ParsePosition pp(0);
151 * UDate currentTime_2 = formatter->parse(dateString, pp );
154 * In the above example, the time value "currentTime_2" obtained from parsing
155 * will be equal to currentTime_1. However, they may not be equal if the am/pm
156 * marker 'a' is left out from the format pattern while the "hour in am/pm"
157 * pattern symbol is used. This information loss can happen when formatting the
161 * When parsing a date string using the abbreviated year pattern ("y" or "yy"),
162 * SimpleDateFormat must interpret the abbreviated year
163 * relative to some century. It does this by adjusting dates to be
164 * within 80 years before and 20 years after the time the SimpleDateFormat
165 * instance is created. For example, using a pattern of "MM/dd/yy" and a
166 * SimpleDateFormat instance created on Jan 1, 1997, the string
167 * "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64"
168 * would be interpreted as May 4, 1964.
169 * During parsing, only strings consisting of exactly two digits, as defined by
170 * <code>Unicode::isDigit()</code>, will be parsed into the default century.
171 * Any other numeric string, such as a one digit string, a three or more digit
172 * string, or a two digit string that isn't all digits (for example, "-1"), is
173 * interpreted literally. So "01/02/3" or "01/02/003" are parsed, using the
174 * same pattern, as Jan 2, 3 AD. Likewise, "01/02/-3" is parsed as Jan 2, 4 BC.
177 * If the year pattern has more than two 'y' characters, the year is
178 * interpreted literally, regardless of the number of digits. So using the
179 * pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D.
182 * When numeric fields abut one another directly, with no intervening delimiter
183 * characters, they constitute a run of abutting numeric fields. Such runs are
184 * parsed specially. For example, the format "HHmmss" parses the input text
185 * "123456" to 12:34:56, parses the input text "12345" to 1:23:45, and fails to
186 * parse "1234". In other words, the leftmost field of the run is flexible,
187 * while the others keep a fixed width. If the parse fails anywhere in the run,
188 * then the leftmost field is shortened by one character, and the entire run is
189 * parsed again. This is repeated until either the parse succeeds or the
190 * leftmost field is one character in length. If the parse still fails at that
191 * point, the parse of the run fails.
194 * For time zones that have no names, SimpleDateFormat uses strings GMT+hours:minutes or
197 * The calendar defines what is the first day of the week, the first week of the
198 * year, whether hours are zero based or not (0 vs 12 or 24), and the timezone.
199 * There is one common number format to handle all the numbers; the digit count
200 * is handled programmatically according to the pattern.
202 * <p><em>User subclasses are not supported.</em> While clients may write
203 * subclasses, such code will not necessarily work and will not be
204 * guaranteed to work stably from release to release.
206 class U_I18N_API SimpleDateFormat
: public DateFormat
{
209 * Construct a SimpleDateFormat using the default pattern for the default
212 * [Note:] Not all locales support SimpleDateFormat; for full generality,
213 * use the factory methods in the DateFormat class.
214 * @param status Output param set to success/failure code.
217 SimpleDateFormat(UErrorCode
& status
);
220 * Construct a SimpleDateFormat using the given pattern and the default locale.
221 * The locale is used to obtain the symbols used in formatting (e.g., the
222 * names of the months), but not to provide the pattern.
224 * [Note:] Not all locales support SimpleDateFormat; for full generality,
225 * use the factory methods in the DateFormat class.
226 * @param pattern the pattern for the format.
227 * @param status Output param set to success/failure code.
230 SimpleDateFormat(const UnicodeString
& pattern
,
234 * Construct a SimpleDateFormat using the given pattern and locale.
235 * The locale is used to obtain the symbols used in formatting (e.g., the
236 * names of the months), but not to provide the pattern.
238 * [Note:] Not all locales support SimpleDateFormat; for full generality,
239 * use the factory methods in the DateFormat class.
240 * @param pattern the pattern for the format.
241 * @param locale the given locale.
242 * @param status Output param set to success/failure code.
245 SimpleDateFormat(const UnicodeString
& pattern
,
246 const Locale
& locale
,
250 * Construct a SimpleDateFormat using the given pattern and locale-specific
251 * symbol data. The formatter takes ownership of the DateFormatSymbols object;
252 * the caller is no longer responsible for deleting it.
253 * @param pattern the given pattern for the format.
254 * @param formatDataToAdopt the symbols to be adopted.
255 * @param status Output param set to success/faulure code.
258 SimpleDateFormat(const UnicodeString
& pattern
,
259 DateFormatSymbols
* formatDataToAdopt
,
263 * Construct a SimpleDateFormat using the given pattern and locale-specific
264 * symbol data. The DateFormatSymbols object is NOT adopted; the caller
265 * remains responsible for deleting it.
266 * @param pattern the given pattern for the format.
267 * @param formatData the formatting symbols to be use.
268 * @param status Output param set to success/faulure code.
271 SimpleDateFormat(const UnicodeString
& pattern
,
272 const DateFormatSymbols
& formatData
,
279 SimpleDateFormat(const SimpleDateFormat
&);
282 * Assignment operator.
285 SimpleDateFormat
& operator=(const SimpleDateFormat
&);
291 virtual ~SimpleDateFormat();
294 * Clone this Format object polymorphically. The caller owns the result and
295 * should delete it when done.
296 * @return A copy of the object.
299 virtual Format
* clone(void) const;
302 * Return true if the given Format objects are semantically equal. Objects
303 * of different subclasses are considered unequal.
304 * @param other the object to be compared with.
305 * @return true if the given Format objects are semantically equal.
308 virtual UBool
operator==(const Format
& other
) const;
311 * Format a date or time, which is the standard millis since 24:00 GMT, Jan
312 * 1, 1970. Overrides DateFormat pure virtual method.
314 * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->>
315 * 1996.07.10 AD at 15:08:56 PDT
317 * @param cal Calendar set to the date and time to be formatted
318 * into a date/time string.
319 * @param appendTo Output parameter to receive result.
320 * Result is appended to existing contents.
321 * @param pos The formatting position. On input: an alignment field,
322 * if desired. On output: the offsets of the alignment field.
323 * @return Reference to 'appendTo' parameter.
326 virtual UnicodeString
& format( Calendar
& cal
,
327 UnicodeString
& appendTo
,
328 FieldPosition
& pos
) const;
331 * Format a date or time, which is the standard millis since 24:00 GMT, Jan
332 * 1, 1970. Overrides DateFormat pure virtual method.
334 * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->>
335 * 1996.07.10 AD at 15:08:56 PDT
337 * @param obj A Formattable containing the date-time value to be formatted
338 * into a date-time string. If the type of the Formattable
339 * is a numeric type, it is treated as if it were an
341 * @param appendTo Output parameter to receive result.
342 * Result is appended to existing contents.
343 * @param pos The formatting position. On input: an alignment field,
344 * if desired. On output: the offsets of the alignment field.
345 * @param status Output param set to success/faulure code.
346 * @return Reference to 'appendTo' parameter.
349 virtual UnicodeString
& format( const Formattable
& obj
,
350 UnicodeString
& appendTo
,
352 UErrorCode
& status
) const;
355 * Redeclared DateFormat method.
356 * @param date the Date value to be formatted.
357 * @param appendTo Output parameter to receive result.
358 * Result is appended to existing contents.
359 * @param fieldPosition The formatting position. On input: an alignment field,
360 * if desired. On output: the offsets of the alignment field.
361 * @return Reference to 'appendTo' parameter.
364 UnicodeString
& format(UDate date
,
365 UnicodeString
& appendTo
,
366 FieldPosition
& fieldPosition
) const;
369 * Redeclared DateFormat method.
370 * @param obj Object to be formatted.
371 * @param appendTo Output parameter to receive result.
372 * Result is appended to existing contents.
373 * @param status Input/output success/failure code.
374 * @return Reference to 'appendTo' parameter.
377 UnicodeString
& format(const Formattable
& obj
,
378 UnicodeString
& appendTo
,
379 UErrorCode
& status
) const;
382 * Redeclared DateFormat method.
383 * @param date Date value to be formatted.
384 * @param appendTo Output parameter to receive result.
385 * Result is appended to existing contents.
386 * @return Reference to 'appendTo' parameter.
389 UnicodeString
& format(UDate date
, UnicodeString
& appendTo
) const;
392 * Parse a date/time string beginning at the given parse position. For
393 * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
394 * that is equivalent to Date(837039928046).
396 * By default, parsing is lenient: If the input is not in the form used by
397 * this object's format method but can still be parsed as a date, then the
398 * parse succeeds. Clients may insist on strict adherence to the format by
399 * calling setLenient(false).
401 * @param text The date/time string to be parsed
402 * @param cal a Calendar set to the date and time to be formatted
403 * into a date/time string.
404 * @param pos On input, the position at which to start parsing; on
405 * output, the position at which parsing terminated, or the
406 * start position if the parse failed.
407 * @return A valid UDate if the input could be parsed.
410 virtual void parse( const UnicodeString
& text
,
412 ParsePosition
& pos
) const;
415 * Parse a date/time string starting at the given parse position. For
416 * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
417 * that is equivalent to Date(837039928046).
419 * By default, parsing is lenient: If the input is not in the form used by
420 * this object's format method but can still be parsed as a date, then the
421 * parse succeeds. Clients may insist on strict adherence to the format by
422 * calling setLenient(false).
424 * @see DateFormat::setLenient(boolean)
426 * @param text The date/time string to be parsed
427 * @param pos On input, the position at which to start parsing; on
428 * output, the position at which parsing terminated, or the
429 * start position if the parse failed.
430 * @return A valid UDate if the input could be parsed.
433 UDate
parse( const UnicodeString
& text
,
434 ParsePosition
& pos
) const;
438 * Parse a date/time string. For example, a time text "07/10/96 4:5 PM, PDT"
439 * will be parsed into a UDate that is equivalent to Date(837039928046).
440 * Parsing begins at the beginning of the string and proceeds as far as
441 * possible. Assuming no parse errors were encountered, this function
442 * doesn't return any information about how much of the string was consumed
443 * by the parsing. If you need that information, use the version of
444 * parse() that takes a ParsePosition.
446 * @param text The date/time string to be parsed
447 * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with
448 * an error value if there was a parse error.
449 * @return A valid UDate if the input could be parsed.
452 virtual UDate
parse( const UnicodeString
& text
,
453 UErrorCode
& status
) const;
456 * Set the start UDate used to interpret two-digit year strings.
457 * When dates are parsed having 2-digit year strings, they are placed within
458 * a assumed range of 100 years starting on the two digit start date. For
459 * example, the string "24-Jan-17" may be in the year 1817, 1917, 2017, or
460 * some other year. SimpleDateFormat chooses a year so that the resultant
461 * date is on or after the two digit start date and within 100 years of the
462 * two digit start date.
464 * By default, the two digit start date is set to 80 years before the current
465 * time at which a SimpleDateFormat object is created.
466 * @param d start UDate used to interpret two-digit year strings.
467 * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with
468 * an error value if there was a parse error.
471 virtual void set2DigitYearStart(UDate d
, UErrorCode
& status
);
474 * Get the start UDate used to interpret two-digit year strings.
475 * When dates are parsed having 2-digit year strings, they are placed within
476 * a assumed range of 100 years starting on the two digit start date. For
477 * example, the string "24-Jan-17" may be in the year 1817, 1917, 2017, or
478 * some other year. SimpleDateFormat chooses a year so that the resultant
479 * date is on or after the two digit start date and within 100 years of the
480 * two digit start date.
482 * By default, the two digit start date is set to 80 years before the current
483 * time at which a SimpleDateFormat object is created.
484 * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with
485 * an error value if there was a parse error.
488 UDate
get2DigitYearStart(UErrorCode
& status
) const;
491 * Return a pattern string describing this date format.
492 * @param result Output param to receive the pattern.
493 * @return A reference to 'result'.
496 virtual UnicodeString
& toPattern(UnicodeString
& result
) const;
499 * Return a localized pattern string describing this date format.
500 * In most cases, this will return the same thing as toPattern(),
501 * but a locale can specify characters to use in pattern descriptions
502 * in place of the ones described in this class's class documentation.
503 * (Presumably, letters that would be more mnemonic in that locale's
504 * language.) This function would produce a pattern using those
507 * @param result Receives the localized pattern.
508 * @param status Output param set to success/failure code on
509 * exit. If the pattern is invalid, this will be
510 * set to a failure result.
511 * @return A reference to 'result'.
514 virtual UnicodeString
& toLocalizedPattern(UnicodeString
& result
,
515 UErrorCode
& status
) const;
518 * Apply the given unlocalized pattern string to this date format.
519 * (i.e., after this call, this formatter will format dates according to
522 * @param pattern The pattern to be applied.
525 virtual void applyPattern(const UnicodeString
& pattern
);
528 * Apply the given localized pattern string to this date format.
529 * (see toLocalizedPattern() for more information on localized patterns.)
531 * @param pattern The localized pattern to be applied.
532 * @param status Output param set to success/failure code on
533 * exit. If the pattern is invalid, this will be
534 * set to a failure result.
537 virtual void applyLocalizedPattern(const UnicodeString
& pattern
,
541 * Gets the date/time formatting symbols (this is an object carrying
542 * the various strings and other symbols used in formatting: e.g., month
543 * names and abbreviations, time zone names, AM/PM strings, etc.)
544 * @return a copy of the date-time formatting data associated
545 * with this date-time formatter.
548 virtual const DateFormatSymbols
* getDateFormatSymbols(void) const;
551 * Set the date/time formatting symbols. The caller no longer owns the
552 * DateFormatSymbols object and should not delete it after making this call.
553 * @param newFormatSymbols the given date-time formatting symbols to copy.
556 virtual void adoptDateFormatSymbols(DateFormatSymbols
* newFormatSymbols
);
559 * Set the date/time formatting data.
560 * @param newFormatSymbols the given date-time formatting symbols to copy.
563 virtual void setDateFormatSymbols(const DateFormatSymbols
& newFormatSymbols
);
566 * Return the class ID for this class. This is useful only for comparing to
567 * a return value from getDynamicClassID(). For example:
569 * . Base* polymorphic_pointer = createPolymorphicObject();
570 * . if (polymorphic_pointer->getDynamicClassID() ==
571 * . erived::getStaticClassID()) ...
573 * @return The class ID for all objects of this class.
576 static UClassID U_EXPORT2
getStaticClassID(void);
579 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
580 * method is to implement a simple version of RTTI, since not all C++
581 * compilers support genuine RTTI. Polymorphic operator==() and clone()
582 * methods call this method.
584 * @return The class ID for this object. All objects of a
585 * given class have the same class ID. Objects of
586 * other classes have different class IDs.
589 virtual UClassID
getDynamicClassID(void) const;
592 * Set the calendar to be used by this date format. Initially, the default
593 * calendar for the specified or default locale is used. The caller should
594 * not delete the Calendar object after it is adopted by this call.
595 * Adopting a new calendar will change to the default symbols.
597 * @param calendarToAdopt Calendar object to be adopted.
600 virtual void adoptCalendar(Calendar
* calendarToAdopt
);
603 friend class DateFormat
;
605 void initializeDefaultCentury(void);
607 SimpleDateFormat(); // default constructor not implemented
610 * Used by the DateFormat factory methods to construct a SimpleDateFormat.
611 * @param timeStyle the time style.
612 * @param dateStyle the date style.
613 * @param locale the given locale.
614 * @param status Output param set to success/failure code on
617 SimpleDateFormat(EStyle timeStyle
, EStyle dateStyle
, const Locale
& locale
, UErrorCode
& status
);
620 * Construct a SimpleDateFormat for the given locale. If no resource data
621 * is available, create an object of last resort, using hard-coded strings.
622 * This is an internal method, called by DateFormat. It should never fail.
623 * @param locale the given locale.
624 * @param status Output param set to success/failure code on
627 SimpleDateFormat(const Locale
& locale
, UErrorCode
& status
); // Use default pattern
630 * Called by format() to format a single field.
632 * @param appendTo Output parameter to receive result.
633 * Result is appended to existing contents.
634 * @param ch The format character we encountered in the pattern.
635 * @param count Number of characters in the current pattern symbol (e.g.,
636 * "yyyy" in the pattern would result in a call to this function
637 * with ch equal to 'y' and count equal to 4)
638 * @param pos The FieldPosition being filled in by the format() call. If
639 * this function is formatting the field specfied by pos, it
640 * will fill in pos with the beginning and ending offsets of the
642 * @param status Receives a status code, which will be U_ZERO_ERROR if the operation
645 void subFormat( UnicodeString
&appendTo
,
650 UErrorCode
& status
) const; // in case of illegal argument
653 * Used by subFormat() to format a numeric value.
654 * Appends to toAppendTo a string representation of "value"
655 * having a number of digits between "minDigits" and
656 * "maxDigits". Uses the DateFormat's NumberFormat.
658 * @param appendTo Output parameter to receive result.
659 * Formatted number is appended to existing contents.
660 * @param value Value to format.
661 * @param minDigits Minimum number of digits the result should have
662 * @param maxDigits Maximum number of digits the result should have
664 void zeroPaddingNumber( UnicodeString
&appendTo
,
667 int32_t maxDigits
) const;
670 * Return true if the given format character, occuring count
671 * times, represents a numeric field.
673 static UBool
isNumeric(UChar formatChar
, int32_t count
);
676 * initializes fCalendar from parameters. Returns fCalendar as a convenience.
677 * @param adoptZone Zone to be adopted, or NULL for TimeZone::createDefault().
678 * @param locale Locale of the calendar
679 * @param status Error code
680 * @return the newly constructed fCalendar
682 Calendar
*initializeCalendar(TimeZone
* adoptZone
, const Locale
& locale
, UErrorCode
& status
);
685 * initializes fSymbols from parameters.
686 * @param locale Locale of the symbols
687 * @param calendar Alias to Calendar that will be used.
688 * @param status Error code
690 void initializeSymbols(const Locale
& locale
, Calendar
* calendar
, UErrorCode
& status
);
693 * Called by several of the constructors to load pattern data and formatting symbols
694 * out of a resource bundle and initialize the locale based on it.
695 * @param timeStyle The time style, as passed to DateFormat::createDateInstance().
696 * @param dateStyle The date style, as passed to DateFormat::createTimeInstance().
697 * @param locale The locale to load the patterns from.
698 * @param status Filled in with an error code if loading the data from the
701 void construct(EStyle timeStyle
, EStyle dateStyle
, const Locale
& locale
, UErrorCode
& status
);
704 * Called by construct() and the various constructors to set up the SimpleDateFormat's
705 * Calendar and NumberFormat objects.
706 * @param locale The locale for which we want a Calendar and a NumberFormat.
707 * @param statuc Filled in with an error code if creating either subobject fails.
709 void initialize(const Locale
& locale
, UErrorCode
& status
);
712 * Private code-size reduction function used by subParse.
713 * @param text the time text being parsed.
714 * @param start where to start parsing.
715 * @param field the date field being parsed.
716 * @param stringArray the string array to parsed.
717 * @param stringArrayCount the size of the array.
718 * @param cal a Calendar set to the date and time to be formatted
719 * into a date/time string.
720 * @return the new start position if matching succeeded; a negative number
721 * indicating matching failure, otherwise.
723 int32_t matchString(const UnicodeString
& text
, int32_t start
, UCalendarDateFields field
,
724 const UnicodeString
* stringArray
, int32_t stringArrayCount
, Calendar
& cal
) const;
727 * Private code-size reduction function used by subParse.
728 * @param text the time text being parsed.
729 * @param start where to start parsing.
730 * @param field the date field being parsed.
731 * @param stringArray the string array to parsed.
732 * @param stringArrayCount the size of the array.
733 * @param cal a Calendar set to the date and time to be formatted
734 * into a date/time string.
735 * @return the new start position if matching succeeded; a negative number
736 * indicating matching failure, otherwise.
738 int32_t matchQuarterString(const UnicodeString
& text
, int32_t start
, UCalendarDateFields field
,
739 const UnicodeString
* stringArray
, int32_t stringArrayCount
, Calendar
& cal
) const;
742 * Private member function that converts the parsed date strings into
743 * timeFields. Returns -start (for ParsePosition) if failed.
744 * @param text the time text to be parsed.
745 * @param start where to start parsing.
746 * @param ch the pattern character for the date field text to be parsed.
747 * @param count the count of a pattern character.
748 * @param obeyCount if true then the count is strictly obeyed.
749 * @param ambiguousYear If true then the two-digit year == the default start year.
750 * @param cal a Calendar set to the date and time to be formatted
751 * into a date/time string.
752 * @return the new start position if matching succeeded; a negative number
753 * indicating matching failure, otherwise.
755 int32_t subParse(const UnicodeString
& text
, int32_t& start
, UChar ch
, int32_t count
,
756 UBool obeyCount
, UBool allowNegative
, UBool ambiguousYear
[], Calendar
& cal
) const;
758 void parseInt(const UnicodeString
& text
,
761 UBool allowNegative
) const;
764 * Translate a pattern, mapping each character in the from string to the
765 * corresponding character in the to string. Return an error if the original
766 * pattern contains an unmapped character, or if a quote is unmatched.
767 * Quoted (single quotes only) material is not translated.
768 * @param originalPattern the original pattern.
769 * @param translatedPattern Output param to receive the translited pattern.
770 * @param from the characters to be translited from.
771 * @param to the characters to be translited to.
772 * @param status Receives a status code, which will be U_ZERO_ERROR
773 * if the operation succeeds.
775 static void translatePattern(const UnicodeString
& originalPattern
,
776 UnicodeString
& translatedPattern
,
777 const UnicodeString
& from
,
778 const UnicodeString
& to
,
782 * Sets the starting date of the 100-year window that dates with 2-digit years
783 * are considered to fall within.
784 * @param startDate the start date
785 * @param status Receives a status code, which will be U_ZERO_ERROR
786 * if the operation succeeds.
788 void parseAmbiguousDatesAsAfter(UDate startDate
, UErrorCode
& status
);
791 * Given text, a start in the text, and a row index, return the column index that
792 * of the zone name that matches (case insensitive) at start, or 0 if none matches.
794 int32_t matchZoneString(const UnicodeString& text, int32_t start, int32_t zi) const;
798 * Given text, a start in the text, and a calendar, return the next offset in the text
799 * after matching the zone string. If we fail to match, return 0. Update the calendar
802 int32_t subParseZoneString(const UnicodeString
& text
, int32_t start
, Calendar
& cal
, UErrorCode
& status
) const;
805 * append the gmt string
807 inline void appendGMT(UnicodeString
&appendTo
, Calendar
& cal
, UErrorCode
& status
) const;
810 * Used to map pattern characters to Calendar field identifiers.
812 static const UCalendarDateFields fgPatternIndexToCalendarField
[];
815 * Map index into pattern character string to DateFormat field number
817 static const UDateFormatField fgPatternIndexToDateFormatField
[];
820 * The formatting pattern for this formatter.
822 UnicodeString fPattern
;
825 * The original locale used (for reloading symbols)
830 * A pointer to an object containing the strings to use in formatting (e.g.,
831 * month and day names, AM and PM strings, time zone names, etc.)
833 DateFormatSymbols
* fSymbols
; // Owned
836 * If dates have ambiguous years, we map them into the century starting
837 * at defaultCenturyStart, which may be any date. If defaultCenturyStart is
838 * set to SYSTEM_DEFAULT_CENTURY, which it is by default, then the system
839 * values are used. The instance values defaultCenturyStart and
840 * defaultCenturyStartYear are only used if explicitly set by the user
841 * through the API method parseAmbiguousDatesAsAfter().
843 UDate fDefaultCenturyStart
;
846 * See documentation for defaultCenturyStart.
848 /*transient*/ int32_t fDefaultCenturyStartYear
;
850 /*transient*/ TimeZone
* parsedTimeZone
; // here to avoid api change
852 UBool fHaveDefaultCentury
;
856 SimpleDateFormat::get2DigitYearStart(UErrorCode
& /*status*/) const
858 return fDefaultCenturyStart
;
861 inline UnicodeString
&
862 SimpleDateFormat::format(const Formattable
& obj
,
863 UnicodeString
& appendTo
,
864 UErrorCode
& status
) const {
865 // Don't use Format:: - use immediate base class only,
866 // in case immediate base modifies behavior later.
867 return DateFormat::format(obj
, appendTo
, status
);
870 inline UnicodeString
&
871 SimpleDateFormat::format(UDate date
,
872 UnicodeString
& appendTo
,
873 FieldPosition
& fieldPosition
) const {
874 // Don't use Format:: - use immediate base class only,
875 // in case immediate base modifies behavior later.
876 return DateFormat::format(date
, appendTo
, fieldPosition
);
879 inline UnicodeString
&
880 SimpleDateFormat::format(UDate date
, UnicodeString
& appendTo
) const {
881 return DateFormat::format(date
, appendTo
);
886 #endif /* #if !UCONFIG_NO_FORMATTING */