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. However, if the client needs something more unusual than
73 * the default patterns in the locales, he can construct a SimpleDateFormat directly
74 * and give it an appropriate pattern (or use one of the factory methods on DateFormat
75 * and modify the pattern after the fact with toPattern() and applyPattern().
76 *
77 * <p><strong>Date and Time Patterns:</strong></p>
78 *
79 * <p>Date and time formats are specified by <em>date and time pattern</em> strings.
80 * Within date and time pattern strings, all unquoted ASCII letters [A-Za-z] are reserved
81 * as pattern letters representing calendar fields. <code>SimpleDateFormat</code> supports
82 * the date and time formatting algorithm and pattern letters defined by
83 * <a href="http://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table">UTS#35
84 * Unicode Locale Data Markup Language (LDML)</a> and further documented for ICU in the
85 * <a href="https://unicode-org.github.io/icu/userguide/format_parse/datetime/#date-field-symbol-table">ICU
86 * User Guide</a>. The following pattern letters are currently available (note that the actual
87 * values depend on CLDR and may change from the examples shown here):</p>
88 *
89 * <table border="1">
90 * <tr>
91 * <th>Field</th>
92 * <th style="text-align: center">Sym.</th>
93 * <th style="text-align: center">No.</th>
94 * <th>Example</th>
95 * <th>Description</th>
96 * </tr>
97 * <tr>
98 * <th rowspan="3">era</th>
99 * <td style="text-align: center" rowspan="3">G</td>
100 * <td style="text-align: center">1..3</td>
101 * <td>AD</td>
102 * <td rowspan="3">Era - Replaced with the Era string for the current date. One to three letters for the
103 * abbreviated form, four letters for the long (wide) form, five for the narrow form.</td>
104 * </tr>
105 * <tr>
106 * <td style="text-align: center">4</td>
107 * <td>Anno Domini</td>
108 * </tr>
109 * <tr>
110 * <td style="text-align: center">5</td>
111 * <td>A</td>
112 * </tr>
113 * <tr>
114 * <th rowspan="6">year</th>
115 * <td style="text-align: center">y</td>
116 * <td style="text-align: center">1..n</td>
117 * <td>1996</td>
118 * <td>Year. Normally the length specifies the padding, but for two letters it also specifies the maximum
119 * length. Example:<div align="center">
120 * <center>
121 * <table border="1" cellpadding="2" cellspacing="0">
122 * <tr>
123 * <th>Year</th>
124 * <th style="text-align: right">y</th>
125 * <th style="text-align: right">yy</th>
126 * <th style="text-align: right">yyy</th>
127 * <th style="text-align: right">yyyy</th>
128 * <th style="text-align: right">yyyyy</th>
129 * </tr>
130 * <tr>
131 * <td>AD 1</td>
132 * <td style="text-align: right">1</td>
133 * <td style="text-align: right">01</td>
134 * <td style="text-align: right">001</td>
135 * <td style="text-align: right">0001</td>
136 * <td style="text-align: right">00001</td>
137 * </tr>
138 * <tr>
139 * <td>AD 12</td>
140 * <td style="text-align: right">12</td>
141 * <td style="text-align: right">12</td>
142 * <td style="text-align: right">012</td>
143 * <td style="text-align: right">0012</td>
144 * <td style="text-align: right">00012</td>
145 * </tr>
146 * <tr>
147 * <td>AD 123</td>
148 * <td style="text-align: right">123</td>
149 * <td style="text-align: right">23</td>
150 * <td style="text-align: right">123</td>
151 * <td style="text-align: right">0123</td>
152 * <td style="text-align: right">00123</td>
153 * </tr>
154 * <tr>
155 * <td>AD 1234</td>
156 * <td style="text-align: right">1234</td>
157 * <td style="text-align: right">34</td>
158 * <td style="text-align: right">1234</td>
159 * <td style="text-align: right">1234</td>
160 * <td style="text-align: right">01234</td>
161 * </tr>
162 * <tr>
163 * <td>AD 12345</td>
164 * <td style="text-align: right">12345</td>
165 * <td style="text-align: right">45</td>
166 * <td style="text-align: right">12345</td>
167 * <td style="text-align: right">12345</td>
168 * <td style="text-align: right">12345</td>
169 * </tr>
170 * </table>
171 * </center></div>
172 * </td>
173 * </tr>
174 * <tr>
175 * <td style="text-align: center">Y</td>
176 * <td style="text-align: center">1..n</td>
177 * <td>1997</td>
178 * <td>Year (in "Week of Year" based calendars). Normally the length specifies the padding,
179 * but for two letters it also specifies the maximum length. This year designation is used in ISO
180 * year-week calendar as defined by ISO 8601, but can be used in non-Gregorian based calendar systems
181 * where week date processing is desired. May not always be the same value as calendar year.</td>
182 * </tr>
183 * <tr>
184 * <td style="text-align: center">u</td>
185 * <td style="text-align: center">1..n</td>
186 * <td>4601</td>
187 * <td>Extended year. This is a single number designating the year of this calendar system, encompassing
188 * all supra-year fields. For example, for the Julian calendar system, year numbers are positive, with an
189 * era of BCE or CE. An extended year value for the Julian calendar system assigns positive values to CE
190 * years and negative values to BCE years, with 1 BCE being year 0.</td>
191 * </tr>
192 * <tr>
193 * <td style="text-align: center" rowspan="3">U</td>
194 * <td style="text-align: center">1..3</td>
195 * <td>甲子</td>
196 * <td rowspan="3">Cyclic year name. Calendars such as the Chinese lunar calendar (and related calendars)
197 * and the Hindu calendars use 60-year cycles of year names. Use one through three letters for the abbreviated
198 * name, four for the full (wide) name, or five for the narrow name (currently the data only provides abbreviated names,
199 * which will be used for all requested name widths). If the calendar does not provide cyclic year name data,
200 * or if the year value to be formatted is out of the range of years for which cyclic name data is provided,
201 * then numeric formatting is used (behaves like 'y').</td>
202 * </tr>
203 * <tr>
204 * <td style="text-align: center">4</td>
205 * <td>(currently also 甲子)</td>
206 * </tr>
207 * <tr>
208 * <td style="text-align: center">5</td>
209 * <td>(currently also 甲子)</td>
210 * </tr>
211 * <tr>
212 * <th rowspan="6">quarter</th>
213 * <td rowspan="3" style="text-align: center">Q</td>
214 * <td style="text-align: center">1..2</td>
215 * <td>02</td>
216 * <td rowspan="3">Quarter - Use one or two for the numerical quarter, three for the abbreviation, or four for the
217 * full (wide) name (five for the narrow name is not yet supported).</td>
218 * </tr>
219 * <tr>
220 * <td style="text-align: center">3</td>
221 * <td>Q2</td>
222 * </tr>
223 * <tr>
224 * <td style="text-align: center">4</td>
225 * <td>2nd quarter</td>
226 * </tr>
227 * <tr>
228 * <td rowspan="3" style="text-align: center">q</td>
229 * <td style="text-align: center">1..2</td>
230 * <td>02</td>
231 * <td rowspan="3"><b>Stand-Alone</b> Quarter - Use one or two for the numerical quarter, three for the abbreviation,
232 * or four for the full name (five for the narrow name is not yet supported).</td>
233 * </tr>
234 * <tr>
235 * <td style="text-align: center">3</td>
236 * <td>Q2</td>
237 * </tr>
238 * <tr>
239 * <td style="text-align: center">4</td>
240 * <td>2nd quarter</td>
241 * </tr>
242 * <tr>
243 * <th rowspan="8">month</th>
244 * <td rowspan="4" style="text-align: center">M</td>
245 * <td style="text-align: center">1..2</td>
246 * <td>09</td>
247 * <td rowspan="4">Month - Use one or two for the numerical month, three for the abbreviation, four for
248 * the full (wide) name, or five for the narrow name. With two ("MM"), the month number is zero-padded
249 * if necessary (e.g. "08")</td>
250 * </tr>
251 * <tr>
252 * <td style="text-align: center">3</td>
253 * <td>Sep</td>
254 * </tr>
255 * <tr>
256 * <td style="text-align: center">4</td>
257 * <td>September</td>
258 * </tr>
259 * <tr>
260 * <td style="text-align: center">5</td>
261 * <td>S</td>
262 * </tr>
263 * <tr>
264 * <td rowspan="4" style="text-align: center">L</td>
265 * <td style="text-align: center">1..2</td>
266 * <td>09</td>
267 * <td rowspan="4"><b>Stand-Alone</b> Month - Use one or two for the numerical month, three for the abbreviation,
268 * four for the full (wide) name, or 5 for the narrow name. With two ("LL"), the month number is zero-padded if
269 * necessary (e.g. "08")</td>
270 * </tr>
271 * <tr>
272 * <td style="text-align: center">3</td>
273 * <td>Sep</td>
274 * </tr>
275 * <tr>
276 * <td style="text-align: center">4</td>
277 * <td>September</td>
278 * </tr>
279 * <tr>
280 * <td style="text-align: center">5</td>
281 * <td>S</td>
282 * </tr>
283 * <tr>
284 * <th rowspan="2">week</th>
285 * <td style="text-align: center">w</td>
286 * <td style="text-align: center">1..2</td>
287 * <td>27</td>
288 * <td>Week of Year. Use "w" to show the minimum number of digits, or "ww" to always show two digits
289 * (zero-padding if necessary, e.g. "08").</td>
290 * </tr>
291 * <tr>
292 * <td style="text-align: center">W</td>
293 * <td style="text-align: center">1</td>
294 * <td>3</td>
295 * <td>Week of Month</td>
296 * </tr>
297 * <tr>
298 * <th rowspan="4">day</th>
299 * <td style="text-align: center">d</td>
300 * <td style="text-align: center">1..2</td>
301 * <td>1</td>
302 * <td>Date - Day of the month. Use "d" to show the minimum number of digits, or "dd" to always show
303 * two digits (zero-padding if necessary, e.g. "08").</td>
304 * </tr>
305 * <tr>
306 * <td style="text-align: center">D</td>
307 * <td style="text-align: center">1..3</td>
308 * <td>345</td>
309 * <td>Day of year</td>
310 * </tr>
311 * <tr>
312 * <td style="text-align: center">F</td>
313 * <td style="text-align: center">1</td>
314 * <td>2</td>
315 * <td>Day of Week in Month. The example is for the 2nd Wed in July</td>
316 * </tr>
317 * <tr>
318 * <td style="text-align: center">g</td>
319 * <td style="text-align: center">1..n</td>
320 * <td>2451334</td>
321 * <td>Modified Julian day. This is different from the conventional Julian day number in two regards.
322 * First, it demarcates days at local zone midnight, rather than noon GMT. Second, it is a local number;
323 * that is, it depends on the local time zone. It can be thought of as a single number that encompasses
324 * all the date-related fields.</td>
325 * </tr>
326 * <tr>
327 * <th rowspan="14">week<br>
328 * day</th>
329 * <td rowspan="4" style="text-align: center">E</td>
330 * <td style="text-align: center">1..3</td>
331 * <td>Tue</td>
332 * <td rowspan="4">Day of week - Use one through three letters for the short day, four for the full (wide) name,
333 * five for the narrow name, or six for the short name.</td>
334 * </tr>
335 * <tr>
336 * <td style="text-align: center">4</td>
337 * <td>Tuesday</td>
338 * </tr>
339 * <tr>
340 * <td style="text-align: center">5</td>
341 * <td>T</td>
342 * </tr>
343 * <tr>
344 * <td style="text-align: center">6</td>
345 * <td>Tu</td>
346 * </tr>
347 * <tr>
348 * <td rowspan="5" style="text-align: center">e</td>
349 * <td style="text-align: center">1..2</td>
350 * <td>2</td>
351 * <td rowspan="5">Local day of week. Same as E except adds a numeric value that will depend on the local
352 * starting day of the week, using one or two letters. For this example, Monday is the first day of the week.</td>
353 * </tr>
354 * <tr>
355 * <td style="text-align: center">3</td>
356 * <td>Tue</td>
357 * </tr>
358 * <tr>
359 * <td style="text-align: center">4</td>
360 * <td>Tuesday</td>
361 * </tr>
362 * <tr>
363 * <td style="text-align: center">5</td>
364 * <td>T</td>
365 * </tr>
366 * <tr>
367 * <td style="text-align: center">6</td>
368 * <td>Tu</td>
369 * </tr>
370 * <tr>
371 * <td rowspan="5" style="text-align: center">c</td>
372 * <td style="text-align: center">1</td>
373 * <td>2</td>
374 * <td rowspan="5"><b>Stand-Alone</b> local day of week - Use one letter for the local numeric value (same
375 * as 'e'), three for the short day, four for the full (wide) name, five for the narrow name, or six for
376 * the short name.</td>
377 * </tr>
378 * <tr>
379 * <td style="text-align: center">3</td>
380 * <td>Tue</td>
381 * </tr>
382 * <tr>
383 * <td style="text-align: center">4</td>
384 * <td>Tuesday</td>
385 * </tr>
386 * <tr>
387 * <td style="text-align: center">5</td>
388 * <td>T</td>
389 * </tr>
390 * <tr>
391 * <td style="text-align: center">6</td>
392 * <td>Tu</td>
393 * </tr>
394 * <tr>
395 * <th>period</th>
396 * <td style="text-align: center">a</td>
397 * <td style="text-align: center">1</td>
398 * <td>AM</td>
399 * <td>AM or PM</td>
400 * </tr>
401 * <tr>
402 * <th rowspan="4">hour</th>
403 * <td style="text-align: center">h</td>
404 * <td style="text-align: center">1..2</td>
405 * <td>11</td>
406 * <td>Hour [1-12]. When used in skeleton data or in a skeleton passed in an API for flexible data pattern
407 * generation, it should match the 12-hour-cycle format preferred by the locale (h or K); it should not match
408 * a 24-hour-cycle format (H or k). Use hh for zero padding.</td>
409 * </tr>
410 * <tr>
411 * <td style="text-align: center">H</td>
412 * <td style="text-align: center">1..2</td>
413 * <td>13</td>
414 * <td>Hour [0-23]. When used in skeleton data or in a skeleton passed in an API for flexible data pattern
415 * generation, it should match the 24-hour-cycle format preferred by the locale (H or k); it should not match a
416 * 12-hour-cycle format (h or K). Use HH for zero padding.</td>
417 * </tr>
418 * <tr>
419 * <td style="text-align: center">K</td>
420 * <td style="text-align: center">1..2</td>
421 * <td>0</td>
422 * <td>Hour [0-11]. When used in a skeleton, only matches K or h, see above. Use KK for zero padding.</td>
423 * </tr>
424 * <tr>
425 * <td style="text-align: center">k</td>
426 * <td style="text-align: center">1..2</td>
427 * <td>24</td>
428 * <td>Hour [1-24]. When used in a skeleton, only matches k or H, see above. Use kk for zero padding.</td>
429 * </tr>
430 * <tr>
431 * <th>minute</th>
432 * <td style="text-align: center">m</td>
433 * <td style="text-align: center">1..2</td>
434 * <td>59</td>
435 * <td>Minute. Use "m" to show the minimum number of digits, or "mm" to always show two digits
436 * (zero-padding if necessary, e.g. "08").</td>
437 * </tr>
438 * <tr>
439 * <th rowspan="3">second</th>
440 * <td style="text-align: center">s</td>
441 * <td style="text-align: center">1..2</td>
442 * <td>12</td>
443 * <td>Second. Use "s" to show the minimum number of digits, or "ss" to always show two digits
444 * (zero-padding if necessary, e.g. "08").</td>
445 * </tr>
446 * <tr>
447 * <td style="text-align: center">S</td>
448 * <td style="text-align: center">1..n</td>
449 * <td>3450</td>
450 * <td>Fractional Second - truncates (like other time fields) to the count of letters when formatting.
451 * Appends zeros if more than 3 letters specified. Truncates at three significant digits when parsing.
452 * (example shows display using pattern SSSS for seconds value 12.34567)</td>
453 * </tr>
454 * <tr>
455 * <td style="text-align: center">A</td>
456 * <td style="text-align: center">1..n</td>
457 * <td>69540000</td>
458 * <td>Milliseconds in day. This field behaves <i>exactly</i> like a composite of all time-related fields,
459 * not including the zone fields. As such, it also reflects discontinuities of those fields on DST transition
460 * days. On a day of DST onset, it will jump forward. On a day of DST cessation, it will jump backward. This
461 * reflects the fact that is must be combined with the offset field to obtain a unique local time value.</td>
462 * </tr>
463 * <tr>
464 * <th rowspan="23">zone</th>
465 * <td rowspan="2" style="text-align: center">z</td>
466 * <td style="text-align: center">1..3</td>
467 * <td>PDT</td>
468 * <td>The <i>short specific non-location format</i>.
469 * Where that is unavailable, falls back to the <i>short localized GMT format</i> ("O").</td>
470 * </tr>
471 * <tr>
472 * <td style="text-align: center">4</td>
473 * <td>Pacific Daylight Time</td>
474 * <td>The <i>long specific non-location format</i>.
475 * Where that is unavailable, falls back to the <i>long localized GMT format</i> ("OOOO").</td>
476 * </tr>
477 * <tr>
478 * <td rowspan="3" style="text-align: center">Z</td>
479 * <td style="text-align: center">1..3</td>
480 * <td>-0800</td>
481 * <td>The <i>ISO8601 basic format</i> with hours, minutes and optional seconds fields.
482 * The format is equivalent to RFC 822 zone format (when optional seconds field is absent).
483 * This is equivalent to the "xxxx" specifier.</td>
484 * </tr>
485 * <tr>
486 * <td style="text-align: center">4</td>
487 * <td>GMT-8:00</td>
488 * <td>The <i>long localized GMT format</i>.
489 * This is equivalent to the "OOOO" specifier.</td>
490 * </tr>
491 * <tr>
492 * <td style="text-align: center">5</td>
493 * <td>-08:00<br>
494 * -07:52:58</td>
495 * <td>The <i>ISO8601 extended format</i> with hours, minutes and optional seconds fields.
496 * The ISO8601 UTC indicator "Z" is used when local time offset is 0.
497 * This is equivalent to the "XXXXX" specifier.</td>
498 * </tr>
499 * <tr>
500 * <td rowspan="2" style="text-align: center">O</td>
501 * <td style="text-align: center">1</td>
502 * <td>GMT-8</td>
503 * <td>The <i>short localized GMT format</i>.</td>
504 * </tr>
505 * <tr>
506 * <td style="text-align: center">4</td>
507 * <td>GMT-08:00</td>
508 * <td>The <i>long localized GMT format</i>.</td>
509 * </tr>
510 * <tr>
511 * <td rowspan="2" style="text-align: center">v</td>
512 * <td style="text-align: center">1</td>
513 * <td>PT</td>
514 * <td>The <i>short generic non-location format</i>.
515 * Where that is unavailable, falls back to the <i>generic location format</i> ("VVVV"),
516 * then the <i>short localized GMT format</i> as the final fallback.</td>
517 * </tr>
518 * <tr>
519 * <td style="text-align: center">4</td>
520 * <td>Pacific Time</td>
521 * <td>The <i>long generic non-location format</i>.
522 * Where that is unavailable, falls back to <i>generic location format</i> ("VVVV").
523 * </tr>
524 * <tr>
525 * <td rowspan="4" style="text-align: center">V</td>
526 * <td style="text-align: center">1</td>
527 * <td>uslax</td>
528 * <td>The short time zone ID.
529 * Where that is unavailable, the special short time zone ID <i>unk</i> (Unknown Zone) is used.<br>
530 * <i><b>Note</b>: This specifier was originally used for a variant of the short specific non-location format,
531 * but it was deprecated in the later version of the LDML specification. In CLDR 23/ICU 51, the definition of
532 * the specifier was changed to designate a short time zone ID.</i></td>
533 * </tr>
534 * <tr>
535 * <td style="text-align: center">2</td>
536 * <td>America/Los_Angeles</td>
537 * <td>The long time zone ID.</td>
538 * </tr>
539 * <tr>
540 * <td style="text-align: center">3</td>
541 * <td>Los Angeles</td>
542 * <td>The exemplar city (location) for the time zone.
543 * Where that is unavailable, the localized exemplar city name for the special zone <i>Etc/Unknown</i> is used
544 * as the fallback (for example, "Unknown City"). </td>
545 * </tr>
546 * <tr>
547 * <td style="text-align: center">4</td>
548 * <td>Los Angeles Time</td>
549 * <td>The <i>generic location format</i>.
550 * Where that is unavailable, falls back to the <i>long localized GMT format</i> ("OOOO";
551 * Note: Fallback is only necessary with a GMT-style Time Zone ID, like Etc/GMT-830.)<br>
552 * This is especially useful when presenting possible timezone choices for user selection,
553 * since the naming is more uniform than the "v" format.</td>
554 * </tr>
555 * <tr>
556 * <td rowspan="5" style="text-align: center">X</td>
557 * <td style="text-align: center">1</td>
558 * <td>-08<br>
559 * +0530<br>
560 * Z</td>
561 * <td>The <i>ISO8601 basic format</i> with hours field and optional minutes field.
562 * The ISO8601 UTC indicator "Z" is used when local time offset is 0.</td>
563 * </tr>
564 * <tr>
565 * <td style="text-align: center">2</td>
566 * <td>-0800<br>
567 * Z</td>
568 * <td>The <i>ISO8601 basic format</i> with hours and minutes fields.
569 * The ISO8601 UTC indicator "Z" is used when local time offset is 0.</td>
570 * </tr>
571 * <tr>
572 * <td style="text-align: center">3</td>
573 * <td>-08:00<br>
574 * Z</td>
575 * <td>The <i>ISO8601 extended format</i> with hours and minutes fields.
576 * The ISO8601 UTC indicator "Z" is used when local time offset is 0.</td>
577 * </tr>
578 * <tr>
579 * <td style="text-align: center">4</td>
580 * <td>-0800<br>
581 * -075258<br>
582 * Z</td>
583 * <td>The <i>ISO8601 basic format</i> with hours, minutes and optional seconds fields.
584 * (Note: The seconds field is not supported by the ISO8601 specification.)
585 * The ISO8601 UTC indicator "Z" is used when local time offset is 0.</td>
586 * </tr>
587 * <tr>
588 * <td style="text-align: center">5</td>
589 * <td>-08:00<br>
590 * -07:52:58<br>
591 * Z</td>
592 * <td>The <i>ISO8601 extended format</i> with hours, minutes and optional seconds fields.
593 * (Note: The seconds field is not supported by the ISO8601 specification.)
594 * The ISO8601 UTC indicator "Z" is used when local time offset is 0.</td>
595 * </tr>
596 * <tr>
597 * <td rowspan="5" style="text-align: center">x</td>
598 * <td style="text-align: center">1</td>
599 * <td>-08<br>
600 * +0530</td>
601 * <td>The <i>ISO8601 basic format</i> with hours field and optional minutes field.</td>
602 * </tr>
603 * <tr>
604 * <td style="text-align: center">2</td>
605 * <td>-0800</td>
606 * <td>The <i>ISO8601 basic format</i> with hours and minutes fields.</td>
607 * </tr>
608 * <tr>
609 * <td style="text-align: center">3</td>
610 * <td>-08:00</td>
611 * <td>The <i>ISO8601 extended format</i> with hours and minutes fields.</td>
612 * </tr>
613 * <tr>
614 * <td style="text-align: center">4</td>
615 * <td>-0800<br>
616 * -075258</td>
617 * <td>The <i>ISO8601 basic format</i> with hours, minutes and optional seconds fields.
618 * (Note: The seconds field is not supported by the ISO8601 specification.)</td>
619 * </tr>
620 * <tr>
621 * <td style="text-align: center">5</td>
622 * <td>-08:00<br>
623 * -07:52:58</td>
624 * <td>The <i>ISO8601 extended format</i> with hours, minutes and optional seconds fields.
625 * (Note: The seconds field is not supported by the ISO8601 specification.)</td>
626 * </tr>
627 * </table>
628 *
629 * <P>
630 * Any characters in the pattern that are not in the ranges of ['a'..'z'] and
631 * ['A'..'Z'] will be treated as quoted text. For instance, characters
632 * like ':', '.', ' ', '#' and '@' will appear in the resulting time text
633 * even they are not embraced within single quotes.
634 * <P>
635 * A pattern containing any invalid pattern letter will result in a failing
636 * UErrorCode result during formatting or parsing.
637 * <P>
638 * Examples using the US locale:
639 * <pre>
640 * \code
641 * Format Pattern Result
642 * -------------- -------
643 * "yyyy.MM.dd G 'at' HH:mm:ss vvvv" ->> 1996.07.10 AD at 15:08:56 Pacific Time
644 * "EEE, MMM d, ''yy" ->> Wed, July 10, '96
645 * "h:mm a" ->> 12:08 PM
646 * "hh 'o''clock' a, zzzz" ->> 12 o'clock PM, Pacific Daylight Time
647 * "K:mm a, vvv" ->> 0:00 PM, PT
648 * "yyyyy.MMMMM.dd GGG hh:mm aaa" ->> 1996.July.10 AD 12:08 PM
649 * \endcode
650 * </pre>
651 * Code Sample:
652 * <pre>
653 * \code
654 * UErrorCode success = U_ZERO_ERROR;
655 * SimpleTimeZone* pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, "PST");
656 * pdt->setStartRule( Calendar::APRIL, 1, Calendar::SUNDAY, 2*60*60*1000);
657 * pdt->setEndRule( Calendar::OCTOBER, -1, Calendar::SUNDAY, 2*60*60*1000);
658 *
659 * // Format the current time.
660 * SimpleDateFormat* formatter
661 * = new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss a zzz", success );
662 * GregorianCalendar cal(success);
663 * UDate currentTime_1 = cal.getTime(success);
664 * FieldPosition fp(FieldPosition::DONT_CARE);
665 * UnicodeString dateString;
666 * formatter->format( currentTime_1, dateString, fp );
667 * cout << "result: " << dateString << endl;
668 *
669 * // Parse the previous string back into a Date.
670 * ParsePosition pp(0);
671 * UDate currentTime_2 = formatter->parse(dateString, pp );
672 * \endcode
673 * </pre>
674 * In the above example, the time value "currentTime_2" obtained from parsing
675 * will be equal to currentTime_1. However, they may not be equal if the am/pm
676 * marker 'a' is left out from the format pattern while the "hour in am/pm"
677 * pattern symbol is used. This information loss can happen when formatting the
678 * time in PM.
679 *
680 * <p>
681 * When parsing a date string using the abbreviated year pattern ("y" or "yy"),
682 * SimpleDateFormat must interpret the abbreviated year
683 * relative to some century. It does this by adjusting dates to be
684 * within 80 years before and 20 years after the time the SimpleDateFormat
685 * instance is created. For example, using a pattern of "MM/dd/yy" and a
686 * SimpleDateFormat instance created on Jan 1, 1997, the string
687 * "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64"
688 * would be interpreted as May 4, 1964.
689 * During parsing, only strings consisting of exactly two digits, as defined by
690 * <code>Unicode::isDigit()</code>, will be parsed into the default century.
691 * Any other numeric string, such as a one digit string, a three or more digit
692 * string, or a two digit string that isn't all digits (for example, "-1"), is
693 * interpreted literally. So "01/02/3" or "01/02/003" are parsed (for the
694 * Gregorian calendar), using the same pattern, as Jan 2, 3 AD. Likewise (but
695 * only in lenient parse mode, the default) "01/02/-3" is parsed as Jan 2, 4 BC.
696 *
697 * <p>
698 * If the year pattern has more than two 'y' characters, the year is
699 * interpreted literally, regardless of the number of digits. So using the
700 * pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D.
701 *
702 * <p>
703 * When numeric fields abut one another directly, with no intervening delimiter
704 * characters, they constitute a run of abutting numeric fields. Such runs are
705 * parsed specially. For example, the format "HHmmss" parses the input text
706 * "123456" to 12:34:56, parses the input text "12345" to 1:23:45, and fails to
707 * parse "1234". In other words, the leftmost field of the run is flexible,
708 * while the others keep a fixed width. If the parse fails anywhere in the run,
709 * then the leftmost field is shortened by one character, and the entire run is
710 * parsed again. This is repeated until either the parse succeeds or the
711 * leftmost field is one character in length. If the parse still fails at that
712 * point, the parse of the run fails.
713 *
714 * <P>
715 * For time zones that have no names, SimpleDateFormat uses strings GMT+hours:minutes or
716 * GMT-hours:minutes.
717 * <P>
718 * The calendar defines what is the first day of the week, the first week of the
719 * year, whether hours are zero based or not (0 vs 12 or 24), and the timezone.
720 * There is one common number format to handle all the numbers; the digit count
721 * is handled programmatically according to the pattern.
722 *
723 * <p><em>User subclasses are not supported.</em> While clients may write
724 * subclasses, such code will not necessarily work and will not be
725 * guaranteed to work stably from release to release.
726 */
727 class U_I18N_API SimpleDateFormat: public DateFormat {
728 public:
729 /**
730 * Construct a SimpleDateFormat using the default pattern for the default
731 * locale.
732 * <P>
733 * [Note:] Not all locales support SimpleDateFormat; for full generality,
734 * use the factory methods in the DateFormat class.
735 * @param status Output param set to success/failure code.
736 * @stable ICU 2.0
737 */
738 SimpleDateFormat(UErrorCode& status);
739
740 /**
741 * Construct a SimpleDateFormat using the given pattern and the default locale.
742 * The locale is used to obtain the symbols used in formatting (e.g., the
743 * names of the months), but not to provide the pattern.
744 * <P>
745 * [Note:] Not all locales support SimpleDateFormat; for full generality,
746 * use the factory methods in the DateFormat class.
747 * @param pattern the pattern for the format.
748 * @param status Output param set to success/failure code.
749 * @stable ICU 2.0
750 */
751 SimpleDateFormat(const UnicodeString& pattern,
752 UErrorCode& status);
753
754 /**
755 * Construct a SimpleDateFormat using the given pattern, numbering system override, and the default locale.
756 * The locale is used to obtain the symbols used in formatting (e.g., the
757 * names of the months), but not to provide the pattern.
758 * <P>
759 * A numbering system override is a string containing either the name of a known numbering system,
760 * or a set of field and numbering system pairs that specify which fields are to be formatted with
761 * the alternate numbering system. For example, to specify that all numeric fields in the specified
762 * date or time pattern are to be rendered using Thai digits, simply specify the numbering system override
763 * as "thai". To specify that just the year portion of the date be formatted using Hebrew numbering,
764 * use the override string "y=hebrew". Numbering system overrides can be combined using a semi-colon
765 * character in the override string, such as "d=decimal;M=arabic;y=hebrew", etc.
766 *
767 * <P>
768 * [Note:] Not all locales support SimpleDateFormat; for full generality,
769 * use the factory methods in the DateFormat class.
770 * @param pattern the pattern for the format.
771 * @param override the override string.
772 * @param status Output param set to success/failure code.
773 * @stable ICU 4.2
774 */
775 SimpleDateFormat(const UnicodeString& pattern,
776 const UnicodeString& override,
777 UErrorCode& status);
778
779 /**
780 * Construct a SimpleDateFormat using the given pattern and locale.
781 * The locale is used to obtain the symbols used in formatting (e.g., the
782 * names of the months), but not to provide the pattern.
783 * <P>
784 * [Note:] Not all locales support SimpleDateFormat; for full generality,
785 * use the factory methods in the DateFormat class.
786 * @param pattern the pattern for the format.
787 * @param locale the given locale.
788 * @param status Output param set to success/failure code.
789 * @stable ICU 2.0
790 */
791 SimpleDateFormat(const UnicodeString& pattern,
792 const Locale& locale,
793 UErrorCode& status);
794
795 /**
796 * Construct a SimpleDateFormat using the given pattern, numbering system override, and locale.
797 * The locale is used to obtain the symbols used in formatting (e.g., the
798 * names of the months), but not to provide the pattern.
799 * <P>
800 * A numbering system override is a string containing either the name of a known numbering system,
801 * or a set of field and numbering system pairs that specify which fields are to be formatted with
802 * the alternate numbering system. For example, to specify that all numeric fields in the specified
803 * date or time pattern are to be rendered using Thai digits, simply specify the numbering system override
804 * as "thai". To specify that just the year portion of the date be formatted using Hebrew numbering,
805 * use the override string "y=hebrew". Numbering system overrides can be combined using a semi-colon
806 * character in the override string, such as "d=decimal;M=arabic;y=hebrew", etc.
807 * <P>
808 * [Note:] Not all locales support SimpleDateFormat; for full generality,
809 * use the factory methods in the DateFormat class.
810 * @param pattern the pattern for the format.
811 * @param override the numbering system override.
812 * @param locale the given locale.
813 * @param status Output param set to success/failure code.
814 * @stable ICU 4.2
815 */
816 SimpleDateFormat(const UnicodeString& pattern,
817 const UnicodeString& override,
818 const Locale& locale,
819 UErrorCode& status);
820
821 /**
822 * Construct a SimpleDateFormat using the given pattern and locale-specific
823 * symbol data. The formatter takes ownership of the DateFormatSymbols object;
824 * the caller is no longer responsible for deleting it.
825 * @param pattern the given pattern for the format.
826 * @param formatDataToAdopt the symbols to be adopted.
827 * @param status Output param set to success/faulure code.
828 * @stable ICU 2.0
829 */
830 SimpleDateFormat(const UnicodeString& pattern,
831 DateFormatSymbols* formatDataToAdopt,
832 UErrorCode& status);
833
834 /**
835 * Construct a SimpleDateFormat using the given pattern and locale-specific
836 * symbol data. The DateFormatSymbols object is NOT adopted; the caller
837 * remains responsible for deleting it.
838 * @param pattern the given pattern for the format.
839 * @param formatData the formatting symbols to be use.
840 * @param status Output param set to success/faulure code.
841 * @stable ICU 2.0
842 */
843 SimpleDateFormat(const UnicodeString& pattern,
844 const DateFormatSymbols& formatData,
845 UErrorCode& status);
846
847 /**
848 * Copy constructor.
849 * @stable ICU 2.0
850 */
851 SimpleDateFormat(const SimpleDateFormat&);
852
853 /**
854 * Assignment operator.
855 * @stable ICU 2.0
856 */
857 SimpleDateFormat& operator=(const SimpleDateFormat&);
858
859 /**
860 * Destructor.
861 * @stable ICU 2.0
862 */
863 virtual ~SimpleDateFormat();
864
865 /**
866 * Clone this Format object polymorphically. The caller owns the result and
867 * should delete it when done.
868 * @return A copy of the object.
869 * @stable ICU 2.0
870 */
871 virtual SimpleDateFormat* clone() const override;
872
873 /**
874 * Return true if the given Format objects are semantically equal. Objects
875 * of different subclasses are considered unequal.
876 * @param other the object to be compared with.
877 * @return true if the given Format objects are semantically equal.
878 * @stable ICU 2.0
879 */
880 virtual bool operator==(const Format& other) const override;
881
882
883 using DateFormat::format;
884
885 /**
886 * Format a date or time, which is the standard millis since 24:00 GMT, Jan
887 * 1, 1970. Overrides DateFormat pure virtual method.
888 * <P>
889 * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->>
890 * 1996.07.10 AD at 15:08:56 PDT
891 *
892 * @param cal Calendar set to the date and time to be formatted
893 * into a date/time string.
894 * @param appendTo Output parameter to receive result.
895 * Result is appended to existing contents.
896 * @param pos The formatting position. On input: an alignment field,
897 * if desired. On output: the offsets of the alignment field.
898 * @return Reference to 'appendTo' parameter.
899 * @stable ICU 2.1
900 */
901 virtual UnicodeString& format( Calendar& cal,
902 UnicodeString& appendTo,
903 FieldPosition& pos) const override;
904
905 /**
906 * Format a date or time, which is the standard millis since 24:00 GMT, Jan
907 * 1, 1970. Overrides DateFormat pure virtual method.
908 * <P>
909 * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->>
910 * 1996.07.10 AD at 15:08:56 PDT
911 *
912 * @param cal Calendar set to the date and time to be formatted
913 * into a date/time string.
914 * @param appendTo Output parameter to receive result.
915 * Result is appended to existing contents.
916 * @param posIter On return, can be used to iterate over positions
917 * of fields generated by this format call. Field values
918 * are defined in UDateFormatField.
919 * @param status Input/output param set to success/failure code.
920 * @return Reference to 'appendTo' parameter.
921 * @stable ICU 4.4
922 */
923 virtual UnicodeString& format( Calendar& cal,
924 UnicodeString& appendTo,
925 FieldPositionIterator* posIter,
926 UErrorCode& status) const override;
927
928 using DateFormat::parse;
929
930 /**
931 * Parse a date/time string beginning at the given parse position. For
932 * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
933 * that is equivalent to Date(837039928046).
934 * <P>
935 * By default, parsing is lenient: If the input is not in the form used by
936 * this object's format method but can still be parsed as a date, then the
937 * parse succeeds. Clients may insist on strict adherence to the format by
938 * calling setLenient(false).
939 * @see DateFormat::setLenient(boolean)
940 *
941 * @param text The date/time string to be parsed
942 * @param cal A Calendar set on input to the date and time to be used for
943 * missing values in the date/time string being parsed, and set
944 * on output to the parsed date/time. When the calendar type is
945 * different from the internal calendar held by this SimpleDateFormat
946 * instance, the internal calendar will be cloned to a work
947 * calendar set to the same milliseconds and time zone as the
948 * cal parameter, field values will be parsed based on the work
949 * calendar, then the result (milliseconds and time zone) will
950 * be set in this calendar.
951 * @param pos On input, the position at which to start parsing; on
952 * output, the position at which parsing terminated, or the
953 * start position if the parse failed.
954 * @stable ICU 2.1
955 */
956 virtual void parse( const UnicodeString& text,
957 Calendar& cal,
958 ParsePosition& pos) const override;
959
960
961 /**
962 * Set the start UDate used to interpret two-digit year strings.
963 * When dates are parsed having 2-digit year strings, they are placed within
964 * a assumed range of 100 years starting on the two digit start date. For
965 * example, the string "24-Jan-17" may be in the year 1817, 1917, 2017, or
966 * some other year. SimpleDateFormat chooses a year so that the resultant
967 * date is on or after the two digit start date and within 100 years of the
968 * two digit start date.
969 * <P>
970 * By default, the two digit start date is set to 80 years before the current
971 * time at which a SimpleDateFormat object is created.
972 * @param d start UDate used to interpret two-digit year strings.
973 * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with
974 * an error value if there was a parse error.
975 * @stable ICU 2.0
976 */
977 virtual void set2DigitYearStart(UDate d, UErrorCode& status);
978
979 /**
980 * Get the start UDate used to interpret two-digit year strings.
981 * When dates are parsed having 2-digit year strings, they are placed within
982 * a assumed range of 100 years starting on the two digit start date. For
983 * example, the string "24-Jan-17" may be in the year 1817, 1917, 2017, or
984 * some other year. SimpleDateFormat chooses a year so that the resultant
985 * date is on or after the two digit start date and within 100 years of the
986 * two digit start date.
987 * <P>
988 * By default, the two digit start date is set to 80 years before the current
989 * time at which a SimpleDateFormat object is created.
990 * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with
991 * an error value if there was a parse error.
992 * @stable ICU 2.0
993 */
994 UDate get2DigitYearStart(UErrorCode& status) const;
995
996 /**
997 * Return a pattern string describing this date format.
998 * @param result Output param to receive the pattern.
999 * @return A reference to 'result'.
1000 * @stable ICU 2.0
1001 */
1002 virtual UnicodeString& toPattern(UnicodeString& result) const;
1003
1004 /**
1005 * Return a localized pattern string describing this date format.
1006 * In most cases, this will return the same thing as toPattern(),
1007 * but a locale can specify characters to use in pattern descriptions
1008 * in place of the ones described in this class's class documentation.
1009 * (Presumably, letters that would be more mnemonic in that locale's
1010 * language.) This function would produce a pattern using those
1011 * letters.
1012 * <p>
1013 * <b>Note:</b> This implementation depends on DateFormatSymbols::getLocalPatternChars()
1014 * to get localized format pattern characters. ICU does not include
1015 * localized pattern character data, therefore, unless user sets localized
1016 * pattern characters manually, this method returns the same result as
1017 * toPattern().
1018 *
1019 * @param result Receives the localized pattern.
1020 * @param status Output param set to success/failure code on
1021 * exit. If the pattern is invalid, this will be
1022 * set to a failure result.
1023 * @return A reference to 'result'.
1024 * @stable ICU 2.0
1025 */
1026 virtual UnicodeString& toLocalizedPattern(UnicodeString& result,
1027 UErrorCode& status) const;
1028
1029 /**
1030 * Apply the given unlocalized pattern string to this date format.
1031 * (i.e., after this call, this formatter will format dates according to
1032 * the new pattern)
1033 *
1034 * @param pattern The pattern to be applied.
1035 * @stable ICU 2.0
1036 */
1037 virtual void applyPattern(const UnicodeString& pattern);
1038
1039 /**
1040 * Apply the given localized pattern string to this date format.
1041 * (see toLocalizedPattern() for more information on localized patterns.)
1042 *
1043 * @param pattern The localized pattern to be applied.
1044 * @param status Output param set to success/failure code on
1045 * exit. If the pattern is invalid, this will be
1046 * set to a failure result.
1047 * @stable ICU 2.0
1048 */
1049 virtual void applyLocalizedPattern(const UnicodeString& pattern,
1050 UErrorCode& status);
1051
1052 /**
1053 * Gets the date/time formatting symbols (this is an object carrying
1054 * the various strings and other symbols used in formatting: e.g., month
1055 * names and abbreviations, time zone names, AM/PM strings, etc.)
1056 * @return a copy of the date-time formatting data associated
1057 * with this date-time formatter.
1058 * @stable ICU 2.0
1059 */
1060 virtual const DateFormatSymbols* getDateFormatSymbols(void) const;
1061
1062 /**
1063 * Set the date/time formatting symbols. The caller no longer owns the
1064 * DateFormatSymbols object and should not delete it after making this call.
1065 * @param newFormatSymbols the given date-time formatting symbols to copy.
1066 * @stable ICU 2.0
1067 */
1068 virtual void adoptDateFormatSymbols(DateFormatSymbols* newFormatSymbols);
1069
1070 /**
1071 * Set the date/time formatting data.
1072 * @param newFormatSymbols the given date-time formatting symbols to copy.
1073 * @stable ICU 2.0
1074 */
1075 virtual void setDateFormatSymbols(const DateFormatSymbols& newFormatSymbols);
1076
1077 /**
1078 * Return the class ID for this class. This is useful only for comparing to
1079 * a return value from getDynamicClassID(). For example:
1080 * <pre>
1081 * . Base* polymorphic_pointer = createPolymorphicObject();
1082 * . if (polymorphic_pointer->getDynamicClassID() ==
1083 * . erived::getStaticClassID()) ...
1084 * </pre>
1085 * @return The class ID for all objects of this class.
1086 * @stable ICU 2.0
1087 */
1088 static UClassID U_EXPORT2 getStaticClassID(void);
1089
1090 /**
1091 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
1092 * method is to implement a simple version of RTTI, since not all C++
1093 * compilers support genuine RTTI. Polymorphic operator==() and clone()
1094 * methods call this method.
1095 *
1096 * @return The class ID for this object. All objects of a
1097 * given class have the same class ID. Objects of
1098 * other classes have different class IDs.
1099 * @stable ICU 2.0
1100 */
1101 virtual UClassID getDynamicClassID(void) const override;
1102
1103 /**
1104 * Set the calendar to be used by this date format. Initially, the default
1105 * calendar for the specified or default locale is used. The caller should
1106 * not delete the Calendar object after it is adopted by this call.
1107 * Adopting a new calendar will change to the default symbols.
1108 *
1109 * @param calendarToAdopt Calendar object to be adopted.
1110 * @stable ICU 2.0
1111 */
1112 virtual void adoptCalendar(Calendar* calendarToAdopt) override;
1113
1114 /* Cannot use #ifndef U_HIDE_INTERNAL_API for the following methods since they are virtual */
1115 /**
1116 * Sets the TimeZoneFormat to be used by this date/time formatter.
1117 * The caller should not delete the TimeZoneFormat object after
1118 * it is adopted by this call.
1119 * @param timeZoneFormatToAdopt The TimeZoneFormat object to be adopted.
1120 * @internal ICU 49 technology preview
1121 */
1122 virtual void adoptTimeZoneFormat(TimeZoneFormat* timeZoneFormatToAdopt);
1123
1124 /**
1125 * Sets the TimeZoneFormat to be used by this date/time formatter.
1126 * @param newTimeZoneFormat The TimeZoneFormat object to copy.
1127 * @internal ICU 49 technology preview
1128 */
1129 virtual void setTimeZoneFormat(const TimeZoneFormat& newTimeZoneFormat);
1130
1131 /**
1132 * Gets the time zone format object associated with this date/time formatter.
1133 * @return the time zone format associated with this date/time formatter.
1134 * @internal ICU 49 technology preview
1135 */
1136 virtual const TimeZoneFormat* getTimeZoneFormat(void) const;
1137
1138 /**
1139 * Set a particular UDisplayContext value in the formatter, such as
1140 * UDISPCTX_CAPITALIZATION_FOR_STANDALONE. Note: For getContext, see
1141 * DateFormat.
1142 * @param value The UDisplayContext value to set.
1143 * @param status Input/output status. If at entry this indicates a failure
1144 * status, the function will do nothing; otherwise this will be
1145 * updated with any new status from the function.
1146 * @stable ICU 53
1147 */
1148 virtual void setContext(UDisplayContext value, UErrorCode& status) override;
1149
1150 /**
1151 * Overrides base class method and
1152 * This method clears per field NumberFormat instances
1153 * previously set by {@see adoptNumberFormat(const UnicodeString&, NumberFormat*, UErrorCode)}
1154 * @param formatToAdopt the NumbeferFormat used
1155 * @stable ICU 54
1156 */
1157 void adoptNumberFormat(NumberFormat *formatToAdopt) override;
1158
1159 /**
1160 * Allow the user to set the NumberFormat for several fields
1161 * It can be a single field like: "y"(year) or "M"(month)
1162 * It can be several field combined together: "yM"(year and month)
1163 * Note:
1164 * 1 symbol field is enough for multiple symbol field (so "y" will override "yy", "yyy")
1165 * If the field is not numeric, then override has no effect (like "MMM" will use abbreviation, not numerical field)
1166 * Per field NumberFormat can also be cleared in {@see DateFormat::setNumberFormat(const NumberFormat& newNumberFormat)}
1167 *
1168 * @param fields the fields to override(like y)
1169 * @param formatToAdopt the NumbeferFormat used
1170 * @param status Receives a status code, which will be U_ZERO_ERROR
1171 * if the operation succeeds.
1172 * @stable ICU 54
1173 */
1174 void adoptNumberFormat(const UnicodeString& fields, NumberFormat *formatToAdopt, UErrorCode &status);
1175
1176 /**
1177 * Get the numbering system to be used for a particular field.
1178 * @param field The UDateFormatField to get
1179 * @stable ICU 54
1180 */
1181 const NumberFormat * getNumberFormatForField(char16_t field) const;
1182
1183 #ifndef U_HIDE_INTERNAL_API
1184 /**
1185 * This is for ICU internal use only. Please do not use.
1186 * Check whether the 'field' is smaller than all the fields covered in
1187 * pattern, return true if it is. The sequence of calendar field,
1188 * from large to small is: ERA, YEAR, MONTH, DATE, AM_PM, HOUR, MINUTE,...
1189 * @param field the calendar field need to check against
1190 * @return true if the 'field' is smaller than all the fields
1191 * covered in pattern. false otherwise.
1192 * @internal ICU 4.0
1193 */
1194 UBool isFieldUnitIgnored(UCalendarDateFields field) const;
1195
1196
1197 /**
1198 * This is for ICU internal use only. Please do not use.
1199 * Check whether the 'field' is smaller than all the fields covered in
1200 * pattern, return true if it is. The sequence of calendar field,
1201 * from large to small is: ERA, YEAR, MONTH, DATE, AM_PM, HOUR, MINUTE,...
1202 * @param pattern the pattern to check against
1203 * @param field the calendar field need to check against
1204 * @return true if the 'field' is smaller than all the fields
1205 * covered in pattern. false otherwise.
1206 * @internal ICU 4.0
1207 */
1208 static UBool isFieldUnitIgnored(const UnicodeString& pattern,
1209 UCalendarDateFields field);
1210
1211 /**
1212 * This is for ICU internal use only. Please do not use.
1213 * Get the locale of this simple date formatter.
1214 * It is used in DateIntervalFormat.
1215 *
1216 * @return locale in this simple date formatter
1217 * @internal ICU 4.0
1218 */
1219 const Locale& getSmpFmtLocale(void) const;
1220 #endif /* U_HIDE_INTERNAL_API */
1221
1222 private:
1223 friend class DateFormat;
1224 friend class DateIntervalFormat;
1225
1226 void initializeDefaultCentury(void);
1227
1228 void initializeBooleanAttributes(void);
1229
1230 SimpleDateFormat() = delete; // default constructor not implemented
1231
1232 /**
1233 * Used by the DateFormat factory methods to construct a SimpleDateFormat.
1234 * @param timeStyle the time style.
1235 * @param dateStyle the date style.
1236 * @param locale the given locale.
1237 * @param status Output param set to success/failure code on
1238 * exit.
1239 */
1240 SimpleDateFormat(EStyle timeStyle, EStyle dateStyle, const Locale& locale, UErrorCode& status);
1241
1242 /**
1243 * Construct a SimpleDateFormat for the given locale. If no resource data
1244 * is available, create an object of last resort, using hard-coded strings.
1245 * This is an internal method, called by DateFormat. It should never fail.
1246 * @param locale the given locale.
1247 * @param status Output param set to success/failure code on
1248 * exit.
1249 */
1250 SimpleDateFormat(const Locale& locale, UErrorCode& status); // Use default pattern
1251
1252 /**
1253 * Hook called by format(... FieldPosition& ...) and format(...FieldPositionIterator&...)
1254 */
1255 UnicodeString& _format(Calendar& cal, UnicodeString& appendTo, FieldPositionHandler& handler, UErrorCode& status) const;
1256
1257 /**
1258 * Called by format() to format a single field.
1259 *
1260 * @param appendTo Output parameter to receive result.
1261 * Result is appended to existing contents.
1262 * @param ch The format character we encountered in the pattern.
1263 * @param count Number of characters in the current pattern symbol (e.g.,
1264 * "yyyy" in the pattern would result in a call to this function
1265 * with ch equal to 'y' and count equal to 4)
1266 * @param capitalizationContext Capitalization context for this date format.
1267 * @param fieldNum Zero-based numbering of current field within the overall format.
1268 * @param handler Records information about field positions.
1269 * @param cal Calendar to use
1270 * @param status Receives a status code, which will be U_ZERO_ERROR if the operation
1271 * succeeds.
1272 */
1273 void subFormat(UnicodeString &appendTo,
1274 char16_t ch,
1275 int32_t count,
1276 UDisplayContext capitalizationContext,
1277 int32_t fieldNum,
1278 char16_t fieldToOutput,
1279 FieldPositionHandler& handler,
1280 Calendar& cal,
1281 UErrorCode& status) const; // in case of illegal argument
1282
1283 /**
1284 * Used by subFormat() to format a numeric value.
1285 * Appends to toAppendTo a string representation of "value"
1286 * having a number of digits between "minDigits" and
1287 * "maxDigits". Uses the DateFormat's NumberFormat.
1288 *
1289 * @param currentNumberFormat
1290 * @param appendTo Output parameter to receive result.
1291 * Formatted number is appended to existing contents.
1292 * @param value Value to format.
1293 * @param minDigits Minimum number of digits the result should have
1294 * @param maxDigits Maximum number of digits the result should have
1295 */
1296 void zeroPaddingNumber(const NumberFormat *currentNumberFormat,
1297 UnicodeString &appendTo,
1298 int32_t value,
1299 int32_t minDigits,
1300 int32_t maxDigits) const;
1301
1302 /**
1303 * Return true if the given format character, occurring count
1304 * times, represents a numeric field.
1305 */
1306 static UBool isNumeric(char16_t formatChar, int32_t count);
1307
1308 /**
1309 * Returns true if the patternOffset is at the start of a numeric field.
1310 */
1311 static UBool isAtNumericField(const UnicodeString &pattern, int32_t patternOffset);
1312
1313 /**
1314 * Returns true if the patternOffset is right after a non-numeric field.
1315 */
1316 static UBool isAfterNonNumericField(const UnicodeString &pattern, int32_t patternOffset);
1317
1318 /**
1319 * initializes fCalendar from parameters. Returns fCalendar as a convenience.
1320 * @param adoptZone Zone to be adopted, or nullptr for TimeZone::createDefault().
1321 * @param locale Locale of the calendar
1322 * @param status Error code
1323 * @return the newly constructed fCalendar
1324 */
1325 Calendar *initializeCalendar(TimeZone* adoptZone, const Locale& locale, UErrorCode& status);
1326
1327 /**
1328 * Called by several of the constructors to load pattern data and formatting symbols
1329 * out of a resource bundle and initialize the locale based on it.
1330 * @param timeStyle The time style, as passed to DateFormat::createDateInstance().
1331 * @param dateStyle The date style, as passed to DateFormat::createTimeInstance().
1332 * @param locale The locale to load the patterns from.
1333 * @param status Filled in with an error code if loading the data from the
1334 * resources fails.
1335 */
1336 void construct(EStyle timeStyle, EStyle dateStyle, const Locale& locale, UErrorCode& status);
1337
1338 /**
1339 * Called by construct() and the various constructors to set up the SimpleDateFormat's
1340 * Calendar and NumberFormat objects.
1341 * @param locale The locale for which we want a Calendar and a NumberFormat.
1342 * @param status Filled in with an error code if creating either subobject fails.
1343 */
1344 void initialize(const Locale& locale, UErrorCode& status);
1345
1346 /**
1347 * Private code-size reduction function used by subParse.
1348 * @param text the time text being parsed.
1349 * @param start where to start parsing.
1350 * @param field the date field being parsed.
1351 * @param stringArray the string array to parsed.
1352 * @param stringArrayCount the size of the array.
1353 * @param monthPattern pointer to leap month pattern, or nullptr if none.
1354 * @param cal a Calendar set to the date and time to be formatted
1355 * into a date/time string.
1356 * @return the new start position if matching succeeded; a negative number
1357 * indicating matching failure, otherwise.
1358 */
1359 int32_t matchString(const UnicodeString& text, int32_t start, UCalendarDateFields field,
1360 const UnicodeString* stringArray, int32_t stringArrayCount,
1361 const UnicodeString* monthPattern, Calendar& cal) const;
1362
1363 /**
1364 * Private code-size reduction function used by subParse. Only for UCAL_MONTH
1365 * @param text the time text being parsed.
1366 * @param start where to start parsing.
1367 * @param wideStringArray the wide string array to parsed.
1368 * @param shortStringArray the short string array to parsed.
1369 * @param stringArrayCount the size of the string arrays.
1370 * @param cal a Calendar set to the date and time to be formatted
1371 * into a date/time string.
1372 * @return the new start position if matching succeeded; a negative number
1373 * indicating matching failure, otherwise.
1374 */
1375 int32_t matchAlphaMonthStrings(const UnicodeString& text, int32_t start,
1376 const UnicodeString* wideStringArray, const UnicodeString* shortStringArray,
1377 int32_t stringArrayCount, Calendar& cal) const;
1378
1379 /**
1380 * Private code-size reduction function used by subParse.
1381 * @param text the time text being parsed.
1382 * @param start where to start parsing.
1383 * @param field the date field being parsed.
1384 * @param stringArray the string array to parsed.
1385 * @param stringArrayCount the size of the array.
1386 * @param cal a Calendar set to the date and time to be formatted
1387 * into a date/time string.
1388 * @return the new start position if matching succeeded; a negative number
1389 * indicating matching failure, otherwise.
1390 */
1391 int32_t matchQuarterString(const UnicodeString& text, int32_t start, UCalendarDateFields field,
1392 const UnicodeString* stringArray, int32_t stringArrayCount, Calendar& cal) const;
1393
1394 /**
1395 * Used by subParse() to match localized day period strings.
1396 */
1397 int32_t matchDayPeriodStrings(const UnicodeString& text, int32_t start,
1398 const UnicodeString* stringArray, int32_t stringArrayCount,
1399 int32_t &dayPeriod) const;
1400
1401 /**
1402 * Private function used by subParse to match literal pattern text.
1403 *
1404 * @param pattern the pattern string
1405 * @param patternOffset the starting offset into the pattern text. On
1406 * output will be set the offset of the first non-literal character in the pattern
1407 * @param text the text being parsed
1408 * @param textOffset the starting offset into the text. On output
1409 * will be set to the offset of the character after the match
1410 * @param whitespaceLenient <code>true</code> if whitespace parse is lenient, <code>false</code> otherwise.
1411 * @param partialMatchLenient <code>true</code> if partial match parse is lenient, <code>false</code> otherwise.
1412 * @param oldLeniency <code>true</code> if old leniency control is lenient, <code>false</code> otherwise.
1413 *
1414 * @return <code>true</code> if the literal text could be matched, <code>false</code> otherwise.
1415 */
1416 static UBool matchLiterals(const UnicodeString &pattern, int32_t &patternOffset,
1417 const UnicodeString &text, int32_t &textOffset,
1418 UBool whitespaceLenient, UBool partialMatchLenient, UBool oldLeniency);
1419
1420 /**
1421 * Private member function that converts the parsed date strings into
1422 * timeFields. Returns -start (for ParsePosition) if failed.
1423 * @param text the time text to be parsed.
1424 * @param start where to start parsing.
1425 * @param ch the pattern character for the date field text to be parsed.
1426 * @param count the count of a pattern character.
1427 * @param obeyCount if true then the count is strictly obeyed.
1428 * @param allowNegative
1429 * @param ambiguousYear If true then the two-digit year == the default start year.
1430 * @param saveHebrewMonth Used to hang onto month until year is known.
1431 * @param cal a Calendar set to the date and time to be formatted
1432 * into a date/time string.
1433 * @param patLoc
1434 * @param numericLeapMonthFormatter If non-null, used to parse numeric leap months.
1435 * @param tzTimeType the type of parsed time zone - standard, daylight or unknown (output).
1436 * This parameter can be nullptr if caller does not need the information.
1437 * @return the new start position if matching succeeded; a negative number
1438 * indicating matching failure, otherwise.
1439 */
1440 int32_t subParse(const UnicodeString& text, int32_t& start, char16_t ch, int32_t count,
1441 UBool obeyCount, UBool allowNegative, UBool ambiguousYear[], int32_t& saveHebrewMonth, Calendar& cal,
1442 int32_t patLoc, MessageFormat * numericLeapMonthFormatter, UTimeZoneFormatTimeType *tzTimeType,
1443 int32_t *dayPeriod=nullptr) const;
1444
1445 void parseInt(const UnicodeString& text,
1446 Formattable& number,
1447 ParsePosition& pos,
1448 UBool allowNegative,
1449 const NumberFormat *fmt) const;
1450
1451 void parseInt(const UnicodeString& text,
1452 Formattable& number,
1453 int32_t maxDigits,
1454 ParsePosition& pos,
1455 UBool allowNegative,
1456 const NumberFormat *fmt) const;
1457
1458 int32_t checkIntSuffix(const UnicodeString& text, int32_t start,
1459 int32_t patLoc, UBool isNegative) const;
1460
1461 /**
1462 * Counts number of digit code points in the specified text.
1463 *
1464 * @param text input text
1465 * @param start start index, inclusive
1466 * @param end end index, exclusive
1467 * @return number of digits found in the text in the specified range.
1468 */
1469 int32_t countDigits(const UnicodeString& text, int32_t start, int32_t end) const;
1470
1471 /**
1472 * Translate a pattern, mapping each character in the from string to the
1473 * corresponding character in the to string. Return an error if the original
1474 * pattern contains an unmapped character, or if a quote is unmatched.
1475 * Quoted (single quotes only) material is not translated.
1476 * @param originalPattern the original pattern.
1477 * @param translatedPattern Output param to receive the translited pattern.
1478 * @param from the characters to be translited from.
1479 * @param to the characters to be translited to.
1480 * @param status Receives a status code, which will be U_ZERO_ERROR
1481 * if the operation succeeds.
1482 */
1483 static void translatePattern(const UnicodeString& originalPattern,
1484 UnicodeString& translatedPattern,
1485 const UnicodeString& from,
1486 const UnicodeString& to,
1487 UErrorCode& status);
1488
1489 /**
1490 * Sets the starting date of the 100-year window that dates with 2-digit years
1491 * are considered to fall within.
1492 * @param startDate the start date
1493 * @param status Receives a status code, which will be U_ZERO_ERROR
1494 * if the operation succeeds.
1495 */
1496 void parseAmbiguousDatesAsAfter(UDate startDate, UErrorCode& status);
1497
1498 /**
1499 * Return the length matched by the given affix, or -1 if none.
1500 * Runs of white space in the affix, match runs of white space in
1501 * the input.
1502 * @param affix pattern string, taken as a literal
1503 * @param input input text
1504 * @param pos offset into input at which to begin matching
1505 * @return length of input that matches, or -1 if match failure
1506 */
1507 int32_t compareSimpleAffix(const UnicodeString& affix,
1508 const UnicodeString& input,
1509 int32_t pos) const;
1510
1511 /**
1512 * Skip over a run of zero or more Pattern_White_Space characters at
1513 * pos in text.
1514 */
1515 int32_t skipPatternWhiteSpace(const UnicodeString& text, int32_t pos) const;
1516
1517 /**
1518 * Skip over a run of zero or more isUWhiteSpace() characters at pos
1519 * in text.
1520 */
1521 int32_t skipUWhiteSpace(const UnicodeString& text, int32_t pos) const;
1522
1523 /**
1524 * Initialize SimpleNumberFormat instance
1525 */
1526 void initSimpleNumberFormatter(UErrorCode &status);
1527
1528 /**
1529 * Initialize NumberFormat instances used for numbering system overrides.
1530 */
1531 void initNumberFormatters(const Locale &locale,UErrorCode &status);
1532
1533 /**
1534 * Parse the given override string and set up structures for number formats
1535 */
1536 void processOverrideString(const Locale &locale, const UnicodeString &str, int8_t type, UErrorCode &status);
1537
1538 /**
1539 * Used to map pattern characters to Calendar field identifiers.
1540 */
1541 static const UCalendarDateFields fgPatternIndexToCalendarField[];
1542
1543 /**
1544 * Map index into pattern character string to DateFormat field number
1545 */
1546 static const UDateFormatField fgPatternIndexToDateFormatField[];
1547
1548 /**
1549 * Lazy TimeZoneFormat instantiation, semantically const
1550 */
1551 TimeZoneFormat *tzFormat(UErrorCode &status) const;
1552
1553 const NumberFormat* getNumberFormatByIndex(UDateFormatField index) const;
1554
1555 /**
1556 * Used to map Calendar field to field level.
1557 * The larger the level, the smaller the field unit.
1558 * For example, UCAL_ERA level is 0, UCAL_YEAR level is 10,
1559 * UCAL_MONTH level is 20.
1560 */
1561 static const int32_t fgCalendarFieldToLevel[];
1562
1563 /**
1564 * Map calendar field letter into calendar field level.
1565 */
1566 static int32_t getLevelFromChar(char16_t ch);
1567
1568 /**
1569 * Tell if a character can be used to define a field in a format string.
1570 */
1571 static UBool isSyntaxChar(char16_t ch);
1572
1573 /**
1574 * The formatting pattern for this formatter.
1575 */
1576 UnicodeString fPattern;
1577
1578 /**
1579 * The numbering system override for dates.
1580 */
1581 UnicodeString fDateOverride;
1582
1583 /**
1584 * The numbering system override for times.
1585 */
1586 UnicodeString fTimeOverride;
1587
1588
1589 /**
1590 * The original locale used (for reloading symbols)
1591 */
1592 Locale fLocale;
1593
1594 /**
1595 * A pointer to an object containing the strings to use in formatting (e.g.,
1596 * month and day names, AM and PM strings, time zone names, etc.)
1597 */
1598 DateFormatSymbols* fSymbols = nullptr; // Owned
1599
1600 /**
1601 * The time zone formatter
1602 */
1603 TimeZoneFormat* fTimeZoneFormat = nullptr;
1604
1605 /**
1606 * If dates have ambiguous years, we map them into the century starting
1607 * at defaultCenturyStart, which may be any date. If defaultCenturyStart is
1608 * set to SYSTEM_DEFAULT_CENTURY, which it is by default, then the system
1609 * values are used. The instance values defaultCenturyStart and
1610 * defaultCenturyStartYear are only used if explicitly set by the user
1611 * through the API method parseAmbiguousDatesAsAfter().
1612 */
1613 UDate fDefaultCenturyStart;
1614
1615 UBool fHasMinute;
1616 UBool fHasSecond;
1617 UBool fHasHanYearChar; // pattern contains the Han year character \u5E74
1618
1619 /**
1620 * Sets fHasMinutes and fHasSeconds.
1621 */
1622 void parsePattern();
1623
1624 /**
1625 * See documentation for defaultCenturyStart.
1626 */
1627 /*transient*/ int32_t fDefaultCenturyStartYear;
1628
1629 struct NSOverride : public UMemory {
1630 const SharedNumberFormat *snf;
1631 int32_t hash;
1632 NSOverride *next;
1633 void free();
NSOverrideNSOverride1634 NSOverride() : snf(nullptr), hash(0), next(nullptr) {
1635 }
1636 ~NSOverride();
1637 };
1638
1639 /**
1640 * The number format in use for each date field. nullptr means fall back
1641 * to fNumberFormat in DateFormat.
1642 */
1643 const SharedNumberFormat **fSharedNumberFormatters = nullptr;
1644
1645 /**
1646 * Number formatter pre-allocated for fast performance
1647 *
1648 * This references the decimal symbols from fNumberFormatter if it is an instance
1649 * of DecimalFormat (and is otherwise null). This should always be cleaned up before
1650 * destroying fNumberFormatter.
1651 */
1652 const number::SimpleNumberFormatter* fSimpleNumberFormatter = nullptr;
1653
1654 UBool fHaveDefaultCentury;
1655
1656 const BreakIterator* fCapitalizationBrkIter = nullptr;
1657 };
1658
1659 inline UDate
get2DigitYearStart(UErrorCode &)1660 SimpleDateFormat::get2DigitYearStart(UErrorCode& /*status*/) const
1661 {
1662 return fDefaultCenturyStart;
1663 }
1664
1665 U_NAMESPACE_END
1666
1667 #endif /* #if !UCONFIG_NO_FORMATTING */
1668
1669 #endif /* U_SHOW_CPLUSPLUS_API */
1670
1671 #endif // _SMPDTFMT
1672 //eof
1673