1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3/*
4*******************************************************************************
5* Copyright (C) 1997-2016, International Business Machines Corporation and *
6* others. All Rights Reserved. *
7*******************************************************************************
8*
9* File SMPDTFMT.CPP
10*
11* Modification History:
12*
13* Date Name Description
14* 02/19/97 aliu Converted from java.
15* 03/31/97 aliu Modified extensively to work with 50 locales.
16* 04/01/97 aliu Added support for centuries.
17* 07/09/97 helena Made ParsePosition into a class.
18* 07/21/98 stephen Added initializeDefaultCentury.
19* Removed getZoneIndex (added in DateFormatSymbols)
20* Removed subParseLong
21* Removed chk
22* 02/22/99 stephen Removed character literals for EBCDIC safety
23* 10/14/99 aliu Updated 2-digit year parsing so that only "00" thru
24* "99" are recognized. {j28 4182066}
25* 11/15/99 weiv Added support for week of year/day of week format
26********************************************************************************
27*/
28
29#define ZID_KEY_MAX 128
30
31#include "unicode/utypes.h"
32
33#if !UCONFIG_NO_FORMATTING
34#include "unicode/smpdtfmt.h"
35#include "unicode/dtfmtsym.h"
36#include "unicode/ures.h"
37#include "unicode/msgfmt.h"
38#include "unicode/calendar.h"
39#include "unicode/gregocal.h"
40#include "unicode/timezone.h"
41#include "unicode/decimfmt.h"
42#include "unicode/dcfmtsym.h"
43#include "unicode/uchar.h"
44#include "unicode/uniset.h"
45#include "unicode/ustring.h"
46#include "unicode/basictz.h"
47#include "unicode/simpleformatter.h"
48#include "unicode/simpletz.h"
49#include "unicode/rbtz.h"
50#include "unicode/tzfmt.h"
51#include "unicode/ucasemap.h"
52#include "unicode/utf16.h"
53#include "unicode/vtzone.h"
54#include "unicode/udisplaycontext.h"
55#include "unicode/brkiter.h"
56#include "unicode/rbnf.h"
57#include "uresimp.h"
58#include "olsontz.h"
59#include "patternprops.h"
60#include "fphdlimp.h"
61#include "hebrwcal.h"
62#include "cstring.h"
63#include "uassert.h"
64#include "cmemory.h"
65#include "umutex.h"
66#include <float.h>
67#include "smpdtfst.h"
68#include "sharednumberformat.h"
69#include "ucasemap_imp.h"
70#include "ustr_imp.h"
71#include "charstr.h"
72#include "uvector.h"
73#include "cstr.h"
74#include "dayperiodrules.h"
75#include "tznames_impl.h" // ZONE_NAME_U16_MAX
76#include "number_utypes.h"
77
78#if defined( U_DEBUG_CALSVC ) || defined (U_DEBUG_CAL)
79#include <stdio.h>
80#endif
81
82// *****************************************************************************
83// class SimpleDateFormat
84// *****************************************************************************
85
86U_NAMESPACE_BEGIN
87
88/**
89 * Last-resort string to use for "GMT" when constructing time zone strings.
90 */
91// For time zones that have no names, use strings GMT+minutes and
92// GMT-minutes. For instance, in France the time zone is GMT+60.
93// Also accepted are GMT+H:MM or GMT-H:MM.
94// Currently not being used
95//static const UChar gGmt[] = {0x0047, 0x004D, 0x0054, 0x0000}; // "GMT"
96//static const UChar gGmtPlus[] = {0x0047, 0x004D, 0x0054, 0x002B, 0x0000}; // "GMT+"
97//static const UChar gGmtMinus[] = {0x0047, 0x004D, 0x0054, 0x002D, 0x0000}; // "GMT-"
98//static const UChar gDefGmtPat[] = {0x0047, 0x004D, 0x0054, 0x007B, 0x0030, 0x007D, 0x0000}; /* GMT{0} */
99//static const UChar gDefGmtNegHmsPat[] = {0x002D, 0x0048, 0x0048, 0x003A, 0x006D, 0x006D, 0x003A, 0x0073, 0x0073, 0x0000}; /* -HH:mm:ss */
100//static const UChar gDefGmtNegHmPat[] = {0x002D, 0x0048, 0x0048, 0x003A, 0x006D, 0x006D, 0x0000}; /* -HH:mm */
101//static const UChar gDefGmtPosHmsPat[] = {0x002B, 0x0048, 0x0048, 0x003A, 0x006D, 0x006D, 0x003A, 0x0073, 0x0073, 0x0000}; /* +HH:mm:ss */
102//static const UChar gDefGmtPosHmPat[] = {0x002B, 0x0048, 0x0048, 0x003A, 0x006D, 0x006D, 0x0000}; /* +HH:mm */
103//static const UChar gUt[] = {0x0055, 0x0054, 0x0000}; // "UT"
104//static const UChar gUtc[] = {0x0055, 0x0054, 0x0043, 0x0000}; // "UT"
105
106typedef enum GmtPatSize {
107 kGmtLen = 3,
108 kGmtPatLen = 6,
109 kNegHmsLen = 9,
110 kNegHmLen = 6,
111 kPosHmsLen = 9,
112 kPosHmLen = 6,
113 kUtLen = 2,
114 kUtcLen = 3
115} GmtPatSize;
116
117// Stuff needed for numbering system overrides
118
119typedef enum OvrStrType {
120 kOvrStrDate = 0,
121 kOvrStrTime = 1,
122 kOvrStrBoth = 2
123} OvrStrType;
124
125static const UDateFormatField kDateFields[] = {
126 UDAT_YEAR_FIELD,
127 UDAT_MONTH_FIELD,
128 UDAT_DATE_FIELD,
129 UDAT_DAY_OF_YEAR_FIELD,
130 UDAT_DAY_OF_WEEK_IN_MONTH_FIELD,
131 UDAT_WEEK_OF_YEAR_FIELD,
132 UDAT_WEEK_OF_MONTH_FIELD,
133 UDAT_YEAR_WOY_FIELD,
134 UDAT_EXTENDED_YEAR_FIELD,
135 UDAT_JULIAN_DAY_FIELD,
136 UDAT_STANDALONE_DAY_FIELD,
137 UDAT_STANDALONE_MONTH_FIELD,
138 UDAT_QUARTER_FIELD,
139 UDAT_STANDALONE_QUARTER_FIELD,
140 UDAT_YEAR_NAME_FIELD,
141 UDAT_RELATED_YEAR_FIELD };
142static const int8_t kDateFieldsCount = 16;
143
144static const UDateFormatField kTimeFields[] = {
145 UDAT_HOUR_OF_DAY1_FIELD,
146 UDAT_HOUR_OF_DAY0_FIELD,
147 UDAT_MINUTE_FIELD,
148 UDAT_SECOND_FIELD,
149 UDAT_FRACTIONAL_SECOND_FIELD,
150 UDAT_HOUR1_FIELD,
151 UDAT_HOUR0_FIELD,
152 UDAT_MILLISECONDS_IN_DAY_FIELD,
153 UDAT_TIMEZONE_RFC_FIELD,
154 UDAT_TIMEZONE_LOCALIZED_GMT_OFFSET_FIELD };
155static const int8_t kTimeFieldsCount = 10;
156
157
158// This is a pattern-of-last-resort used when we can't load a usable pattern out
159// of a resource.
160static const UChar gDefaultPattern[] =
161{
162 0x79, 0x79, 0x79, 0x79, 0x4D, 0x4D, 0x64, 0x64, 0x20, 0x68, 0x68, 0x3A, 0x6D, 0x6D, 0x20, 0x61, 0
163}; /* "yyyyMMdd hh:mm a" */
164
165// This prefix is designed to NEVER MATCH real text, in order to
166// suppress the parsing of negative numbers. Adjust as needed (if
167// this becomes valid Unicode).
168static const UChar SUPPRESS_NEGATIVE_PREFIX[] = {0xAB00, 0};
169
170/**
171 * These are the tags we expect to see in normal resource bundle files associated
172 * with a locale.
173 */
174static const UChar QUOTE = 0x27; // Single quote
175
176/*
177 * The field range check bias for each UDateFormatField.
178 * The bias is added to the minimum and maximum values
179 * before they are compared to the parsed number.
180 * For example, the calendar stores zero-based month numbers
181 * but the parsed month numbers start at 1, so the bias is 1.
182 *
183 * A value of -1 means that the value is not checked.
184 */
185static const int32_t gFieldRangeBias[] = {
186 -1, // 'G' - UDAT_ERA_FIELD
187 -1, // 'y' - UDAT_YEAR_FIELD
188 1, // 'M' - UDAT_MONTH_FIELD
189 0, // 'd' - UDAT_DATE_FIELD
190 -1, // 'k' - UDAT_HOUR_OF_DAY1_FIELD
191 -1, // 'H' - UDAT_HOUR_OF_DAY0_FIELD
192 0, // 'm' - UDAT_MINUTE_FIELD
193 0, // 's' - UDAT_SECOND_FIELD
194 -1, // 'S' - UDAT_FRACTIONAL_SECOND_FIELD (0-999?)
195 -1, // 'E' - UDAT_DAY_OF_WEEK_FIELD (1-7?)
196 -1, // 'D' - UDAT_DAY_OF_YEAR_FIELD (1 - 366?)
197 -1, // 'F' - UDAT_DAY_OF_WEEK_IN_MONTH_FIELD (1-5?)
198 -1, // 'w' - UDAT_WEEK_OF_YEAR_FIELD (1-52?)
199 -1, // 'W' - UDAT_WEEK_OF_MONTH_FIELD (1-5?)
200 -1, // 'a' - UDAT_AM_PM_FIELD
201 -1, // 'h' - UDAT_HOUR1_FIELD
202 -1, // 'K' - UDAT_HOUR0_FIELD
203 -1, // 'z' - UDAT_TIMEZONE_FIELD
204 -1, // 'Y' - UDAT_YEAR_WOY_FIELD
205 -1, // 'e' - UDAT_DOW_LOCAL_FIELD
206 -1, // 'u' - UDAT_EXTENDED_YEAR_FIELD
207 -1, // 'g' - UDAT_JULIAN_DAY_FIELD
208 -1, // 'A' - UDAT_MILLISECONDS_IN_DAY_FIELD
209 -1, // 'Z' - UDAT_TIMEZONE_RFC_FIELD
210 -1, // 'v' - UDAT_TIMEZONE_GENERIC_FIELD
211 0, // 'c' - UDAT_STANDALONE_DAY_FIELD
212 1, // 'L' - UDAT_STANDALONE_MONTH_FIELD
213 -1, // 'Q' - UDAT_QUARTER_FIELD (1-4?)
214 -1, // 'q' - UDAT_STANDALONE_QUARTER_FIELD
215 -1, // 'V' - UDAT_TIMEZONE_SPECIAL_FIELD
216 -1, // 'U' - UDAT_YEAR_NAME_FIELD
217 -1, // 'O' - UDAT_TIMEZONE_LOCALIZED_GMT_OFFSET_FIELD
218 -1, // 'X' - UDAT_TIMEZONE_ISO_FIELD
219 -1, // 'x' - UDAT_TIMEZONE_ISO_LOCAL_FIELD
220 -1, // 'r' - UDAT_RELATED_YEAR_FIELD
221#if UDAT_HAS_PATTERN_CHAR_FOR_TIME_SEPARATOR
222 -1, // ':' - UDAT_TIME_SEPARATOR_FIELD
223#else
224 -1, // (no pattern character currently) - UDAT_TIME_SEPARATOR_FIELD
225#endif
226};
227
228// When calendar uses hebr numbering (i.e. he@calendar=hebrew),
229// offset the years within the current millenium down to 1-999
230static const int32_t HEBREW_CAL_CUR_MILLENIUM_START_YEAR = 5000;
231static const int32_t HEBREW_CAL_CUR_MILLENIUM_END_YEAR = 6000;
232
233static UMutex LOCK;
234
235UOBJECT_DEFINE_RTTI_IMPLEMENTATION(SimpleDateFormat)
236
237SimpleDateFormat::NSOverride::~NSOverride() {
238 if (snf != NULL) {
239 snf->removeRef();
240 }
241}
242
243
244void SimpleDateFormat::NSOverride::free() {
245 NSOverride *cur = this;
246 while (cur) {
247 NSOverride *next_temp = cur->next;
248 delete cur;
249 cur = next_temp;
250 }
251}
252
253// no matter what the locale's default number format looked like, we want
254// to modify it so that it doesn't use thousands separators, doesn't always
255// show the decimal point, and recognizes integers only when parsing
256static void fixNumberFormatForDates(NumberFormat &nf) {
257 nf.setGroupingUsed(FALSE);
258 DecimalFormat* decfmt = dynamic_cast<DecimalFormat*>(&nf);
259 if (decfmt != NULL) {
260 decfmt->setDecimalSeparatorAlwaysShown(FALSE);
261 }
262 nf.setParseIntegerOnly(TRUE);
263 nf.setMinimumFractionDigits(0); // To prevent "Jan 1.00, 1997.00"
264}
265
266static const SharedNumberFormat *createSharedNumberFormat(
267 NumberFormat *nfToAdopt) {
268 fixNumberFormatForDates(*nfToAdopt);
269 const SharedNumberFormat *result = new SharedNumberFormat(nfToAdopt);
270 if (result == NULL) {
271 delete nfToAdopt;
272 }
273 return result;
274}
275
276static const SharedNumberFormat *createSharedNumberFormat(
277 const Locale &loc, UErrorCode &status) {
278 NumberFormat *nf = NumberFormat::createInstance(loc, status);
279 if (U_FAILURE(status)) {
280 return NULL;
281 }
282 const SharedNumberFormat *result = createSharedNumberFormat(nf);
283 if (result == NULL) {
284 status = U_MEMORY_ALLOCATION_ERROR;
285 }
286 return result;
287}
288
289static const SharedNumberFormat **allocSharedNumberFormatters() {
290 const SharedNumberFormat **result = (const SharedNumberFormat**)
291 uprv_malloc(UDAT_FIELD_COUNT * sizeof(const SharedNumberFormat*));
292 if (result == NULL) {
293 return NULL;
294 }
295 for (int32_t i = 0; i < UDAT_FIELD_COUNT; ++i) {
296 result[i] = NULL;
297 }
298 return result;
299}
300
301static void freeSharedNumberFormatters(const SharedNumberFormat ** list) {
302 for (int32_t i = 0; i < UDAT_FIELD_COUNT; ++i) {
303 SharedObject::clearPtr(list[i]);
304 }
305 uprv_free(list);
306}
307
308const NumberFormat *SimpleDateFormat::getNumberFormatByIndex(
309 UDateFormatField index) const {
310 if (fSharedNumberFormatters == NULL ||
311 fSharedNumberFormatters[index] == NULL) {
312 return fNumberFormat;
313 }
314 return &(**fSharedNumberFormatters[index]);
315}
316
317//----------------------------------------------------------------------
318
319SimpleDateFormat::~SimpleDateFormat()
320{
321 delete fSymbols;
322 if (fSharedNumberFormatters) {
323 freeSharedNumberFormatters(fSharedNumberFormatters);
324 }
325 if (fTimeZoneFormat) {
326 delete fTimeZoneFormat;
327 }
328 freeFastNumberFormatters();
329
330#if !UCONFIG_NO_BREAK_ITERATION
331 delete fCapitalizationBrkIter;
332#endif
333}
334
335//----------------------------------------------------------------------
336
337SimpleDateFormat::SimpleDateFormat(UErrorCode& status)
338 : fLocale(Locale::getDefault()),
339 fSymbols(NULL),
340 fTimeZoneFormat(NULL),
341 fSharedNumberFormatters(NULL),
342 fCapitalizationBrkIter(NULL)
343{
344 initializeBooleanAttributes();
345 construct(kShort, (EStyle) (kShort + kDateOffset), fLocale, status);
346 initializeDefaultCentury();
347}
348
349//----------------------------------------------------------------------
350
351SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
352 UErrorCode &status)
353: fPattern(pattern),
354 fLocale(Locale::getDefault()),
355 fSymbols(NULL),
356 fTimeZoneFormat(NULL),
357 fSharedNumberFormatters(NULL),
358 fCapitalizationBrkIter(NULL)
359{
360 fDateOverride.setToBogus();
361 fTimeOverride.setToBogus();
362 initializeBooleanAttributes();
363 initializeCalendar(NULL,fLocale,status);
364 fSymbols = DateFormatSymbols::createForLocale(fLocale, status);
365 initialize(fLocale, status);
366 initializeDefaultCentury();
367
368}
369//----------------------------------------------------------------------
370
371SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
372 const UnicodeString& override,
373 UErrorCode &status)
374: fPattern(pattern),
375 fLocale(Locale::getDefault()),
376 fSymbols(NULL),
377 fTimeZoneFormat(NULL),
378 fSharedNumberFormatters(NULL),
379 fCapitalizationBrkIter(NULL)
380{
381 fDateOverride.setTo(override);
382 fTimeOverride.setToBogus();
383 initializeBooleanAttributes();
384 initializeCalendar(NULL,fLocale,status);
385 fSymbols = DateFormatSymbols::createForLocale(fLocale, status);
386 initialize(fLocale, status);
387 initializeDefaultCentury();
388
389 processOverrideString(fLocale,override,kOvrStrBoth,status);
390
391}
392
393//----------------------------------------------------------------------
394
395SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
396 const Locale& locale,
397 UErrorCode& status)
398: fPattern(pattern),
399 fLocale(locale),
400 fTimeZoneFormat(NULL),
401 fSharedNumberFormatters(NULL),
402 fCapitalizationBrkIter(NULL)
403{
404
405 fDateOverride.setToBogus();
406 fTimeOverride.setToBogus();
407 initializeBooleanAttributes();
408
409 initializeCalendar(NULL,fLocale,status);
410 fSymbols = DateFormatSymbols::createForLocale(fLocale, status);
411 initialize(fLocale, status);
412 initializeDefaultCentury();
413}
414
415//----------------------------------------------------------------------
416
417SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
418 const UnicodeString& override,
419 const Locale& locale,
420 UErrorCode& status)
421: fPattern(pattern),
422 fLocale(locale),
423 fTimeZoneFormat(NULL),
424 fSharedNumberFormatters(NULL),
425 fCapitalizationBrkIter(NULL)
426{
427
428 fDateOverride.setTo(override);
429 fTimeOverride.setToBogus();
430 initializeBooleanAttributes();
431
432 initializeCalendar(NULL,fLocale,status);
433 fSymbols = DateFormatSymbols::createForLocale(fLocale, status);
434 initialize(fLocale, status);
435 initializeDefaultCentury();
436
437 processOverrideString(locale,override,kOvrStrBoth,status);
438
439}
440
441//----------------------------------------------------------------------
442
443SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
444 DateFormatSymbols* symbolsToAdopt,
445 UErrorCode& status)
446: fPattern(pattern),
447 fLocale(Locale::getDefault()),
448 fSymbols(symbolsToAdopt),
449 fTimeZoneFormat(NULL),
450 fSharedNumberFormatters(NULL),
451 fCapitalizationBrkIter(NULL)
452{
453
454 fDateOverride.setToBogus();
455 fTimeOverride.setToBogus();
456 initializeBooleanAttributes();
457
458 initializeCalendar(NULL,fLocale,status);
459 initialize(fLocale, status);
460 initializeDefaultCentury();
461}
462
463//----------------------------------------------------------------------
464
465SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
466 const DateFormatSymbols& symbols,
467 UErrorCode& status)
468: fPattern(pattern),
469 fLocale(Locale::getDefault()),
470 fSymbols(new DateFormatSymbols(symbols)),
471 fTimeZoneFormat(NULL),
472 fSharedNumberFormatters(NULL),
473 fCapitalizationBrkIter(NULL)
474{
475
476 fDateOverride.setToBogus();
477 fTimeOverride.setToBogus();
478 initializeBooleanAttributes();
479
480 initializeCalendar(NULL, fLocale, status);
481 initialize(fLocale, status);
482 initializeDefaultCentury();
483}
484
485//----------------------------------------------------------------------
486
487// Not for public consumption; used by DateFormat
488SimpleDateFormat::SimpleDateFormat(EStyle timeStyle,
489 EStyle dateStyle,
490 const Locale& locale,
491 UErrorCode& status)
492: fLocale(locale),
493 fSymbols(NULL),
494 fTimeZoneFormat(NULL),
495 fSharedNumberFormatters(NULL),
496 fCapitalizationBrkIter(NULL)
497{
498 initializeBooleanAttributes();
499 construct(timeStyle, dateStyle, fLocale, status);
500 if(U_SUCCESS(status)) {
501 initializeDefaultCentury();
502 }
503}
504
505//----------------------------------------------------------------------
506
507/**
508 * Not for public consumption; used by DateFormat. This constructor
509 * never fails. If the resource data is not available, it uses the
510 * the last resort symbols.
511 */
512SimpleDateFormat::SimpleDateFormat(const Locale& locale,
513 UErrorCode& status)
514: fPattern(gDefaultPattern),
515 fLocale(locale),
516 fSymbols(NULL),
517 fTimeZoneFormat(NULL),
518 fSharedNumberFormatters(NULL),
519 fCapitalizationBrkIter(NULL)
520{
521 if (U_FAILURE(status)) return;
522 initializeBooleanAttributes();
523 initializeCalendar(NULL, fLocale, status);
524 fSymbols = DateFormatSymbols::createForLocale(fLocale, status);
525 if (U_FAILURE(status))
526 {
527 status = U_ZERO_ERROR;
528 delete fSymbols;
529 // This constructor doesn't fail; it uses last resort data
530 fSymbols = new DateFormatSymbols(status);
531 /* test for NULL */
532 if (fSymbols == 0) {
533 status = U_MEMORY_ALLOCATION_ERROR;
534 return;
535 }
536 }
537
538 fDateOverride.setToBogus();
539 fTimeOverride.setToBogus();
540
541 initialize(fLocale, status);
542 if(U_SUCCESS(status)) {
543 initializeDefaultCentury();
544 }
545}
546
547//----------------------------------------------------------------------
548
549SimpleDateFormat::SimpleDateFormat(const SimpleDateFormat& other)
550: DateFormat(other),
551 fLocale(other.fLocale),
552 fSymbols(NULL),
553 fTimeZoneFormat(NULL),
554 fSharedNumberFormatters(NULL),
555 fCapitalizationBrkIter(NULL)
556{
557 initializeBooleanAttributes();
558 *this = other;
559}
560
561//----------------------------------------------------------------------
562
563SimpleDateFormat& SimpleDateFormat::operator=(const SimpleDateFormat& other)
564{
565 if (this == &other) {
566 return *this;
567 }
568 DateFormat::operator=(other);
569 fDateOverride = other.fDateOverride;
570 fTimeOverride = other.fTimeOverride;
571
572 delete fSymbols;
573 fSymbols = NULL;
574
575 if (other.fSymbols)
576 fSymbols = new DateFormatSymbols(*other.fSymbols);
577
578 fDefaultCenturyStart = other.fDefaultCenturyStart;
579 fDefaultCenturyStartYear = other.fDefaultCenturyStartYear;
580 fHaveDefaultCentury = other.fHaveDefaultCentury;
581
582 fPattern = other.fPattern;
583 fHasMinute = other.fHasMinute;
584 fHasSecond = other.fHasSecond;
585
586 // TimeZoneFormat in ICU4C only depends on a locale for now
587 if (fLocale != other.fLocale) {
588 delete fTimeZoneFormat;
589 fTimeZoneFormat = NULL; // forces lazy instantiation with the other locale
590 fLocale = other.fLocale;
591 }
592
593#if !UCONFIG_NO_BREAK_ITERATION
594 if (other.fCapitalizationBrkIter != NULL) {
595 fCapitalizationBrkIter = (other.fCapitalizationBrkIter)->clone();
596 }
597#endif
598
599 if (fSharedNumberFormatters != NULL) {
600 freeSharedNumberFormatters(fSharedNumberFormatters);
601 fSharedNumberFormatters = NULL;
602 }
603 if (other.fSharedNumberFormatters != NULL) {
604 fSharedNumberFormatters = allocSharedNumberFormatters();
605 if (fSharedNumberFormatters) {
606 for (int32_t i = 0; i < UDAT_FIELD_COUNT; ++i) {
607 SharedObject::copyPtr(
608 other.fSharedNumberFormatters[i],
609 fSharedNumberFormatters[i]);
610 }
611 }
612 }
613
614 UErrorCode localStatus = U_ZERO_ERROR;
615 freeFastNumberFormatters();
616 initFastNumberFormatters(localStatus);
617
618 return *this;
619}
620
621//----------------------------------------------------------------------
622
623SimpleDateFormat*
624SimpleDateFormat::clone() const
625{
626 return new SimpleDateFormat(*this);
627}
628
629//----------------------------------------------------------------------
630
631UBool
632SimpleDateFormat::operator==(const Format& other) const
633{
634 if (DateFormat::operator==(other)) {
635 // The DateFormat::operator== check for fCapitalizationContext equality above
636 // is sufficient to check equality of all derived context-related data.
637 // DateFormat::operator== guarantees following cast is safe
638 SimpleDateFormat* that = (SimpleDateFormat*)&other;
639 return (fPattern == that->fPattern &&
640 fSymbols != NULL && // Check for pathological object
641 that->fSymbols != NULL && // Check for pathological object
642 *fSymbols == *that->fSymbols &&
643 fHaveDefaultCentury == that->fHaveDefaultCentury &&
644 fDefaultCenturyStart == that->fDefaultCenturyStart);
645 }
646 return FALSE;
647}
648
649//----------------------------------------------------------------------
650
651void SimpleDateFormat::construct(EStyle timeStyle,
652 EStyle dateStyle,
653 const Locale& locale,
654 UErrorCode& status)
655{
656 // called by several constructors to load pattern data from the resources
657 if (U_FAILURE(status)) return;
658
659 // We will need the calendar to know what type of symbols to load.
660 initializeCalendar(NULL, locale, status);
661 if (U_FAILURE(status)) return;
662
663 // Load date time patterns directly from resources.
664 const char* cType = fCalendar ? fCalendar->getType() : NULL;
665 LocalUResourceBundlePointer bundle(ures_open(NULL, locale.getBaseName(), &status));
666 if (U_FAILURE(status)) return;
667
668 UBool cTypeIsGregorian = TRUE;
669 LocalUResourceBundlePointer dateTimePatterns;
670 if (cType != NULL && uprv_strcmp(cType, "gregorian") != 0) {
671 CharString resourcePath("calendar/", status);
672 resourcePath.append(cType, status).append("/DateTimePatterns", status);
673 dateTimePatterns.adoptInstead(
674 ures_getByKeyWithFallback(bundle.getAlias(), resourcePath.data(),
675 (UResourceBundle*)NULL, &status));
676 cTypeIsGregorian = FALSE;
677 }
678
679 // Check for "gregorian" fallback.
680 if (cTypeIsGregorian || status == U_MISSING_RESOURCE_ERROR) {
681 status = U_ZERO_ERROR;
682 dateTimePatterns.adoptInstead(
683 ures_getByKeyWithFallback(bundle.getAlias(),
684 "calendar/gregorian/DateTimePatterns",
685 (UResourceBundle*)NULL, &status));
686 }
687 if (U_FAILURE(status)) return;
688
689 LocalUResourceBundlePointer currentBundle;
690
691 if (ures_getSize(dateTimePatterns.getAlias()) <= kDateTime)
692 {
693 status = U_INVALID_FORMAT_ERROR;
694 return;
695 }
696
697 setLocaleIDs(ures_getLocaleByType(dateTimePatterns.getAlias(), ULOC_VALID_LOCALE, &status),
698 ures_getLocaleByType(dateTimePatterns.getAlias(), ULOC_ACTUAL_LOCALE, &status));
699
700 // create a symbols object from the locale
701 fSymbols = DateFormatSymbols::createForLocale(locale, status);
702 if (U_FAILURE(status)) return;
703 /* test for NULL */
704 if (fSymbols == 0) {
705 status = U_MEMORY_ALLOCATION_ERROR;
706 return;
707 }
708
709 const UChar *resStr,*ovrStr;
710 int32_t resStrLen,ovrStrLen = 0;
711 fDateOverride.setToBogus();
712 fTimeOverride.setToBogus();
713
714 // if the pattern should include both date and time information, use the date/time
715 // pattern string as a guide to tell use how to glue together the appropriate date
716 // and time pattern strings.
717 if ((timeStyle != kNone) && (dateStyle != kNone))
718 {
719 currentBundle.adoptInstead(
720 ures_getByIndex(dateTimePatterns.getAlias(), (int32_t)timeStyle, NULL, &status));
721 if (U_FAILURE(status)) {
722 status = U_INVALID_FORMAT_ERROR;
723 return;
724 }
725 switch (ures_getType(currentBundle.getAlias())) {
726 case URES_STRING: {
727 resStr = ures_getString(currentBundle.getAlias(), &resStrLen, &status);
728 break;
729 }
730 case URES_ARRAY: {
731 resStr = ures_getStringByIndex(currentBundle.getAlias(), 0, &resStrLen, &status);
732 ovrStr = ures_getStringByIndex(currentBundle.getAlias(), 1, &ovrStrLen, &status);
733 fTimeOverride.setTo(TRUE, ovrStr, ovrStrLen);
734 break;
735 }
736 default: {
737 status = U_INVALID_FORMAT_ERROR;
738 return;
739 }
740 }
741
742 UnicodeString tempus1(TRUE, resStr, resStrLen);
743
744 currentBundle.adoptInstead(
745 ures_getByIndex(dateTimePatterns.getAlias(), (int32_t)dateStyle, NULL, &status));
746 if (U_FAILURE(status)) {
747 status = U_INVALID_FORMAT_ERROR;
748 return;
749 }
750 switch (ures_getType(currentBundle.getAlias())) {
751 case URES_STRING: {
752 resStr = ures_getString(currentBundle.getAlias(), &resStrLen, &status);
753 break;
754 }
755 case URES_ARRAY: {
756 resStr = ures_getStringByIndex(currentBundle.getAlias(), 0, &resStrLen, &status);
757 ovrStr = ures_getStringByIndex(currentBundle.getAlias(), 1, &ovrStrLen, &status);
758 fDateOverride.setTo(TRUE, ovrStr, ovrStrLen);
759 break;
760 }
761 default: {
762 status = U_INVALID_FORMAT_ERROR;
763 return;
764 }
765 }
766
767 UnicodeString tempus2(TRUE, resStr, resStrLen);
768
769 int32_t glueIndex = kDateTime;
770 int32_t patternsSize = ures_getSize(dateTimePatterns.getAlias());
771 if (patternsSize >= (kDateTimeOffset + kShort + 1)) {
772 // Get proper date time format
773 glueIndex = (int32_t)(kDateTimeOffset + (dateStyle - kDateOffset));
774 }
775
776 resStr = ures_getStringByIndex(dateTimePatterns.getAlias(), glueIndex, &resStrLen, &status);
777 SimpleFormatter(UnicodeString(TRUE, resStr, resStrLen), 2, 2, status).
778 format(tempus1, tempus2, fPattern, status);
779 }
780 // if the pattern includes just time data or just date date, load the appropriate
781 // pattern string from the resources
782 // setTo() - see DateFormatSymbols::assignArray comments
783 else if (timeStyle != kNone) {
784 currentBundle.adoptInstead(
785 ures_getByIndex(dateTimePatterns.getAlias(), (int32_t)timeStyle, NULL, &status));
786 if (U_FAILURE(status)) {
787 status = U_INVALID_FORMAT_ERROR;
788 return;
789 }
790 switch (ures_getType(currentBundle.getAlias())) {
791 case URES_STRING: {
792 resStr = ures_getString(currentBundle.getAlias(), &resStrLen, &status);
793 break;
794 }
795 case URES_ARRAY: {
796 resStr = ures_getStringByIndex(currentBundle.getAlias(), 0, &resStrLen, &status);
797 ovrStr = ures_getStringByIndex(currentBundle.getAlias(), 1, &ovrStrLen, &status);
798 fDateOverride.setTo(TRUE, ovrStr, ovrStrLen);
799 break;
800 }
801 default: {
802 status = U_INVALID_FORMAT_ERROR;
803 return;
804 }
805 }
806 fPattern.setTo(TRUE, resStr, resStrLen);
807 }
808 else if (dateStyle != kNone) {
809 currentBundle.adoptInstead(
810 ures_getByIndex(dateTimePatterns.getAlias(), (int32_t)dateStyle, NULL, &status));
811 if (U_FAILURE(status)) {
812 status = U_INVALID_FORMAT_ERROR;
813 return;
814 }
815 switch (ures_getType(currentBundle.getAlias())) {
816 case URES_STRING: {
817 resStr = ures_getString(currentBundle.getAlias(), &resStrLen, &status);
818 break;
819 }
820 case URES_ARRAY: {
821 resStr = ures_getStringByIndex(currentBundle.getAlias(), 0, &resStrLen, &status);
822 ovrStr = ures_getStringByIndex(currentBundle.getAlias(), 1, &ovrStrLen, &status);
823 fDateOverride.setTo(TRUE, ovrStr, ovrStrLen);
824 break;
825 }
826 default: {
827 status = U_INVALID_FORMAT_ERROR;
828 return;
829 }
830 }
831 fPattern.setTo(TRUE, resStr, resStrLen);
832 }
833
834 // and if it includes _neither_, that's an error
835 else
836 status = U_INVALID_FORMAT_ERROR;
837
838 // finally, finish initializing by creating a Calendar and a NumberFormat
839 initialize(locale, status);
840}
841
842//----------------------------------------------------------------------
843
844Calendar*
845SimpleDateFormat::initializeCalendar(TimeZone* adoptZone, const Locale& locale, UErrorCode& status)
846{
847 if(!U_FAILURE(status)) {
848 fCalendar = Calendar::createInstance(adoptZone?adoptZone:TimeZone::createDefault(), locale, status);
849 }
850 return fCalendar;
851}
852
853void
854SimpleDateFormat::initialize(const Locale& locale,
855 UErrorCode& status)
856{
857 if (U_FAILURE(status)) return;
858
859 parsePattern(); // Need this before initNumberFormatters(), to set fHasHanYearChar
860
861 // Simple-minded hack to force Gannen year numbering for ja@calendar=japanese
862 // if format is non-numeric (includes 年) and fDateOverride is not already specified.
863 // Now this does get updated if applyPattern subsequently changes the pattern type.
864 if (fDateOverride.isBogus() && fHasHanYearChar &&
865 fCalendar != nullptr && uprv_strcmp(fCalendar->getType(),"japanese") == 0 &&
866 uprv_strcmp(fLocale.getLanguage(),"ja") == 0) {
867 fDateOverride.setTo(u"y=jpanyear", -1);
868 }
869
870 // We don't need to check that the row count is >= 1, since all 2d arrays have at
871 // least one row
872 fNumberFormat = NumberFormat::createInstance(locale, status);
873 if (fNumberFormat != NULL && U_SUCCESS(status))
874 {
875 fixNumberFormatForDates(*fNumberFormat);
876 //fNumberFormat->setLenient(TRUE); // Java uses a custom DateNumberFormat to format/parse
877
878 initNumberFormatters(locale, status);
879 initFastNumberFormatters(status);
880
881 }
882 else if (U_SUCCESS(status))
883 {
884 status = U_MISSING_RESOURCE_ERROR;
885 }
886}
887
888/* Initialize the fields we use to disambiguate ambiguous years. Separate
889 * so we can call it from readObject().
890 */
891void SimpleDateFormat::initializeDefaultCentury()
892{
893 if(fCalendar) {
894 fHaveDefaultCentury = fCalendar->haveDefaultCentury();
895 if(fHaveDefaultCentury) {
896 fDefaultCenturyStart = fCalendar->defaultCenturyStart();
897 fDefaultCenturyStartYear = fCalendar->defaultCenturyStartYear();
898 } else {
899 fDefaultCenturyStart = DBL_MIN;
900 fDefaultCenturyStartYear = -1;
901 }
902 }
903}
904
905/*
906 * Initialize the boolean attributes. Separate so we can call it from all constructors.
907 */
908void SimpleDateFormat::initializeBooleanAttributes()
909{
910 UErrorCode status = U_ZERO_ERROR;
911
912 setBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE, true, status);
913 setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, true, status);
914 setBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH, true, status);
915 setBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, true, status);
916}
917
918/* Define one-century window into which to disambiguate dates using
919 * two-digit years. Make public in JDK 1.2.
920 */
921void SimpleDateFormat::parseAmbiguousDatesAsAfter(UDate startDate, UErrorCode& status)
922{
923 if(U_FAILURE(status)) {
924 return;
925 }
926 if(!fCalendar) {
927 status = U_ILLEGAL_ARGUMENT_ERROR;
928 return;
929 }
930
931 fCalendar->setTime(startDate, status);
932 if(U_SUCCESS(status)) {
933 fHaveDefaultCentury = TRUE;
934 fDefaultCenturyStart = startDate;
935 fDefaultCenturyStartYear = fCalendar->get(UCAL_YEAR, status);
936 }
937}
938
939//----------------------------------------------------------------------
940
941UnicodeString&
942SimpleDateFormat::format(Calendar& cal, UnicodeString& appendTo, FieldPosition& pos) const
943{
944 UErrorCode status = U_ZERO_ERROR;
945 FieldPositionOnlyHandler handler(pos);
946 return _format(cal, appendTo, handler, status);
947}
948
949//----------------------------------------------------------------------
950
951UnicodeString&
952SimpleDateFormat::format(Calendar& cal, UnicodeString& appendTo,
953 FieldPositionIterator* posIter, UErrorCode& status) const
954{
955 FieldPositionIteratorHandler handler(posIter, status);
956 return _format(cal, appendTo, handler, status);
957}
958
959//----------------------------------------------------------------------
960
961UnicodeString&
962SimpleDateFormat::_format(Calendar& cal, UnicodeString& appendTo,
963 FieldPositionHandler& handler, UErrorCode& status) const
964{
965 if ( U_FAILURE(status) ) {
966 return appendTo;
967 }
968 Calendar* workCal = &cal;
969 Calendar* calClone = NULL;
970 if (&cal != fCalendar && uprv_strcmp(cal.getType(), fCalendar->getType()) != 0) {
971 // Different calendar type
972 // We use the time and time zone from the input calendar, but
973 // do not use the input calendar for field calculation.
974 calClone = fCalendar->clone();
975 if (calClone != NULL) {
976 UDate t = cal.getTime(status);
977 calClone->setTime(t, status);
978 calClone->setTimeZone(cal.getTimeZone());
979 workCal = calClone;
980 } else {
981 status = U_MEMORY_ALLOCATION_ERROR;
982 return appendTo;
983 }
984 }
985
986 UBool inQuote = FALSE;
987 UChar prevCh = 0;
988 int32_t count = 0;
989 int32_t fieldNum = 0;
990 UDisplayContext capitalizationContext = getContext(UDISPCTX_TYPE_CAPITALIZATION, status);
991
992 // loop through the pattern string character by character
993 for (int32_t i = 0; i < fPattern.length() && U_SUCCESS(status); ++i) {
994 UChar ch = fPattern[i];
995
996 // Use subFormat() to format a repeated pattern character
997 // when a different pattern or non-pattern character is seen
998 if (ch != prevCh && count > 0) {
999 subFormat(appendTo, prevCh, count, capitalizationContext, fieldNum++,
1000 prevCh, handler, *workCal, status);
1001 count = 0;
1002 }
1003 if (ch == QUOTE) {
1004 // Consecutive single quotes are a single quote literal,
1005 // either outside of quotes or between quotes
1006 if ((i+1) < fPattern.length() && fPattern[i+1] == QUOTE) {
1007 appendTo += (UChar)QUOTE;
1008 ++i;
1009 } else {
1010 inQuote = ! inQuote;
1011 }
1012 }
1013 else if (!inQuote && isSyntaxChar(ch)) {
1014 // ch is a date-time pattern character to be interpreted
1015 // by subFormat(); count the number of times it is repeated
1016 prevCh = ch;
1017 ++count;
1018 }
1019 else {
1020 // Append quoted characters and unquoted non-pattern characters
1021 appendTo += ch;
1022 }
1023 }
1024
1025 // Format the last item in the pattern, if any
1026 if (count > 0) {
1027 subFormat(appendTo, prevCh, count, capitalizationContext, fieldNum++,
1028 prevCh, handler, *workCal, status);
1029 }
1030
1031 if (calClone != NULL) {
1032 delete calClone;
1033 }
1034
1035 return appendTo;
1036}
1037
1038//----------------------------------------------------------------------
1039
1040/* Map calendar field into calendar field level.
1041 * the larger the level, the smaller the field unit.
1042 * For example, UCAL_ERA level is 0, UCAL_YEAR level is 10,
1043 * UCAL_MONTH level is 20.
1044 * NOTE: if new fields adds in, the table needs to update.
1045 */
1046const int32_t
1047SimpleDateFormat::fgCalendarFieldToLevel[] =
1048{
1049 /*GyM*/ 0, 10, 20,
1050 /*wW*/ 20, 30,
1051 /*dDEF*/ 30, 20, 30, 30,
1052 /*ahHm*/ 40, 50, 50, 60,
1053 /*sS*/ 70, 80,
1054 /*z?Y*/ 0, 0, 10,
1055 /*eug*/ 30, 10, 0,
1056 /*A?.*/ 40, 0, 0
1057};
1058
1059int32_t SimpleDateFormat::getLevelFromChar(UChar ch) {
1060 // Map date field LETTER into calendar field level.
1061 // the larger the level, the smaller the field unit.
1062 // NOTE: if new fields adds in, the table needs to update.
1063 static const int32_t mapCharToLevel[] = {
1064 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1065 //
1066 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1067 // ! " # $ % & ' ( ) * + , - . /
1068 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1069#if UDAT_HAS_PATTERN_CHAR_FOR_TIME_SEPARATOR
1070 // 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
1071 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1,
1072#else
1073 // 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
1074 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1075#endif
1076 // @ A B C D E F G H I J K L M N O
1077 -1, 40, -1, -1, 20, 30, 30, 0, 50, -1, -1, 50, 20, 20, -1, 0,
1078 // P Q R S T U V W X Y Z [ \ ] ^ _
1079 -1, 20, -1, 80, -1, 10, 0, 30, 0, 10, 0, -1, -1, -1, -1, -1,
1080 // ` a b c d e f g h i j k l m n o
1081 -1, 40, -1, 30, 30, 30, -1, 0, 50, -1, -1, 50, 0, 60, -1, -1,
1082 // p q r s t u v w x y z { | } ~
1083 -1, 20, 10, 70, -1, 10, 0, 20, 0, 10, 0, -1, -1, -1, -1, -1
1084 };
1085
1086 return ch < UPRV_LENGTHOF(mapCharToLevel) ? mapCharToLevel[ch] : -1;
1087}
1088
1089UBool SimpleDateFormat::isSyntaxChar(UChar ch) {
1090 static const UBool mapCharToIsSyntax[] = {
1091 //
1092 FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
1093 //
1094 FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
1095 //
1096 FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
1097 //
1098 FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
1099 // ! " # $ % & '
1100 FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
1101 // ( ) * + , - . /
1102 FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
1103 // 0 1 2 3 4 5 6 7
1104 FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
1105#if UDAT_HAS_PATTERN_CHAR_FOR_TIME_SEPARATOR
1106 // 8 9 : ; < = > ?
1107 FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE,
1108#else
1109 // 8 9 : ; < = > ?
1110 FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
1111#endif
1112 // @ A B C D E F G
1113 FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
1114 // H I J K L M N O
1115 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
1116 // P Q R S T U V W
1117 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
1118 // X Y Z [ \ ] ^ _
1119 TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE,
1120 // ` a b c d e f g
1121 FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
1122 // h i j k l m n o
1123 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
1124 // p q r s t u v w
1125 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
1126 // x y z { | } ~
1127 TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE
1128 };
1129
1130 return ch < UPRV_LENGTHOF(mapCharToIsSyntax) ? mapCharToIsSyntax[ch] : FALSE;
1131}
1132
1133// Map index into pattern character string to Calendar field number.
1134const UCalendarDateFields
1135SimpleDateFormat::fgPatternIndexToCalendarField[] =
1136{
1137 /*GyM*/ UCAL_ERA, UCAL_YEAR, UCAL_MONTH,
1138 /*dkH*/ UCAL_DATE, UCAL_HOUR_OF_DAY, UCAL_HOUR_OF_DAY,
1139 /*msS*/ UCAL_MINUTE, UCAL_SECOND, UCAL_MILLISECOND,
1140 /*EDF*/ UCAL_DAY_OF_WEEK, UCAL_DAY_OF_YEAR, UCAL_DAY_OF_WEEK_IN_MONTH,
1141 /*wWa*/ UCAL_WEEK_OF_YEAR, UCAL_WEEK_OF_MONTH, UCAL_AM_PM,
1142 /*hKz*/ UCAL_HOUR, UCAL_HOUR, UCAL_ZONE_OFFSET,
1143 /*Yeu*/ UCAL_YEAR_WOY, UCAL_DOW_LOCAL, UCAL_EXTENDED_YEAR,
1144 /*gAZ*/ UCAL_JULIAN_DAY, UCAL_MILLISECONDS_IN_DAY, UCAL_ZONE_OFFSET,
1145 /*v*/ UCAL_ZONE_OFFSET,
1146 /*c*/ UCAL_DOW_LOCAL,
1147 /*L*/ UCAL_MONTH,
1148 /*Q*/ UCAL_MONTH,
1149 /*q*/ UCAL_MONTH,
1150 /*V*/ UCAL_ZONE_OFFSET,
1151 /*U*/ UCAL_YEAR,
1152 /*O*/ UCAL_ZONE_OFFSET,
1153 /*Xx*/ UCAL_ZONE_OFFSET, UCAL_ZONE_OFFSET,
1154 /*r*/ UCAL_EXTENDED_YEAR,
1155 /*bB*/ UCAL_FIELD_COUNT, UCAL_FIELD_COUNT, // no mappings to calendar fields
1156#if UDAT_HAS_PATTERN_CHAR_FOR_TIME_SEPARATOR
1157 /*:*/ UCAL_FIELD_COUNT, /* => no useful mapping to any calendar field */
1158#else
1159 /*no pattern char for UDAT_TIME_SEPARATOR_FIELD*/ UCAL_FIELD_COUNT, /* => no useful mapping to any calendar field */
1160#endif
1161};
1162
1163// Map index into pattern character string to DateFormat field number
1164const UDateFormatField
1165SimpleDateFormat::fgPatternIndexToDateFormatField[] = {
1166 /*GyM*/ UDAT_ERA_FIELD, UDAT_YEAR_FIELD, UDAT_MONTH_FIELD,
1167 /*dkH*/ UDAT_DATE_FIELD, UDAT_HOUR_OF_DAY1_FIELD, UDAT_HOUR_OF_DAY0_FIELD,
1168 /*msS*/ UDAT_MINUTE_FIELD, UDAT_SECOND_FIELD, UDAT_FRACTIONAL_SECOND_FIELD,
1169 /*EDF*/ UDAT_DAY_OF_WEEK_FIELD, UDAT_DAY_OF_YEAR_FIELD, UDAT_DAY_OF_WEEK_IN_MONTH_FIELD,
1170 /*wWa*/ UDAT_WEEK_OF_YEAR_FIELD, UDAT_WEEK_OF_MONTH_FIELD, UDAT_AM_PM_FIELD,
1171 /*hKz*/ UDAT_HOUR1_FIELD, UDAT_HOUR0_FIELD, UDAT_TIMEZONE_FIELD,
1172 /*Yeu*/ UDAT_YEAR_WOY_FIELD, UDAT_DOW_LOCAL_FIELD, UDAT_EXTENDED_YEAR_FIELD,
1173 /*gAZ*/ UDAT_JULIAN_DAY_FIELD, UDAT_MILLISECONDS_IN_DAY_FIELD, UDAT_TIMEZONE_RFC_FIELD,
1174 /*v*/ UDAT_TIMEZONE_GENERIC_FIELD,
1175 /*c*/ UDAT_STANDALONE_DAY_FIELD,
1176 /*L*/ UDAT_STANDALONE_MONTH_FIELD,
1177 /*Q*/ UDAT_QUARTER_FIELD,
1178 /*q*/ UDAT_STANDALONE_QUARTER_FIELD,
1179 /*V*/ UDAT_TIMEZONE_SPECIAL_FIELD,
1180 /*U*/ UDAT_YEAR_NAME_FIELD,
1181 /*O*/ UDAT_TIMEZONE_LOCALIZED_GMT_OFFSET_FIELD,
1182 /*Xx*/ UDAT_TIMEZONE_ISO_FIELD, UDAT_TIMEZONE_ISO_LOCAL_FIELD,
1183 /*r*/ UDAT_RELATED_YEAR_FIELD,
1184 /*bB*/ UDAT_AM_PM_MIDNIGHT_NOON_FIELD, UDAT_FLEXIBLE_DAY_PERIOD_FIELD,
1185#if UDAT_HAS_PATTERN_CHAR_FOR_TIME_SEPARATOR
1186 /*:*/ UDAT_TIME_SEPARATOR_FIELD,
1187#else
1188 /*no pattern char for UDAT_TIME_SEPARATOR_FIELD*/ UDAT_TIME_SEPARATOR_FIELD,
1189#endif
1190};
1191
1192//----------------------------------------------------------------------
1193
1194/**
1195 * Append symbols[value] to dst. Make sure the array index is not out
1196 * of bounds.
1197 */
1198static inline void
1199_appendSymbol(UnicodeString& dst,
1200 int32_t value,
1201 const UnicodeString* symbols,
1202 int32_t symbolsCount) {
1203 U_ASSERT(0 <= value && value < symbolsCount);
1204 if (0 <= value && value < symbolsCount) {
1205 dst += symbols[value];
1206 }
1207}
1208
1209static inline void
1210_appendSymbolWithMonthPattern(UnicodeString& dst, int32_t value, const UnicodeString* symbols, int32_t symbolsCount,
1211 const UnicodeString* monthPattern, UErrorCode& status) {
1212 U_ASSERT(0 <= value && value < symbolsCount);
1213 if (0 <= value && value < symbolsCount) {
1214 if (monthPattern == NULL) {
1215 dst += symbols[value];
1216 } else {
1217 SimpleFormatter(*monthPattern, 1, 1, status).format(symbols[value], dst, status);
1218 }
1219 }
1220}
1221
1222//----------------------------------------------------------------------
1223
1224static number::LocalizedNumberFormatter*
1225createFastFormatter(const DecimalFormat* df, int32_t minInt, int32_t maxInt, UErrorCode& status) {
1226 const number::LocalizedNumberFormatter* lnfBase = df->toNumberFormatter(status);
1227 if (U_FAILURE(status)) {
1228 return nullptr;
1229 }
1230 return lnfBase->integerWidth(
1231 number::IntegerWidth::zeroFillTo(minInt).truncateAt(maxInt)
1232 ).clone().orphan();
1233}
1234
1235void SimpleDateFormat::initFastNumberFormatters(UErrorCode& status) {
1236 if (U_FAILURE(status)) {
1237 return;
1238 }
1239 auto* df = dynamic_cast<const DecimalFormat*>(fNumberFormat);
1240 if (df == nullptr) {
1241 return;
1242 }
1243 fFastNumberFormatters[SMPDTFMT_NF_1x10] = createFastFormatter(df, 1, 10, status);
1244 fFastNumberFormatters[SMPDTFMT_NF_2x10] = createFastFormatter(df, 2, 10, status);
1245 fFastNumberFormatters[SMPDTFMT_NF_3x10] = createFastFormatter(df, 3, 10, status);
1246 fFastNumberFormatters[SMPDTFMT_NF_4x10] = createFastFormatter(df, 4, 10, status);
1247 fFastNumberFormatters[SMPDTFMT_NF_2x2] = createFastFormatter(df, 2, 2, status);
1248}
1249
1250void SimpleDateFormat::freeFastNumberFormatters() {
1251 delete fFastNumberFormatters[SMPDTFMT_NF_1x10];
1252 delete fFastNumberFormatters[SMPDTFMT_NF_2x10];
1253 delete fFastNumberFormatters[SMPDTFMT_NF_3x10];
1254 delete fFastNumberFormatters[SMPDTFMT_NF_4x10];
1255 delete fFastNumberFormatters[SMPDTFMT_NF_2x2];
1256 fFastNumberFormatters[SMPDTFMT_NF_1x10] = nullptr;
1257 fFastNumberFormatters[SMPDTFMT_NF_2x10] = nullptr;
1258 fFastNumberFormatters[SMPDTFMT_NF_3x10] = nullptr;
1259 fFastNumberFormatters[SMPDTFMT_NF_4x10] = nullptr;
1260 fFastNumberFormatters[SMPDTFMT_NF_2x2] = nullptr;
1261}
1262
1263
1264void
1265SimpleDateFormat::initNumberFormatters(const Locale &locale,UErrorCode &status) {
1266 if (U_FAILURE(status)) {
1267 return;
1268 }
1269 if ( fDateOverride.isBogus() && fTimeOverride.isBogus() ) {
1270 return;
1271 }
1272 umtx_lock(&LOCK);
1273 if (fSharedNumberFormatters == NULL) {
1274 fSharedNumberFormatters = allocSharedNumberFormatters();
1275 if (fSharedNumberFormatters == NULL) {
1276 status = U_MEMORY_ALLOCATION_ERROR;
1277 }
1278 }
1279 umtx_unlock(&LOCK);
1280
1281 if (U_FAILURE(status)) {
1282 return;
1283 }
1284
1285 processOverrideString(locale,fDateOverride,kOvrStrDate,status);
1286 processOverrideString(locale,fTimeOverride,kOvrStrTime,status);
1287}
1288
1289void
1290SimpleDateFormat::processOverrideString(const Locale &locale, const UnicodeString &str, int8_t type, UErrorCode &status) {
1291 if (str.isBogus() || U_FAILURE(status)) {
1292 return;
1293 }
1294
1295 int32_t start = 0;
1296 int32_t len;
1297 UnicodeString nsName;
1298 UnicodeString ovrField;
1299 UBool moreToProcess = TRUE;
1300 NSOverride *overrideList = NULL;
1301
1302 while (moreToProcess) {
1303 int32_t delimiterPosition = str.indexOf((UChar)ULOC_KEYWORD_ITEM_SEPARATOR_UNICODE,start);
1304 if (delimiterPosition == -1) {
1305 moreToProcess = FALSE;
1306 len = str.length() - start;
1307 } else {
1308 len = delimiterPosition - start;
1309 }
1310 UnicodeString currentString(str,start,len);
1311 int32_t equalSignPosition = currentString.indexOf((UChar)ULOC_KEYWORD_ASSIGN_UNICODE,0);
1312 if (equalSignPosition == -1) { // Simple override string such as "hebrew"
1313 nsName.setTo(currentString);
1314 ovrField.setToBogus();
1315 } else { // Field specific override string such as "y=hebrew"
1316 nsName.setTo(currentString,equalSignPosition+1);
1317 ovrField.setTo(currentString,0,1); // We just need the first character.
1318 }
1319
1320 int32_t nsNameHash = nsName.hashCode();
1321 // See if the numbering system is in the override list, if not, then add it.
1322 NSOverride *curr = overrideList;
1323 const SharedNumberFormat *snf = NULL;
1324 UBool found = FALSE;
1325 while ( curr && !found ) {
1326 if ( curr->hash == nsNameHash ) {
1327 snf = curr->snf;
1328 found = TRUE;
1329 }
1330 curr = curr->next;
1331 }
1332
1333 if (!found) {
1334 LocalPointer<NSOverride> cur(new NSOverride);
1335 if (!cur.isNull()) {
1336 char kw[ULOC_KEYWORD_AND_VALUES_CAPACITY];
1337 uprv_strcpy(kw,"numbers=");
1338 nsName.extract(0,len,kw+8,ULOC_KEYWORD_AND_VALUES_CAPACITY-8,US_INV);
1339
1340 Locale ovrLoc(locale.getLanguage(),locale.getCountry(),locale.getVariant(),kw);
1341 cur->hash = nsNameHash;
1342 cur->next = overrideList;
1343 SharedObject::copyPtr(
1344 createSharedNumberFormat(ovrLoc, status), cur->snf);
1345 if (U_FAILURE(status)) {
1346 if (overrideList) {
1347 overrideList->free();
1348 }
1349 return;
1350 }
1351 snf = cur->snf;
1352 overrideList = cur.orphan();
1353 } else {
1354 status = U_MEMORY_ALLOCATION_ERROR;
1355 if (overrideList) {
1356 overrideList->free();
1357 }
1358 return;
1359 }
1360 }
1361
1362 // Now that we have an appropriate number formatter, fill in the appropriate spaces in the
1363 // number formatters table.
1364 if (ovrField.isBogus()) {
1365 switch (type) {
1366 case kOvrStrDate:
1367 case kOvrStrBoth: {
1368 for ( int8_t i=0 ; i<kDateFieldsCount; i++ ) {
1369 SharedObject::copyPtr(snf, fSharedNumberFormatters[kDateFields[i]]);
1370 }
1371 if (type==kOvrStrDate) {
1372 break;
1373 }
1374 U_FALLTHROUGH;
1375 }
1376 case kOvrStrTime : {
1377 for ( int8_t i=0 ; i<kTimeFieldsCount; i++ ) {
1378 SharedObject::copyPtr(snf, fSharedNumberFormatters[kTimeFields[i]]);
1379 }
1380 break;
1381 }
1382 }
1383 } else {
1384 // if the pattern character is unrecognized, signal an error and bail out
1385 UDateFormatField patternCharIndex =
1386 DateFormatSymbols::getPatternCharIndex(ovrField.charAt(0));
1387 if (patternCharIndex == UDAT_FIELD_COUNT) {
1388 status = U_INVALID_FORMAT_ERROR;
1389 if (overrideList) {
1390 overrideList->free();
1391 }
1392 return;
1393 }
1394 SharedObject::copyPtr(snf, fSharedNumberFormatters[patternCharIndex]);
1395 }
1396
1397 start = delimiterPosition + 1;
1398 }
1399 if (overrideList) {
1400 overrideList->free();
1401 }
1402}
1403
1404//---------------------------------------------------------------------
1405void
1406SimpleDateFormat::subFormat(UnicodeString &appendTo,
1407 char16_t ch,
1408 int32_t count,
1409 UDisplayContext capitalizationContext,
1410 int32_t fieldNum,
1411 char16_t fieldToOutput,
1412 FieldPositionHandler& handler,
1413 Calendar& cal,
1414 UErrorCode& status) const
1415{
1416 if (U_FAILURE(status)) {
1417 return;
1418 }
1419
1420 // this function gets called by format() to produce the appropriate substitution
1421 // text for an individual pattern symbol (e.g., "HH" or "yyyy")
1422
1423 UDateFormatField patternCharIndex = DateFormatSymbols::getPatternCharIndex(ch);
1424 const int32_t maxIntCount = 10;
1425 int32_t beginOffset = appendTo.length();
1426 const NumberFormat *currentNumberFormat;
1427 DateFormatSymbols::ECapitalizationContextUsageType capContextUsageType = DateFormatSymbols::kCapContextUsageOther;
1428
1429 UBool isHebrewCalendar = (uprv_strcmp(cal.getType(),"hebrew") == 0);
1430 UBool isChineseCalendar = (uprv_strcmp(cal.getType(),"chinese") == 0 || uprv_strcmp(cal.getType(),"dangi") == 0);
1431
1432 // if the pattern character is unrecognized, signal an error and dump out
1433 if (patternCharIndex == UDAT_FIELD_COUNT)
1434 {
1435 if (ch != 0x6C) { // pattern char 'l' (SMALL LETTER L) just gets ignored
1436 status = U_INVALID_FORMAT_ERROR;
1437 }
1438 return;
1439 }
1440
1441 UCalendarDateFields field = fgPatternIndexToCalendarField[patternCharIndex];
1442 int32_t value = 0;
1443 // Don't get value unless it is useful
1444 if (field < UCAL_FIELD_COUNT) {
1445 value = (patternCharIndex != UDAT_RELATED_YEAR_FIELD)? cal.get(field, status): cal.getRelatedYear(status);
1446 }
1447 if (U_FAILURE(status)) {
1448 return;
1449 }
1450
1451 currentNumberFormat = getNumberFormatByIndex(patternCharIndex);
1452 if (currentNumberFormat == NULL) {
1453 status = U_INTERNAL_PROGRAM_ERROR;
1454 return;
1455 }
1456 UnicodeString hebr("hebr", 4, US_INV);
1457
1458 switch (patternCharIndex) {
1459
1460 // for any "G" symbol, write out the appropriate era string
1461 // "GGGG" is wide era name, "GGGGG" is narrow era name, anything else is abbreviated name
1462 case UDAT_ERA_FIELD:
1463 if (isChineseCalendar) {
1464 zeroPaddingNumber(currentNumberFormat,appendTo, value, 1, 9); // as in ICU4J
1465 } else {
1466 if (count == 5) {
1467 _appendSymbol(appendTo, value, fSymbols->fNarrowEras, fSymbols->fNarrowErasCount);
1468 capContextUsageType = DateFormatSymbols::kCapContextUsageEraNarrow;
1469 } else if (count == 4) {
1470 _appendSymbol(appendTo, value, fSymbols->fEraNames, fSymbols->fEraNamesCount);
1471 capContextUsageType = DateFormatSymbols::kCapContextUsageEraWide;
1472 } else {
1473 _appendSymbol(appendTo, value, fSymbols->fEras, fSymbols->fErasCount);
1474 capContextUsageType = DateFormatSymbols::kCapContextUsageEraAbbrev;
1475 }
1476 }
1477 break;
1478
1479 case UDAT_YEAR_NAME_FIELD:
1480 if (fSymbols->fShortYearNames != NULL && value <= fSymbols->fShortYearNamesCount) {
1481 // the Calendar YEAR field runs 1 through 60 for cyclic years
1482 _appendSymbol(appendTo, value - 1, fSymbols->fShortYearNames, fSymbols->fShortYearNamesCount);
1483 break;
1484 }
1485 // else fall through to numeric year handling, do not break here
1486 U_FALLTHROUGH;
1487
1488 // OLD: for "yyyy", write out the whole year; for "yy", write out the last 2 digits
1489 // NEW: UTS#35:
1490//Year y yy yyy yyyy yyyyy
1491//AD 1 1 01 001 0001 00001
1492//AD 12 12 12 012 0012 00012
1493//AD 123 123 23 123 0123 00123
1494//AD 1234 1234 34 1234 1234 01234
1495//AD 12345 12345 45 12345 12345 12345
1496 case UDAT_YEAR_FIELD:
1497 case UDAT_YEAR_WOY_FIELD:
1498 if (fDateOverride.compare(hebr)==0 && value>HEBREW_CAL_CUR_MILLENIUM_START_YEAR && value<HEBREW_CAL_CUR_MILLENIUM_END_YEAR) {
1499 value-=HEBREW_CAL_CUR_MILLENIUM_START_YEAR;
1500 }
1501 if(count == 2)
1502 zeroPaddingNumber(currentNumberFormat, appendTo, value, 2, 2);
1503 else
1504 zeroPaddingNumber(currentNumberFormat, appendTo, value, count, maxIntCount);
1505 break;
1506
1507 // for "MMMM"/"LLLL", write out the whole month name, for "MMM"/"LLL", write out the month
1508 // abbreviation, for "M"/"L" or "MM"/"LL", write out the month as a number with the
1509 // appropriate number of digits
1510 // for "MMMMM"/"LLLLL", use the narrow form
1511 case UDAT_MONTH_FIELD:
1512 case UDAT_STANDALONE_MONTH_FIELD:
1513 if ( isHebrewCalendar ) {
1514 HebrewCalendar *hc = (HebrewCalendar*)&cal;
1515 if (hc->isLeapYear(hc->get(UCAL_YEAR,status)) && value == 6 && count >= 3 )
1516 value = 13; // Show alternate form for Adar II in leap years in Hebrew calendar.
1517 if (!hc->isLeapYear(hc->get(UCAL_YEAR,status)) && value >= 6 && count < 3 )
1518 value--; // Adjust the month number down 1 in Hebrew non-leap years, i.e. Adar is 6, not 7.
1519 }
1520 {
1521 int32_t isLeapMonth = (fSymbols->fLeapMonthPatterns != NULL && fSymbols->fLeapMonthPatternsCount >= DateFormatSymbols::kMonthPatternsCount)?
1522 cal.get(UCAL_IS_LEAP_MONTH, status): 0;
1523 // should consolidate the next section by using arrays of pointers & counts for the right symbols...
1524 if (count == 5) {
1525 if (patternCharIndex == UDAT_MONTH_FIELD) {
1526 _appendSymbolWithMonthPattern(appendTo, value, fSymbols->fNarrowMonths, fSymbols->fNarrowMonthsCount,
1527 (isLeapMonth!=0)? &(fSymbols->fLeapMonthPatterns[DateFormatSymbols::kLeapMonthPatternFormatNarrow]): NULL, status);
1528 } else {
1529 _appendSymbolWithMonthPattern(appendTo, value, fSymbols->fStandaloneNarrowMonths, fSymbols->fStandaloneNarrowMonthsCount,
1530 (isLeapMonth!=0)? &(fSymbols->fLeapMonthPatterns[DateFormatSymbols::kLeapMonthPatternStandaloneNarrow]): NULL, status);
1531 }
1532 capContextUsageType = DateFormatSymbols::kCapContextUsageMonthNarrow;
1533 } else if (count == 4) {
1534 if (patternCharIndex == UDAT_MONTH_FIELD) {
1535 _appendSymbolWithMonthPattern(appendTo, value, fSymbols->fMonths, fSymbols->fMonthsCount,
1536 (isLeapMonth!=0)? &(fSymbols->fLeapMonthPatterns[DateFormatSymbols::kLeapMonthPatternFormatWide]): NULL, status);
1537 capContextUsageType = DateFormatSymbols::kCapContextUsageMonthFormat;
1538 } else {
1539 _appendSymbolWithMonthPattern(appendTo, value, fSymbols->fStandaloneMonths, fSymbols->fStandaloneMonthsCount,
1540 (isLeapMonth!=0)? &(fSymbols->fLeapMonthPatterns[DateFormatSymbols::kLeapMonthPatternStandaloneWide]): NULL, status);
1541 capContextUsageType = DateFormatSymbols::kCapContextUsageMonthStandalone;
1542 }
1543 } else if (count == 3) {
1544 if (patternCharIndex == UDAT_MONTH_FIELD) {
1545 _appendSymbolWithMonthPattern(appendTo, value, fSymbols->fShortMonths, fSymbols->fShortMonthsCount,
1546 (isLeapMonth!=0)? &(fSymbols->fLeapMonthPatterns[DateFormatSymbols::kLeapMonthPatternFormatAbbrev]): NULL, status);
1547 capContextUsageType = DateFormatSymbols::kCapContextUsageMonthFormat;
1548 } else {
1549 _appendSymbolWithMonthPattern(appendTo, value, fSymbols->fStandaloneShortMonths, fSymbols->fStandaloneShortMonthsCount,
1550 (isLeapMonth!=0)? &(fSymbols->fLeapMonthPatterns[DateFormatSymbols::kLeapMonthPatternStandaloneAbbrev]): NULL, status);
1551 capContextUsageType = DateFormatSymbols::kCapContextUsageMonthStandalone;
1552 }
1553 } else {
1554 UnicodeString monthNumber;
1555 zeroPaddingNumber(currentNumberFormat,monthNumber, value + 1, count, maxIntCount);
1556 _appendSymbolWithMonthPattern(appendTo, 0, &monthNumber, 1,
1557 (isLeapMonth!=0)? &(fSymbols->fLeapMonthPatterns[DateFormatSymbols::kLeapMonthPatternNumeric]): NULL, status);
1558 }
1559 }
1560 break;
1561
1562 // for "k" and "kk", write out the hour, adjusting midnight to appear as "24"
1563 case UDAT_HOUR_OF_DAY1_FIELD:
1564 if (value == 0)
1565 zeroPaddingNumber(currentNumberFormat,appendTo, cal.getMaximum(UCAL_HOUR_OF_DAY) + 1, count, maxIntCount);
1566 else
1567 zeroPaddingNumber(currentNumberFormat,appendTo, value, count, maxIntCount);
1568 break;
1569
1570 case UDAT_FRACTIONAL_SECOND_FIELD:
1571 // Fractional seconds left-justify
1572 {
1573 int32_t minDigits = (count > 3) ? 3 : count;
1574 if (count == 1) {
1575 value /= 100;
1576 } else if (count == 2) {
1577 value /= 10;
1578 }
1579 zeroPaddingNumber(currentNumberFormat, appendTo, value, minDigits, maxIntCount);
1580 if (count > 3) {
1581 zeroPaddingNumber(currentNumberFormat, appendTo, 0, count - 3, maxIntCount);
1582 }
1583 }
1584 break;
1585
1586 // for "ee" or "e", use local numeric day-of-the-week
1587 // for "EEEEEE" or "eeeeee", write out the short day-of-the-week name
1588 // for "EEEEE" or "eeeee", write out the narrow day-of-the-week name
1589 // for "EEEE" or "eeee", write out the wide day-of-the-week name
1590 // for "EEE" or "EE" or "E" or "eee", write out the abbreviated day-of-the-week name
1591 case UDAT_DOW_LOCAL_FIELD:
1592 if ( count < 3 ) {
1593 zeroPaddingNumber(currentNumberFormat,appendTo, value, count, maxIntCount);
1594 break;
1595 }
1596 // fall through to EEEEE-EEE handling, but for that we don't want local day-of-week,
1597 // we want standard day-of-week, so first fix value to work for EEEEE-EEE.
1598 value = cal.get(UCAL_DAY_OF_WEEK, status);
1599 if (U_FAILURE(status)) {
1600 return;
1601 }
1602 // fall through, do not break here
1603 U_FALLTHROUGH;
1604 case UDAT_DAY_OF_WEEK_FIELD:
1605 if (count == 5) {
1606 _appendSymbol(appendTo, value, fSymbols->fNarrowWeekdays,
1607 fSymbols->fNarrowWeekdaysCount);
1608 capContextUsageType = DateFormatSymbols::kCapContextUsageDayNarrow;
1609 } else if (count == 4) {
1610 _appendSymbol(appendTo, value, fSymbols->fWeekdays,
1611 fSymbols->fWeekdaysCount);
1612 capContextUsageType = DateFormatSymbols::kCapContextUsageDayFormat;
1613 } else if (count == 6) {
1614 _appendSymbol(appendTo, value, fSymbols->fShorterWeekdays,
1615 fSymbols->fShorterWeekdaysCount);
1616 capContextUsageType = DateFormatSymbols::kCapContextUsageDayFormat;
1617 } else {
1618 _appendSymbol(appendTo, value, fSymbols->fShortWeekdays,
1619 fSymbols->fShortWeekdaysCount);
1620 capContextUsageType = DateFormatSymbols::kCapContextUsageDayFormat;
1621 }
1622 break;
1623
1624 // for "ccc", write out the abbreviated day-of-the-week name
1625 // for "cccc", write out the wide day-of-the-week name
1626 // for "ccccc", use the narrow day-of-the-week name
1627 // for "ccccc", use the short day-of-the-week name
1628 case UDAT_STANDALONE_DAY_FIELD:
1629 if ( count < 3 ) {
1630 zeroPaddingNumber(currentNumberFormat,appendTo, value, 1, maxIntCount);
1631 break;
1632 }
1633 // fall through to alpha DOW handling, but for that we don't want local day-of-week,
1634 // we want standard day-of-week, so first fix value.
1635 value = cal.get(UCAL_DAY_OF_WEEK, status);
1636 if (U_FAILURE(status)) {
1637 return;
1638 }
1639 if (count == 5) {
1640 _appendSymbol(appendTo, value, fSymbols->fStandaloneNarrowWeekdays,
1641 fSymbols->fStandaloneNarrowWeekdaysCount);
1642 capContextUsageType = DateFormatSymbols::kCapContextUsageDayNarrow;
1643 } else if (count == 4) {
1644 _appendSymbol(appendTo, value, fSymbols->fStandaloneWeekdays,
1645 fSymbols->fStandaloneWeekdaysCount);
1646 capContextUsageType = DateFormatSymbols::kCapContextUsageDayStandalone;
1647 } else if (count == 6) {
1648 _appendSymbol(appendTo, value, fSymbols->fStandaloneShorterWeekdays,
1649 fSymbols->fStandaloneShorterWeekdaysCount);
1650 capContextUsageType = DateFormatSymbols::kCapContextUsageDayStandalone;
1651 } else { // count == 3
1652 _appendSymbol(appendTo, value, fSymbols->fStandaloneShortWeekdays,
1653 fSymbols->fStandaloneShortWeekdaysCount);
1654 capContextUsageType = DateFormatSymbols::kCapContextUsageDayStandalone;
1655 }
1656 break;
1657
1658 // for "a" symbol, write out the whole AM/PM string
1659 case UDAT_AM_PM_FIELD:
1660 if (count < 5) {
1661 _appendSymbol(appendTo, value, fSymbols->fAmPms,
1662 fSymbols->fAmPmsCount);
1663 } else {
1664 _appendSymbol(appendTo, value, fSymbols->fNarrowAmPms,
1665 fSymbols->fNarrowAmPmsCount);
1666 }
1667 break;
1668
1669 // if we see pattern character for UDAT_TIME_SEPARATOR_FIELD (none currently defined),
1670 // write out the time separator string. Leave support in for future definition.
1671 case UDAT_TIME_SEPARATOR_FIELD:
1672 {
1673 UnicodeString separator;
1674 appendTo += fSymbols->getTimeSeparatorString(separator);
1675 }
1676 break;
1677
1678 // for "h" and "hh", write out the hour, adjusting noon and midnight to show up
1679 // as "12"
1680 case UDAT_HOUR1_FIELD:
1681 if (value == 0)
1682 zeroPaddingNumber(currentNumberFormat,appendTo, cal.getLeastMaximum(UCAL_HOUR) + 1, count, maxIntCount);
1683 else
1684 zeroPaddingNumber(currentNumberFormat,appendTo, value, count, maxIntCount);
1685 break;
1686
1687 case UDAT_TIMEZONE_FIELD: // 'z'
1688 case UDAT_TIMEZONE_RFC_FIELD: // 'Z'
1689 case UDAT_TIMEZONE_GENERIC_FIELD: // 'v'
1690 case UDAT_TIMEZONE_SPECIAL_FIELD: // 'V'
1691 case UDAT_TIMEZONE_LOCALIZED_GMT_OFFSET_FIELD: // 'O'
1692 case UDAT_TIMEZONE_ISO_FIELD: // 'X'
1693 case UDAT_TIMEZONE_ISO_LOCAL_FIELD: // 'x'
1694 {
1695 UChar zsbuf[ZONE_NAME_U16_MAX];
1696 UnicodeString zoneString(zsbuf, 0, UPRV_LENGTHOF(zsbuf));
1697 const TimeZone& tz = cal.getTimeZone();
1698 UDate date = cal.getTime(status);
1699 const TimeZoneFormat *tzfmt = tzFormat(status);
1700 if (U_SUCCESS(status)) {
1701 if (patternCharIndex == UDAT_TIMEZONE_FIELD) {
1702 if (count < 4) {
1703 // "z", "zz", "zzz"
1704 tzfmt->format(UTZFMT_STYLE_SPECIFIC_SHORT, tz, date, zoneString);
1705 capContextUsageType = DateFormatSymbols::kCapContextUsageMetazoneShort;
1706 } else {
1707 // "zzzz" or longer
1708 tzfmt->format(UTZFMT_STYLE_SPECIFIC_LONG, tz, date, zoneString);
1709 capContextUsageType = DateFormatSymbols::kCapContextUsageMetazoneLong;
1710 }
1711 }
1712 else if (patternCharIndex == UDAT_TIMEZONE_RFC_FIELD) {
1713 if (count < 4) {
1714 // "Z"
1715 tzfmt->format(UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL, tz, date, zoneString);
1716 } else if (count == 5) {
1717 // "ZZZZZ"
1718 tzfmt->format(UTZFMT_STYLE_ISO_EXTENDED_FULL, tz, date, zoneString);
1719 } else {
1720 // "ZZ", "ZZZ", "ZZZZ"
1721 tzfmt->format(UTZFMT_STYLE_LOCALIZED_GMT, tz, date, zoneString);
1722 }
1723 }
1724 else if (patternCharIndex == UDAT_TIMEZONE_GENERIC_FIELD) {
1725 if (count == 1) {
1726 // "v"
1727 tzfmt->format(UTZFMT_STYLE_GENERIC_SHORT, tz, date, zoneString);
1728 capContextUsageType = DateFormatSymbols::kCapContextUsageMetazoneShort;
1729 } else if (count == 4) {
1730 // "vvvv"
1731 tzfmt->format(UTZFMT_STYLE_GENERIC_LONG, tz, date, zoneString);
1732 capContextUsageType = DateFormatSymbols::kCapContextUsageMetazoneLong;
1733 }
1734 }
1735 else if (patternCharIndex == UDAT_TIMEZONE_SPECIAL_FIELD) {
1736 if (count == 1) {
1737 // "V"
1738 tzfmt->format(UTZFMT_STYLE_ZONE_ID_SHORT, tz, date, zoneString);
1739 } else if (count == 2) {
1740 // "VV"
1741 tzfmt->format(UTZFMT_STYLE_ZONE_ID, tz, date, zoneString);
1742 } else if (count == 3) {
1743 // "VVV"
1744 tzfmt->format(UTZFMT_STYLE_EXEMPLAR_LOCATION, tz, date, zoneString);
1745 } else if (count == 4) {
1746 // "VVVV"
1747 tzfmt->format(UTZFMT_STYLE_GENERIC_LOCATION, tz, date, zoneString);
1748 capContextUsageType = DateFormatSymbols::kCapContextUsageZoneLong;
1749 }
1750 }
1751 else if (patternCharIndex == UDAT_TIMEZONE_LOCALIZED_GMT_OFFSET_FIELD) {
1752 if (count == 1) {
1753 // "O"
1754 tzfmt->format(UTZFMT_STYLE_LOCALIZED_GMT_SHORT, tz, date, zoneString);
1755 } else if (count == 4) {
1756 // "OOOO"
1757 tzfmt->format(UTZFMT_STYLE_LOCALIZED_GMT, tz, date, zoneString);
1758 }
1759 }
1760 else if (patternCharIndex == UDAT_TIMEZONE_ISO_FIELD) {
1761 if (count == 1) {
1762 // "X"
1763 tzfmt->format(UTZFMT_STYLE_ISO_BASIC_SHORT, tz, date, zoneString);
1764 } else if (count == 2) {
1765 // "XX"
1766 tzfmt->format(UTZFMT_STYLE_ISO_BASIC_FIXED, tz, date, zoneString);
1767 } else if (count == 3) {
1768 // "XXX"
1769 tzfmt->format(UTZFMT_STYLE_ISO_EXTENDED_FIXED, tz, date, zoneString);
1770 } else if (count == 4) {
1771 // "XXXX"
1772 tzfmt->format(UTZFMT_STYLE_ISO_BASIC_FULL, tz, date, zoneString);
1773 } else if (count == 5) {
1774 // "XXXXX"
1775 tzfmt->format(UTZFMT_STYLE_ISO_EXTENDED_FULL, tz, date, zoneString);
1776 }
1777 }
1778 else if (patternCharIndex == UDAT_TIMEZONE_ISO_LOCAL_FIELD) {
1779 if (count == 1) {
1780 // "x"
1781 tzfmt->format(UTZFMT_STYLE_ISO_BASIC_LOCAL_SHORT, tz, date, zoneString);
1782 } else if (count == 2) {
1783 // "xx"
1784 tzfmt->format(UTZFMT_STYLE_ISO_BASIC_LOCAL_FIXED, tz, date, zoneString);
1785 } else if (count == 3) {
1786 // "xxx"
1787 tzfmt->format(UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FIXED, tz, date, zoneString);
1788 } else if (count == 4) {
1789 // "xxxx"
1790 tzfmt->format(UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL, tz, date, zoneString);
1791 } else if (count == 5) {
1792 // "xxxxx"
1793 tzfmt->format(UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FULL, tz, date, zoneString);
1794 }
1795 }
1796 else {
1797 UPRV_UNREACHABLE;
1798 }
1799 }
1800 appendTo += zoneString;
1801 }
1802 break;
1803
1804 case UDAT_QUARTER_FIELD:
1805 if (count >= 4)
1806 _appendSymbol(appendTo, value/3, fSymbols->fQuarters,
1807 fSymbols->fQuartersCount);
1808 else if (count == 3)
1809 _appendSymbol(appendTo, value/3, fSymbols->fShortQuarters,
1810 fSymbols->fShortQuartersCount);
1811 else
1812 zeroPaddingNumber(currentNumberFormat,appendTo, (value/3) + 1, count, maxIntCount);
1813 break;
1814
1815 case UDAT_STANDALONE_QUARTER_FIELD:
1816 if (count >= 4)
1817 _appendSymbol(appendTo, value/3, fSymbols->fStandaloneQuarters,
1818 fSymbols->fStandaloneQuartersCount);
1819 else if (count == 3)
1820 _appendSymbol(appendTo, value/3, fSymbols->fStandaloneShortQuarters,
1821 fSymbols->fStandaloneShortQuartersCount);
1822 else
1823 zeroPaddingNumber(currentNumberFormat,appendTo, (value/3) + 1, count, maxIntCount);
1824 break;
1825
1826 case UDAT_AM_PM_MIDNIGHT_NOON_FIELD:
1827 {
1828 const UnicodeString *toAppend = NULL;
1829 int32_t hour = cal.get(UCAL_HOUR_OF_DAY, status);
1830
1831 // Note: "midnight" can be ambiguous as to whether it refers to beginning of day or end of day.
1832 // For ICU 57 output of "midnight" is temporarily suppressed.
1833
1834 // For "midnight" and "noon":
1835 // Time, as displayed, must be exactly noon or midnight.
1836 // This means minutes and seconds, if present, must be zero.
1837 if ((/*hour == 0 ||*/ hour == 12) &&
1838 (!fHasMinute || cal.get(UCAL_MINUTE, status) == 0) &&
1839 (!fHasSecond || cal.get(UCAL_SECOND, status) == 0)) {
1840 // Stealing am/pm value to use as our array index.
1841 // It works out: am/midnight are both 0, pm/noon are both 1,
1842 // 12 am is 12 midnight, and 12 pm is 12 noon.
1843 int32_t val = cal.get(UCAL_AM_PM, status);
1844
1845 if (count <= 3) {
1846 toAppend = &fSymbols->fAbbreviatedDayPeriods[val];
1847 } else if (count == 4 || count > 5) {
1848 toAppend = &fSymbols->fWideDayPeriods[val];
1849 } else { // count == 5
1850 toAppend = &fSymbols->fNarrowDayPeriods[val];
1851 }
1852 }
1853
1854 // toAppend is NULL if time isn't exactly midnight or noon (as displayed).
1855 // toAppend is bogus if time is midnight or noon, but no localized string exists.
1856 // In either case, fall back to am/pm.
1857 if (toAppend == NULL || toAppend->isBogus()) {
1858 // Reformat with identical arguments except ch, now changed to 'a'.
1859 // We are passing a different fieldToOutput because we want to add
1860 // 'b' to field position. This makes this fallback stable when
1861 // there is a data change on locales.
1862 subFormat(appendTo, u'a', count, capitalizationContext, fieldNum, u'b', handler, cal, status);
1863 return;
1864 } else {
1865 appendTo += *toAppend;
1866 }
1867
1868 break;
1869 }
1870
1871 case UDAT_FLEXIBLE_DAY_PERIOD_FIELD:
1872 {
1873 // TODO: Maybe fetch the DayperiodRules during initialization (instead of at the first
1874 // loading of an instance) if a relevant pattern character (b or B) is used.
1875 const DayPeriodRules *ruleSet = DayPeriodRules::getInstance(this->getSmpFmtLocale(), status);
1876 if (U_FAILURE(status)) {
1877 // Data doesn't conform to spec, therefore loading failed.
1878 break;
1879 }
1880 if (ruleSet == NULL) {
1881 // Data doesn't exist for the locale we're looking for.
1882 // Falling back to am/pm.
1883 // We are passing a different fieldToOutput because we want to add
1884 // 'B' to field position. This makes this fallback stable when
1885 // there is a data change on locales.
1886 subFormat(appendTo, u'a', count, capitalizationContext, fieldNum, u'B', handler, cal, status);
1887 return;
1888 }
1889
1890 // Get current display time.
1891 int32_t hour = cal.get(UCAL_HOUR_OF_DAY, status);
1892 int32_t minute = 0;
1893 if (fHasMinute) {
1894 minute = cal.get(UCAL_MINUTE, status);
1895 }
1896 int32_t second = 0;
1897 if (fHasSecond) {
1898 second = cal.get(UCAL_SECOND, status);
1899 }
1900
1901 // Determine day period.
1902 DayPeriodRules::DayPeriod periodType;
1903 if (hour == 0 && minute == 0 && second == 0 && ruleSet->hasMidnight()) {
1904 periodType = DayPeriodRules::DAYPERIOD_MIDNIGHT;
1905 } else if (hour == 12 && minute == 0 && second == 0 && ruleSet->hasNoon()) {
1906 periodType = DayPeriodRules::DAYPERIOD_NOON;
1907 } else {
1908 periodType = ruleSet->getDayPeriodForHour(hour);
1909 }
1910
1911 // Rule set exists, therefore periodType can't be UNKNOWN.
1912 // Get localized string.
1913 U_ASSERT(periodType != DayPeriodRules::DAYPERIOD_UNKNOWN);
1914 UnicodeString *toAppend = NULL;
1915 int32_t index;
1916
1917 // Note: "midnight" can be ambiguous as to whether it refers to beginning of day or end of day.
1918 // For ICU 57 output of "midnight" is temporarily suppressed.
1919
1920 if (periodType != DayPeriodRules::DAYPERIOD_AM &&
1921 periodType != DayPeriodRules::DAYPERIOD_PM &&
1922 periodType != DayPeriodRules::DAYPERIOD_MIDNIGHT) {
1923 index = (int32_t)periodType;
1924 if (count <= 3) {
1925 toAppend = &fSymbols->fAbbreviatedDayPeriods[index]; // i.e. short
1926 } else if (count == 4 || count > 5) {
1927 toAppend = &fSymbols->fWideDayPeriods[index];
1928 } else { // count == 5
1929 toAppend = &fSymbols->fNarrowDayPeriods[index];
1930 }
1931 }
1932
1933 // Fallback schedule:
1934 // Midnight/Noon -> General Periods -> AM/PM.
1935
1936 // Midnight/Noon -> General Periods.
1937 if ((toAppend == NULL || toAppend->isBogus()) &&
1938 (periodType == DayPeriodRules::DAYPERIOD_MIDNIGHT ||
1939 periodType == DayPeriodRules::DAYPERIOD_NOON)) {
1940 periodType = ruleSet->getDayPeriodForHour(hour);
1941 index = (int32_t)periodType;
1942
1943 if (count <= 3) {
1944 toAppend = &fSymbols->fAbbreviatedDayPeriods[index]; // i.e. short
1945 } else if (count == 4 || count > 5) {
1946 toAppend = &fSymbols->fWideDayPeriods[index];
1947 } else { // count == 5
1948 toAppend = &fSymbols->fNarrowDayPeriods[index];
1949 }
1950 }
1951
1952 // General Periods -> AM/PM.
1953 if (periodType == DayPeriodRules::DAYPERIOD_AM ||
1954 periodType == DayPeriodRules::DAYPERIOD_PM ||
1955 toAppend->isBogus()) {
1956 // We are passing a different fieldToOutput because we want to add
1957 // 'B' to field position iterator. This makes this fallback stable when
1958 // there is a data change on locales.
1959 subFormat(appendTo, u'a', count, capitalizationContext, fieldNum, u'B', handler, cal, status);
1960 return;
1961 }
1962 else {
1963 appendTo += *toAppend;
1964 }
1965
1966 break;
1967 }
1968
1969 // all of the other pattern symbols can be formatted as simple numbers with
1970 // appropriate zero padding
1971 default:
1972 zeroPaddingNumber(currentNumberFormat,appendTo, value, count, maxIntCount);
1973 break;
1974 }
1975#if !UCONFIG_NO_BREAK_ITERATION
1976 // if first field, check to see whether we need to and are able to titlecase it
1977 if (fieldNum == 0 && fCapitalizationBrkIter != NULL && appendTo.length() > beginOffset &&
1978 u_islower(appendTo.char32At(beginOffset))) {
1979 UBool titlecase = FALSE;
1980 switch (capitalizationContext) {
1981 case UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE:
1982 titlecase = TRUE;
1983 break;
1984 case UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU:
1985 titlecase = fSymbols->fCapitalization[capContextUsageType][0];
1986 break;
1987 case UDISPCTX_CAPITALIZATION_FOR_STANDALONE:
1988 titlecase = fSymbols->fCapitalization[capContextUsageType][1];
1989 break;
1990 default:
1991 // titlecase = FALSE;
1992 break;
1993 }
1994 if (titlecase) {
1995 BreakIterator* const mutableCapitalizationBrkIter = fCapitalizationBrkIter->clone();
1996 UnicodeString firstField(appendTo, beginOffset);
1997 firstField.toTitle(mutableCapitalizationBrkIter, fLocale, U_TITLECASE_NO_LOWERCASE | U_TITLECASE_NO_BREAK_ADJUSTMENT);
1998 appendTo.replaceBetween(beginOffset, appendTo.length(), firstField);
1999 delete mutableCapitalizationBrkIter;
2000 }
2001 }
2002#endif
2003
2004 handler.addAttribute(DateFormatSymbols::getPatternCharIndex(fieldToOutput), beginOffset, appendTo.length());
2005}
2006
2007//----------------------------------------------------------------------
2008
2009void SimpleDateFormat::adoptNumberFormat(NumberFormat *formatToAdopt) {
2010 fixNumberFormatForDates(*formatToAdopt);
2011 delete fNumberFormat;
2012 fNumberFormat = formatToAdopt;
2013
2014 // We successfully set the default number format. Now delete the overrides
2015 // (can't fail).
2016 if (fSharedNumberFormatters) {
2017 freeSharedNumberFormatters(fSharedNumberFormatters);
2018 fSharedNumberFormatters = NULL;
2019 }
2020
2021 // Also re-compute the fast formatters.
2022 UErrorCode localStatus = U_ZERO_ERROR;
2023 freeFastNumberFormatters();
2024 initFastNumberFormatters(localStatus);
2025}
2026
2027void SimpleDateFormat::adoptNumberFormat(const UnicodeString& fields, NumberFormat *formatToAdopt, UErrorCode &status){
2028 fixNumberFormatForDates(*formatToAdopt);
2029 LocalPointer<NumberFormat> fmt(formatToAdopt);
2030 if (U_FAILURE(status)) {
2031 return;
2032 }
2033
2034 // We must ensure fSharedNumberFormatters is allocated.
2035 if (fSharedNumberFormatters == NULL) {
2036 fSharedNumberFormatters = allocSharedNumberFormatters();
2037 if (fSharedNumberFormatters == NULL) {
2038 status = U_MEMORY_ALLOCATION_ERROR;
2039 return;
2040 }
2041 }
2042 const SharedNumberFormat *newFormat = createSharedNumberFormat(fmt.orphan());
2043 if (newFormat == NULL) {
2044 status = U_MEMORY_ALLOCATION_ERROR;
2045 return;
2046 }
2047 for (int i=0; i<fields.length(); i++) {
2048 UChar field = fields.charAt(i);
2049 // if the pattern character is unrecognized, signal an error and bail out
2050 UDateFormatField patternCharIndex = DateFormatSymbols::getPatternCharIndex(field);
2051 if (patternCharIndex == UDAT_FIELD_COUNT) {
2052 status = U_INVALID_FORMAT_ERROR;
2053 newFormat->deleteIfZeroRefCount();
2054 return;
2055 }
2056
2057 // Set the number formatter in the table
2058 SharedObject::copyPtr(
2059 newFormat, fSharedNumberFormatters[patternCharIndex]);
2060 }
2061 newFormat->deleteIfZeroRefCount();
2062}
2063
2064const NumberFormat *
2065SimpleDateFormat::getNumberFormatForField(UChar field) const {
2066 UDateFormatField index = DateFormatSymbols::getPatternCharIndex(field);
2067 if (index == UDAT_FIELD_COUNT) {
2068 return NULL;
2069 }
2070 return getNumberFormatByIndex(index);
2071}
2072
2073//----------------------------------------------------------------------
2074void
2075SimpleDateFormat::zeroPaddingNumber(
2076 const NumberFormat *currentNumberFormat,
2077 UnicodeString &appendTo,
2078 int32_t value, int32_t minDigits, int32_t maxDigits) const
2079{
2080 const number::LocalizedNumberFormatter* fastFormatter = nullptr;
2081 // NOTE: This uses the heuristic that these five min/max int settings account for the vast majority
2082 // of SimpleDateFormat number formatting cases at the time of writing (ICU 62).
2083 if (currentNumberFormat == fNumberFormat) {
2084 if (maxDigits == 10) {
2085 if (minDigits == 1) {
2086 fastFormatter = fFastNumberFormatters[SMPDTFMT_NF_1x10];
2087 } else if (minDigits == 2) {
2088 fastFormatter = fFastNumberFormatters[SMPDTFMT_NF_2x10];
2089 } else if (minDigits == 3) {
2090 fastFormatter = fFastNumberFormatters[SMPDTFMT_NF_3x10];
2091 } else if (minDigits == 4) {
2092 fastFormatter = fFastNumberFormatters[SMPDTFMT_NF_4x10];
2093 }
2094 } else if (maxDigits == 2) {
2095 if (minDigits == 2) {
2096 fastFormatter = fFastNumberFormatters[SMPDTFMT_NF_2x2];
2097 }
2098 }
2099 }
2100 if (fastFormatter != nullptr) {
2101 // Can use fast path
2102 number::impl::UFormattedNumberData result;
2103 result.quantity.setToInt(value);
2104 UErrorCode localStatus = U_ZERO_ERROR;
2105 fastFormatter->formatImpl(&result, localStatus);
2106 if (U_FAILURE(localStatus)) {
2107 return;
2108 }
2109 appendTo.append(result.getStringRef().toTempUnicodeString());
2110 return;
2111 }
2112
2113 // Check for RBNF (no clone necessary)
2114 auto* rbnf = dynamic_cast<const RuleBasedNumberFormat*>(currentNumberFormat);
2115 if (rbnf != nullptr) {
2116 FieldPosition pos(FieldPosition::DONT_CARE);
2117 rbnf->format(value, appendTo, pos); // 3rd arg is there to speed up processing
2118 return;
2119 }
2120
2121 // Fall back to slow path (clone and mutate the NumberFormat)
2122 if (currentNumberFormat != nullptr) {
2123 FieldPosition pos(FieldPosition::DONT_CARE);
2124 LocalPointer<NumberFormat> nf(currentNumberFormat->clone());
2125 nf->setMinimumIntegerDigits(minDigits);
2126 nf->setMaximumIntegerDigits(maxDigits);
2127 nf->format(value, appendTo, pos); // 3rd arg is there to speed up processing
2128 }
2129}
2130
2131//----------------------------------------------------------------------
2132
2133/**
2134 * Return true if the given format character, occuring count
2135 * times, represents a numeric field.
2136 */
2137UBool SimpleDateFormat::isNumeric(UChar formatChar, int32_t count) {
2138 return DateFormatSymbols::isNumericPatternChar(formatChar, count);
2139}
2140
2141UBool
2142SimpleDateFormat::isAtNumericField(const UnicodeString &pattern, int32_t patternOffset) {
2143 if (patternOffset >= pattern.length()) {
2144 // not at any field
2145 return FALSE;
2146 }
2147 UChar ch = pattern.charAt(patternOffset);
2148 UDateFormatField f = DateFormatSymbols::getPatternCharIndex(ch);
2149 if (f == UDAT_FIELD_COUNT) {
2150 // not at any field
2151 return FALSE;
2152 }
2153 int32_t i = patternOffset;
2154 while (pattern.charAt(++i) == ch) {}
2155 return DateFormatSymbols::isNumericField(f, i - patternOffset);
2156}
2157
2158UBool
2159SimpleDateFormat::isAfterNonNumericField(const UnicodeString &pattern, int32_t patternOffset) {
2160 if (patternOffset <= 0) {
2161 // not after any field
2162 return FALSE;
2163 }
2164 UChar ch = pattern.charAt(--patternOffset);
2165 UDateFormatField f = DateFormatSymbols::getPatternCharIndex(ch);
2166 if (f == UDAT_FIELD_COUNT) {
2167 // not after any field
2168 return FALSE;
2169 }
2170 int32_t i = patternOffset;
2171 while (pattern.charAt(--i) == ch) {}
2172 return !DateFormatSymbols::isNumericField(f, patternOffset - i);
2173}
2174
2175void
2176SimpleDateFormat::parse(const UnicodeString& text, Calendar& cal, ParsePosition& parsePos) const
2177{
2178 UErrorCode status = U_ZERO_ERROR;
2179 int32_t pos = parsePos.getIndex();
2180 if(parsePos.getIndex() < 0) {
2181 parsePos.setErrorIndex(0);
2182 return;
2183 }
2184 int32_t start = pos;
2185
2186 // Hold the day period until everything else is parsed, because we need
2187 // the hour to interpret time correctly.
2188 int32_t dayPeriodInt = -1;
2189
2190 UBool ambiguousYear[] = { FALSE };
2191 int32_t saveHebrewMonth = -1;
2192 int32_t count = 0;
2193 UTimeZoneFormatTimeType tzTimeType = UTZFMT_TIME_TYPE_UNKNOWN;
2194
2195 // For parsing abutting numeric fields. 'abutPat' is the
2196 // offset into 'pattern' of the first of 2 or more abutting
2197 // numeric fields. 'abutStart' is the offset into 'text'
2198 // where parsing the fields begins. 'abutPass' starts off as 0
2199 // and increments each time we try to parse the fields.
2200 int32_t abutPat = -1; // If >=0, we are in a run of abutting numeric fields
2201 int32_t abutStart = 0;
2202 int32_t abutPass = 0;
2203 UBool inQuote = FALSE;
2204
2205 MessageFormat * numericLeapMonthFormatter = NULL;
2206
2207 Calendar* calClone = NULL;
2208 Calendar *workCal = &cal;
2209 if (&cal != fCalendar && uprv_strcmp(cal.getType(), fCalendar->getType()) != 0) {
2210 // Different calendar type
2211 // We use the time/zone from the input calendar, but
2212 // do not use the input calendar for field calculation.
2213 calClone = fCalendar->clone();
2214 if (calClone != NULL) {
2215 calClone->setTime(cal.getTime(status),status);
2216 if (U_FAILURE(status)) {
2217 goto ExitParse;
2218 }
2219 calClone->setTimeZone(cal.getTimeZone());
2220 workCal = calClone;
2221 } else {
2222 status = U_MEMORY_ALLOCATION_ERROR;
2223 goto ExitParse;
2224 }
2225 }
2226
2227 if (fSymbols->fLeapMonthPatterns != NULL && fSymbols->fLeapMonthPatternsCount >= DateFormatSymbols::kMonthPatternsCount) {
2228 numericLeapMonthFormatter = new MessageFormat(fSymbols->fLeapMonthPatterns[DateFormatSymbols::kLeapMonthPatternNumeric], fLocale, status);
2229 if (numericLeapMonthFormatter == NULL) {
2230 status = U_MEMORY_ALLOCATION_ERROR;
2231 goto ExitParse;
2232 } else if (U_FAILURE(status)) {
2233 goto ExitParse; // this will delete numericLeapMonthFormatter
2234 }
2235 }
2236
2237 for (int32_t i=0; i<fPattern.length(); ++i) {
2238 UChar ch = fPattern.charAt(i);
2239
2240 // Handle alphabetic field characters.
2241 if (!inQuote && isSyntaxChar(ch)) {
2242 int32_t fieldPat = i;
2243
2244 // Count the length of this field specifier
2245 count = 1;
2246 while ((i+1)<fPattern.length() &&
2247 fPattern.charAt(i+1) == ch) {
2248 ++count;
2249 ++i;
2250 }
2251
2252 if (isNumeric(ch, count)) {
2253 if (abutPat < 0) {
2254 // Determine if there is an abutting numeric field.
2255 // Record the start of a set of abutting numeric fields.
2256 if (isAtNumericField(fPattern, i + 1)) {
2257 abutPat = fieldPat;
2258 abutStart = pos;
2259 abutPass = 0;
2260 }
2261 }
2262 } else {
2263 abutPat = -1; // End of any abutting fields
2264 }
2265
2266 // Handle fields within a run of abutting numeric fields. Take
2267 // the pattern "HHmmss" as an example. We will try to parse
2268 // 2/2/2 characters of the input text, then if that fails,
2269 // 1/2/2. We only adjust the width of the leftmost field; the
2270 // others remain fixed. This allows "123456" => 12:34:56, but
2271 // "12345" => 1:23:45. Likewise, for the pattern "yyyyMMdd" we
2272 // try 4/2/2, 3/2/2, 2/2/2, and finally 1/2/2.
2273 if (abutPat >= 0) {
2274 // If we are at the start of a run of abutting fields, then
2275 // shorten this field in each pass. If we can't shorten
2276 // this field any more, then the parse of this set of
2277 // abutting numeric fields has failed.
2278 if (fieldPat == abutPat) {
2279 count -= abutPass++;
2280 if (count == 0) {
2281 status = U_PARSE_ERROR;
2282 goto ExitParse;
2283 }
2284 }
2285
2286 pos = subParse(text, pos, ch, count,
2287 TRUE, FALSE, ambiguousYear, saveHebrewMonth, *workCal, i, numericLeapMonthFormatter, &tzTimeType);
2288
2289 // If the parse fails anywhere in the run, back up to the
2290 // start of the run and retry.
2291 if (pos < 0) {
2292 i = abutPat - 1;
2293 pos = abutStart;
2294 continue;
2295 }
2296 }
2297
2298 // Handle non-numeric fields and non-abutting numeric
2299 // fields.
2300 else if (ch != 0x6C) { // pattern char 'l' (SMALL LETTER L) just gets ignored
2301 int32_t s = subParse(text, pos, ch, count,
2302 FALSE, TRUE, ambiguousYear, saveHebrewMonth, *workCal, i, numericLeapMonthFormatter, &tzTimeType, &dayPeriodInt);
2303
2304 if (s == -pos-1) {
2305 // era not present, in special cases allow this to continue
2306 // from the position where the era was expected
2307 s = pos;
2308
2309 if (i+1 < fPattern.length()) {
2310 // move to next pattern character
2311 UChar c = fPattern.charAt(i+1);
2312
2313 // check for whitespace
2314 if (PatternProps::isWhiteSpace(c)) {
2315 i++;
2316 // Advance over run in pattern
2317 while ((i+1)<fPattern.length() &&
2318 PatternProps::isWhiteSpace(fPattern.charAt(i+1))) {
2319 ++i;
2320 }
2321 }
2322 }
2323 }
2324 else if (s <= 0) {
2325 status = U_PARSE_ERROR;
2326 goto ExitParse;
2327 }
2328 pos = s;
2329 }
2330 }
2331
2332 // Handle literal pattern characters. These are any
2333 // quoted characters and non-alphabetic unquoted
2334 // characters.
2335 else {
2336
2337 abutPat = -1; // End of any abutting fields
2338
2339 if (! matchLiterals(fPattern, i, text, pos, getBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE, status), getBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH, status), isLenient())) {
2340 status = U_PARSE_ERROR;
2341 goto ExitParse;
2342 }
2343 }
2344 }
2345
2346 // Special hack for trailing "." after non-numeric field.
2347 if (text.charAt(pos) == 0x2e && getBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE, status)) {
2348 // only do if the last field is not numeric
2349 if (isAfterNonNumericField(fPattern, fPattern.length())) {
2350 pos++; // skip the extra "."
2351 }
2352 }
2353
2354 // If dayPeriod is set, use it in conjunction with hour-of-day to determine am/pm.
2355 if (dayPeriodInt >= 0) {
2356 DayPeriodRules::DayPeriod dayPeriod = (DayPeriodRules::DayPeriod)dayPeriodInt;
2357 const DayPeriodRules *ruleSet = DayPeriodRules::getInstance(this->getSmpFmtLocale(), status);
2358
2359 if (!cal.isSet(UCAL_HOUR) && !cal.isSet(UCAL_HOUR_OF_DAY)) {
2360 // If hour is not set, set time to the midpoint of current day period, overwriting
2361 // minutes if it's set.
2362 double midPoint = ruleSet->getMidPointForDayPeriod(dayPeriod, status);
2363
2364 // If we can't get midPoint we do nothing.
2365 if (U_SUCCESS(status)) {
2366 // Truncate midPoint toward zero to get the hour.
2367 // Any leftover means it was a half-hour.
2368 int32_t midPointHour = (int32_t) midPoint;
2369 int32_t midPointMinute = (midPoint - midPointHour) > 0 ? 30 : 0;
2370
2371 // No need to set am/pm because hour-of-day is set last therefore takes precedence.
2372 cal.set(UCAL_HOUR_OF_DAY, midPointHour);
2373 cal.set(UCAL_MINUTE, midPointMinute);
2374 }
2375 } else {
2376 int hourOfDay;
2377
2378 if (cal.isSet(UCAL_HOUR_OF_DAY)) { // Hour is parsed in 24-hour format.
2379 hourOfDay = cal.get(UCAL_HOUR_OF_DAY, status);
2380 } else { // Hour is parsed in 12-hour format.
2381 hourOfDay = cal.get(UCAL_HOUR, status);
2382 // cal.get() turns 12 to 0 for 12-hour time; change 0 to 12
2383 // so 0 unambiguously means a 24-hour time from above.
2384 if (hourOfDay == 0) { hourOfDay = 12; }
2385 }
2386 U_ASSERT(0 <= hourOfDay && hourOfDay <= 23);
2387
2388
2389 // If hour-of-day is 0 or 13 thru 23 then input time in unambiguously in 24-hour format.
2390 if (hourOfDay == 0 || (13 <= hourOfDay && hourOfDay <= 23)) {
2391 // Make hour-of-day take precedence over (hour + am/pm) by setting it again.
2392 cal.set(UCAL_HOUR_OF_DAY, hourOfDay);
2393 } else {
2394 // We have a 12-hour time and need to choose between am and pm.
2395 // Behave as if dayPeriod spanned 6 hours each way from its center point.
2396 // This will parse correctly for consistent time + period (e.g. 10 at night) as
2397 // well as provide a reasonable recovery for inconsistent time + period (e.g.
2398 // 9 in the afternoon).
2399
2400 // Assume current time is in the AM.
2401 // - Change 12 back to 0 for easier handling of 12am.
2402 // - Append minutes as fractional hours because e.g. 8:15 and 8:45 could be parsed
2403 // into different half-days if center of dayPeriod is at 14:30.
2404 // - cal.get(MINUTE) will return 0 if MINUTE is unset, which works.
2405 if (hourOfDay == 12) { hourOfDay = 0; }
2406 double currentHour = hourOfDay + (cal.get(UCAL_MINUTE, status)) / 60.0;
2407 double midPointHour = ruleSet->getMidPointForDayPeriod(dayPeriod, status);
2408
2409 if (U_SUCCESS(status)) {
2410 double hoursAheadMidPoint = currentHour - midPointHour;
2411
2412 // Assume current time is in the AM.
2413 if (-6 <= hoursAheadMidPoint && hoursAheadMidPoint < 6) {
2414 // Assumption holds; set time as such.
2415 cal.set(UCAL_AM_PM, 0);
2416 } else {
2417 cal.set(UCAL_AM_PM, 1);
2418 }
2419 }
2420 }
2421 }
2422 }
2423
2424 // At this point the fields of Calendar have been set. Calendar
2425 // will fill in default values for missing fields when the time
2426 // is computed.
2427
2428 parsePos.setIndex(pos);
2429
2430 // This part is a problem: When we call parsedDate.after, we compute the time.
2431 // Take the date April 3 2004 at 2:30 am. When this is first set up, the year
2432 // will be wrong if we're parsing a 2-digit year pattern. It will be 1904.
2433 // April 3 1904 is a Sunday (unlike 2004) so it is the DST onset day. 2:30 am
2434 // is therefore an "impossible" time, since the time goes from 1:59 to 3:00 am
2435 // on that day. It is therefore parsed out to fields as 3:30 am. Then we
2436 // add 100 years, and get April 3 2004 at 3:30 am. Note that April 3 2004 is
2437 // a Saturday, so it can have a 2:30 am -- and it should. [LIU]
2438 /*
2439 UDate parsedDate = calendar.getTime();
2440 if( ambiguousYear[0] && !parsedDate.after(fDefaultCenturyStart) ) {
2441 calendar.add(Calendar.YEAR, 100);
2442 parsedDate = calendar.getTime();
2443 }
2444 */
2445 // Because of the above condition, save off the fields in case we need to readjust.
2446 // The procedure we use here is not particularly efficient, but there is no other
2447 // way to do this given the API restrictions present in Calendar. We minimize
2448 // inefficiency by only performing this computation when it might apply, that is,
2449 // when the two-digit year is equal to the start year, and thus might fall at the
2450 // front or the back of the default century. This only works because we adjust
2451 // the year correctly to start with in other cases -- see subParse().
2452 if (ambiguousYear[0] || tzTimeType != UTZFMT_TIME_TYPE_UNKNOWN) // If this is true then the two-digit year == the default start year
2453 {
2454 // We need a copy of the fields, and we need to avoid triggering a call to
2455 // complete(), which will recalculate the fields. Since we can't access
2456 // the fields[] array in Calendar, we clone the entire object. This will
2457 // stop working if Calendar.clone() is ever rewritten to call complete().
2458 Calendar *copy;
2459 if (ambiguousYear[0]) {
2460 copy = cal.clone();
2461 // Check for failed cloning.
2462 if (copy == NULL) {
2463 status = U_MEMORY_ALLOCATION_ERROR;
2464 goto ExitParse;
2465 }
2466 UDate parsedDate = copy->getTime(status);
2467 // {sfb} check internalGetDefaultCenturyStart
2468 if (fHaveDefaultCentury && (parsedDate < fDefaultCenturyStart)) {
2469 // We can't use add here because that does a complete() first.
2470 cal.set(UCAL_YEAR, fDefaultCenturyStartYear + 100);
2471 }
2472 delete copy;
2473 }
2474
2475 if (tzTimeType != UTZFMT_TIME_TYPE_UNKNOWN) {
2476 copy = cal.clone();
2477 // Check for failed cloning.
2478 if (copy == NULL) {
2479 status = U_MEMORY_ALLOCATION_ERROR;
2480 goto ExitParse;
2481 }
2482 const TimeZone & tz = cal.getTimeZone();
2483 BasicTimeZone *btz = NULL;
2484
2485 if (dynamic_cast<const OlsonTimeZone *>(&tz) != NULL
2486 || dynamic_cast<const SimpleTimeZone *>(&tz) != NULL
2487 || dynamic_cast<const RuleBasedTimeZone *>(&tz) != NULL
2488 || dynamic_cast<const VTimeZone *>(&tz) != NULL) {
2489 btz = (BasicTimeZone*)&tz;
2490 }
2491
2492 // Get local millis
2493 copy->set(UCAL_ZONE_OFFSET, 0);
2494 copy->set(UCAL_DST_OFFSET, 0);
2495 UDate localMillis = copy->getTime(status);
2496
2497 // Make sure parsed time zone type (Standard or Daylight)
2498 // matches the rule used by the parsed time zone.
2499 int32_t raw, dst;
2500 if (btz != NULL) {
2501 if (tzTimeType == UTZFMT_TIME_TYPE_STANDARD) {
2502 btz->getOffsetFromLocal(localMillis,
2503 BasicTimeZone::kStandard, BasicTimeZone::kStandard, raw, dst, status);
2504 } else {
2505 btz->getOffsetFromLocal(localMillis,
2506 BasicTimeZone::kDaylight, BasicTimeZone::kDaylight, raw, dst, status);
2507 }
2508 } else {
2509 // No good way to resolve ambiguous time at transition,
2510 // but following code work in most case.
2511 tz.getOffset(localMillis, TRUE, raw, dst, status);
2512 }
2513
2514 // Now, compare the results with parsed type, either standard or daylight saving time
2515 int32_t resolvedSavings = dst;
2516 if (tzTimeType == UTZFMT_TIME_TYPE_STANDARD) {
2517 if (dst != 0) {
2518 // Override DST_OFFSET = 0 in the result calendar
2519 resolvedSavings = 0;
2520 }
2521 } else { // tztype == TZTYPE_DST
2522 if (dst == 0) {
2523 if (btz != NULL) {
2524 UDate time = localMillis + raw;
2525 // We use the nearest daylight saving time rule.
2526 TimeZoneTransition beforeTrs, afterTrs;
2527 UDate beforeT = time, afterT = time;
2528 int32_t beforeSav = 0, afterSav = 0;
2529 UBool beforeTrsAvail, afterTrsAvail;
2530
2531 // Search for DST rule before or on the time
2532 while (TRUE) {
2533 beforeTrsAvail = btz->getPreviousTransition(beforeT, TRUE, beforeTrs);
2534 if (!beforeTrsAvail) {
2535 break;
2536 }
2537 beforeT = beforeTrs.getTime() - 1;
2538 beforeSav = beforeTrs.getFrom()->getDSTSavings();
2539 if (beforeSav != 0) {
2540 break;
2541 }
2542 }
2543
2544 // Search for DST rule after the time
2545 while (TRUE) {
2546 afterTrsAvail = btz->getNextTransition(afterT, FALSE, afterTrs);
2547 if (!afterTrsAvail) {
2548 break;
2549 }
2550 afterT = afterTrs.getTime();
2551 afterSav = afterTrs.getTo()->getDSTSavings();
2552 if (afterSav != 0) {
2553 break;
2554 }
2555 }
2556
2557 if (beforeTrsAvail && afterTrsAvail) {
2558 if (time - beforeT > afterT - time) {
2559 resolvedSavings = afterSav;
2560 } else {
2561 resolvedSavings = beforeSav;
2562 }
2563 } else if (beforeTrsAvail && beforeSav != 0) {
2564 resolvedSavings = beforeSav;
2565 } else if (afterTrsAvail && afterSav != 0) {
2566 resolvedSavings = afterSav;
2567 } else {
2568 resolvedSavings = btz->getDSTSavings();
2569 }
2570 } else {
2571 resolvedSavings = tz.getDSTSavings();
2572 }
2573 if (resolvedSavings == 0) {
2574 // final fallback
2575 resolvedSavings = U_MILLIS_PER_HOUR;
2576 }
2577 }
2578 }
2579 cal.set(UCAL_ZONE_OFFSET, raw);
2580 cal.set(UCAL_DST_OFFSET, resolvedSavings);
2581 delete copy;
2582 }
2583 }
2584ExitParse:
2585 // Set the parsed result if local calendar is used
2586 // instead of the input calendar
2587 if (U_SUCCESS(status) && workCal != &cal) {
2588 cal.setTimeZone(workCal->getTimeZone());
2589 cal.setTime(workCal->getTime(status), status);
2590 }
2591
2592 if (numericLeapMonthFormatter != NULL) {
2593 delete numericLeapMonthFormatter;
2594 }
2595 if (calClone != NULL) {
2596 delete calClone;
2597 }
2598
2599 // If any Calendar calls failed, we pretend that we
2600 // couldn't parse the string, when in reality this isn't quite accurate--
2601 // we did parse it; the Calendar calls just failed.
2602 if (U_FAILURE(status)) {
2603 parsePos.setErrorIndex(pos);
2604 parsePos.setIndex(start);
2605 }
2606}
2607
2608//----------------------------------------------------------------------
2609
2610static int32_t
2611matchStringWithOptionalDot(const UnicodeString &text,
2612 int32_t index,
2613 const UnicodeString &data);
2614
2615int32_t SimpleDateFormat::matchQuarterString(const UnicodeString& text,
2616 int32_t start,
2617 UCalendarDateFields field,
2618 const UnicodeString* data,
2619 int32_t dataCount,
2620 Calendar& cal) const
2621{
2622 int32_t i = 0;
2623 int32_t count = dataCount;
2624
2625 // There may be multiple strings in the data[] array which begin with
2626 // the same prefix (e.g., Cerven and Cervenec (June and July) in Czech).
2627 // We keep track of the longest match, and return that. Note that this
2628 // unfortunately requires us to test all array elements.
2629 int32_t bestMatchLength = 0, bestMatch = -1;
2630 UnicodeString bestMatchName;
2631
2632 for (; i < count; ++i) {
2633 int32_t matchLength = 0;
2634 if ((matchLength = matchStringWithOptionalDot(text, start, data[i])) > bestMatchLength) {
2635 bestMatchLength = matchLength;
2636 bestMatch = i;
2637 }
2638 }
2639
2640 if (bestMatch >= 0) {
2641 cal.set(field, bestMatch * 3);
2642 return start + bestMatchLength;
2643 }
2644
2645 return -start;
2646}
2647
2648int32_t SimpleDateFormat::matchDayPeriodStrings(const UnicodeString& text, int32_t start,
2649 const UnicodeString* data, int32_t dataCount,
2650 int32_t &dayPeriod) const
2651{
2652
2653 int32_t bestMatchLength = 0, bestMatch = -1;
2654
2655 for (int32_t i = 0; i < dataCount; ++i) {
2656 int32_t matchLength = 0;
2657 if ((matchLength = matchStringWithOptionalDot(text, start, data[i])) > bestMatchLength) {
2658 bestMatchLength = matchLength;
2659 bestMatch = i;
2660 }
2661 }
2662
2663 if (bestMatch >= 0) {
2664 dayPeriod = bestMatch;
2665 return start + bestMatchLength;
2666 }
2667
2668 return -start;
2669}
2670
2671//----------------------------------------------------------------------
2672UBool SimpleDateFormat::matchLiterals(const UnicodeString &pattern,
2673 int32_t &patternOffset,
2674 const UnicodeString &text,
2675 int32_t &textOffset,
2676 UBool whitespaceLenient,
2677 UBool partialMatchLenient,
2678 UBool oldLeniency)
2679{
2680 UBool inQuote = FALSE;
2681 UnicodeString literal;
2682 int32_t i = patternOffset;
2683
2684 // scan pattern looking for contiguous literal characters
2685 for ( ; i < pattern.length(); i += 1) {
2686 UChar ch = pattern.charAt(i);
2687
2688 if (!inQuote && isSyntaxChar(ch)) {
2689 break;
2690 }
2691
2692 if (ch == QUOTE) {
2693 // Match a quote literal ('') inside OR outside of quotes
2694 if ((i + 1) < pattern.length() && pattern.charAt(i + 1) == QUOTE) {
2695 i += 1;
2696 } else {
2697 inQuote = !inQuote;
2698 continue;
2699 }
2700 }
2701
2702 literal += ch;
2703 }
2704
2705 // at this point, literal contains the literal text
2706 // and i is the index of the next non-literal pattern character.
2707 int32_t p;
2708 int32_t t = textOffset;
2709
2710 if (whitespaceLenient) {
2711 // trim leading, trailing whitespace from
2712 // the literal text
2713 literal.trim();
2714
2715 // ignore any leading whitespace in the text
2716 while (t < text.length() && u_isWhitespace(text.charAt(t))) {
2717 t += 1;
2718 }
2719 }
2720
2721 for (p = 0; p < literal.length() && t < text.length();) {
2722 UBool needWhitespace = FALSE;
2723
2724 while (p < literal.length() && PatternProps::isWhiteSpace(literal.charAt(p))) {
2725 needWhitespace = TRUE;
2726 p += 1;
2727 }
2728
2729 if (needWhitespace) {
2730 int32_t tStart = t;
2731
2732 while (t < text.length()) {
2733 UChar tch = text.charAt(t);
2734
2735 if (!u_isUWhiteSpace(tch) && !PatternProps::isWhiteSpace(tch)) {
2736 break;
2737 }
2738
2739 t += 1;
2740 }
2741
2742 // TODO: should we require internal spaces
2743 // in lenient mode? (There won't be any
2744 // leading or trailing spaces)
2745 if (!whitespaceLenient && t == tStart) {
2746 // didn't find matching whitespace:
2747 // an error in strict mode
2748 return FALSE;
2749 }
2750
2751 // In strict mode, this run of whitespace
2752 // may have been at the end.
2753 if (p >= literal.length()) {
2754 break;
2755 }
2756 }
2757 if (t >= text.length() || literal.charAt(p) != text.charAt(t)) {
2758 // Ran out of text, or found a non-matching character:
2759 // OK in lenient mode, an error in strict mode.
2760 if (whitespaceLenient) {
2761 if (t == textOffset && text.charAt(t) == 0x2e &&
2762 isAfterNonNumericField(pattern, patternOffset)) {
2763 // Lenient mode and the literal input text begins with a "." and
2764 // we are after a non-numeric field: We skip the "."
2765 ++t;
2766 continue; // Do not update p.
2767 }
2768 // if it is actual whitespace and we're whitespace lenient it's OK
2769
2770 UChar wsc = text.charAt(t);
2771 if(PatternProps::isWhiteSpace(wsc)) {
2772 // Lenient mode and it's just whitespace we skip it
2773 ++t;
2774 continue; // Do not update p.
2775 }
2776 }
2777 // hack around oldleniency being a bit of a catch-all bucket and we're just adding support specifically for paritial matches
2778 if(partialMatchLenient && oldLeniency) {
2779 break;
2780 }
2781
2782 return FALSE;
2783 }
2784 ++p;
2785 ++t;
2786 }
2787
2788 // At this point if we're in strict mode we have a complete match.
2789 // If we're in lenient mode we may have a partial match, or no
2790 // match at all.
2791 if (p <= 0) {
2792 // no match. Pretend it matched a run of whitespace
2793 // and ignorables in the text.
2794 const UnicodeSet *ignorables = NULL;
2795 UDateFormatField patternCharIndex = DateFormatSymbols::getPatternCharIndex(pattern.charAt(i));
2796 if (patternCharIndex != UDAT_FIELD_COUNT) {
2797 ignorables = SimpleDateFormatStaticSets::getIgnorables(patternCharIndex);
2798 }
2799
2800 for (t = textOffset; t < text.length(); t += 1) {
2801 UChar ch = text.charAt(t);
2802
2803 if (ignorables == NULL || !ignorables->contains(ch)) {
2804 break;
2805 }
2806 }
2807 }
2808
2809 // if we get here, we've got a complete match.
2810 patternOffset = i - 1;
2811 textOffset = t;
2812
2813 return TRUE;
2814}
2815
2816//----------------------------------------------------------------------
2817
2818int32_t SimpleDateFormat::matchString(const UnicodeString& text,
2819 int32_t start,
2820 UCalendarDateFields field,
2821 const UnicodeString* data,
2822 int32_t dataCount,
2823 const UnicodeString* monthPattern,
2824 Calendar& cal) const
2825{
2826 int32_t i = 0;
2827 int32_t count = dataCount;
2828
2829 if (field == UCAL_DAY_OF_WEEK) i = 1;
2830
2831 // There may be multiple strings in the data[] array which begin with
2832 // the same prefix (e.g., Cerven and Cervenec (June and July) in Czech).
2833 // We keep track of the longest match, and return that. Note that this
2834 // unfortunately requires us to test all array elements.
2835 int32_t bestMatchLength = 0, bestMatch = -1;
2836 UnicodeString bestMatchName;
2837 int32_t isLeapMonth = 0;
2838
2839 for (; i < count; ++i) {
2840 int32_t matchLen = 0;
2841 if ((matchLen = matchStringWithOptionalDot(text, start, data[i])) > bestMatchLength) {
2842 bestMatch = i;
2843 bestMatchLength = matchLen;
2844 }
2845
2846 if (monthPattern != NULL) {
2847 UErrorCode status = U_ZERO_ERROR;
2848 UnicodeString leapMonthName;
2849 SimpleFormatter(*monthPattern, 1, 1, status).format(data[i], leapMonthName, status);
2850 if (U_SUCCESS(status)) {
2851 if ((matchLen = matchStringWithOptionalDot(text, start, leapMonthName)) > bestMatchLength) {
2852 bestMatch = i;
2853 bestMatchLength = matchLen;
2854 isLeapMonth = 1;
2855 }
2856 }
2857 }
2858 }
2859
2860 if (bestMatch >= 0) {
2861 if (field < UCAL_FIELD_COUNT) {
2862 // Adjustment for Hebrew Calendar month Adar II
2863 if (!strcmp(cal.getType(),"hebrew") && field==UCAL_MONTH && bestMatch==13) {
2864 cal.set(field,6);
2865 } else {
2866 if (field == UCAL_YEAR) {
2867 bestMatch++; // only get here for cyclic year names, which match 1-based years 1-60
2868 }
2869 cal.set(field, bestMatch);
2870 }
2871 if (monthPattern != NULL) {
2872 cal.set(UCAL_IS_LEAP_MONTH, isLeapMonth);
2873 }
2874 }
2875
2876 return start + bestMatchLength;
2877 }
2878
2879 return -start;
2880}
2881
2882static int32_t
2883matchStringWithOptionalDot(const UnicodeString &text,
2884 int32_t index,
2885 const UnicodeString &data) {
2886 UErrorCode sts = U_ZERO_ERROR;
2887 int32_t matchLenText = 0;
2888 int32_t matchLenData = 0;
2889
2890 u_caseInsensitivePrefixMatch(text.getBuffer() + index, text.length() - index,
2891 data.getBuffer(), data.length(),
2892 0 /* default case option */,
2893 &matchLenText, &matchLenData,
2894 &sts);
2895 U_ASSERT (U_SUCCESS(sts));
2896
2897 if (matchLenData == data.length() /* normal match */
2898 || (data.charAt(data.length() - 1) == 0x2e
2899 && matchLenData == data.length() - 1 /* match without trailing dot */)) {
2900 return matchLenText;
2901 }
2902
2903 return 0;
2904}
2905
2906//----------------------------------------------------------------------
2907
2908void
2909SimpleDateFormat::set2DigitYearStart(UDate d, UErrorCode& status)
2910{
2911 parseAmbiguousDatesAsAfter(d, status);
2912}
2913
2914/**
2915 * Private member function that converts the parsed date strings into
2916 * timeFields. Returns -start (for ParsePosition) if failed.
2917 */
2918int32_t SimpleDateFormat::subParse(const UnicodeString& text, int32_t& start, UChar ch, int32_t count,
2919 UBool obeyCount, UBool allowNegative, UBool ambiguousYear[], int32_t& saveHebrewMonth, Calendar& cal,
2920 int32_t patLoc, MessageFormat * numericLeapMonthFormatter, UTimeZoneFormatTimeType *tzTimeType,
2921 int32_t *dayPeriod) const
2922{
2923 Formattable number;
2924 int32_t value = 0;
2925 int32_t i;
2926 int32_t ps = 0;
2927 UErrorCode status = U_ZERO_ERROR;
2928 ParsePosition pos(0);
2929 UDateFormatField patternCharIndex = DateFormatSymbols::getPatternCharIndex(ch);
2930 const NumberFormat *currentNumberFormat;
2931 UnicodeString temp;
2932 UBool gotNumber = FALSE;
2933
2934#if defined (U_DEBUG_CAL)
2935 //fprintf(stderr, "%s:%d - [%c] st=%d \n", __FILE__, __LINE__, (char) ch, start);
2936#endif
2937
2938 if (patternCharIndex == UDAT_FIELD_COUNT) {
2939 return -start;
2940 }
2941
2942 currentNumberFormat = getNumberFormatByIndex(patternCharIndex);
2943 if (currentNumberFormat == NULL) {
2944 return -start;
2945 }
2946 UCalendarDateFields field = fgPatternIndexToCalendarField[patternCharIndex]; // UCAL_FIELD_COUNT if irrelevant
2947 UnicodeString hebr("hebr", 4, US_INV);
2948
2949 if (numericLeapMonthFormatter != NULL) {
2950 numericLeapMonthFormatter->setFormats((const Format **)&currentNumberFormat, 1);
2951 }
2952 UBool isChineseCalendar = (uprv_strcmp(cal.getType(),"chinese") == 0 || uprv_strcmp(cal.getType(),"dangi") == 0);
2953
2954 // If there are any spaces here, skip over them. If we hit the end
2955 // of the string, then fail.
2956 for (;;) {
2957 if (start >= text.length()) {
2958 return -start;
2959 }
2960 UChar32 c = text.char32At(start);
2961 if (!u_isUWhiteSpace(c) /*||*/ && !PatternProps::isWhiteSpace(c)) {
2962 break;
2963 }
2964 start += U16_LENGTH(c);
2965 }
2966 pos.setIndex(start);
2967
2968 // We handle a few special cases here where we need to parse
2969 // a number value. We handle further, more generic cases below. We need
2970 // to handle some of them here because some fields require extra processing on
2971 // the parsed value.
2972 if (patternCharIndex == UDAT_HOUR_OF_DAY1_FIELD || // k
2973 patternCharIndex == UDAT_HOUR_OF_DAY0_FIELD || // H
2974 patternCharIndex == UDAT_HOUR1_FIELD || // h
2975 patternCharIndex == UDAT_HOUR0_FIELD || // K
2976 (patternCharIndex == UDAT_DOW_LOCAL_FIELD && count <= 2) || // e
2977 (patternCharIndex == UDAT_STANDALONE_DAY_FIELD && count <= 2) || // c
2978 (patternCharIndex == UDAT_MONTH_FIELD && count <= 2) || // M
2979 (patternCharIndex == UDAT_STANDALONE_MONTH_FIELD && count <= 2) || // L
2980 (patternCharIndex == UDAT_QUARTER_FIELD && count <= 2) || // Q
2981 (patternCharIndex == UDAT_STANDALONE_QUARTER_FIELD && count <= 2) || // q
2982 patternCharIndex == UDAT_YEAR_FIELD || // y
2983 patternCharIndex == UDAT_YEAR_WOY_FIELD || // Y
2984 patternCharIndex == UDAT_YEAR_NAME_FIELD || // U (falls back to numeric)
2985 (patternCharIndex == UDAT_ERA_FIELD && isChineseCalendar) || // G
2986 patternCharIndex == UDAT_FRACTIONAL_SECOND_FIELD) // S
2987 {
2988 int32_t parseStart = pos.getIndex();
2989 // It would be good to unify this with the obeyCount logic below,
2990 // but that's going to be difficult.
2991 const UnicodeString* src;
2992
2993 UBool parsedNumericLeapMonth = FALSE;
2994 if (numericLeapMonthFormatter != NULL && (patternCharIndex == UDAT_MONTH_FIELD || patternCharIndex == UDAT_STANDALONE_MONTH_FIELD)) {
2995 int32_t argCount;
2996 Formattable * args = numericLeapMonthFormatter->parse(text, pos, argCount);
2997 if (args != NULL && argCount == 1 && pos.getIndex() > parseStart && args[0].isNumeric()) {
2998 parsedNumericLeapMonth = TRUE;
2999 number.setLong(args[0].getLong());
3000 cal.set(UCAL_IS_LEAP_MONTH, 1);
3001 delete[] args;
3002 } else {
3003 pos.setIndex(parseStart);
3004 cal.set(UCAL_IS_LEAP_MONTH, 0);
3005 }
3006 }
3007
3008 if (!parsedNumericLeapMonth) {
3009 if (obeyCount) {
3010 if ((start+count) > text.length()) {
3011 return -start;
3012 }
3013
3014 text.extractBetween(0, start + count, temp);
3015 src = &temp;
3016 } else {
3017 src = &text;
3018 }
3019
3020 parseInt(*src, number, pos, allowNegative,currentNumberFormat);
3021 }
3022
3023 int32_t txtLoc = pos.getIndex();
3024
3025 if (txtLoc > parseStart) {
3026 value = number.getLong();
3027 gotNumber = TRUE;
3028
3029 // suffix processing
3030 if (value < 0 ) {
3031 txtLoc = checkIntSuffix(text, txtLoc, patLoc+1, TRUE);
3032 if (txtLoc != pos.getIndex()) {
3033 value *= -1;
3034 }
3035 }
3036 else {
3037 txtLoc = checkIntSuffix(text, txtLoc, patLoc+1, FALSE);
3038 }
3039
3040 if (!getBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE, status)) {
3041 // Check the range of the value
3042 int32_t bias = gFieldRangeBias[patternCharIndex];
3043 if (bias >= 0 && (value > cal.getMaximum(field) + bias || value < cal.getMinimum(field) + bias)) {
3044 return -start;
3045 }
3046 }
3047
3048 pos.setIndex(txtLoc);
3049 }
3050 }
3051
3052 // Make sure that we got a number if
3053 // we want one, and didn't get one
3054 // if we don't want one.
3055 switch (patternCharIndex) {
3056 case UDAT_HOUR_OF_DAY1_FIELD:
3057 case UDAT_HOUR_OF_DAY0_FIELD:
3058 case UDAT_HOUR1_FIELD:
3059 case UDAT_HOUR0_FIELD:
3060 // special range check for hours:
3061 if (value < 0 || value > 24) {
3062 return -start;
3063 }
3064
3065 // fall through to gotNumber check
3066 U_FALLTHROUGH;
3067 case UDAT_YEAR_FIELD:
3068 case UDAT_YEAR_WOY_FIELD:
3069 case UDAT_FRACTIONAL_SECOND_FIELD:
3070 // these must be a number
3071 if (! gotNumber) {
3072 return -start;
3073 }
3074
3075 break;
3076
3077 default:
3078 // we check the rest of the fields below.
3079 break;
3080 }
3081
3082 switch (patternCharIndex) {
3083 case UDAT_ERA_FIELD:
3084 if (isChineseCalendar) {
3085 if (!gotNumber) {
3086 return -start;
3087 }
3088 cal.set(UCAL_ERA, value);
3089 return pos.getIndex();
3090 }
3091 if (count == 5) {
3092 ps = matchString(text, start, UCAL_ERA, fSymbols->fNarrowEras, fSymbols->fNarrowErasCount, NULL, cal);
3093 } else if (count == 4) {
3094 ps = matchString(text, start, UCAL_ERA, fSymbols->fEraNames, fSymbols->fEraNamesCount, NULL, cal);
3095 } else {
3096 ps = matchString(text, start, UCAL_ERA, fSymbols->fEras, fSymbols->fErasCount, NULL, cal);
3097 }
3098
3099 // check return position, if it equals -start, then matchString error
3100 // special case the return code so we don't necessarily fail out until we
3101 // verify no year information also
3102 if (ps == -start)
3103 ps--;
3104
3105 return ps;
3106
3107 case UDAT_YEAR_FIELD:
3108 // If there are 3 or more YEAR pattern characters, this indicates
3109 // that the year value is to be treated literally, without any
3110 // two-digit year adjustments (e.g., from "01" to 2001). Otherwise
3111 // we made adjustments to place the 2-digit year in the proper
3112 // century, for parsed strings from "00" to "99". Any other string
3113 // is treated literally: "2250", "-1", "1", "002".
3114 if (fDateOverride.compare(hebr)==0 && value < 1000) {
3115 value += HEBREW_CAL_CUR_MILLENIUM_START_YEAR;
3116 } else if (text.moveIndex32(start, 2) == pos.getIndex() && !isChineseCalendar
3117 && u_isdigit(text.char32At(start))
3118 && u_isdigit(text.char32At(text.moveIndex32(start, 1))))
3119 {
3120 // only adjust year for patterns less than 3.
3121 if(count < 3) {
3122 // Assume for example that the defaultCenturyStart is 6/18/1903.
3123 // This means that two-digit years will be forced into the range
3124 // 6/18/1903 to 6/17/2003. As a result, years 00, 01, and 02
3125 // correspond to 2000, 2001, and 2002. Years 04, 05, etc. correspond
3126 // to 1904, 1905, etc. If the year is 03, then it is 2003 if the
3127 // other fields specify a date before 6/18, or 1903 if they specify a
3128 // date afterwards. As a result, 03 is an ambiguous year. All other
3129 // two-digit years are unambiguous.
3130 if(fHaveDefaultCentury) { // check if this formatter even has a pivot year
3131 int32_t ambiguousTwoDigitYear = fDefaultCenturyStartYear % 100;
3132 ambiguousYear[0] = (value == ambiguousTwoDigitYear);
3133 value += (fDefaultCenturyStartYear/100)*100 +
3134 (value < ambiguousTwoDigitYear ? 100 : 0);
3135 }
3136 }
3137 }
3138 cal.set(UCAL_YEAR, value);
3139
3140 // Delayed checking for adjustment of Hebrew month numbers in non-leap years.
3141 if (saveHebrewMonth >= 0) {
3142 HebrewCalendar *hc = (HebrewCalendar*)&cal;
3143 if (!hc->isLeapYear(value) && saveHebrewMonth >= 6) {
3144 cal.set(UCAL_MONTH,saveHebrewMonth);
3145 } else {
3146 cal.set(UCAL_MONTH,saveHebrewMonth-1);
3147 }
3148 saveHebrewMonth = -1;
3149 }
3150 return pos.getIndex();
3151
3152 case UDAT_YEAR_WOY_FIELD:
3153 // Comment is the same as for UDAT_Year_FIELDs - look above
3154 if (fDateOverride.compare(hebr)==0 && value < 1000) {
3155 value += HEBREW_CAL_CUR_MILLENIUM_START_YEAR;
3156 } else if (text.moveIndex32(start, 2) == pos.getIndex()
3157 && u_isdigit(text.char32At(start))
3158 && u_isdigit(text.char32At(text.moveIndex32(start, 1)))
3159 && fHaveDefaultCentury )
3160 {
3161 int32_t ambiguousTwoDigitYear = fDefaultCenturyStartYear % 100;
3162 ambiguousYear[0] = (value == ambiguousTwoDigitYear);
3163 value += (fDefaultCenturyStartYear/100)*100 +
3164 (value < ambiguousTwoDigitYear ? 100 : 0);
3165 }
3166 cal.set(UCAL_YEAR_WOY, value);
3167 return pos.getIndex();
3168
3169 case UDAT_YEAR_NAME_FIELD:
3170 if (fSymbols->fShortYearNames != NULL) {
3171 int32_t newStart = matchString(text, start, UCAL_YEAR, fSymbols->fShortYearNames, fSymbols->fShortYearNamesCount, NULL, cal);
3172 if (newStart > 0) {
3173 return newStart;
3174 }
3175 }
3176 if (gotNumber && (getBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC,status) || value > fSymbols->fShortYearNamesCount)) {
3177 cal.set(UCAL_YEAR, value);
3178 return pos.getIndex();
3179 }
3180 return -start;
3181
3182 case UDAT_MONTH_FIELD:
3183 case UDAT_STANDALONE_MONTH_FIELD:
3184 if (gotNumber) // i.e., M or MM.
3185 {
3186 // When parsing month numbers from the Hebrew Calendar, we might need to adjust the month depending on whether
3187 // or not it was a leap year. We may or may not yet know what year it is, so might have to delay checking until
3188 // the year is parsed.
3189 if (!strcmp(cal.getType(),"hebrew")) {
3190 HebrewCalendar *hc = (HebrewCalendar*)&cal;
3191 if (cal.isSet(UCAL_YEAR)) {
3192 UErrorCode monthStatus = U_ZERO_ERROR;
3193 if (!hc->isLeapYear(hc->get(UCAL_YEAR, monthStatus)) && value >= 6) {
3194 cal.set(UCAL_MONTH, value);
3195 } else {
3196 cal.set(UCAL_MONTH, value - 1);
3197 }
3198 } else {
3199 saveHebrewMonth = value;
3200 }
3201 } else {
3202 // Don't want to parse the month if it is a string
3203 // while pattern uses numeric style: M/MM, L/LL
3204 // [We computed 'value' above.]
3205 cal.set(UCAL_MONTH, value - 1);
3206 }
3207 return pos.getIndex();
3208 } else {
3209 // count >= 3 // i.e., MMM/MMMM, LLL/LLLL
3210 // Want to be able to parse both short and long forms.
3211 // Try count == 4 first:
3212 UnicodeString * wideMonthPat = NULL;
3213 UnicodeString * shortMonthPat = NULL;
3214 if (fSymbols->fLeapMonthPatterns != NULL && fSymbols->fLeapMonthPatternsCount >= DateFormatSymbols::kMonthPatternsCount) {
3215 if (patternCharIndex==UDAT_MONTH_FIELD) {
3216 wideMonthPat = &fSymbols->fLeapMonthPatterns[DateFormatSymbols::kLeapMonthPatternFormatWide];
3217 shortMonthPat = &fSymbols->fLeapMonthPatterns[DateFormatSymbols::kLeapMonthPatternFormatAbbrev];
3218 } else {
3219 wideMonthPat = &fSymbols->fLeapMonthPatterns[DateFormatSymbols::kLeapMonthPatternStandaloneWide];
3220 shortMonthPat = &fSymbols->fLeapMonthPatterns[DateFormatSymbols::kLeapMonthPatternStandaloneAbbrev];
3221 }
3222 }
3223 int32_t newStart = 0;
3224 if (patternCharIndex==UDAT_MONTH_FIELD) {
3225 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count == 4) {
3226 newStart = matchString(text, start, UCAL_MONTH, fSymbols->fMonths, fSymbols->fMonthsCount, wideMonthPat, cal); // try MMMM
3227 if (newStart > 0) {
3228 return newStart;
3229 }
3230 }
3231 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count == 3) {
3232 newStart = matchString(text, start, UCAL_MONTH, fSymbols->fShortMonths, fSymbols->fShortMonthsCount, shortMonthPat, cal); // try MMM
3233 }
3234 } else {
3235 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count == 4) {
3236 newStart = matchString(text, start, UCAL_MONTH, fSymbols->fStandaloneMonths, fSymbols->fStandaloneMonthsCount, wideMonthPat, cal); // try LLLL
3237 if (newStart > 0) {
3238 return newStart;
3239 }
3240 }
3241 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count == 3) {
3242 newStart = matchString(text, start, UCAL_MONTH, fSymbols->fStandaloneShortMonths, fSymbols->fStandaloneShortMonthsCount, shortMonthPat, cal); // try LLL
3243 }
3244 }
3245 if (newStart > 0 || !getBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, status)) // currently we do not try to parse MMMMM/LLLLL: #8860
3246 return newStart;
3247 // else we allowing parsing as number, below
3248 }
3249 break;
3250
3251 case UDAT_HOUR_OF_DAY1_FIELD:
3252 // [We computed 'value' above.]
3253 if (value == cal.getMaximum(UCAL_HOUR_OF_DAY) + 1)
3254 value = 0;
3255
3256 // fall through to set field
3257 U_FALLTHROUGH;
3258 case UDAT_HOUR_OF_DAY0_FIELD:
3259 cal.set(UCAL_HOUR_OF_DAY, value);
3260 return pos.getIndex();
3261
3262 case UDAT_FRACTIONAL_SECOND_FIELD:
3263 // Fractional seconds left-justify
3264 i = countDigits(text, start, pos.getIndex());
3265 if (i < 3) {
3266 while (i < 3) {
3267 value *= 10;
3268 i++;
3269 }
3270 } else {
3271 int32_t a = 1;
3272 while (i > 3) {
3273 a *= 10;
3274 i--;
3275 }
3276 value /= a;
3277 }
3278 cal.set(UCAL_MILLISECOND, value);
3279 return pos.getIndex();
3280
3281 case UDAT_DOW_LOCAL_FIELD:
3282 if (gotNumber) // i.e., e or ee
3283 {
3284 // [We computed 'value' above.]
3285 cal.set(UCAL_DOW_LOCAL, value);
3286 return pos.getIndex();
3287 }
3288 // else for eee-eeeee fall through to handling of EEE-EEEEE
3289 // fall through, do not break here
3290 U_FALLTHROUGH;
3291 case UDAT_DAY_OF_WEEK_FIELD:
3292 {
3293 // Want to be able to parse both short and long forms.
3294 // Try count == 4 (EEEE) wide first:
3295 int32_t newStart = 0;
3296 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count == 4) {
3297 if ((newStart = matchString(text, start, UCAL_DAY_OF_WEEK,
3298 fSymbols->fWeekdays, fSymbols->fWeekdaysCount, NULL, cal)) > 0)
3299 return newStart;
3300 }
3301 // EEEE wide failed, now try EEE abbreviated
3302 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count == 3) {
3303 if ((newStart = matchString(text, start, UCAL_DAY_OF_WEEK,
3304 fSymbols->fShortWeekdays, fSymbols->fShortWeekdaysCount, NULL, cal)) > 0)
3305 return newStart;
3306 }
3307 // EEE abbreviated failed, now try EEEEEE short
3308 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count == 6) {
3309 if ((newStart = matchString(text, start, UCAL_DAY_OF_WEEK,
3310 fSymbols->fShorterWeekdays, fSymbols->fShorterWeekdaysCount, NULL, cal)) > 0)
3311 return newStart;
3312 }
3313 // EEEEEE short failed, now try EEEEE narrow
3314 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count == 5) {
3315 if ((newStart = matchString(text, start, UCAL_DAY_OF_WEEK,
3316 fSymbols->fNarrowWeekdays, fSymbols->fNarrowWeekdaysCount, NULL, cal)) > 0)
3317 return newStart;
3318 }
3319 if (!getBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, status) || patternCharIndex == UDAT_DAY_OF_WEEK_FIELD)
3320 return newStart;
3321 // else we allowing parsing as number, below
3322 }
3323 break;
3324
3325 case UDAT_STANDALONE_DAY_FIELD:
3326 {
3327 if (gotNumber) // c or cc
3328 {
3329 // [We computed 'value' above.]
3330 cal.set(UCAL_DOW_LOCAL, value);
3331 return pos.getIndex();
3332 }
3333 // Want to be able to parse both short and long forms.
3334 // Try count == 4 (cccc) first:
3335 int32_t newStart = 0;
3336 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count == 4) {
3337 if ((newStart = matchString(text, start, UCAL_DAY_OF_WEEK,
3338 fSymbols->fStandaloneWeekdays, fSymbols->fStandaloneWeekdaysCount, NULL, cal)) > 0)
3339 return newStart;
3340 }
3341 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count == 3) {
3342 if ((newStart = matchString(text, start, UCAL_DAY_OF_WEEK,
3343 fSymbols->fStandaloneShortWeekdays, fSymbols->fStandaloneShortWeekdaysCount, NULL, cal)) > 0)
3344 return newStart;
3345 }
3346 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count == 6) {
3347 if ((newStart = matchString(text, start, UCAL_DAY_OF_WEEK,
3348 fSymbols->fStandaloneShorterWeekdays, fSymbols->fStandaloneShorterWeekdaysCount, NULL, cal)) > 0)
3349 return newStart;
3350 }
3351 if (!getBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, status))
3352 return newStart;
3353 // else we allowing parsing as number, below
3354 }
3355 break;
3356
3357 case UDAT_AM_PM_FIELD:
3358 {
3359 // optionally try both wide/abbrev and narrow forms
3360 int32_t newStart = 0;
3361 // try wide/abbrev
3362 if( getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count < 5 ) {
3363 if ((newStart = matchString(text, start, UCAL_AM_PM, fSymbols->fAmPms, fSymbols->fAmPmsCount, NULL, cal)) > 0) {
3364 return newStart;
3365 }
3366 }
3367 // try narrow
3368 if( getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count >= 5 ) {
3369 if ((newStart = matchString(text, start, UCAL_AM_PM, fSymbols->fNarrowAmPms, fSymbols->fNarrowAmPmsCount, NULL, cal)) > 0) {
3370 return newStart;
3371 }
3372 }
3373 // no matches for given options
3374 return -start;
3375 }
3376
3377 case UDAT_HOUR1_FIELD:
3378 // [We computed 'value' above.]
3379 if (value == cal.getLeastMaximum(UCAL_HOUR)+1)
3380 value = 0;
3381
3382 // fall through to set field
3383 U_FALLTHROUGH;
3384 case UDAT_HOUR0_FIELD:
3385 cal.set(UCAL_HOUR, value);
3386 return pos.getIndex();
3387
3388 case UDAT_QUARTER_FIELD:
3389 if (gotNumber) // i.e., Q or QQ.
3390 {
3391 // Don't want to parse the month if it is a string
3392 // while pattern uses numeric style: Q or QQ.
3393 // [We computed 'value' above.]
3394 cal.set(UCAL_MONTH, (value - 1) * 3);
3395 return pos.getIndex();
3396 } else {
3397 // count >= 3 // i.e., QQQ or QQQQ
3398 // Want to be able to parse both short and long forms.
3399 // Try count == 4 first:
3400 int32_t newStart = 0;
3401
3402 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count == 4) {
3403 if ((newStart = matchQuarterString(text, start, UCAL_MONTH,
3404 fSymbols->fQuarters, fSymbols->fQuartersCount, cal)) > 0)
3405 return newStart;
3406 }
3407 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count == 3) {
3408 if ((newStart = matchQuarterString(text, start, UCAL_MONTH,
3409 fSymbols->fShortQuarters, fSymbols->fShortQuartersCount, cal)) > 0)
3410 return newStart;
3411 }
3412 if (!getBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, status))
3413 return newStart;
3414 // else we allowing parsing as number, below
3415 if(!getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status))
3416 return -start;
3417 }
3418 break;
3419
3420 case UDAT_STANDALONE_QUARTER_FIELD:
3421 if (gotNumber) // i.e., q or qq.
3422 {
3423 // Don't want to parse the month if it is a string
3424 // while pattern uses numeric style: q or q.
3425 // [We computed 'value' above.]
3426 cal.set(UCAL_MONTH, (value - 1) * 3);
3427 return pos.getIndex();
3428 } else {
3429 // count >= 3 // i.e., qqq or qqqq
3430 // Want to be able to parse both short and long forms.
3431 // Try count == 4 first:
3432 int32_t newStart = 0;
3433
3434 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count == 4) {
3435 if ((newStart = matchQuarterString(text, start, UCAL_MONTH,
3436 fSymbols->fStandaloneQuarters, fSymbols->fStandaloneQuartersCount, cal)) > 0)
3437 return newStart;
3438 }
3439 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count == 3) {
3440 if ((newStart = matchQuarterString(text, start, UCAL_MONTH,
3441 fSymbols->fStandaloneShortQuarters, fSymbols->fStandaloneShortQuartersCount, cal)) > 0)
3442 return newStart;
3443 }
3444 if (!getBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, status))
3445 return newStart;
3446 // else we allowing parsing as number, below
3447 if(!getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status))
3448 return -start;
3449 }
3450 break;
3451
3452 case UDAT_TIMEZONE_FIELD: // 'z'
3453 {
3454 UTimeZoneFormatStyle style = (count < 4) ? UTZFMT_STYLE_SPECIFIC_SHORT : UTZFMT_STYLE_SPECIFIC_LONG;
3455 const TimeZoneFormat *tzfmt = tzFormat(status);
3456 if (U_SUCCESS(status)) {
3457 TimeZone *tz = tzfmt->parse(style, text, pos, tzTimeType);
3458 if (tz != NULL) {
3459 cal.adoptTimeZone(tz);
3460 return pos.getIndex();
3461 }
3462 }
3463 return -start;
3464 }
3465 break;
3466 case UDAT_TIMEZONE_RFC_FIELD: // 'Z'
3467 {
3468 UTimeZoneFormatStyle style = (count < 4) ?
3469 UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL : ((count == 5) ? UTZFMT_STYLE_ISO_EXTENDED_FULL: UTZFMT_STYLE_LOCALIZED_GMT);
3470 const TimeZoneFormat *tzfmt = tzFormat(status);
3471 if (U_SUCCESS(status)) {
3472 TimeZone *tz = tzfmt->parse(style, text, pos, tzTimeType);
3473 if (tz != NULL) {
3474 cal.adoptTimeZone(tz);
3475 return pos.getIndex();
3476 }
3477 }
3478 return -start;
3479 }
3480 case UDAT_TIMEZONE_GENERIC_FIELD: // 'v'
3481 {
3482 UTimeZoneFormatStyle style = (count < 4) ? UTZFMT_STYLE_GENERIC_SHORT : UTZFMT_STYLE_GENERIC_LONG;
3483 const TimeZoneFormat *tzfmt = tzFormat(status);
3484 if (U_SUCCESS(status)) {
3485 TimeZone *tz = tzfmt->parse(style, text, pos, tzTimeType);
3486 if (tz != NULL) {
3487 cal.adoptTimeZone(tz);
3488 return pos.getIndex();
3489 }
3490 }
3491 return -start;
3492 }
3493 case UDAT_TIMEZONE_SPECIAL_FIELD: // 'V'
3494 {
3495 UTimeZoneFormatStyle style;
3496 switch (count) {
3497 case 1:
3498 style = UTZFMT_STYLE_ZONE_ID_SHORT;
3499 break;
3500 case 2:
3501 style = UTZFMT_STYLE_ZONE_ID;
3502 break;
3503 case 3:
3504 style = UTZFMT_STYLE_EXEMPLAR_LOCATION;
3505 break;
3506 default:
3507 style = UTZFMT_STYLE_GENERIC_LOCATION;
3508 break;
3509 }
3510 const TimeZoneFormat *tzfmt = tzFormat(status);
3511 if (U_SUCCESS(status)) {
3512 TimeZone *tz = tzfmt->parse(style, text, pos, tzTimeType);
3513 if (tz != NULL) {
3514 cal.adoptTimeZone(tz);
3515 return pos.getIndex();
3516 }
3517 }
3518 return -start;
3519 }
3520 case UDAT_TIMEZONE_LOCALIZED_GMT_OFFSET_FIELD: // 'O'
3521 {
3522 UTimeZoneFormatStyle style = (count < 4) ? UTZFMT_STYLE_LOCALIZED_GMT_SHORT : UTZFMT_STYLE_LOCALIZED_GMT;
3523 const TimeZoneFormat *tzfmt = tzFormat(status);
3524 if (U_SUCCESS(status)) {
3525 TimeZone *tz = tzfmt->parse(style, text, pos, tzTimeType);
3526 if (tz != NULL) {
3527 cal.adoptTimeZone(tz);
3528 return pos.getIndex();
3529 }
3530 }
3531 return -start;
3532 }
3533 case UDAT_TIMEZONE_ISO_FIELD: // 'X'
3534 {
3535 UTimeZoneFormatStyle style;
3536 switch (count) {
3537 case 1:
3538 style = UTZFMT_STYLE_ISO_BASIC_SHORT;
3539 break;
3540 case 2:
3541 style = UTZFMT_STYLE_ISO_BASIC_FIXED;
3542 break;
3543 case 3:
3544 style = UTZFMT_STYLE_ISO_EXTENDED_FIXED;
3545 break;
3546 case 4:
3547 style = UTZFMT_STYLE_ISO_BASIC_FULL;
3548 break;
3549 default:
3550 style = UTZFMT_STYLE_ISO_EXTENDED_FULL;
3551 break;
3552 }
3553 const TimeZoneFormat *tzfmt = tzFormat(status);
3554 if (U_SUCCESS(status)) {
3555 TimeZone *tz = tzfmt->parse(style, text, pos, tzTimeType);
3556 if (tz != NULL) {
3557 cal.adoptTimeZone(tz);
3558 return pos.getIndex();
3559 }
3560 }
3561 return -start;
3562 }
3563 case UDAT_TIMEZONE_ISO_LOCAL_FIELD: // 'x'
3564 {
3565 UTimeZoneFormatStyle style;
3566 switch (count) {
3567 case 1:
3568 style = UTZFMT_STYLE_ISO_BASIC_LOCAL_SHORT;
3569 break;
3570 case 2:
3571 style = UTZFMT_STYLE_ISO_BASIC_LOCAL_FIXED;
3572 break;
3573 case 3:
3574 style = UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FIXED;
3575 break;
3576 case 4:
3577 style = UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL;
3578 break;
3579 default:
3580 style = UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FULL;
3581 break;
3582 }
3583 const TimeZoneFormat *tzfmt = tzFormat(status);
3584 if (U_SUCCESS(status)) {
3585 TimeZone *tz = tzfmt->parse(style, text, pos, tzTimeType);
3586 if (tz != NULL) {
3587 cal.adoptTimeZone(tz);
3588 return pos.getIndex();
3589 }
3590 }
3591 return -start;
3592 }
3593 // currently no pattern character is defined for UDAT_TIME_SEPARATOR_FIELD
3594 // so we should not get here. Leave support in for future definition.
3595 case UDAT_TIME_SEPARATOR_FIELD:
3596 {
3597 static const UChar def_sep = DateFormatSymbols::DEFAULT_TIME_SEPARATOR;
3598 static const UChar alt_sep = DateFormatSymbols::ALTERNATE_TIME_SEPARATOR;
3599
3600 // Try matching a time separator.
3601 int32_t count_sep = 1;
3602 UnicodeString data[3];
3603 fSymbols->getTimeSeparatorString(data[0]);
3604
3605 // Add the default, if different from the locale.
3606 if (data[0].compare(&def_sep, 1) != 0) {
3607 data[count_sep++].setTo(def_sep);
3608 }
3609
3610 // If lenient, add also the alternate, if different from the locale.
3611 if (isLenient() && data[0].compare(&alt_sep, 1) != 0) {
3612 data[count_sep++].setTo(alt_sep);
3613 }
3614
3615 return matchString(text, start, UCAL_FIELD_COUNT /* => nothing to set */, data, count_sep, NULL, cal);
3616 }
3617
3618 case UDAT_AM_PM_MIDNIGHT_NOON_FIELD:
3619 {
3620 U_ASSERT(dayPeriod != NULL);
3621 int32_t ampmStart = subParse(text, start, 0x61, count,
3622 obeyCount, allowNegative, ambiguousYear, saveHebrewMonth, cal,
3623 patLoc, numericLeapMonthFormatter, tzTimeType);
3624
3625 if (ampmStart > 0) {
3626 return ampmStart;
3627 } else {
3628 int32_t newStart = 0;
3629
3630 // Only match the first two strings from the day period strings array.
3631 if (getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count == 3) {
3632 if ((newStart = matchDayPeriodStrings(text, start, fSymbols->fAbbreviatedDayPeriods,
3633 2, *dayPeriod)) > 0) {
3634 return newStart;
3635 }
3636 }
3637 if (getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count == 5) {
3638 if ((newStart = matchDayPeriodStrings(text, start, fSymbols->fNarrowDayPeriods,
3639 2, *dayPeriod)) > 0) {
3640 return newStart;
3641 }
3642 }
3643 // count == 4, but allow other counts
3644 if (getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status)) {
3645 if ((newStart = matchDayPeriodStrings(text, start, fSymbols->fWideDayPeriods,
3646 2, *dayPeriod)) > 0) {
3647 return newStart;
3648 }
3649 }
3650
3651 return -start;
3652 }
3653 }
3654
3655 case UDAT_FLEXIBLE_DAY_PERIOD_FIELD:
3656 {
3657 U_ASSERT(dayPeriod != NULL);
3658 int32_t newStart = 0;
3659
3660 if (getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count == 3) {
3661 if ((newStart = matchDayPeriodStrings(text, start, fSymbols->fAbbreviatedDayPeriods,
3662 fSymbols->fAbbreviatedDayPeriodsCount, *dayPeriod)) > 0) {
3663 return newStart;
3664 }
3665 }
3666 if (getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count == 5) {
3667 if ((newStart = matchDayPeriodStrings(text, start, fSymbols->fNarrowDayPeriods,
3668 fSymbols->fNarrowDayPeriodsCount, *dayPeriod)) > 0) {
3669 return newStart;
3670 }
3671 }
3672 if (getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH, status) || count == 4) {
3673 if ((newStart = matchDayPeriodStrings(text, start, fSymbols->fWideDayPeriods,
3674 fSymbols->fWideDayPeriodsCount, *dayPeriod)) > 0) {
3675 return newStart;
3676 }
3677 }
3678
3679 return -start;
3680 }
3681
3682 default:
3683 // Handle "generic" fields
3684 // this is now handled below, outside the switch block
3685 break;
3686 }
3687 // Handle "generic" fields:
3688 // switch default case now handled here (outside switch block) to allow
3689 // parsing of some string fields as digits for lenient case
3690
3691 int32_t parseStart = pos.getIndex();
3692 const UnicodeString* src;
3693 if (obeyCount) {
3694 if ((start+count) > text.length()) {
3695 return -start;
3696 }
3697 text.extractBetween(0, start + count, temp);
3698 src = &temp;
3699 } else {
3700 src = &text;
3701 }
3702 parseInt(*src, number, pos, allowNegative,currentNumberFormat);
3703 if (pos.getIndex() != parseStart) {
3704 int32_t val = number.getLong();
3705
3706 // Don't need suffix processing here (as in number processing at the beginning of the function);
3707 // the new fields being handled as numeric values (month, weekdays, quarters) should not have suffixes.
3708
3709 if (!getBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, status)) {
3710 // Check the range of the value
3711 int32_t bias = gFieldRangeBias[patternCharIndex];
3712 if (bias >= 0 && (val > cal.getMaximum(field) + bias || val < cal.getMinimum(field) + bias)) {
3713 return -start;
3714 }
3715 }
3716
3717 // For the following, need to repeat some of the "if (gotNumber)" code above:
3718 // UDAT_[STANDALONE_]MONTH_FIELD, UDAT_DOW_LOCAL_FIELD, UDAT_STANDALONE_DAY_FIELD,
3719 // UDAT_[STANDALONE_]QUARTER_FIELD
3720 switch (patternCharIndex) {
3721 case UDAT_MONTH_FIELD:
3722 // See notes under UDAT_MONTH_FIELD case above
3723 if (!strcmp(cal.getType(),"hebrew")) {
3724 HebrewCalendar *hc = (HebrewCalendar*)&cal;
3725 if (cal.isSet(UCAL_YEAR)) {
3726 UErrorCode monthStatus = U_ZERO_ERROR;
3727 if (!hc->isLeapYear(hc->get(UCAL_YEAR, monthStatus)) && val >= 6) {
3728 cal.set(UCAL_MONTH, val);
3729 } else {
3730 cal.set(UCAL_MONTH, val - 1);
3731 }
3732 } else {
3733 saveHebrewMonth = val;
3734 }
3735 } else {
3736 cal.set(UCAL_MONTH, val - 1);
3737 }
3738 break;
3739 case UDAT_STANDALONE_MONTH_FIELD:
3740 cal.set(UCAL_MONTH, val - 1);
3741 break;
3742 case UDAT_DOW_LOCAL_FIELD:
3743 case UDAT_STANDALONE_DAY_FIELD:
3744 cal.set(UCAL_DOW_LOCAL, val);
3745 break;
3746 case UDAT_QUARTER_FIELD:
3747 case UDAT_STANDALONE_QUARTER_FIELD:
3748 cal.set(UCAL_MONTH, (val - 1) * 3);
3749 break;
3750 case UDAT_RELATED_YEAR_FIELD:
3751 cal.setRelatedYear(val);
3752 break;
3753 default:
3754 cal.set(field, val);
3755 break;
3756 }
3757 return pos.getIndex();
3758 }
3759 return -start;
3760}
3761
3762/**
3763 * Parse an integer using fNumberFormat. This method is semantically
3764 * const, but actually may modify fNumberFormat.
3765 */
3766void SimpleDateFormat::parseInt(const UnicodeString& text,
3767 Formattable& number,
3768 ParsePosition& pos,
3769 UBool allowNegative,
3770 const NumberFormat *fmt) const {
3771 parseInt(text, number, -1, pos, allowNegative,fmt);
3772}
3773
3774/**
3775 * Parse an integer using fNumberFormat up to maxDigits.
3776 */
3777void SimpleDateFormat::parseInt(const UnicodeString& text,
3778 Formattable& number,
3779 int32_t maxDigits,
3780 ParsePosition& pos,
3781 UBool allowNegative,
3782 const NumberFormat *fmt) const {
3783 UnicodeString oldPrefix;
3784 auto* fmtAsDF = dynamic_cast<const DecimalFormat*>(fmt);
3785 LocalPointer<DecimalFormat> df;
3786 if (!allowNegative && fmtAsDF != nullptr) {
3787 df.adoptInstead(fmtAsDF->clone());
3788 if (df.isNull()) {
3789 // Memory allocation error
3790 return;
3791 }
3792 df->setNegativePrefix(UnicodeString(TRUE, SUPPRESS_NEGATIVE_PREFIX, -1));
3793 fmt = df.getAlias();
3794 }
3795 int32_t oldPos = pos.getIndex();
3796 fmt->parse(text, number, pos);
3797
3798 if (maxDigits > 0) {
3799 // adjust the result to fit into
3800 // the maxDigits and move the position back
3801 int32_t nDigits = pos.getIndex() - oldPos;
3802 if (nDigits > maxDigits) {
3803 int32_t val = number.getLong();
3804 nDigits -= maxDigits;
3805 while (nDigits > 0) {
3806 val /= 10;
3807 nDigits--;
3808 }
3809 pos.setIndex(oldPos + maxDigits);
3810 number.setLong(val);
3811 }
3812 }
3813}
3814
3815int32_t SimpleDateFormat::countDigits(const UnicodeString& text, int32_t start, int32_t end) const {
3816 int32_t numDigits = 0;
3817 int32_t idx = start;
3818 while (idx < end) {
3819 UChar32 cp = text.char32At(idx);
3820 if (u_isdigit(cp)) {
3821 numDigits++;
3822 }
3823 idx += U16_LENGTH(cp);
3824 }
3825 return numDigits;
3826}
3827
3828//----------------------------------------------------------------------
3829
3830void SimpleDateFormat::translatePattern(const UnicodeString& originalPattern,
3831 UnicodeString& translatedPattern,
3832 const UnicodeString& from,
3833 const UnicodeString& to,
3834 UErrorCode& status)
3835{
3836 // run through the pattern and convert any pattern symbols from the version
3837 // in "from" to the corresponding character in "to". This code takes
3838 // quoted strings into account (it doesn't try to translate them), and it signals
3839 // an error if a particular "pattern character" doesn't appear in "from".
3840 // Depending on the values of "from" and "to" this can convert from generic
3841 // to localized patterns or localized to generic.
3842 if (U_FAILURE(status)) {
3843 return;
3844 }
3845
3846 translatedPattern.remove();
3847 UBool inQuote = FALSE;
3848 for (int32_t i = 0; i < originalPattern.length(); ++i) {
3849 UChar c = originalPattern[i];
3850 if (inQuote) {
3851 if (c == QUOTE) {
3852 inQuote = FALSE;
3853 }
3854 } else {
3855 if (c == QUOTE) {
3856 inQuote = TRUE;
3857 } else if (isSyntaxChar(c)) {
3858 int32_t ci = from.indexOf(c);
3859 if (ci == -1) {
3860 status = U_INVALID_FORMAT_ERROR;
3861 return;
3862 }
3863 c = to[ci];
3864 }
3865 }
3866 translatedPattern += c;
3867 }
3868 if (inQuote) {
3869 status = U_INVALID_FORMAT_ERROR;
3870 return;
3871 }
3872}
3873
3874//----------------------------------------------------------------------
3875
3876UnicodeString&
3877SimpleDateFormat::toPattern(UnicodeString& result) const
3878{
3879 result = fPattern;
3880 return result;
3881}
3882
3883//----------------------------------------------------------------------
3884
3885UnicodeString&
3886SimpleDateFormat::toLocalizedPattern(UnicodeString& result,
3887 UErrorCode& status) const
3888{
3889 translatePattern(fPattern, result,
3890 UnicodeString(DateFormatSymbols::getPatternUChars()),
3891 fSymbols->fLocalPatternChars, status);
3892 return result;
3893}
3894
3895//----------------------------------------------------------------------
3896
3897void
3898SimpleDateFormat::applyPattern(const UnicodeString& pattern)
3899{
3900 fPattern = pattern;
3901 parsePattern();
3902
3903 // Hack to update use of Gannen year numbering for ja@calendar=japanese -
3904 // use only if format is non-numeric (includes 年) and no other fDateOverride.
3905 if (fCalendar != nullptr && uprv_strcmp(fCalendar->getType(),"japanese") == 0 &&
3906 uprv_strcmp(fLocale.getLanguage(),"ja") == 0) {
3907 if (fDateOverride==UnicodeString(u"y=jpanyear") && !fHasHanYearChar) {
3908 // Gannen numbering is set but new pattern should not use it, unset;
3909 // use procedure from adoptNumberFormat to clear overrides
3910 if (fSharedNumberFormatters) {
3911 freeSharedNumberFormatters(fSharedNumberFormatters);
3912 fSharedNumberFormatters = NULL;
3913 }
3914 fDateOverride.setToBogus(); // record status
3915 } else if (fDateOverride.isBogus() && fHasHanYearChar) {
3916 // No current override (=> no Gannen numbering) but new pattern needs it;
3917 // use procedures from initNUmberFormatters / adoptNumberFormat
3918 umtx_lock(&LOCK);
3919 if (fSharedNumberFormatters == NULL) {
3920 fSharedNumberFormatters = allocSharedNumberFormatters();
3921 }
3922 umtx_unlock(&LOCK);
3923 if (fSharedNumberFormatters != NULL) {
3924 Locale ovrLoc(fLocale.getLanguage(),fLocale.getCountry(),fLocale.getVariant(),"numbers=jpanyear");
3925 UErrorCode status = U_ZERO_ERROR;
3926 const SharedNumberFormat *snf = createSharedNumberFormat(ovrLoc, status);
3927 if (U_SUCCESS(status)) {
3928 // Now that we have an appropriate number formatter, fill in the
3929 // appropriate slot in the number formatters table.
3930 UDateFormatField patternCharIndex = DateFormatSymbols::getPatternCharIndex(u'y');
3931 SharedObject::copyPtr(snf, fSharedNumberFormatters[patternCharIndex]);
3932 snf->deleteIfZeroRefCount();
3933 fDateOverride.setTo(u"y=jpanyear", -1); // record status
3934 }
3935 }
3936 }
3937 }
3938}
3939
3940//----------------------------------------------------------------------
3941
3942void
3943SimpleDateFormat::applyLocalizedPattern(const UnicodeString& pattern,
3944 UErrorCode &status)
3945{
3946 translatePattern(pattern, fPattern,
3947 fSymbols->fLocalPatternChars,
3948 UnicodeString(DateFormatSymbols::getPatternUChars()), status);
3949}
3950
3951//----------------------------------------------------------------------
3952
3953const DateFormatSymbols*
3954SimpleDateFormat::getDateFormatSymbols() const
3955{
3956 return fSymbols;
3957}
3958
3959//----------------------------------------------------------------------
3960
3961void
3962SimpleDateFormat::adoptDateFormatSymbols(DateFormatSymbols* newFormatSymbols)
3963{
3964 delete fSymbols;
3965 fSymbols = newFormatSymbols;
3966}
3967
3968//----------------------------------------------------------------------
3969void
3970SimpleDateFormat::setDateFormatSymbols(const DateFormatSymbols& newFormatSymbols)
3971{
3972 delete fSymbols;
3973 fSymbols = new DateFormatSymbols(newFormatSymbols);
3974}
3975
3976//----------------------------------------------------------------------
3977const TimeZoneFormat*
3978SimpleDateFormat::getTimeZoneFormat(void) const {
3979 // TimeZoneFormat initialization might fail when out of memory.
3980 // If we always initialize TimeZoneFormat instance, we can return
3981 // such status there. For now, this implementation lazily instantiates
3982 // a TimeZoneFormat for performance optimization reasons, but cannot
3983 // propagate such error (probably just out of memory case) to the caller.
3984 UErrorCode status = U_ZERO_ERROR;
3985 return (const TimeZoneFormat*)tzFormat(status);
3986}
3987
3988//----------------------------------------------------------------------
3989void
3990SimpleDateFormat::adoptTimeZoneFormat(TimeZoneFormat* timeZoneFormatToAdopt)
3991{
3992 delete fTimeZoneFormat;
3993 fTimeZoneFormat = timeZoneFormatToAdopt;
3994}
3995
3996//----------------------------------------------------------------------
3997void
3998SimpleDateFormat::setTimeZoneFormat(const TimeZoneFormat& newTimeZoneFormat)
3999{
4000 delete fTimeZoneFormat;
4001 fTimeZoneFormat = new TimeZoneFormat(newTimeZoneFormat);
4002}
4003
4004//----------------------------------------------------------------------
4005
4006
4007void SimpleDateFormat::adoptCalendar(Calendar* calendarToAdopt)
4008{
4009 UErrorCode status = U_ZERO_ERROR;
4010 Locale calLocale(fLocale);
4011 calLocale.setKeywordValue("calendar", calendarToAdopt->getType(), status);
4012 DateFormatSymbols *newSymbols =
4013 DateFormatSymbols::createForLocale(calLocale, status);
4014 if (U_FAILURE(status)) {
4015 delete calendarToAdopt;
4016 return;
4017 }
4018 DateFormat::adoptCalendar(calendarToAdopt);
4019 delete fSymbols;
4020 fSymbols = newSymbols;
4021 initializeDefaultCentury(); // we need a new century (possibly)
4022}
4023
4024
4025//----------------------------------------------------------------------
4026
4027
4028// override the DateFormat implementation in order to
4029// lazily initialize fCapitalizationBrkIter
4030void
4031SimpleDateFormat::setContext(UDisplayContext value, UErrorCode& status)
4032{
4033 DateFormat::setContext(value, status);
4034#if !UCONFIG_NO_BREAK_ITERATION
4035 if (U_SUCCESS(status)) {
4036 if ( fCapitalizationBrkIter == NULL && (value==UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE ||
4037 value==UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU || value==UDISPCTX_CAPITALIZATION_FOR_STANDALONE) ) {
4038 status = U_ZERO_ERROR;
4039 fCapitalizationBrkIter = BreakIterator::createSentenceInstance(fLocale, status);
4040 if (U_FAILURE(status)) {
4041 delete fCapitalizationBrkIter;
4042 fCapitalizationBrkIter = NULL;
4043 }
4044 }
4045 }
4046#endif
4047}
4048
4049
4050//----------------------------------------------------------------------
4051
4052
4053UBool
4054SimpleDateFormat::isFieldUnitIgnored(UCalendarDateFields field) const {
4055 return isFieldUnitIgnored(fPattern, field);
4056}
4057
4058
4059UBool
4060SimpleDateFormat::isFieldUnitIgnored(const UnicodeString& pattern,
4061 UCalendarDateFields field) {
4062 int32_t fieldLevel = fgCalendarFieldToLevel[field];
4063 int32_t level;
4064 UChar ch;
4065 UBool inQuote = FALSE;
4066 UChar prevCh = 0;
4067 int32_t count = 0;
4068
4069 for (int32_t i = 0; i < pattern.length(); ++i) {
4070 ch = pattern[i];
4071 if (ch != prevCh && count > 0) {
4072 level = getLevelFromChar(prevCh);
4073 // the larger the level, the smaller the field unit.
4074 if (fieldLevel <= level) {
4075 return FALSE;
4076 }
4077 count = 0;
4078 }
4079 if (ch == QUOTE) {
4080 if ((i+1) < pattern.length() && pattern[i+1] == QUOTE) {
4081 ++i;
4082 } else {
4083 inQuote = ! inQuote;
4084 }
4085 }
4086 else if (!inQuote && isSyntaxChar(ch)) {
4087 prevCh = ch;
4088 ++count;
4089 }
4090 }
4091 if (count > 0) {
4092 // last item
4093 level = getLevelFromChar(prevCh);
4094 if (fieldLevel <= level) {
4095 return FALSE;
4096 }
4097 }
4098 return TRUE;
4099}
4100
4101//----------------------------------------------------------------------
4102
4103const Locale&
4104SimpleDateFormat::getSmpFmtLocale(void) const {
4105 return fLocale;
4106}
4107
4108//----------------------------------------------------------------------
4109
4110int32_t
4111SimpleDateFormat::checkIntSuffix(const UnicodeString& text, int32_t start,
4112 int32_t patLoc, UBool isNegative) const {
4113 // local variables
4114 UnicodeString suf;
4115 int32_t patternMatch;
4116 int32_t textPreMatch;
4117 int32_t textPostMatch;
4118
4119 // check that we are still in range
4120 if ( (start > text.length()) ||
4121 (start < 0) ||
4122 (patLoc < 0) ||
4123 (patLoc > fPattern.length())) {
4124 // out of range, don't advance location in text
4125 return start;
4126 }
4127
4128 // get the suffix
4129 DecimalFormat* decfmt = dynamic_cast<DecimalFormat*>(fNumberFormat);
4130 if (decfmt != NULL) {
4131 if (isNegative) {
4132 suf = decfmt->getNegativeSuffix(suf);
4133 }
4134 else {
4135 suf = decfmt->getPositiveSuffix(suf);
4136 }
4137 }
4138
4139 // check for suffix
4140 if (suf.length() <= 0) {
4141 return start;
4142 }
4143
4144 // check suffix will be encountered in the pattern
4145 patternMatch = compareSimpleAffix(suf,fPattern,patLoc);
4146
4147 // check if a suffix will be encountered in the text
4148 textPreMatch = compareSimpleAffix(suf,text,start);
4149
4150 // check if a suffix was encountered in the text
4151 textPostMatch = compareSimpleAffix(suf,text,start-suf.length());
4152
4153 // check for suffix match
4154 if ((textPreMatch >= 0) && (patternMatch >= 0) && (textPreMatch == patternMatch)) {
4155 return start;
4156 }
4157 else if ((textPostMatch >= 0) && (patternMatch >= 0) && (textPostMatch == patternMatch)) {
4158 return start - suf.length();
4159 }
4160
4161 // should not get here
4162 return start;
4163}
4164
4165//----------------------------------------------------------------------
4166
4167int32_t
4168SimpleDateFormat::compareSimpleAffix(const UnicodeString& affix,
4169 const UnicodeString& input,
4170 int32_t pos) const {
4171 int32_t start = pos;
4172 for (int32_t i=0; i<affix.length(); ) {
4173 UChar32 c = affix.char32At(i);
4174 int32_t len = U16_LENGTH(c);
4175 if (PatternProps::isWhiteSpace(c)) {
4176 // We may have a pattern like: \u200F \u0020
4177 // and input text like: \u200F \u0020
4178 // Note that U+200F and U+0020 are Pattern_White_Space but only
4179 // U+0020 is UWhiteSpace. So we have to first do a direct
4180 // match of the run of Pattern_White_Space in the pattern,
4181 // then match any extra characters.
4182 UBool literalMatch = FALSE;
4183 while (pos < input.length() &&
4184 input.char32At(pos) == c) {
4185 literalMatch = TRUE;
4186 i += len;
4187 pos += len;
4188 if (i == affix.length()) {
4189 break;
4190 }
4191 c = affix.char32At(i);
4192 len = U16_LENGTH(c);
4193 if (!PatternProps::isWhiteSpace(c)) {
4194 break;
4195 }
4196 }
4197
4198 // Advance over run in pattern
4199 i = skipPatternWhiteSpace(affix, i);
4200
4201 // Advance over run in input text
4202 // Must see at least one white space char in input,
4203 // unless we've already matched some characters literally.
4204 int32_t s = pos;
4205 pos = skipUWhiteSpace(input, pos);
4206 if (pos == s && !literalMatch) {
4207 return -1;
4208 }
4209
4210 // If we skip UWhiteSpace in the input text, we need to skip it in the pattern.
4211 // Otherwise, the previous lines may have skipped over text (such as U+00A0) that
4212 // is also in the affix.
4213 i = skipUWhiteSpace(affix, i);
4214 } else {
4215 if (pos < input.length() &&
4216 input.char32At(pos) == c) {
4217 i += len;
4218 pos += len;
4219 } else {
4220 return -1;
4221 }
4222 }
4223 }
4224 return pos - start;
4225}
4226
4227//----------------------------------------------------------------------
4228
4229int32_t
4230SimpleDateFormat::skipPatternWhiteSpace(const UnicodeString& text, int32_t pos) const {
4231 const UChar* s = text.getBuffer();
4232 return (int32_t)(PatternProps::skipWhiteSpace(s + pos, text.length() - pos) - s);
4233}
4234
4235//----------------------------------------------------------------------
4236
4237int32_t
4238SimpleDateFormat::skipUWhiteSpace(const UnicodeString& text, int32_t pos) const {
4239 while (pos < text.length()) {
4240 UChar32 c = text.char32At(pos);
4241 if (!u_isUWhiteSpace(c)) {
4242 break;
4243 }
4244 pos += U16_LENGTH(c);
4245 }
4246 return pos;
4247}
4248
4249//----------------------------------------------------------------------
4250
4251// Lazy TimeZoneFormat instantiation, semantically const.
4252TimeZoneFormat *
4253SimpleDateFormat::tzFormat(UErrorCode &status) const {
4254 if (fTimeZoneFormat == NULL) {
4255 umtx_lock(&LOCK);
4256 {
4257 if (fTimeZoneFormat == NULL) {
4258 TimeZoneFormat *tzfmt = TimeZoneFormat::createInstance(fLocale, status);
4259 if (U_FAILURE(status)) {
4260 return NULL;
4261 }
4262
4263 const_cast<SimpleDateFormat *>(this)->fTimeZoneFormat = tzfmt;
4264 }
4265 }
4266 umtx_unlock(&LOCK);
4267 }
4268 return fTimeZoneFormat;
4269}
4270
4271void SimpleDateFormat::parsePattern() {
4272 fHasMinute = FALSE;
4273 fHasSecond = FALSE;
4274 fHasHanYearChar = FALSE;
4275
4276 int len = fPattern.length();
4277 UBool inQuote = FALSE;
4278 for (int32_t i = 0; i < len; ++i) {
4279 UChar ch = fPattern[i];
4280 if (ch == QUOTE) {
4281 inQuote = !inQuote;
4282 }
4283 if (ch == 0x5E74) { // don't care whether this is inside quotes
4284 fHasHanYearChar = TRUE;
4285 }
4286 if (!inQuote) {
4287 if (ch == 0x6D) { // 0x6D == 'm'
4288 fHasMinute = TRUE;
4289 }
4290 if (ch == 0x73) { // 0x73 == 's'
4291 fHasSecond = TRUE;
4292 }
4293 }
4294 }
4295}
4296
4297U_NAMESPACE_END
4298
4299#endif /* #if !UCONFIG_NO_FORMATTING */
4300
4301//eof
4302