]>
Commit | Line | Data |
---|---|---|
b75a7d8f | 1 | /* |
73c04bcf | 2 | * Copyright (C) 1997-2006, International Business Machines Corporation and others. All Rights Reserved. |
b75a7d8f A |
3 | ******************************************************************************* |
4 | * | |
5 | * File SMPDTFMT.H | |
6 | * | |
7 | * Modification History: | |
8 | * | |
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 | |
19 | * {j28 4182066}. | |
20 | ******************************************************************************* | |
21 | */ | |
22 | ||
23 | #ifndef SMPDTFMT_H | |
24 | #define SMPDTFMT_H | |
25 | ||
26 | #include "unicode/utypes.h" | |
27 | ||
73c04bcf A |
28 | /** |
29 | * \file | |
30 | * \brief C++ API: Format and parse dates in a language-independent manner. | |
31 | */ | |
32 | ||
b75a7d8f A |
33 | #if !UCONFIG_NO_FORMATTING |
34 | ||
35 | #include "unicode/datefmt.h" | |
36 | ||
37 | U_NAMESPACE_BEGIN | |
38 | ||
39 | class DateFormatSymbols; | |
40 | class DateFormat; | |
41 | ||
42 | /** | |
73c04bcf | 43 | * |
b75a7d8f A |
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. | |
48 | * <P> | |
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(). | |
57 | * <P> | |
58 | * Date/Time format syntax: | |
59 | * <P> | |
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 | |
62 | * as the following: | |
63 | * <pre> | |
64 | * \code | |
374ca955 A |
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 | |
73c04bcf | 87 | * z time zone (Time) Pacific Standard Time |
374ca955 | 88 | * Z time zone (RFC 822) (Number) -0800 |
73c04bcf | 89 | * v time zone (generic) (Text) Pacific Time |
374ca955 A |
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' | |
b75a7d8f A |
94 | * \endcode |
95 | * </pre> | |
96 | * The count of pattern letters determine the format. | |
97 | * <P> | |
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") | |
100 | * <P> | |
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".) | |
374ca955 | 105 | * Unlike other fields, fractional seconds are padded on the right with zero. |
b75a7d8f A |
106 | * <P> |
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".) | |
109 | * <P> | |
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. | |
114 | * <P> | |
115 | * A pattern containing any invalid pattern letter will result in a failing | |
116 | * UErrorCode result during formatting or parsing. | |
117 | * <P> | |
118 | * Examples using the US locale: | |
119 | * <pre> | |
120 | * \code | |
121 | * Format Pattern Result | |
122 | * -------------- ------- | |
73c04bcf | 123 | * "yyyy.MM.dd G 'at' HH:mm:ss vvvv" ->> 1996.07.10 AD at 15:08:56 Pacific Time |
b75a7d8f A |
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 | |
73c04bcf | 127 | * "K:mm a, vvv" ->> 0:00 PM, PT |
b75a7d8f A |
128 | * "yyyyy.MMMMM.dd GGG hh:mm aaa" ->> 1996.July.10 AD 12:08 PM |
129 | * \endcode | |
130 | * </pre> | |
131 | * Code Sample: | |
132 | * <pre> | |
133 | * \code | |
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); | |
138 | * | |
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; | |
148 | * | |
149 | * // Parse the previous string back into a Date. | |
150 | * ParsePosition pp(0); | |
151 | * UDate currentTime_2 = formatter->parse(dateString, pp ); | |
152 | * \endcode | |
153 | * </pre> | |
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 | |
158 | * time in PM. | |
159 | * | |
160 | * <p> | |
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. | |
175 | * | |
176 | * <p> | |
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. | |
180 | * | |
181 | * <p> | |
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. | |
192 | * | |
193 | * <P> | |
194 | * For time zones that have no names, SimpleDateFormat uses strings GMT+hours:minutes or | |
195 | * GMT-hours:minutes. | |
196 | * <P> | |
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. | |
374ca955 A |
201 | * |
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. | |
b75a7d8f A |
205 | */ |
206 | class U_I18N_API SimpleDateFormat: public DateFormat { | |
207 | public: | |
208 | /** | |
209 | * Construct a SimpleDateFormat using the default pattern for the default | |
210 | * locale. | |
211 | * <P> | |
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. | |
215 | * @stable ICU 2.0 | |
216 | */ | |
217 | SimpleDateFormat(UErrorCode& status); | |
218 | ||
219 | /** | |
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. | |
223 | * <P> | |
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. | |
228 | * @stable ICU 2.0 | |
229 | */ | |
230 | SimpleDateFormat(const UnicodeString& pattern, | |
231 | UErrorCode& status); | |
232 | ||
233 | /** | |
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. | |
237 | * <P> | |
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. | |
243 | * @stable ICU 2.0 | |
244 | */ | |
245 | SimpleDateFormat(const UnicodeString& pattern, | |
246 | const Locale& locale, | |
247 | UErrorCode& status); | |
248 | ||
249 | /** | |
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. | |
256 | * @stable ICU 2.0 | |
257 | */ | |
258 | SimpleDateFormat(const UnicodeString& pattern, | |
259 | DateFormatSymbols* formatDataToAdopt, | |
260 | UErrorCode& status); | |
261 | ||
262 | /** | |
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. | |
269 | * @stable ICU 2.0 | |
270 | */ | |
271 | SimpleDateFormat(const UnicodeString& pattern, | |
272 | const DateFormatSymbols& formatData, | |
273 | UErrorCode& status); | |
274 | ||
275 | /** | |
276 | * Copy constructor. | |
277 | * @stable ICU 2.0 | |
278 | */ | |
279 | SimpleDateFormat(const SimpleDateFormat&); | |
280 | ||
281 | /** | |
282 | * Assignment operator. | |
283 | * @stable ICU 2.0 | |
284 | */ | |
285 | SimpleDateFormat& operator=(const SimpleDateFormat&); | |
286 | ||
287 | /** | |
288 | * Destructor. | |
289 | * @stable ICU 2.0 | |
290 | */ | |
291 | virtual ~SimpleDateFormat(); | |
292 | ||
293 | /** | |
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. | |
297 | * @stable ICU 2.0 | |
298 | */ | |
299 | virtual Format* clone(void) const; | |
300 | ||
301 | /** | |
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. | |
306 | * @stable ICU 2.0 | |
307 | */ | |
308 | virtual UBool operator==(const Format& other) const; | |
309 | ||
310 | /** | |
311 | * Format a date or time, which is the standard millis since 24:00 GMT, Jan | |
312 | * 1, 1970. Overrides DateFormat pure virtual method. | |
313 | * <P> | |
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 | |
316 | * | |
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. | |
324 | * @stable ICU 2.1 | |
325 | */ | |
326 | virtual UnicodeString& format( Calendar& cal, | |
327 | UnicodeString& appendTo, | |
328 | FieldPosition& pos) const; | |
329 | ||
330 | /** | |
331 | * Format a date or time, which is the standard millis since 24:00 GMT, Jan | |
332 | * 1, 1970. Overrides DateFormat pure virtual method. | |
333 | * <P> | |
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 | |
336 | * | |
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 | |
340 | * instance of Date. | |
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. | |
347 | * @stable ICU 2.0 | |
348 | */ | |
349 | virtual UnicodeString& format( const Formattable& obj, | |
350 | UnicodeString& appendTo, | |
351 | FieldPosition& pos, | |
352 | UErrorCode& status) const; | |
353 | ||
354 | /** | |
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. | |
362 | * @stable ICU 2.1 | |
363 | */ | |
364 | UnicodeString& format(UDate date, | |
365 | UnicodeString& appendTo, | |
366 | FieldPosition& fieldPosition) const; | |
367 | ||
368 | /** | |
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. | |
375 | * @stable ICU 2.0 | |
376 | */ | |
377 | UnicodeString& format(const Formattable& obj, | |
378 | UnicodeString& appendTo, | |
379 | UErrorCode& status) const; | |
380 | ||
381 | /** | |
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. | |
387 | * @stable ICU 2.0 | |
388 | */ | |
389 | UnicodeString& format(UDate date, UnicodeString& appendTo) const; | |
390 | ||
391 | /** | |
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). | |
395 | * <P> | |
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). | |
400 | * | |
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. | |
408 | * @stable ICU 2.1 | |
409 | */ | |
410 | virtual void parse( const UnicodeString& text, | |
411 | Calendar& cal, | |
412 | ParsePosition& pos) const; | |
413 | ||
414 | /** | |
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). | |
418 | * <P> | |
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). | |
423 | * | |
424 | * @see DateFormat::setLenient(boolean) | |
425 | * | |
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. | |
431 | * @stable ICU 2.0 | |
432 | */ | |
433 | UDate parse( const UnicodeString& text, | |
434 | ParsePosition& pos) const; | |
435 | ||
436 | ||
437 | /** | |
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. | |
445 | * | |
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. | |
450 | * @stable ICU 2.0 | |
451 | */ | |
452 | virtual UDate parse( const UnicodeString& text, | |
453 | UErrorCode& status) const; | |
454 | ||
455 | /** | |
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. | |
463 | * <P> | |
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. | |
469 | * @stable ICU 2.0 | |
470 | */ | |
471 | virtual void set2DigitYearStart(UDate d, UErrorCode& status); | |
472 | ||
473 | /** | |
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. | |
481 | * <P> | |
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. | |
486 | * @stable ICU 2.0 | |
487 | */ | |
488 | UDate get2DigitYearStart(UErrorCode& status) const; | |
489 | ||
490 | /** | |
491 | * Return a pattern string describing this date format. | |
492 | * @param result Output param to receive the pattern. | |
493 | * @return A reference to 'result'. | |
494 | * @stable ICU 2.0 | |
495 | */ | |
496 | virtual UnicodeString& toPattern(UnicodeString& result) const; | |
497 | ||
498 | /** | |
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 | |
505 | * letters. | |
506 | * | |
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'. | |
512 | * @stable ICU 2.0 | |
513 | */ | |
514 | virtual UnicodeString& toLocalizedPattern(UnicodeString& result, | |
515 | UErrorCode& status) const; | |
516 | ||
517 | /** | |
518 | * Apply the given unlocalized pattern string to this date format. | |
519 | * (i.e., after this call, this formatter will format dates according to | |
520 | * the new pattern) | |
521 | * | |
522 | * @param pattern The pattern to be applied. | |
523 | * @stable ICU 2.0 | |
524 | */ | |
525 | virtual void applyPattern(const UnicodeString& pattern); | |
526 | ||
527 | /** | |
528 | * Apply the given localized pattern string to this date format. | |
529 | * (see toLocalizedPattern() for more information on localized patterns.) | |
530 | * | |
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. | |
535 | * @stable ICU 2.0 | |
536 | */ | |
537 | virtual void applyLocalizedPattern(const UnicodeString& pattern, | |
538 | UErrorCode& status); | |
539 | ||
540 | /** | |
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. | |
546 | * @stable ICU 2.0 | |
547 | */ | |
548 | virtual const DateFormatSymbols* getDateFormatSymbols(void) const; | |
549 | ||
550 | /** | |
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. | |
554 | * @stable ICU 2.0 | |
555 | */ | |
556 | virtual void adoptDateFormatSymbols(DateFormatSymbols* newFormatSymbols); | |
557 | ||
558 | /** | |
559 | * Set the date/time formatting data. | |
560 | * @param newFormatSymbols the given date-time formatting symbols to copy. | |
561 | * @stable ICU 2.0 | |
562 | */ | |
563 | virtual void setDateFormatSymbols(const DateFormatSymbols& newFormatSymbols); | |
564 | ||
565 | /** | |
566 | * Return the class ID for this class. This is useful only for comparing to | |
567 | * a return value from getDynamicClassID(). For example: | |
568 | * <pre> | |
569 | * . Base* polymorphic_pointer = createPolymorphicObject(); | |
570 | * . if (polymorphic_pointer->getDynamicClassID() == | |
571 | * . erived::getStaticClassID()) ... | |
572 | * </pre> | |
573 | * @return The class ID for all objects of this class. | |
574 | * @stable ICU 2.0 | |
575 | */ | |
374ca955 | 576 | static UClassID U_EXPORT2 getStaticClassID(void); |
b75a7d8f A |
577 | |
578 | /** | |
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. | |
583 | * | |
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. | |
587 | * @stable ICU 2.0 | |
588 | */ | |
589 | virtual UClassID getDynamicClassID(void) const; | |
590 | ||
591 | /** | |
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. | |
596 | * | |
597 | * @param calendarToAdopt Calendar object to be adopted. | |
598 | * @stable ICU 2.0 | |
599 | */ | |
600 | virtual void adoptCalendar(Calendar* calendarToAdopt); | |
601 | ||
602 | private: | |
b75a7d8f A |
603 | friend class DateFormat; |
604 | ||
605 | void initializeDefaultCentury(void); | |
606 | ||
607 | SimpleDateFormat(); // default constructor not implemented | |
608 | ||
609 | /** | |
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 | |
615 | * exit. | |
616 | */ | |
617 | SimpleDateFormat(EStyle timeStyle, EStyle dateStyle, const Locale& locale, UErrorCode& status); | |
618 | ||
619 | /** | |
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 | |
625 | * exit. | |
626 | */ | |
627 | SimpleDateFormat(const Locale& locale, UErrorCode& status); // Use default pattern | |
628 | ||
629 | /** | |
630 | * Called by format() to format a single field. | |
631 | * | |
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 | |
641 | * field. | |
642 | * @param status Receives a status code, which will be U_ZERO_ERROR if the operation | |
643 | * succeeds. | |
644 | */ | |
645 | void subFormat( UnicodeString &appendTo, | |
646 | UChar ch, | |
647 | int32_t count, | |
648 | FieldPosition& pos, | |
649 | Calendar& cal, | |
650 | UErrorCode& status) const; // in case of illegal argument | |
651 | ||
652 | /** | |
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. | |
657 | * | |
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 | |
663 | */ | |
664 | void zeroPaddingNumber( UnicodeString &appendTo, | |
665 | int32_t value, | |
666 | int32_t minDigits, | |
667 | int32_t maxDigits) const; | |
668 | ||
669 | /** | |
670 | * Return true if the given format character, occuring count | |
671 | * times, represents a numeric field. | |
672 | */ | |
673 | static UBool isNumeric(UChar formatChar, int32_t count); | |
674 | ||
675 | /** | |
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 | |
681 | */ | |
682 | Calendar *initializeCalendar(TimeZone* adoptZone, const Locale& locale, UErrorCode& status); | |
683 | ||
684 | /** | |
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 | |
689 | */ | |
690 | void initializeSymbols(const Locale& locale, Calendar* calendar, UErrorCode& status); | |
691 | ||
692 | /** | |
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 | |
699 | * resources fails. | |
700 | */ | |
701 | void construct(EStyle timeStyle, EStyle dateStyle, const Locale& locale, UErrorCode& status); | |
702 | ||
703 | /** | |
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. | |
708 | */ | |
709 | void initialize(const Locale& locale, UErrorCode& status); | |
710 | ||
711 | /** | |
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. | |
722 | */ | |
723 | int32_t matchString(const UnicodeString& text, int32_t start, UCalendarDateFields field, | |
724 | const UnicodeString* stringArray, int32_t stringArrayCount, Calendar& cal) const; | |
725 | ||
73c04bcf A |
726 | /** |
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. | |
737 | */ | |
738 | int32_t matchQuarterString(const UnicodeString& text, int32_t start, UCalendarDateFields field, | |
739 | const UnicodeString* stringArray, int32_t stringArrayCount, Calendar& cal) const; | |
740 | ||
b75a7d8f A |
741 | /** |
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. | |
754 | */ | |
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; | |
757 | ||
758 | void parseInt(const UnicodeString& text, | |
759 | Formattable& number, | |
760 | ParsePosition& pos, | |
761 | UBool allowNegative) const; | |
762 | ||
763 | /** | |
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. | |
374ca955 | 772 | * @param status Receives a status code, which will be U_ZERO_ERROR |
b75a7d8f A |
773 | * if the operation succeeds. |
774 | */ | |
775 | static void translatePattern(const UnicodeString& originalPattern, | |
776 | UnicodeString& translatedPattern, | |
777 | const UnicodeString& from, | |
778 | const UnicodeString& to, | |
779 | UErrorCode& status); | |
780 | ||
781 | /** | |
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 | |
374ca955 | 785 | * @param status Receives a status code, which will be U_ZERO_ERROR |
b75a7d8f A |
786 | * if the operation succeeds. |
787 | */ | |
788 | void parseAmbiguousDatesAsAfter(UDate startDate, UErrorCode& status); | |
789 | ||
73c04bcf A |
790 | /** |
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. | |
793 | * | |
794 | int32_t matchZoneString(const UnicodeString& text, int32_t start, int32_t zi) const; | |
795 | */ | |
796 | ||
797 | /** | |
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 | |
800 | * as appropriate. | |
801 | */ | |
802 | int32_t subParseZoneString(const UnicodeString& text, int32_t start, Calendar& cal, UErrorCode& status) const; | |
803 | ||
804 | /** | |
805 | * append the gmt string | |
806 | */ | |
807 | inline void appendGMT(UnicodeString &appendTo, Calendar& cal, UErrorCode& status) const; | |
808 | ||
b75a7d8f A |
809 | /** |
810 | * Used to map pattern characters to Calendar field identifiers. | |
811 | */ | |
812 | static const UCalendarDateFields fgPatternIndexToCalendarField[]; | |
813 | ||
814 | /** | |
815 | * Map index into pattern character string to DateFormat field number | |
816 | */ | |
374ca955 | 817 | static const UDateFormatField fgPatternIndexToDateFormatField[]; |
b75a7d8f A |
818 | |
819 | /** | |
820 | * The formatting pattern for this formatter. | |
821 | */ | |
822 | UnicodeString fPattern; | |
823 | ||
824 | /** | |
825 | * The original locale used (for reloading symbols) | |
826 | */ | |
827 | Locale fLocale; | |
828 | ||
829 | /** | |
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.) | |
832 | */ | |
833 | DateFormatSymbols* fSymbols; // Owned | |
834 | ||
835 | /** | |
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(). | |
842 | */ | |
843 | UDate fDefaultCenturyStart; | |
844 | ||
845 | /** | |
846 | * See documentation for defaultCenturyStart. | |
847 | */ | |
848 | /*transient*/ int32_t fDefaultCenturyStartYear; | |
b75a7d8f | 849 | |
73c04bcf A |
850 | /*transient*/ TimeZone* parsedTimeZone; // here to avoid api change |
851 | ||
374ca955 | 852 | UBool fHaveDefaultCentury; |
b75a7d8f A |
853 | }; |
854 | ||
b75a7d8f A |
855 | inline UDate |
856 | SimpleDateFormat::get2DigitYearStart(UErrorCode& /*status*/) const | |
857 | { | |
858 | return fDefaultCenturyStart; | |
859 | } | |
860 | ||
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); | |
868 | } | |
869 | ||
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); | |
877 | } | |
878 | ||
879 | inline UnicodeString& | |
880 | SimpleDateFormat::format(UDate date, UnicodeString& appendTo) const { | |
881 | return DateFormat::format(date, appendTo); | |
882 | } | |
883 | ||
884 | U_NAMESPACE_END | |
885 | ||
886 | #endif /* #if !UCONFIG_NO_FORMATTING */ | |
887 | ||
888 | #endif // _SMPDTFMT | |
889 | //eof |