Coverage Report

Created: 2025-09-05 07:16

/src/icu/icu4c/source/i18n/unicode/smpdtfmt.h
Line
Count
Source (jump to first uncovered line)
1
// © 2016 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
/*
4
* Copyright (C) 1997-2016, International Business Machines Corporation and
5
* others. All Rights Reserved.
6
*******************************************************************************
7
*
8
* File SMPDTFMT.H
9
*
10
* Modification History:
11
*
12
*   Date        Name        Description
13
*   02/19/97    aliu        Converted from java.
14
*   07/09/97    helena      Make ParsePosition into a class.
15
*   07/21/98    stephen     Added GMT_PLUS, GMT_MINUS
16
*                            Changed setTwoDigitStartDate to set2DigitYearStart
17
*                            Changed getTwoDigitStartDate to get2DigitYearStart
18
*                            Removed subParseLong
19
*                            Removed getZoneIndex (added in DateFormatSymbols)
20
*   06/14/99    stephen     Removed fgTimeZoneDataSuffix
21
*   10/14/99    aliu        Updated class doc to describe 2-digit year parsing
22
*                           {j28 4182066}.
23
*******************************************************************************
24
*/
25
26
#ifndef SMPDTFMT_H
27
#define SMPDTFMT_H
28
29
#include "unicode/utypes.h"
30
31
#if U_SHOW_CPLUSPLUS_API
32
33
/**
34
 * \file
35
 * \brief C++ API: Format and parse dates in a language-independent manner.
36
 */
37
38
#if !UCONFIG_NO_FORMATTING
39
40
#include "unicode/datefmt.h"
41
#include "unicode/udisplaycontext.h"
42
#include "unicode/tzfmt.h"  /* for UTimeZoneFormatTimeType */
43
#include "unicode/brkiter.h"
44
45
U_NAMESPACE_BEGIN
46
47
class DateFormatSymbols;
48
class DateFormat;
49
class MessageFormat;
50
class FieldPositionHandler;
51
class TimeZoneFormat;
52
class SharedNumberFormat;
53
class SimpleDateFormatMutableNFs;
54
class DateIntervalFormat;
55
56
namespace number {
57
class LocalizedNumberFormatter;
58
class SimpleNumberFormatter;
59
}
60
61
/**
62
 *
63
 * SimpleDateFormat is a concrete class for formatting and parsing dates in a
64
 * language-independent manner. It allows for formatting (millis -> text),
65
 * parsing (text -> millis), and normalization. Formats/Parses a date or time,
66
 * which is the standard milliseconds since 24:00 GMT, Jan 1, 1970.
67
 * <P>
68
 * Clients are encouraged to create a date-time formatter using DateFormat::getInstance(),
69
 * getDateInstance(), getDateInstance(), or getDateTimeInstance() rather than
70
 * explicitly constructing an instance of SimpleDateFormat.  This way, the client
71
 * is guaranteed to get an appropriate formatting pattern for whatever locale the
72
 * program is running in.  If the client needs more control, they should consider using
73
 * DateFormat::createInstanceForSkeleton().  However, if the client needs something more unusual than
74
 * the default patterns in the locales, he can construct a SimpleDateFormat directly
75
 * and give it an appropriate pattern (or use one of the factory methods on DateFormat
76
 * and modify the pattern after the fact with toPattern() and applyPattern().
77
 *
78
 * <p><strong>Date and Time Patterns:</strong></p>
79
 *
80
 * <p>Date and time formats are specified by <em>date and time pattern</em> strings.
81
 * The full syntax for date and time patterns can be found at
82
 * <a href="https://unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns">https://unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns</a>.
83
 *
84
 * Within date and time pattern strings, all unquoted ASCII letters [A-Za-z] are reserved
85
 * as pattern letters representing calendar fields. Some of the most commonly used pattern letters are:</p>
86
 *
87
 * <table border="1">
88
 *     <tr>
89
 *         <th style="text-align: center">Sym.</th>
90
 *         <th style="text-align: center">No.</th>
91
 *         <th>Example</th>
92
 *         <th>Description</th>
93
 *     </tr>
94
 *     <tr>
95
 *         <td style="text-align: center">G</td>
96
 *         <td style="text-align: center">1..3</td>
97
 *         <td>AD</td>
98
 *         <td>Era - Replaced with the Era string for the current date. One to three letters for the
99
 *         abbreviated form, four letters for the long (wide) form, five for the narrow form.</td>
100
 *     </tr>
101
 *     <tr>
102
 *         <td style="text-align: center">y</td>
103
 *         <td style="text-align: center">1..n</td>
104
 *         <td>1996</td>
105
 *         <td>Year. Normally the length specifies the padding, but for two letters it also specifies the maximum
106
 *         length. Example:<div style="text-align: center">
107
 *             <center>
108
 *             <table border="1" cellpadding="2" cellspacing="0">
109
 *                 <tr>
110
 *                     <th>Year</th>
111
 *                     <th style="text-align: right">y</th>
112
 *                     <th style="text-align: right">yy</th>
113
 *                     <th style="text-align: right">yyy</th>
114
 *                     <th style="text-align: right">yyyy</th>
115
 *                     <th style="text-align: right">yyyyy</th>
116
 *                 </tr>
117
 *                 <tr>
118
 *                     <td>AD 1</td>
119
 *                     <td style="text-align: right">1</td>
120
 *                     <td style="text-align: right">01</td>
121
 *                     <td style="text-align: right">001</td>
122
 *                     <td style="text-align: right">0001</td>
123
 *                     <td style="text-align: right">00001</td>
124
 *                 </tr>
125
 *                 <tr>
126
 *                     <td>AD 12</td>
127
 *                     <td style="text-align: right">12</td>
128
 *                     <td style="text-align: right">12</td>
129
 *                     <td style="text-align: right">012</td>
130
 *                     <td style="text-align: right">0012</td>
131
 *                     <td style="text-align: right">00012</td>
132
 *                 </tr>
133
 *                 <tr>
134
 *                     <td>AD 123</td>
135
 *                     <td style="text-align: right">123</td>
136
 *                     <td style="text-align: right">23</td>
137
 *                     <td style="text-align: right">123</td>
138
 *                     <td style="text-align: right">0123</td>
139
 *                     <td style="text-align: right">00123</td>
140
 *                 </tr>
141
 *                 <tr>
142
 *                     <td>AD 1234</td>
143
 *                     <td style="text-align: right">1234</td>
144
 *                     <td style="text-align: right">34</td>
145
 *                     <td style="text-align: right">1234</td>
146
 *                     <td style="text-align: right">1234</td>
147
 *                     <td style="text-align: right">01234</td>
148
 *                 </tr>
149
 *                 <tr>
150
 *                     <td>AD 12345</td>
151
 *                     <td style="text-align: right">12345</td>
152
 *                     <td style="text-align: right">45</td>
153
 *                     <td style="text-align: right">12345</td>
154
 *                     <td style="text-align: right">12345</td>
155
 *                     <td style="text-align: right">12345</td>
156
 *                 </tr>
157
 *             </table>
158
 *             </center></div>
159
 *         </td>
160
 *     </tr>
161
 *     <tr>
162
 *         <td rowspan="3" style="text-align: center">Q</td>
163
 *         <td style="text-align: center">1..2</td>
164
 *         <td>02</td>
165
 *         <td rowspan="3">Quarter - Use one or two for the numerical quarter, three for the abbreviation, or four
166
 *         for the full (wide) name (five for the narrow name is not yet supported).</td>
167
 *     </tr>
168
 *     <tr>
169
 *         <td style="text-align: center">3</td>
170
 *         <td>Q2</td>
171
 *     </tr>
172
 *     <tr>
173
 *         <td style="text-align: center">4</td>
174
 *         <td>2nd quarter</td>
175
 *     </tr>
176
 *     <tr>
177
 *         <td rowspan="4" style="text-align: center">M</td>
178
 *         <td style="text-align: center">1..2</td>
179
 *         <td>09</td>
180
 *         <td rowspan="4">Month - Use one or two for the numerical month, three for the abbreviation, four for
181
 *         the full (wide) name, or five for the narrow name. With two ("MM"), the month number is zero-padded
182
 *         if necessary (e.g. "08").</td>
183
 *     </tr>
184
 *     <tr>
185
 *         <td style="text-align: center">3</td>
186
 *         <td>Sep</td>
187
 *     </tr>
188
 *     <tr>
189
 *         <td style="text-align: center">4</td>
190
 *         <td>September</td>
191
 *     </tr>
192
 *     <tr>
193
 *         <td style="text-align: center">5</td>
194
 *         <td>S</td>
195
 *     </tr>
196
 *     <tr>
197
 *         <td style="text-align: center">d</td>
198
 *         <td style="text-align: center">1..2</td>
199
 *         <td>1</td>
200
 *         <td>Date - Day of the month. Use "d" to show the minimum number of digits, or "dd" to always show
201
 *         two digits (zero-padding if necessary, e.g. "08").</td>
202
 *     </tr>
203
 *     <tr>
204
 *         <td rowspan="4" style="text-align: center">E</td>
205
 *         <td style="text-align: center">1..3</td>
206
 *         <td>Tue</td>
207
 *         <td rowspan="4">Day of week - Use one through three letters for the short day, four for the full (wide) name,
208
 *         five for the narrow name, or six for the short name.</td>
209
 *     </tr>
210
 *     <tr>
211
 *         <td style="text-align: center">4</td>
212
 *         <td>Tuesday</td>
213
 *     </tr>
214
 *     <tr>
215
 *         <td style="text-align: center">5</td>
216
 *         <td>T</td>
217
 *     </tr>
218
 *     <tr>
219
 *         <td style="text-align: center">6</td>
220
 *         <td>Tu</td>
221
 *     </tr>
222
 *     <tr>
223
 *         <td style="text-align: center">a</td>
224
 *         <td style="text-align: center">1</td>
225
 *         <td>AM</td>
226
 *         <td>AM or PM</td>
227
 *     </tr>
228
 *     <tr>
229
 *         <td style="text-align: center">h</td>
230
 *         <td style="text-align: center">1..2</td>
231
 *         <td>11</td>
232
 *         <td>Hour [1-12]. When used in skeleton data or in a skeleton passed in an API for flexible data pattern
233
 *         generation, it should match the 12-hour-cycle format preferred by the locale (h or K); it should not match
234
 *         a 24-hour-cycle format (H or k). Use hh for zero padding.</td>
235
 *     </tr>
236
 *     <tr>
237
 *         <td style="text-align: center">H</td>
238
 *         <td style="text-align: center">1..2</td>
239
 *         <td>13</td>
240
 *         <td>Hour [0-23]. When used in skeleton data or in a skeleton passed in an API for flexible data pattern
241
 *         generation, it should match the 24-hour-cycle format preferred by the locale (H or k); it should not match a
242
 *         12-hour-cycle format (h or K). Use HH for zero padding.</td>
243
 *     </tr>
244
 *     <tr>
245
 *         <td style="text-align: center">m</td>
246
 *         <td style="text-align: center">1..2</td>
247
 *         <td>59</td>
248
 *         <td>Minute. Use "m" to show the minimum number of digits, or "mm" to always show two digits
249
 *         (zero-padding if necessary, e.g. "08")..</td>
250
 *     </tr>
251
 *     <tr>
252
 *         <td style="text-align: center">s</td>
253
 *         <td style="text-align: center">1..2</td>
254
 *         <td>12</td>
255
 *         <td>Second. Use "s" to show the minimum number of digits, or "ss" to always show two digits
256
 *         (zero-padding if necessary, e.g. "08").</td>
257
 *     </tr>
258
 *     <tr>
259
 *         <td rowspan="2" style="text-align: center">z</td>
260
 *         <td style="text-align: center">1..3</td>
261
 *         <td>PDT</td>
262
 *         <td>Time zone.  The <i>short specific non-location format</i>.
263
 *         Where that is unavailable, falls back to the <i>short localized GMT format</i> ("O").</td>
264
 *     </tr>
265
 *     <tr>
266
 *         <td style="text-align: center">4</td>
267
 *         <td>Pacific Daylight Time</td>
268
 *         <td>The <i>long specific non-location format</i>.
269
 *         Where that is unavailable, falls back to the <i>long localized GMT format</i> ("OOOO").</td>
270
 *     </tr>
271
 *     <tr>
272
 *         <td rowspan="2" style="text-align: center">v</td>
273
 *         <td style="text-align: center">1</td>
274
 *         <td>PT</td>
275
 *         <td>Time zone. The <i>short generic non-location format</i>.
276
 *         Where that is unavailable, falls back to the <i>generic location format</i> ("VVVV"),
277
 *         then the <i>short localized GMT format</i> as the final fallback.</td>
278
 *     </tr>
279
 *     <tr>
280
 *         <td style="text-align: center">4</td>
281
 *         <td>Pacific Time</td>
282
 *         <td>The <i>long generic non-location format</i>.
283
 *         Where that is unavailable, falls back to <i>generic location format</i> ("VVVV").
284
 *     </tr>
285
 * </table>
286
 *
287
 * <P>
288
 * Any characters in the pattern that are not in the ranges of ['a'..'z'] and
289
 * ['A'..'Z'] will be treated as quoted text. For instance, characters
290
 * like ':', '.', ' ', '#' and '@' will appear in the resulting time text
291
 * even they are not embraced within single quotes.
292
 * <P>
293
 * A pattern containing any invalid pattern letter will result in a failing
294
 * UErrorCode result during formatting or parsing.
295
 * <P>
296
 * Examples using the US locale:
297
 * <pre>
298
 * \code
299
 *    Format Pattern                         Result
300
 *    --------------                         -------
301
 *    "yyyy.MM.dd G 'at' HH:mm:ss vvvv" ->>  1996.07.10 AD at 15:08:56 Pacific Time
302
 *    "EEE, MMM d, ''yy"                ->>  Wed, July 10, '96
303
 *    "h:mm a"                          ->>  12:08 PM
304
 *    "hh 'o''clock' a, zzzz"           ->>  12 o'clock PM, Pacific Daylight Time
305
 *    "K:mm a, vvv"                     ->>  0:00 PM, PT
306
 *    "yyyyy.MMMMM.dd GGG hh:mm aaa"    ->>  1996.July.10 AD 12:08 PM
307
 * \endcode
308
 * </pre>
309
 * Code Sample:
310
 * <pre>
311
 * \code
312
 *     UErrorCode success = U_ZERO_ERROR;
313
 *     SimpleTimeZone* pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, "PST");
314
 *     pdt->setStartRule( Calendar::APRIL, 1, Calendar::SUNDAY, 2*60*60*1000);
315
 *     pdt->setEndRule( Calendar::OCTOBER, -1, Calendar::SUNDAY, 2*60*60*1000);
316
 *
317
 *     // Format the current time.
318
 *     SimpleDateFormat* formatter
319
 *         = new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss a zzz", success );
320
 *     GregorianCalendar cal(success);
321
 *     UDate currentTime_1 = cal.getTime(success);
322
 *     FieldPosition fp(FieldPosition::DONT_CARE);
323
 *     UnicodeString dateString;
324
 *     formatter->format( currentTime_1, dateString, fp );
325
 *     cout << "result: " << dateString << endl;
326
 *
327
 *     // Parse the previous string back into a Date.
328
 *     ParsePosition pp(0);
329
 *     UDate currentTime_2 = formatter->parse(dateString, pp );
330
 * \endcode
331
 * </pre>
332
 * In the above example, the time value "currentTime_2" obtained from parsing
333
 * will be equal to currentTime_1. However, they may not be equal if the am/pm
334
 * marker 'a' is left out from the format pattern while the "hour in am/pm"
335
 * pattern symbol is used. This information loss can happen when formatting the
336
 * time in PM.
337
 *
338
 * <p>
339
 * When parsing a date string using the abbreviated year pattern ("y" or "yy"),
340
 * SimpleDateFormat must interpret the abbreviated year
341
 * relative to some century.  It does this by adjusting dates to be
342
 * within 80 years before and 20 years after the time the SimpleDateFormat
343
 * instance is created. For example, using a pattern of "MM/dd/yy" and a
344
 * SimpleDateFormat instance created on Jan 1, 1997,  the string
345
 * "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64"
346
 * would be interpreted as May 4, 1964.
347
 * During parsing, only strings consisting of exactly two digits, as defined by
348
 * <code>Unicode::isDigit()</code>, will be parsed into the default century.
349
 * Any other numeric string, such as a one digit string, a three or more digit
350
 * string, or a two digit string that isn't all digits (for example, "-1"), is
351
 * interpreted literally.  So "01/02/3" or "01/02/003" are parsed (for the
352
 * Gregorian calendar), using the same pattern, as Jan 2, 3 AD.  Likewise (but
353
 * only in lenient parse mode, the default) "01/02/-3" is parsed as Jan 2, 4 BC.
354
 *
355
 * <p>
356
 * If the year pattern has more than two 'y' characters, the year is
357
 * interpreted literally, regardless of the number of digits.  So using the
358
 * pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D.
359
 *
360
 * <p>
361
 * When numeric fields abut one another directly, with no intervening delimiter
362
 * characters, they constitute a run of abutting numeric fields.  Such runs are
363
 * parsed specially.  For example, the format "HHmmss" parses the input text
364
 * "123456" to 12:34:56, parses the input text "12345" to 1:23:45, and fails to
365
 * parse "1234".  In other words, the leftmost field of the run is flexible,
366
 * while the others keep a fixed width.  If the parse fails anywhere in the run,
367
 * then the leftmost field is shortened by one character, and the entire run is
368
 * parsed again. This is repeated until either the parse succeeds or the
369
 * leftmost field is one character in length.  If the parse still fails at that
370
 * point, the parse of the run fails.
371
 *
372
 * <P>
373
 * For time zones that have no names, SimpleDateFormat uses strings GMT+hours:minutes or
374
 * GMT-hours:minutes.
375
 * <P>
376
 * The calendar defines what is the first day of the week, the first week of the
377
 * year, whether hours are zero based or not (0 vs 12 or 24), and the timezone.
378
 * There is one common number format to handle all the numbers; the digit count
379
 * is handled programmatically according to the pattern.
380
 *
381
 * <p><em>User subclasses are not supported.</em> While clients may write
382
 * subclasses, such code will not necessarily work and will not be
383
 * guaranteed to work stably from release to release.
384
 */
385
class U_I18N_API_CLASS SimpleDateFormat: public DateFormat {
386
public:
387
    /**
388
     * Construct a SimpleDateFormat using the default pattern for the default
389
     * locale.
390
     * <P>
391
     * [Note:] Not all locales support SimpleDateFormat; for full generality,
392
     * use the factory methods in the DateFormat class.
393
     * @param status    Output param set to success/failure code.
394
     * @stable ICU 2.0
395
     */
396
    U_I18N_API SimpleDateFormat(UErrorCode& status);
397
398
    /**
399
     * Construct a SimpleDateFormat using the given pattern and the default locale.
400
     * The locale is used to obtain the symbols used in formatting (e.g., the
401
     * names of the months), but not to provide the pattern.
402
     * <P>
403
     * [Note:] Not all locales support SimpleDateFormat; for full generality,
404
     * use the factory methods in the DateFormat class.
405
     * @param pattern    the pattern for the format.
406
     * @param status     Output param set to success/failure code.
407
     * @stable ICU 2.0
408
     */
409
    U_I18N_API SimpleDateFormat(const UnicodeString& pattern,
410
                     UErrorCode& status);
411
412
    /**
413
     * Construct a SimpleDateFormat using the given pattern, numbering system override, and the default locale.
414
     * The locale is used to obtain the symbols used in formatting (e.g., the
415
     * names of the months), but not to provide the pattern.
416
     * <P>
417
     * A numbering system override is a string containing either the name of a known numbering system,
418
     * or a set of field and numbering system pairs that specify which fields are to be formatted with
419
     * the alternate numbering system.  For example, to specify that all numeric fields in the specified
420
     * date or time pattern are to be rendered using Thai digits, simply specify the numbering system override
421
     * as "thai".  To specify that just the year portion of the date be formatted using Hebrew numbering,
422
     * use the override string "y=hebrew".  Numbering system overrides can be combined using a semi-colon
423
     * character in the override string, such as "d=decimal;M=arabic;y=hebrew", etc.
424
     *
425
     * <P>
426
     * [Note:] Not all locales support SimpleDateFormat; for full generality,
427
     * use the factory methods in the DateFormat class.
428
     * @param pattern    the pattern for the format.
429
     * @param override   the override string.
430
     * @param status     Output param set to success/failure code.
431
     * @stable ICU 4.2
432
     */
433
    U_I18N_API SimpleDateFormat(const UnicodeString& pattern,
434
                                const UnicodeString& override,
435
                                UErrorCode& status);
436
437
    /**
438
     * Construct a SimpleDateFormat using the given pattern and locale.
439
     * The locale is used to obtain the symbols used in formatting (e.g., the
440
     * names of the months), but not to provide the pattern.
441
     * <P>
442
     * [Note:] Not all locales support SimpleDateFormat; for full generality,
443
     * use the factory methods in the DateFormat class.
444
     * @param pattern    the pattern for the format.
445
     * @param locale     the given locale.
446
     * @param status     Output param set to success/failure code.
447
     * @stable ICU 2.0
448
     */
449
    U_I18N_API SimpleDateFormat(const UnicodeString& pattern, const Locale& locale, UErrorCode& status);
450
451
    /**
452
     * Construct a SimpleDateFormat using the given pattern, numbering system override, and locale.
453
     * The locale is used to obtain the symbols used in formatting (e.g., the
454
     * names of the months), but not to provide the pattern.
455
     * <P>
456
     * A numbering system override is a string containing either the name of a known numbering system,
457
     * or a set of field and numbering system pairs that specify which fields are to be formatted with
458
     * the alternate numbering system.  For example, to specify that all numeric fields in the specified
459
     * date or time pattern are to be rendered using Thai digits, simply specify the numbering system override
460
     * as "thai".  To specify that just the year portion of the date be formatted using Hebrew numbering,
461
     * use the override string "y=hebrew".  Numbering system overrides can be combined using a semi-colon
462
     * character in the override string, such as "d=decimal;M=arabic;y=hebrew", etc.
463
     * <P>
464
     * [Note:] Not all locales support SimpleDateFormat; for full generality,
465
     * use the factory methods in the DateFormat class.
466
     * @param pattern    the pattern for the format.
467
     * @param override   the numbering system override.
468
     * @param locale     the given locale.
469
     * @param status     Output param set to success/failure code.
470
     * @stable ICU 4.2
471
     */
472
    U_I18N_API SimpleDateFormat(const UnicodeString& pattern,
473
                                const UnicodeString& override,
474
                                const Locale& locale,
475
                                UErrorCode& status);
476
477
    /**
478
     * Construct a SimpleDateFormat using the given pattern and locale-specific
479
     * symbol data.  The formatter takes ownership of the DateFormatSymbols object;
480
     * the caller is no longer responsible for deleting it.
481
     * @param pattern           the given pattern for the format.
482
     * @param formatDataToAdopt the symbols to be adopted.
483
     * @param status            Output param set to success/faulure code.
484
     * @stable ICU 2.0
485
     */
486
    U_I18N_API SimpleDateFormat(const UnicodeString& pattern,
487
                                DateFormatSymbols* formatDataToAdopt,
488
                                UErrorCode& status);
489
490
    /**
491
     * Construct a SimpleDateFormat using the given pattern and locale-specific
492
     * symbol data.  The DateFormatSymbols object is NOT adopted; the caller
493
     * remains responsible for deleting it.
494
     * @param pattern           the given pattern for the format.
495
     * @param formatData        the formatting symbols to be use.
496
     * @param status            Output param set to success/faulure code.
497
     * @stable ICU 2.0
498
     */
499
    U_I18N_API SimpleDateFormat(const UnicodeString& pattern,
500
                                const DateFormatSymbols& formatData,
501
                                UErrorCode& status);
502
503
    /**
504
     * Copy constructor.
505
     * @stable ICU 2.0
506
     */
507
    U_I18N_API SimpleDateFormat(const SimpleDateFormat&);
508
509
    /**
510
     * Assignment operator.
511
     * @stable ICU 2.0
512
     */
513
    U_I18N_API SimpleDateFormat& operator=(const SimpleDateFormat&);
514
515
    /**
516
     * Destructor.
517
     * @stable ICU 2.0
518
     */
519
    U_I18N_API virtual ~SimpleDateFormat();
520
521
    /**
522
     * Clone this Format object polymorphically. The caller owns the result and
523
     * should delete it when done.
524
     * @return    A copy of the object.
525
     * @stable ICU 2.0
526
     */
527
    U_I18N_API virtual SimpleDateFormat* clone() const override;
528
529
    /**
530
     * Return true if the given Format objects are semantically equal. Objects
531
     * of different subclasses are considered unequal.
532
     * @param other    the object to be compared with.
533
     * @return         true if the given Format objects are semantically equal.
534
     * @stable ICU 2.0
535
     */
536
    U_I18N_API virtual bool operator==(const Format& other) const override;
537
538
    using DateFormat::format;
539
540
    /**
541
     * Format a date or time, which is the standard millis since 24:00 GMT, Jan
542
     * 1, 1970. Overrides DateFormat pure virtual method.
543
     * <P>
544
     * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->>
545
     * 1996.07.10 AD at 15:08:56 PDT
546
     *
547
     * @param cal       Calendar set to the date and time to be formatted
548
     *                  into a date/time string.
549
     * @param appendTo  Output parameter to receive result.
550
     *                  Result is appended to existing contents.
551
     * @param pos       The formatting position. On input: an alignment field,
552
     *                  if desired. On output: the offsets of the alignment field.
553
     * @return          Reference to 'appendTo' parameter.
554
     * @stable ICU 2.1
555
     */
556
    U_I18N_API virtual UnicodeString& format(Calendar& cal,
557
                                             UnicodeString& appendTo,
558
                                             FieldPosition& pos) const override;
559
560
    /**
561
     * Format a date or time, which is the standard millis since 24:00 GMT, Jan
562
     * 1, 1970. Overrides DateFormat pure virtual method.
563
     * <P>
564
     * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->>
565
     * 1996.07.10 AD at 15:08:56 PDT
566
     *
567
     * @param cal       Calendar set to the date and time to be formatted
568
     *                  into a date/time string.
569
     * @param appendTo  Output parameter to receive result.
570
     *                  Result is appended to existing contents.
571
     * @param posIter   On return, can be used to iterate over positions
572
     *                  of fields generated by this format call.  Field values
573
     *                  are defined in UDateFormatField.
574
     * @param status    Input/output param set to success/failure code.
575
     * @return          Reference to 'appendTo' parameter.
576
     * @stable ICU 4.4
577
     */
578
    U_I18N_API virtual UnicodeString& format(Calendar& cal,
579
                                             UnicodeString& appendTo,
580
                                             FieldPositionIterator* posIter,
581
                                             UErrorCode& status) const override;
582
583
    using DateFormat::parse;
584
585
    /**
586
     * Parse a date/time string beginning at the given parse position. For
587
     * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
588
     * that is equivalent to Date(837039928046).
589
     * <P>
590
     * By default, parsing is lenient: If the input is not in the form used by
591
     * this object's format method but can still be parsed as a date, then the
592
     * parse succeeds. Clients may insist on strict adherence to the format by
593
     * calling setLenient(false).
594
     * @see DateFormat::setLenient(boolean)
595
     *
596
     * @param text  The date/time string to be parsed
597
     * @param cal   A Calendar set on input to the date and time to be used for
598
     *              missing values in the date/time string being parsed, and set
599
     *              on output to the parsed date/time. When the calendar type is
600
     *              different from the internal calendar held by this SimpleDateFormat
601
     *              instance, the internal calendar will be cloned to a work
602
     *              calendar set to the same milliseconds and time zone as the
603
     *              cal parameter, field values will be parsed based on the work
604
     *              calendar, then the result (milliseconds and time zone) will
605
     *              be set in this calendar.
606
     * @param pos   On input, the position at which to start parsing; on
607
     *              output, the position at which parsing terminated, or the
608
     *              start position if the parse failed.
609
     * @stable ICU 2.1
610
     */
611
    U_I18N_API virtual void parse(const UnicodeString& text,
612
                                  Calendar& cal,
613
                                  ParsePosition& pos) const override;
614
615
    /**
616
     * Set the start UDate used to interpret two-digit year strings.
617
     * When dates are parsed having 2-digit year strings, they are placed within
618
     * a assumed range of 100 years starting on the two digit start date.  For
619
     * example, the string "24-Jan-17" may be in the year 1817, 1917, 2017, or
620
     * some other year.  SimpleDateFormat chooses a year so that the resultant
621
     * date is on or after the two digit start date and within 100 years of the
622
     * two digit start date.
623
     * <P>
624
     * By default, the two digit start date is set to 80 years before the current
625
     * time at which a SimpleDateFormat object is created.
626
     * @param d      start UDate used to interpret two-digit year strings.
627
     * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with
628
     *               an error value if there was a parse error.
629
     * @stable ICU 2.0
630
     */
631
    U_I18N_API virtual void set2DigitYearStart(UDate d, UErrorCode& status);
632
633
    /**
634
     * Get the start UDate used to interpret two-digit year strings.
635
     * When dates are parsed having 2-digit year strings, they are placed within
636
     * a assumed range of 100 years starting on the two digit start date.  For
637
     * example, the string "24-Jan-17" may be in the year 1817, 1917, 2017, or
638
     * some other year.  SimpleDateFormat chooses a year so that the resultant
639
     * date is on or after the two digit start date and within 100 years of the
640
     * two digit start date.
641
     * <P>
642
     * By default, the two digit start date is set to 80 years before the current
643
     * time at which a SimpleDateFormat object is created.
644
     * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with
645
     *               an error value if there was a parse error.
646
     * @stable ICU 2.0
647
     */
648
    U_I18N_API UDate get2DigitYearStart(UErrorCode& status) const;
649
650
    /**
651
     * Return a pattern string describing this date format.
652
     * @param result Output param to receive the pattern.
653
     * @return       A reference to 'result'.
654
     * @stable ICU 2.0
655
     */
656
    U_I18N_API virtual UnicodeString& toPattern(UnicodeString& result) const;
657
658
    /**
659
     * Return a localized pattern string describing this date format.
660
     * In most cases, this will return the same thing as toPattern(),
661
     * but a locale can specify characters to use in pattern descriptions
662
     * in place of the ones described in this class's class documentation.
663
     * (Presumably, letters that would be more mnemonic in that locale's
664
     * language.)  This function would produce a pattern using those
665
     * letters.
666
     * <p>
667
     * <b>Note:</b> This implementation depends on DateFormatSymbols::getLocalPatternChars()
668
     * to get localized format pattern characters. ICU does not include
669
     * localized pattern character data, therefore, unless user sets localized
670
     * pattern characters manually, this method returns the same result as
671
     * toPattern().
672
     *
673
     * @param result    Receives the localized pattern.
674
     * @param status    Output param set to success/failure code on
675
     *                  exit. If the pattern is invalid, this will be
676
     *                  set to a failure result.
677
     * @return          A reference to 'result'.
678
     * @stable ICU 2.0
679
     */
680
    U_I18N_API virtual UnicodeString& toLocalizedPattern(UnicodeString& result,
681
                                                         UErrorCode& status) const;
682
683
    /**
684
     * Apply the given unlocalized pattern string to this date format.
685
     * (i.e., after this call, this formatter will format dates according to
686
     * the new pattern)
687
     *
688
     * @param pattern   The pattern to be applied.
689
     * @stable ICU 2.0
690
     */
691
    U_I18N_API virtual void applyPattern(const UnicodeString& pattern);
692
693
    /**
694
     * Apply the given localized pattern string to this date format.
695
     * (see toLocalizedPattern() for more information on localized patterns.)
696
     *
697
     * @param pattern   The localized pattern to be applied.
698
     * @param status    Output param set to success/failure code on
699
     *                  exit. If the pattern is invalid, this will be
700
     *                  set to a failure result.
701
     * @stable ICU 2.0
702
     */
703
    U_I18N_API virtual void applyLocalizedPattern(const UnicodeString& pattern, UErrorCode& status);
704
705
    /**
706
     * Gets the date/time formatting symbols (this is an object carrying
707
     * the various strings and other symbols used in formatting: e.g., month
708
     * names and abbreviations, time zone names, AM/PM strings, etc.)
709
     * @return a copy of the date-time formatting data associated
710
     * with this date-time formatter.
711
     * @stable ICU 2.0
712
     */
713
    U_I18N_API virtual const DateFormatSymbols* getDateFormatSymbols() const;
714
715
    /**
716
     * Set the date/time formatting symbols.  The caller no longer owns the
717
     * DateFormatSymbols object and should not delete it after making this call.
718
     * @param newFormatSymbols the given date-time formatting symbols to copy.
719
     * @stable ICU 2.0
720
     */
721
    U_I18N_API virtual void adoptDateFormatSymbols(DateFormatSymbols* newFormatSymbols);
722
723
    /**
724
     * Set the date/time formatting data.
725
     * @param newFormatSymbols the given date-time formatting symbols to copy.
726
     * @stable ICU 2.0
727
     */
728
    U_I18N_API virtual void setDateFormatSymbols(const DateFormatSymbols& newFormatSymbols);
729
730
    /**
731
     * Return the class ID for this class. This is useful only for comparing to
732
     * a return value from getDynamicClassID(). For example:
733
     * <pre>
734
     * .   Base* polymorphic_pointer = createPolymorphicObject();
735
     * .   if (polymorphic_pointer->getDynamicClassID() ==
736
     * .       erived::getStaticClassID()) ...
737
     * </pre>
738
     * @return          The class ID for all objects of this class.
739
     * @stable ICU 2.0
740
     */
741
    U_I18N_API static UClassID getStaticClassID();
742
743
    /**
744
     * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
745
     * method is to implement a simple version of RTTI, since not all C++
746
     * compilers support genuine RTTI. Polymorphic operator==() and clone()
747
     * methods call this method.
748
     *
749
     * @return          The class ID for this object. All objects of a
750
     *                  given class have the same class ID.  Objects of
751
     *                  other classes have different class IDs.
752
     * @stable ICU 2.0
753
     */
754
    U_I18N_API virtual UClassID getDynamicClassID() const override;
755
756
    /**
757
     * Set the calendar to be used by this date format. Initially, the default
758
     * calendar for the specified or default locale is used.  The caller should
759
     * not delete the Calendar object after it is adopted by this call.
760
     * Adopting a new calendar will change to the default symbols.
761
     *
762
     * @param calendarToAdopt    Calendar object to be adopted.
763
     * @stable ICU 2.0
764
     */
765
    U_I18N_API virtual void adoptCalendar(Calendar* calendarToAdopt) override;
766
767
    /* Cannot use #ifndef U_HIDE_INTERNAL_API for the following methods since they are virtual */
768
    /**
769
     * Sets the TimeZoneFormat to be used by this date/time formatter.
770
     * The caller should not delete the TimeZoneFormat object after
771
     * it is adopted by this call.
772
     * @param timeZoneFormatToAdopt The TimeZoneFormat object to be adopted.
773
     * @internal ICU 49 technology preview
774
     */
775
    U_I18N_API virtual void adoptTimeZoneFormat(TimeZoneFormat* timeZoneFormatToAdopt);
776
777
    /**
778
     * Sets the TimeZoneFormat to be used by this date/time formatter.
779
     * @param newTimeZoneFormat The TimeZoneFormat object to copy.
780
     * @internal ICU 49 technology preview
781
     */
782
    U_I18N_API virtual void setTimeZoneFormat(const TimeZoneFormat& newTimeZoneFormat);
783
784
    /**
785
     * Gets the time zone format object associated with this date/time formatter.
786
     * @return the time zone format associated with this date/time formatter.
787
     * @internal ICU 49 technology preview
788
     */
789
    U_I18N_API virtual const TimeZoneFormat* getTimeZoneFormat() const;
790
791
    /**
792
     * Set a particular UDisplayContext value in the formatter, such as
793
     * UDISPCTX_CAPITALIZATION_FOR_STANDALONE. Note: For getContext, see
794
     * DateFormat.
795
     * @param value The UDisplayContext value to set.
796
     * @param status Input/output status. If at entry this indicates a failure
797
     *               status, the function will do nothing; otherwise this will be
798
     *               updated with any new status from the function.
799
     * @stable ICU 53
800
     */
801
    U_I18N_API virtual void setContext(UDisplayContext value, UErrorCode& status) override;
802
803
    /**
804
     * Overrides base class method and
805
     * This method clears per field NumberFormat instances
806
     * previously set by {@see adoptNumberFormat(const UnicodeString&, NumberFormat*, UErrorCode)}
807
     * @param formatToAdopt the NumbeferFormat used
808
     * @stable ICU 54
809
     */
810
    U_I18N_API void adoptNumberFormat(NumberFormat* formatToAdopt) override;
811
812
    /**
813
     * Allow the user to set the NumberFormat for several fields
814
     * It can be a single field like: "y"(year) or "M"(month)
815
     * It can be several field combined together: "yM"(year and month)
816
     * Note:
817
     * 1 symbol field is enough for multiple symbol field (so "y" will override "yy", "yyy")
818
     * If the field is not numeric, then override has no effect (like "MMM" will use abbreviation, not numerical field)
819
     * Per field NumberFormat can also be cleared in {@see DateFormat::setNumberFormat(const NumberFormat& newNumberFormat)}
820
     *
821
     * @param fields  the fields to override(like y)
822
     * @param formatToAdopt the NumbeferFormat used
823
     * @param status  Receives a status code, which will be U_ZERO_ERROR
824
     *                if the operation succeeds.
825
     * @stable ICU 54
826
     */
827
    U_I18N_API void adoptNumberFormat(const UnicodeString& fields,
828
                                      NumberFormat* formatToAdopt,
829
                                      UErrorCode& status);
830
831
    /**
832
     * Get the numbering system to be used for a particular field.
833
     * @param field The UDateFormatField to get
834
     * @stable ICU 54
835
     */
836
    U_I18N_API const NumberFormat* getNumberFormatForField(char16_t field) const;
837
838
#ifndef U_HIDE_INTERNAL_API
839
    /**
840
     * This is for ICU internal use only. Please do not use.
841
     * Check whether the 'field' is smaller than all the fields covered in
842
     * pattern, return true if it is. The sequence of calendar field,
843
     * from large to small is: ERA, YEAR, MONTH, DATE, AM_PM, HOUR, MINUTE,...
844
     * @param field    the calendar field need to check against
845
     * @return         true if the 'field' is smaller than all the fields
846
     *                 covered in pattern. false otherwise.
847
     * @internal ICU 4.0
848
     */
849
    U_I18N_API UBool isFieldUnitIgnored(UCalendarDateFields field) const;
850
851
    /**
852
     * This is for ICU internal use only. Please do not use.
853
     * Check whether the 'field' is smaller than all the fields covered in
854
     * pattern, return true if it is. The sequence of calendar field,
855
     * from large to small is: ERA, YEAR, MONTH, DATE, AM_PM, HOUR, MINUTE,...
856
     * @param pattern  the pattern to check against
857
     * @param field    the calendar field need to check against
858
     * @return         true if the 'field' is smaller than all the fields
859
     *                 covered in pattern. false otherwise.
860
     * @internal ICU 4.0
861
     */
862
    U_I18N_API static UBool isFieldUnitIgnored(const UnicodeString& pattern, UCalendarDateFields field);
863
864
    /**
865
     * This is for ICU internal use only. Please do not use.
866
     * Get the locale of this simple date formatter.
867
     * It is used in DateIntervalFormat.
868
     *
869
     * @return   locale in this simple date formatter
870
     * @internal ICU 4.0
871
     */
872
    U_I18N_API const Locale& getSmpFmtLocale() const;
873
#endif  /* U_HIDE_INTERNAL_API */
874
875
private:
876
    friend class DateFormat;
877
    friend class DateIntervalFormat;
878
879
    void initializeDefaultCentury();
880
881
    void initializeBooleanAttributes();
882
883
    SimpleDateFormat() = delete; // default constructor not implemented
884
885
    /**
886
     * Used by the DateFormat factory methods to construct a SimpleDateFormat.
887
     * @param timeStyle the time style.
888
     * @param dateStyle the date style.
889
     * @param locale    the given locale.
890
     * @param status    Output param set to success/failure code on
891
     *                  exit.
892
     */
893
    SimpleDateFormat(EStyle timeStyle, EStyle dateStyle, const Locale& locale, UErrorCode& status);
894
895
    /**
896
     * Construct a SimpleDateFormat for the given locale.  If no resource data
897
     * is available, create an object of last resort, using hard-coded strings.
898
     * This is an internal method, called by DateFormat.  It should never fail.
899
     * @param locale    the given locale.
900
     * @param status    Output param set to success/failure code on
901
     *                  exit.
902
     */
903
    SimpleDateFormat(const Locale& locale, UErrorCode& status); // Use default pattern
904
905
    /**
906
     * Hook called by format(... FieldPosition& ...) and format(...FieldPositionIterator&...)
907
     */
908
    UnicodeString& _format(Calendar& cal, UnicodeString& appendTo, FieldPositionHandler& handler, UErrorCode& status) const;
909
910
    /**
911
     * Called by format() to format a single field.
912
     *
913
     * @param appendTo  Output parameter to receive result.
914
     *                  Result is appended to existing contents.
915
     * @param ch        The format character we encountered in the pattern.
916
     * @param count     Number of characters in the current pattern symbol (e.g.,
917
     *                  "yyyy" in the pattern would result in a call to this function
918
     *                  with ch equal to 'y' and count equal to 4)
919
     * @param capitalizationContext Capitalization context for this date format.
920
     * @param fieldNum  Zero-based numbering of current field within the overall format.
921
     * @param handler   Records information about field positions.
922
     * @param cal       Calendar to use
923
     * @param status    Receives a status code, which will be U_ZERO_ERROR if the operation
924
     *                  succeeds.
925
     */
926
    void subFormat(UnicodeString &appendTo,
927
                   char16_t ch,
928
                   int32_t count,
929
                   UDisplayContext capitalizationContext,
930
                   int32_t fieldNum,
931
                   char16_t fieldToOutput,
932
                   FieldPositionHandler& handler,
933
                   Calendar& cal,
934
                   UErrorCode& status) const; // in case of illegal argument
935
936
    /**
937
     * Used by subFormat() to format a numeric value.
938
     * Appends to toAppendTo a string representation of "value"
939
     * having a number of digits between "minDigits" and
940
     * "maxDigits".  Uses the DateFormat's NumberFormat.
941
     *
942
     * @param currentNumberFormat
943
     * @param appendTo  Output parameter to receive result.
944
     *                  Formatted number is appended to existing contents.
945
     * @param value     Value to format.
946
     * @param minDigits Minimum number of digits the result should have
947
     * @param maxDigits Maximum number of digits the result should have
948
     */
949
    void zeroPaddingNumber(const NumberFormat *currentNumberFormat,
950
                           UnicodeString &appendTo,
951
                           int32_t value,
952
                           int32_t minDigits,
953
                           int32_t maxDigits) const;
954
955
    /**
956
     * Return true if the given format character, occurring count
957
     * times, represents a numeric field.
958
     */
959
    static UBool isNumeric(char16_t formatChar, int32_t count);
960
961
    /**
962
     * Returns true if the patternOffset is at the start of a numeric field.
963
     */
964
    static UBool isAtNumericField(const UnicodeString &pattern, int32_t patternOffset);
965
966
    /**
967
     * Returns true if the patternOffset is right after a non-numeric field.
968
     */
969
    static UBool isAfterNonNumericField(const UnicodeString &pattern, int32_t patternOffset);
970
971
    /**
972
     * initializes fCalendar from parameters.  Returns fCalendar as a convenience.
973
     * @param adoptZone  Zone to be adopted, or nullptr for TimeZone::createDefault().
974
     * @param locale Locale of the calendar
975
     * @param status Error code
976
     * @return the newly constructed fCalendar
977
     */
978
    Calendar *initializeCalendar(TimeZone* adoptZone, const Locale& locale, UErrorCode& status);
979
980
    /**
981
     * Called by several of the constructors to load pattern data and formatting symbols
982
     * out of a resource bundle and initialize the locale based on it.
983
     * @param timeStyle     The time style, as passed to DateFormat::createDateInstance().
984
     * @param dateStyle     The date style, as passed to DateFormat::createTimeInstance().
985
     * @param locale        The locale to load the patterns from.
986
     * @param status        Filled in with an error code if loading the data from the
987
     *                      resources fails.
988
     */
989
    void construct(EStyle timeStyle, EStyle dateStyle, const Locale& locale, UErrorCode& status);
990
991
    /**
992
     * Called by construct() and the various constructors to set up the SimpleDateFormat's
993
     * Calendar and NumberFormat objects.
994
     * @param locale    The locale for which we want a Calendar and a NumberFormat.
995
     * @param status    Filled in with an error code if creating either subobject fails.
996
     */
997
    void initialize(const Locale& locale, UErrorCode& status);
998
999
    /**
1000
     * Private code-size reduction function used by subParse.
1001
     * @param text the time text being parsed.
1002
     * @param start where to start parsing.
1003
     * @param field the date field being parsed.
1004
     * @param stringArray the string array to parsed.
1005
     * @param stringArrayCount the size of the array.
1006
     * @param monthPattern pointer to leap month pattern, or nullptr if none.
1007
     * @param cal a Calendar set to the date and time to be formatted
1008
     *            into a date/time string.
1009
     * @return the new start position if matching succeeded; a negative number
1010
     * indicating matching failure, otherwise.
1011
     */
1012
    int32_t matchString(const UnicodeString& text, int32_t start, UCalendarDateFields field,
1013
                        const UnicodeString* stringArray, int32_t stringArrayCount,
1014
                        const UnicodeString* monthPattern, Calendar& cal) const;
1015
1016
    /**
1017
     * Private code-size reduction function used by subParse. Only for UCAL_MONTH
1018
     * @param text the time text being parsed.
1019
     * @param start where to start parsing.
1020
     * @param wideStringArray the wide string array to parsed.
1021
     * @param shortStringArray the short string array to parsed.
1022
     * @param stringArrayCount the size of the string arrays.
1023
     * @param cal a Calendar set to the date and time to be formatted
1024
     *            into a date/time string.
1025
     * @return the new start position if matching succeeded; a negative number
1026
     * indicating matching failure, otherwise.
1027
     */
1028
    int32_t matchAlphaMonthStrings(const UnicodeString& text, int32_t start,
1029
                        const UnicodeString* wideStringArray, const UnicodeString* shortStringArray,
1030
                        int32_t stringArrayCount, Calendar& cal) const;
1031
1032
    /**
1033
     * Private code-size reduction function used by subParse.
1034
     * @param text the time text being parsed.
1035
     * @param start where to start parsing.
1036
     * @param field the date field being parsed.
1037
     * @param stringArray the string array to parsed.
1038
     * @param stringArrayCount the size of the array.
1039
     * @param cal a Calendar set to the date and time to be formatted
1040
     *            into a date/time string.
1041
     * @return the new start position if matching succeeded; a negative number
1042
     * indicating matching failure, otherwise.
1043
     */
1044
    int32_t matchQuarterString(const UnicodeString& text, int32_t start, UCalendarDateFields field,
1045
                               const UnicodeString* stringArray, int32_t stringArrayCount, Calendar& cal) const;
1046
1047
    /**
1048
     * Used by subParse() to match localized day period strings.
1049
     */
1050
    int32_t matchDayPeriodStrings(const UnicodeString& text, int32_t start,
1051
                                  const UnicodeString* stringArray, int32_t stringArrayCount,
1052
                                  int32_t &dayPeriod) const;
1053
1054
    /**
1055
     * Private function used by subParse to match literal pattern text.
1056
     *
1057
     * @param pattern the pattern string
1058
     * @param patternOffset the starting offset into the pattern text. On
1059
     *        output will be set the offset of the first non-literal character in the pattern
1060
     * @param text the text being parsed
1061
     * @param textOffset the starting offset into the text. On output
1062
     *                   will be set to the offset of the character after the match
1063
     * @param whitespaceLenient <code>true</code> if whitespace parse is lenient, <code>false</code> otherwise.
1064
     * @param partialMatchLenient <code>true</code> if partial match parse is lenient, <code>false</code> otherwise.
1065
     * @param oldLeniency <code>true</code> if old leniency control is lenient, <code>false</code> otherwise.
1066
     *
1067
     * @return <code>true</code> if the literal text could be matched, <code>false</code> otherwise.
1068
     */
1069
    static UBool matchLiterals(const UnicodeString &pattern, int32_t &patternOffset,
1070
                               const UnicodeString &text, int32_t &textOffset,
1071
                               UBool whitespaceLenient, UBool partialMatchLenient, UBool oldLeniency);
1072
1073
    /**
1074
     * Private member function that converts the parsed date strings into
1075
     * timeFields. Returns -start (for ParsePosition) if failed.
1076
     * @param text the time text to be parsed.
1077
     * @param start where to start parsing.
1078
     * @param ch the pattern character for the date field text to be parsed.
1079
     * @param count the count of a pattern character.
1080
     * @param obeyCount if true then the count is strictly obeyed.
1081
     * @param allowNegative
1082
     * @param ambiguousYear If true then the two-digit year == the default start year.
1083
     * @param saveHebrewMonth Used to hang onto month until year is known.
1084
     * @param cal a Calendar set to the date and time to be formatted
1085
     *            into a date/time string.
1086
     * @param patLoc
1087
     * @param numericLeapMonthFormatter If non-null, used to parse numeric leap months.
1088
     * @param tzTimeType the type of parsed time zone - standard, daylight or unknown (output).
1089
     *      This parameter can be nullptr if caller does not need the information.
1090
     * @return the new start position if matching succeeded; a negative number
1091
     * indicating matching failure, otherwise.
1092
     */
1093
    int32_t subParse(const UnicodeString& text, int32_t& start, char16_t ch, int32_t count,
1094
                     UBool obeyCount, UBool allowNegative, UBool ambiguousYear[], int32_t& saveHebrewMonth, Calendar& cal,
1095
                     int32_t patLoc, MessageFormat * numericLeapMonthFormatter, UTimeZoneFormatTimeType *tzTimeType,
1096
                     int32_t *dayPeriod=nullptr) const;
1097
1098
    void parseInt(const UnicodeString& text,
1099
                  Formattable& number,
1100
                  ParsePosition& pos,
1101
                  UBool allowNegative,
1102
                  const NumberFormat *fmt) const;
1103
1104
    void parseInt(const UnicodeString& text,
1105
                  Formattable& number,
1106
                  int32_t maxDigits,
1107
                  ParsePosition& pos,
1108
                  UBool allowNegative,
1109
                  const NumberFormat *fmt) const;
1110
1111
    int32_t checkIntSuffix(const UnicodeString& text, int32_t start,
1112
                           int32_t patLoc, UBool isNegative) const;
1113
1114
    /**
1115
     * Counts number of digit code points in the specified text.
1116
     *
1117
     * @param text  input text
1118
     * @param start start index, inclusive
1119
     * @param end   end index, exclusive
1120
     * @return  number of digits found in the text in the specified range.
1121
    */
1122
    int32_t countDigits(const UnicodeString& text, int32_t start, int32_t end) const;
1123
1124
    /**
1125
     * Translate a pattern, mapping each character in the from string to the
1126
     * corresponding character in the to string. Return an error if the original
1127
     * pattern contains an unmapped character, or if a quote is unmatched.
1128
     * Quoted (single quotes only) material is not translated.
1129
     * @param originalPattern   the original pattern.
1130
     * @param translatedPattern Output param to receive the translited pattern.
1131
     * @param from              the characters to be translited from.
1132
     * @param to                the characters to be translited to.
1133
     * @param status            Receives a status code, which will be U_ZERO_ERROR
1134
     *                          if the operation succeeds.
1135
     */
1136
    static void translatePattern(const UnicodeString& originalPattern,
1137
                                UnicodeString& translatedPattern,
1138
                                const UnicodeString& from,
1139
                                const UnicodeString& to,
1140
                                UErrorCode& status);
1141
1142
    /**
1143
     * Sets the starting date of the 100-year window that dates with 2-digit years
1144
     * are considered to fall within.
1145
     * @param startDate the start date
1146
     * @param status    Receives a status code, which will be U_ZERO_ERROR
1147
     *                  if the operation succeeds.
1148
     */
1149
    void         parseAmbiguousDatesAsAfter(UDate startDate, UErrorCode& status);
1150
1151
    /**
1152
     * Return the length matched by the given affix, or -1 if none.
1153
     * Runs of white space in the affix, match runs of white space in
1154
     * the input.
1155
     * @param affix pattern string, taken as a literal
1156
     * @param input input text
1157
     * @param pos offset into input at which to begin matching
1158
     * @return length of input that matches, or -1 if match failure
1159
     */
1160
    int32_t compareSimpleAffix(const UnicodeString& affix,
1161
                   const UnicodeString& input,
1162
                   int32_t pos) const;
1163
1164
    /**
1165
     * Skip over a run of zero or more Pattern_White_Space characters at
1166
     * pos in text.
1167
     */
1168
    int32_t skipPatternWhiteSpace(const UnicodeString& text, int32_t pos) const;
1169
1170
    /**
1171
     * Skip over a run of zero or more isUWhiteSpace() characters at pos
1172
     * in text.
1173
     */
1174
    int32_t skipUWhiteSpace(const UnicodeString& text, int32_t pos) const;
1175
1176
    /**
1177
     * Initialize SimpleNumberFormat instance
1178
     */
1179
    void initSimpleNumberFormatter(UErrorCode &status);
1180
1181
    /**
1182
     * Initialize NumberFormat instances used for numbering system overrides.
1183
     */
1184
    void initNumberFormatters(const Locale &locale,UErrorCode &status);
1185
1186
    /**
1187
     * Parse the given override string and set up structures for number formats
1188
     */
1189
    void processOverrideString(const Locale &locale, const UnicodeString &str, int8_t type, UErrorCode &status);
1190
1191
    /**
1192
     * Used to map pattern characters to Calendar field identifiers.
1193
     */
1194
    static const UCalendarDateFields fgPatternIndexToCalendarField[];
1195
1196
    /**
1197
     * Map index into pattern character string to DateFormat field number
1198
     */
1199
    static const UDateFormatField fgPatternIndexToDateFormatField[];
1200
1201
    /**
1202
     * Lazy TimeZoneFormat instantiation, semantically const
1203
     */
1204
    TimeZoneFormat *tzFormat(UErrorCode &status) const;
1205
1206
    const NumberFormat* getNumberFormatByIndex(UDateFormatField index) const;
1207
1208
    /**
1209
     * Used to map Calendar field to field level.
1210
     * The larger the level, the smaller the field unit.
1211
     * For example, UCAL_ERA level is 0, UCAL_YEAR level is 10,
1212
     * UCAL_MONTH level is 20.
1213
     */
1214
    static const int32_t fgCalendarFieldToLevel[];
1215
1216
    /**
1217
     * Map calendar field letter into calendar field level.
1218
     */
1219
    static int32_t getLevelFromChar(char16_t ch);
1220
1221
    /**
1222
     * Tell if a character can be used to define a field in a format string.
1223
     */
1224
    static UBool isSyntaxChar(char16_t ch);
1225
1226
    /**
1227
     * The formatting pattern for this formatter.
1228
     */
1229
    UnicodeString       fPattern;
1230
1231
    /**
1232
     * The numbering system override for dates.
1233
     */
1234
    UnicodeString       fDateOverride;
1235
1236
    /**
1237
     * The numbering system override for times.
1238
     */
1239
    UnicodeString       fTimeOverride;
1240
1241
1242
    /**
1243
     * The original locale used (for reloading symbols)
1244
     */
1245
    Locale              fLocale;
1246
1247
    /**
1248
     * A pointer to an object containing the strings to use in formatting (e.g.,
1249
     * month and day names, AM and PM strings, time zone names, etc.)
1250
     */
1251
    DateFormatSymbols*  fSymbols = nullptr;   // Owned
1252
1253
    /**
1254
     * The time zone formatter
1255
     */
1256
    TimeZoneFormat* fTimeZoneFormat = nullptr;
1257
1258
    /**
1259
     * If dates have ambiguous years, we map them into the century starting
1260
     * at defaultCenturyStart, which may be any date.  If defaultCenturyStart is
1261
     * set to SYSTEM_DEFAULT_CENTURY, which it is by default, then the system
1262
     * values are used.  The instance values defaultCenturyStart and
1263
     * defaultCenturyStartYear are only used if explicitly set by the user
1264
     * through the API method parseAmbiguousDatesAsAfter().
1265
     */
1266
    UDate                fDefaultCenturyStart;
1267
1268
    UBool                fHasMinute;
1269
    UBool                fHasSecond;
1270
    UBool                fHasHanYearChar; // pattern contains the Han year character \u5E74
1271
1272
    /**
1273
     * Sets fHasMinutes and fHasSeconds.
1274
     */
1275
    void                 parsePattern();
1276
1277
    /**
1278
     * See documentation for defaultCenturyStart.
1279
     */
1280
    /*transient*/ int32_t   fDefaultCenturyStartYear;
1281
1282
    struct NSOverride : public UMemory {
1283
        const SharedNumberFormat *snf;
1284
        int32_t hash;
1285
        NSOverride *next;
1286
        void free();
1287
637
        NSOverride() : snf(nullptr), hash(0), next(nullptr) {
1288
637
        }
1289
        ~NSOverride();
1290
    };
1291
1292
    /**
1293
     * The number format in use for each date field. nullptr means fall back
1294
     * to fNumberFormat in DateFormat.
1295
     */
1296
    const SharedNumberFormat    **fSharedNumberFormatters = nullptr;
1297
1298
    /**
1299
     * Number formatter pre-allocated for fast performance
1300
     * 
1301
     * This references the decimal symbols from fNumberFormatter if it is an instance
1302
     * of DecimalFormat (and is otherwise null). This should always be cleaned up before
1303
     * destroying fNumberFormatter.
1304
     */
1305
    const number::SimpleNumberFormatter* fSimpleNumberFormatter = nullptr;
1306
1307
    UBool fHaveDefaultCentury;
1308
1309
    const BreakIterator* fCapitalizationBrkIter = nullptr;
1310
};
1311
1312
inline UDate
1313
SimpleDateFormat::get2DigitYearStart(UErrorCode& /*status*/) const
1314
0
{
1315
0
    return fDefaultCenturyStart;
1316
0
}
1317
1318
U_NAMESPACE_END
1319
1320
#endif /* #if !UCONFIG_NO_FORMATTING */
1321
1322
#endif /* U_SHOW_CPLUSPLUS_API */
1323
1324
#endif // _SMPDTFMT
1325
//eof