2 *******************************************************************************
3 * Copyright (C) 1997-2016, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
9 * Modification History:
11 * Date Name Description
12 * 02/19/97 aliu Converted from java.
13 * 03/31/97 aliu Modified extensively to work with 50 locales.
14 * 04/01/97 aliu Added support for centuries.
15 * 07/09/97 helena Made ParsePosition into a class.
16 * 07/21/98 stephen Added initializeDefaultCentury.
17 * Removed getZoneIndex (added in DateFormatSymbols)
18 * Removed subParseLong
20 * 02/22/99 stephen Removed character literals for EBCDIC safety
21 * 10/14/99 aliu Updated 2-digit year parsing so that only "00" thru
22 * "99" are recognized. {j28 4182066}
23 * 11/15/99 weiv Added support for week of year/day of week format
24 ********************************************************************************
27 #define ZID_KEY_MAX 128
29 #include "unicode/utypes.h"
31 #if !UCONFIG_NO_FORMATTING
32 #include "unicode/smpdtfmt.h"
33 #include "unicode/dtfmtsym.h"
34 #include "unicode/ures.h"
35 #include "unicode/msgfmt.h"
36 #include "unicode/calendar.h"
37 #include "unicode/gregocal.h"
38 #include "unicode/timezone.h"
39 #include "unicode/decimfmt.h"
40 #include "unicode/dcfmtsym.h"
41 #include "unicode/uchar.h"
42 #include "unicode/uniset.h"
43 #include "unicode/ustring.h"
44 #include "unicode/basictz.h"
45 #include "unicode/simpleformatter.h"
46 #include "unicode/simpletz.h"
47 #include "unicode/rbtz.h"
48 #include "unicode/tzfmt.h"
49 #include "unicode/utf16.h"
50 #include "unicode/vtzone.h"
51 #include "unicode/udisplaycontext.h"
52 #include "unicode/brkiter.h"
54 #include "patternprops.h"
64 #include "sharednumberformat.h"
69 #include "dayperiodrules.h"
71 #if defined( U_DEBUG_CALSVC ) || defined (U_DEBUG_CAL)
75 // *****************************************************************************
76 // class SimpleDateFormat
77 // *****************************************************************************
82 * Last-resort string to use for "GMT" when constructing time zone strings.
84 // For time zones that have no names, use strings GMT+minutes and
85 // GMT-minutes. For instance, in France the time zone is GMT+60.
86 // Also accepted are GMT+H:MM or GMT-H:MM.
87 // Currently not being used
88 //static const UChar gGmt[] = {0x0047, 0x004D, 0x0054, 0x0000}; // "GMT"
89 //static const UChar gGmtPlus[] = {0x0047, 0x004D, 0x0054, 0x002B, 0x0000}; // "GMT+"
90 //static const UChar gGmtMinus[] = {0x0047, 0x004D, 0x0054, 0x002D, 0x0000}; // "GMT-"
91 //static const UChar gDefGmtPat[] = {0x0047, 0x004D, 0x0054, 0x007B, 0x0030, 0x007D, 0x0000}; /* GMT{0} */
92 //static const UChar gDefGmtNegHmsPat[] = {0x002D, 0x0048, 0x0048, 0x003A, 0x006D, 0x006D, 0x003A, 0x0073, 0x0073, 0x0000}; /* -HH:mm:ss */
93 //static const UChar gDefGmtNegHmPat[] = {0x002D, 0x0048, 0x0048, 0x003A, 0x006D, 0x006D, 0x0000}; /* -HH:mm */
94 //static const UChar gDefGmtPosHmsPat[] = {0x002B, 0x0048, 0x0048, 0x003A, 0x006D, 0x006D, 0x003A, 0x0073, 0x0073, 0x0000}; /* +HH:mm:ss */
95 //static const UChar gDefGmtPosHmPat[] = {0x002B, 0x0048, 0x0048, 0x003A, 0x006D, 0x006D, 0x0000}; /* +HH:mm */
96 //static const UChar gUt[] = {0x0055, 0x0054, 0x0000}; // "UT"
97 //static const UChar gUtc[] = {0x0055, 0x0054, 0x0043, 0x0000}; // "UT"
99 typedef enum GmtPatSize
{
110 // Stuff needed for numbering system overrides
112 typedef enum OvrStrType
{
118 static const UDateFormatField kDateFields
[] = {
122 UDAT_DAY_OF_YEAR_FIELD
,
123 UDAT_DAY_OF_WEEK_IN_MONTH_FIELD
,
124 UDAT_WEEK_OF_YEAR_FIELD
,
125 UDAT_WEEK_OF_MONTH_FIELD
,
127 UDAT_EXTENDED_YEAR_FIELD
,
128 UDAT_JULIAN_DAY_FIELD
,
129 UDAT_STANDALONE_DAY_FIELD
,
130 UDAT_STANDALONE_MONTH_FIELD
,
132 UDAT_STANDALONE_QUARTER_FIELD
,
133 UDAT_YEAR_NAME_FIELD
,
134 UDAT_RELATED_YEAR_FIELD
};
135 static const int8_t kDateFieldsCount
= 16;
137 static const UDateFormatField kTimeFields
[] = {
138 UDAT_HOUR_OF_DAY1_FIELD
,
139 UDAT_HOUR_OF_DAY0_FIELD
,
142 UDAT_FRACTIONAL_SECOND_FIELD
,
145 UDAT_MILLISECONDS_IN_DAY_FIELD
,
146 UDAT_TIMEZONE_RFC_FIELD
,
147 UDAT_TIMEZONE_LOCALIZED_GMT_OFFSET_FIELD
};
148 static const int8_t kTimeFieldsCount
= 10;
151 // This is a pattern-of-last-resort used when we can't load a usable pattern out
153 static const UChar gDefaultPattern
[] =
155 0x79, 0x79, 0x79, 0x79, 0x4D, 0x4D, 0x64, 0x64, 0x20, 0x68, 0x68, 0x3A, 0x6D, 0x6D, 0x20, 0x61, 0
156 }; /* "yyyyMMdd hh:mm a" */
158 // This prefix is designed to NEVER MATCH real text, in order to
159 // suppress the parsing of negative numbers. Adjust as needed (if
160 // this becomes valid Unicode).
161 static const UChar SUPPRESS_NEGATIVE_PREFIX
[] = {0xAB00, 0};
164 * These are the tags we expect to see in normal resource bundle files associated
167 static const char gDateTimePatternsTag
[]="DateTimePatterns";
169 //static const UChar gEtcUTC[] = {0x45, 0x74, 0x63, 0x2F, 0x55, 0x54, 0x43, 0x00}; // "Etc/UTC"
170 static const UChar QUOTE
= 0x27; // Single quote
173 * The field range check bias for each UDateFormatField.
174 * The bias is added to the minimum and maximum values
175 * before they are compared to the parsed number.
176 * For example, the calendar stores zero-based month numbers
177 * but the parsed month numbers start at 1, so the bias is 1.
179 * A value of -1 means that the value is not checked.
181 static const int32_t gFieldRangeBias
[] = {
182 -1, // 'G' - UDAT_ERA_FIELD
183 -1, // 'y' - UDAT_YEAR_FIELD
184 1, // 'M' - UDAT_MONTH_FIELD
185 0, // 'd' - UDAT_DATE_FIELD
186 -1, // 'k' - UDAT_HOUR_OF_DAY1_FIELD
187 -1, // 'H' - UDAT_HOUR_OF_DAY0_FIELD
188 0, // 'm' - UDAT_MINUTE_FIELD
189 0, // 's' - UDAT_SECOND_FIELD
190 -1, // 'S' - UDAT_FRACTIONAL_SECOND_FIELD (0-999?)
191 -1, // 'E' - UDAT_DAY_OF_WEEK_FIELD (1-7?)
192 -1, // 'D' - UDAT_DAY_OF_YEAR_FIELD (1 - 366?)
193 -1, // 'F' - UDAT_DAY_OF_WEEK_IN_MONTH_FIELD (1-5?)
194 -1, // 'w' - UDAT_WEEK_OF_YEAR_FIELD (1-52?)
195 -1, // 'W' - UDAT_WEEK_OF_MONTH_FIELD (1-5?)
196 -1, // 'a' - UDAT_AM_PM_FIELD
197 -1, // 'h' - UDAT_HOUR1_FIELD
198 -1, // 'K' - UDAT_HOUR0_FIELD
199 -1, // 'z' - UDAT_TIMEZONE_FIELD
200 -1, // 'Y' - UDAT_YEAR_WOY_FIELD
201 -1, // 'e' - UDAT_DOW_LOCAL_FIELD
202 -1, // 'u' - UDAT_EXTENDED_YEAR_FIELD
203 -1, // 'g' - UDAT_JULIAN_DAY_FIELD
204 -1, // 'A' - UDAT_MILLISECONDS_IN_DAY_FIELD
205 -1, // 'Z' - UDAT_TIMEZONE_RFC_FIELD
206 -1, // 'v' - UDAT_TIMEZONE_GENERIC_FIELD
207 0, // 'c' - UDAT_STANDALONE_DAY_FIELD
208 1, // 'L' - UDAT_STANDALONE_MONTH_FIELD
209 -1, // 'Q' - UDAT_QUARTER_FIELD (1-4?)
210 -1, // 'q' - UDAT_STANDALONE_QUARTER_FIELD
211 -1, // 'V' - UDAT_TIMEZONE_SPECIAL_FIELD
212 -1, // 'U' - UDAT_YEAR_NAME_FIELD
213 -1, // 'O' - UDAT_TIMEZONE_LOCALIZED_GMT_OFFSET_FIELD
214 -1, // 'X' - UDAT_TIMEZONE_ISO_FIELD
215 -1, // 'x' - UDAT_TIMEZONE_ISO_LOCAL_FIELD
216 -1, // 'r' - UDAT_RELATED_YEAR_FIELD
217 #if UDAT_HAS_PATTERN_CHAR_FOR_TIME_SEPARATOR
218 -1, // ':' - UDAT_TIME_SEPARATOR_FIELD
220 -1, // (no pattern character currently) - UDAT_TIME_SEPARATOR_FIELD
223 // A slightly looser range check for lenient parsing
224 static const int32_t gFieldRangeBiasLenient
[] = {
225 -1, // 'G' - UDAT_ERA_FIELD
226 -1, // 'y' - UDAT_YEAR_FIELD
227 8, // 'M' - UDAT_MONTH_FIELD (allow calendar max + 7, e.g. 19 for grego 1-based month)
228 18, // 'd' - UDAT_DATE_FIELD (allow calendar max + 18, e.g. 49 for grego; tests require at least 40 for grego)
229 -1, // 'k' - UDAT_HOUR_OF_DAY1_FIELD
230 -1, // 'H' - UDAT_HOUR_OF_DAY0_FIELD
231 40, // 'm' - UDAT_MINUTE_FIELD (allow calendar max + 40, e.g. 99)
232 40, // 's' - UDAT_SECOND_FIELD (allow calendar max + 40, e.g. 99)
233 -1, // 'S' - UDAT_FRACTIONAL_SECOND_FIELD (0-999?)
234 -1, // 'E' - UDAT_DAY_OF_WEEK_FIELD (1-7?)
235 -1, // 'D' - UDAT_DAY_OF_YEAR_FIELD (1 - 366?)
236 -1, // 'F' - UDAT_DAY_OF_WEEK_IN_MONTH_FIELD (1-5?)
237 -1, // 'w' - UDAT_WEEK_OF_YEAR_FIELD (1-52?)
238 -1, // 'W' - UDAT_WEEK_OF_MONTH_FIELD (1-5?)
239 -1, // 'a' - UDAT_AM_PM_FIELD
240 -1, // 'h' - UDAT_HOUR1_FIELD
241 -1, // 'K' - UDAT_HOUR0_FIELD
242 -1, // 'z' - UDAT_TIMEZONE_FIELD
243 -1, // 'Y' - UDAT_YEAR_WOY_FIELD
244 -1, // 'e' - UDAT_DOW_LOCAL_FIELD
245 -1, // 'u' - UDAT_EXTENDED_YEAR_FIELD
246 -1, // 'g' - UDAT_JULIAN_DAY_FIELD
247 -1, // 'A' - UDAT_MILLISECONDS_IN_DAY_FIELD
248 -1, // 'Z' - UDAT_TIMEZONE_RFC_FIELD
249 -1, // 'v' - UDAT_TIMEZONE_GENERIC_FIELD
250 18, // 'c' - UDAT_STANDALONE_DAY_FIELD (allow calendar max + 18, e.g. 49 for grego)
251 8, // 'L' - UDAT_STANDALONE_MONTH_FIELD (allow calendar max + 7, e.g. 19 for grego 1-based month)
252 -1, // 'Q' - UDAT_QUARTER_FIELD (1-4?)
253 -1, // 'q' - UDAT_STANDALONE_QUARTER_FIELD
254 -1, // 'V' - UDAT_TIMEZONE_SPECIAL_FIELD
255 -1, // 'U' - UDAT_YEAR_NAME_FIELD
256 -1, // 'O' - UDAT_TIMEZONE_LOCALIZED_GMT_OFFSET_FIELD
257 -1, // 'X' - UDAT_TIMEZONE_ISO_FIELD
258 -1, // 'x' - UDAT_TIMEZONE_ISO_LOCAL_FIELD
259 -1, // 'r' - UDAT_RELATED_YEAR_FIELD
260 #if UDAT_HAS_PATTERN_CHAR_FOR_TIME_SEPARATOR
261 -1, // ':' - UDAT_TIME_SEPARATOR_FIELD
263 -1, // (no pattern character currently) - UDAT_TIME_SEPARATOR_FIELD
267 // When calendar uses hebr numbering (i.e. he@calendar=hebrew),
268 // offset the years within the current millenium down to 1-999
269 static const int32_t HEBREW_CAL_CUR_MILLENIUM_START_YEAR
= 5000;
270 static const int32_t HEBREW_CAL_CUR_MILLENIUM_END_YEAR
= 6000;
272 static UMutex LOCK
= U_MUTEX_INITIALIZER
;
274 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(SimpleDateFormat
)
276 SimpleDateFormat::NSOverride::~NSOverride() {
283 void SimpleDateFormat::NSOverride::free() {
284 NSOverride
*cur
= this;
286 NSOverride
*next
= cur
->next
;
292 // no matter what the locale's default number format looked like, we want
293 // to modify it so that it doesn't use thousands separators, doesn't always
294 // show the decimal point, and recognizes integers only when parsing
295 static void fixNumberFormatForDates(NumberFormat
&nf
) {
296 nf
.setGroupingUsed(FALSE
);
297 DecimalFormat
* decfmt
= dynamic_cast<DecimalFormat
*>(&nf
);
298 if (decfmt
!= NULL
) {
299 decfmt
->setDecimalSeparatorAlwaysShown(FALSE
);
301 nf
.setParseIntegerOnly(TRUE
);
302 nf
.setMinimumFractionDigits(0); // To prevent "Jan 1.00, 1997.00"
305 static const SharedNumberFormat
*createSharedNumberFormat(
306 NumberFormat
*nfToAdopt
) {
307 fixNumberFormatForDates(*nfToAdopt
);
308 const SharedNumberFormat
*result
= new SharedNumberFormat(nfToAdopt
);
309 if (result
== NULL
) {
315 static const SharedNumberFormat
*createSharedNumberFormat(
316 const Locale
&loc
, UErrorCode
&status
) {
317 NumberFormat
*nf
= NumberFormat::createInstance(loc
, status
);
318 if (U_FAILURE(status
)) {
321 const SharedNumberFormat
*result
= createSharedNumberFormat(nf
);
322 if (result
== NULL
) {
323 status
= U_MEMORY_ALLOCATION_ERROR
;
328 static const SharedNumberFormat
**allocSharedNumberFormatters() {
329 const SharedNumberFormat
**result
= (const SharedNumberFormat
**)
330 uprv_malloc(UDAT_FIELD_COUNT
* sizeof(const SharedNumberFormat
*));
331 if (result
== NULL
) {
334 for (int32_t i
= 0; i
< UDAT_FIELD_COUNT
; ++i
) {
340 static void freeSharedNumberFormatters(const SharedNumberFormat
** list
) {
341 for (int32_t i
= 0; i
< UDAT_FIELD_COUNT
; ++i
) {
342 SharedObject::clearPtr(list
[i
]);
347 const NumberFormat
*SimpleDateFormat::getNumberFormatByIndex(
348 UDateFormatField index
) const {
349 if (fSharedNumberFormatters
== NULL
||
350 fSharedNumberFormatters
[index
] == NULL
) {
351 return fNumberFormat
;
353 return &(**fSharedNumberFormatters
[index
]);
356 class SimpleDateFormatMutableNFNode
{
358 const NumberFormat
*key
;
360 SimpleDateFormatMutableNFNode()
361 : key(NULL
), value(NULL
) { }
362 ~SimpleDateFormatMutableNFNode() {
366 SimpleDateFormatMutableNFNode(const SimpleDateFormatMutableNFNode
&);
367 SimpleDateFormatMutableNFNode
&operator=(const SimpleDateFormatMutableNFNode
&);
370 // Single threaded cache of non const NumberFormats. Designed to be stack
371 // allocated and used for a single format call.
372 class SimpleDateFormatMutableNFs
: public UMemory
{
374 SimpleDateFormatMutableNFs() {
377 // Returns a non-const clone of nf which can be safely modified.
378 // Subsequent calls with same nf will return the same non-const clone.
379 // This object maintains ownership of all returned non-const
380 // NumberFormat objects. On memory allocation error returns NULL.
381 // Caller must check for NULL return value.
382 NumberFormat
*get(const NumberFormat
*nf
) {
387 while (nodes
[idx
].value
) {
388 if (nf
== nodes
[idx
].key
) {
389 return nodes
[idx
].value
;
393 U_ASSERT(idx
< UDAT_FIELD_COUNT
);
395 nodes
[idx
].value
= (NumberFormat
*) nf
->clone();
396 return nodes
[idx
].value
;
399 // +1 extra for sentinel. If each field had its own NumberFormat, this
400 // cache would have to allocate UDAT_FIELD_COUNT mutable versions worst
402 SimpleDateFormatMutableNFNode nodes
[UDAT_FIELD_COUNT
+ 1];
403 SimpleDateFormatMutableNFs(const SimpleDateFormatMutableNFs
&);
404 SimpleDateFormatMutableNFs
&operator=(const SimpleDateFormatMutableNFs
&);
407 //----------------------------------------------------------------------
409 SimpleDateFormat::~SimpleDateFormat()
412 if (fSharedNumberFormatters
) {
413 freeSharedNumberFormatters(fSharedNumberFormatters
);
415 if (fTimeZoneFormat
) {
416 delete fTimeZoneFormat
;
419 #if !UCONFIG_NO_BREAK_ITERATION
420 delete fCapitalizationBrkIter
;
424 //----------------------------------------------------------------------
426 SimpleDateFormat::SimpleDateFormat(UErrorCode
& status
)
427 : fLocale(Locale::getDefault()),
429 fTimeZoneFormat(NULL
),
430 fSharedNumberFormatters(NULL
),
431 fCapitalizationBrkIter(NULL
)
433 initializeBooleanAttributes();
434 construct(kShort
, (EStyle
) (kShort
+ kDateOffset
), fLocale
, status
);
435 initializeDefaultCentury();
438 //----------------------------------------------------------------------
440 SimpleDateFormat::SimpleDateFormat(const UnicodeString
& pattern
,
443 fLocale(Locale::getDefault()),
445 fTimeZoneFormat(NULL
),
446 fSharedNumberFormatters(NULL
),
447 fCapitalizationBrkIter(NULL
)
449 fDateOverride
.setToBogus();
450 fTimeOverride
.setToBogus();
451 initializeBooleanAttributes();
452 initializeCalendar(NULL
,fLocale
,status
);
453 fSymbols
= DateFormatSymbols::createForLocale(fLocale
, status
);
454 initialize(fLocale
, status
);
455 initializeDefaultCentury();
459 //----------------------------------------------------------------------
461 SimpleDateFormat::SimpleDateFormat(const UnicodeString
& pattern
,
462 const UnicodeString
& override
,
465 fLocale(Locale::getDefault()),
467 fTimeZoneFormat(NULL
),
468 fSharedNumberFormatters(NULL
),
469 fCapitalizationBrkIter(NULL
)
471 fDateOverride
.setTo(override
);
472 fTimeOverride
.setToBogus();
473 initializeBooleanAttributes();
474 initializeCalendar(NULL
,fLocale
,status
);
475 fSymbols
= DateFormatSymbols::createForLocale(fLocale
, status
);
476 initialize(fLocale
, status
);
477 initializeDefaultCentury();
479 processOverrideString(fLocale
,override
,kOvrStrBoth
,status
);
483 //----------------------------------------------------------------------
485 SimpleDateFormat::SimpleDateFormat(const UnicodeString
& pattern
,
486 const Locale
& locale
,
490 fTimeZoneFormat(NULL
),
491 fSharedNumberFormatters(NULL
),
492 fCapitalizationBrkIter(NULL
)
495 fDateOverride
.setToBogus();
496 fTimeOverride
.setToBogus();
497 initializeBooleanAttributes();
499 initializeCalendar(NULL
,fLocale
,status
);
500 fSymbols
= DateFormatSymbols::createForLocale(fLocale
, status
);
501 initialize(fLocale
, status
);
502 initializeDefaultCentury();
505 //----------------------------------------------------------------------
507 SimpleDateFormat::SimpleDateFormat(const UnicodeString
& pattern
,
508 const UnicodeString
& override
,
509 const Locale
& locale
,
513 fTimeZoneFormat(NULL
),
514 fSharedNumberFormatters(NULL
),
515 fCapitalizationBrkIter(NULL
)
518 fDateOverride
.setTo(override
);
519 fTimeOverride
.setToBogus();
520 initializeBooleanAttributes();
522 initializeCalendar(NULL
,fLocale
,status
);
523 fSymbols
= DateFormatSymbols::createForLocale(fLocale
, status
);
524 initialize(fLocale
, status
);
525 initializeDefaultCentury();
527 processOverrideString(locale
,override
,kOvrStrBoth
,status
);
531 //----------------------------------------------------------------------
533 SimpleDateFormat::SimpleDateFormat(const UnicodeString
& pattern
,
534 DateFormatSymbols
* symbolsToAdopt
,
537 fLocale(Locale::getDefault()),
538 fSymbols(symbolsToAdopt
),
539 fTimeZoneFormat(NULL
),
540 fSharedNumberFormatters(NULL
),
541 fCapitalizationBrkIter(NULL
)
544 fDateOverride
.setToBogus();
545 fTimeOverride
.setToBogus();
546 initializeBooleanAttributes();
548 initializeCalendar(NULL
,fLocale
,status
);
549 initialize(fLocale
, status
);
550 initializeDefaultCentury();
553 //----------------------------------------------------------------------
555 SimpleDateFormat::SimpleDateFormat(const UnicodeString
& pattern
,
556 const DateFormatSymbols
& symbols
,
559 fLocale(Locale::getDefault()),
560 fSymbols(new DateFormatSymbols(symbols
)),
561 fTimeZoneFormat(NULL
),
562 fSharedNumberFormatters(NULL
),
563 fCapitalizationBrkIter(NULL
)
566 fDateOverride
.setToBogus();
567 fTimeOverride
.setToBogus();
568 initializeBooleanAttributes();
570 initializeCalendar(NULL
, fLocale
, status
);
571 initialize(fLocale
, status
);
572 initializeDefaultCentury();
575 //----------------------------------------------------------------------
577 // Not for public consumption; used by DateFormat
578 SimpleDateFormat::SimpleDateFormat(EStyle timeStyle
,
580 const Locale
& locale
,
584 fTimeZoneFormat(NULL
),
585 fSharedNumberFormatters(NULL
),
586 fCapitalizationBrkIter(NULL
)
588 initializeBooleanAttributes();
589 construct(timeStyle
, dateStyle
, fLocale
, status
);
590 if(U_SUCCESS(status
)) {
591 initializeDefaultCentury();
595 //----------------------------------------------------------------------
598 * Not for public consumption; used by DateFormat. This constructor
599 * never fails. If the resource data is not available, it uses the
600 * the last resort symbols.
602 SimpleDateFormat::SimpleDateFormat(const Locale
& locale
,
604 : fPattern(gDefaultPattern
),
607 fTimeZoneFormat(NULL
),
608 fSharedNumberFormatters(NULL
),
609 fCapitalizationBrkIter(NULL
)
611 if (U_FAILURE(status
)) return;
612 initializeBooleanAttributes();
613 initializeCalendar(NULL
, fLocale
, status
);
614 fSymbols
= DateFormatSymbols::createForLocale(fLocale
, status
);
615 if (U_FAILURE(status
))
617 status
= U_ZERO_ERROR
;
619 // This constructor doesn't fail; it uses last resort data
620 fSymbols
= new DateFormatSymbols(status
);
623 status
= U_MEMORY_ALLOCATION_ERROR
;
628 fDateOverride
.setToBogus();
629 fTimeOverride
.setToBogus();
631 initialize(fLocale
, status
);
632 if(U_SUCCESS(status
)) {
633 initializeDefaultCentury();
637 //----------------------------------------------------------------------
639 SimpleDateFormat::SimpleDateFormat(const SimpleDateFormat
& other
)
641 fLocale(other
.fLocale
),
643 fTimeZoneFormat(NULL
),
644 fSharedNumberFormatters(NULL
),
645 fCapitalizationBrkIter(NULL
)
647 initializeBooleanAttributes();
651 //----------------------------------------------------------------------
653 SimpleDateFormat
& SimpleDateFormat::operator=(const SimpleDateFormat
& other
)
655 if (this == &other
) {
658 DateFormat::operator=(other
);
659 fDateOverride
= other
.fDateOverride
;
660 fTimeOverride
= other
.fTimeOverride
;
666 fSymbols
= new DateFormatSymbols(*other
.fSymbols
);
668 fDefaultCenturyStart
= other
.fDefaultCenturyStart
;
669 fDefaultCenturyStartYear
= other
.fDefaultCenturyStartYear
;
670 fHaveDefaultCentury
= other
.fHaveDefaultCentury
;
672 fPattern
= other
.fPattern
;
673 fHasMinute
= other
.fHasMinute
;
674 fHasSecond
= other
.fHasSecond
;
676 fLocale
= other
.fLocale
;
677 // TimeZoneFormat can now be set independently via setter.
678 // If it is NULL, it will be lazily initialized from locale
679 delete fTimeZoneFormat
;
680 fTimeZoneFormat
= NULL
;
681 if (other
.fTimeZoneFormat
) {
682 fTimeZoneFormat
= new TimeZoneFormat(*other
.fTimeZoneFormat
);
685 #if !UCONFIG_NO_BREAK_ITERATION
686 if (other
.fCapitalizationBrkIter
!= NULL
) {
687 fCapitalizationBrkIter
= (other
.fCapitalizationBrkIter
)->clone();
691 if (fSharedNumberFormatters
!= NULL
) {
692 freeSharedNumberFormatters(fSharedNumberFormatters
);
693 fSharedNumberFormatters
= NULL
;
695 if (other
.fSharedNumberFormatters
!= NULL
) {
696 fSharedNumberFormatters
= allocSharedNumberFormatters();
697 if (fSharedNumberFormatters
) {
698 for (int32_t i
= 0; i
< UDAT_FIELD_COUNT
; ++i
) {
699 SharedObject::copyPtr(
700 other
.fSharedNumberFormatters
[i
],
701 fSharedNumberFormatters
[i
]);
709 //----------------------------------------------------------------------
712 SimpleDateFormat::clone() const
714 return new SimpleDateFormat(*this);
717 //----------------------------------------------------------------------
720 SimpleDateFormat::operator==(const Format
& other
) const
722 if (DateFormat::operator==(other
)) {
723 // The DateFormat::operator== check for fCapitalizationContext equality above
724 // is sufficient to check equality of all derived context-related data.
725 // DateFormat::operator== guarantees following cast is safe
726 SimpleDateFormat
* that
= (SimpleDateFormat
*)&other
;
727 return (fPattern
== that
->fPattern
&&
728 fSymbols
!= NULL
&& // Check for pathological object
729 that
->fSymbols
!= NULL
&& // Check for pathological object
730 *fSymbols
== *that
->fSymbols
&&
731 fHaveDefaultCentury
== that
->fHaveDefaultCentury
&&
732 fDefaultCenturyStart
== that
->fDefaultCenturyStart
&&
733 // Check fTimeZoneFormat, it can be set independently via setter
734 ((fTimeZoneFormat
== NULL
&& that
->fTimeZoneFormat
== NULL
) ||
735 (fTimeZoneFormat
!= NULL
&& that
->fTimeZoneFormat
!= NULL
&& *fTimeZoneFormat
== *that
->fTimeZoneFormat
)) &&
736 // Check override strings (these also indicate any relevant
737 // differences in fNumberFormatters, fOverrideList)
738 fDateOverride
== that
->fDateOverride
&&
739 fTimeOverride
== that
->fTimeOverride
);
744 //----------------------------------------------------------------------
746 void SimpleDateFormat::construct(EStyle timeStyle
,
748 const Locale
& locale
,
751 // called by several constructors to load pattern data from the resources
752 if (U_FAILURE(status
)) return;
754 // We will need the calendar to know what type of symbols to load.
755 initializeCalendar(NULL
, locale
, status
);
756 if (U_FAILURE(status
)) return;
758 CalendarData
calData(locale
, fCalendar
?fCalendar
->getType():NULL
, status
);
759 UResourceBundle
*dateTimePatterns
= calData
.getByKey(gDateTimePatternsTag
, status
);
760 UResourceBundle
*currentBundle
;
762 if (U_FAILURE(status
)) return;
764 if (ures_getSize(dateTimePatterns
) <= kDateTime
)
766 status
= U_INVALID_FORMAT_ERROR
;
770 setLocaleIDs(ures_getLocaleByType(dateTimePatterns
, ULOC_VALID_LOCALE
, &status
),
771 ures_getLocaleByType(dateTimePatterns
, ULOC_ACTUAL_LOCALE
, &status
));
773 // create a symbols object from the locale
774 fSymbols
= DateFormatSymbols::createForLocale(locale
, status
);
775 if (U_FAILURE(status
)) return;
778 status
= U_MEMORY_ALLOCATION_ERROR
;
782 const UChar
*resStr
,*ovrStr
;
783 int32_t resStrLen
,ovrStrLen
= 0;
784 fDateOverride
.setToBogus();
785 fTimeOverride
.setToBogus();
787 // if the pattern should include both date and time information, use the date/time
788 // pattern string as a guide to tell use how to glue together the appropriate date
789 // and time pattern strings.
790 if ((timeStyle
!= kNone
) && (dateStyle
!= kNone
))
792 currentBundle
= ures_getByIndex(dateTimePatterns
, (int32_t)timeStyle
, NULL
, &status
);
793 if (U_FAILURE(status
)) {
794 status
= U_INVALID_FORMAT_ERROR
;
797 switch (ures_getType(currentBundle
)) {
799 resStr
= ures_getString(currentBundle
, &resStrLen
, &status
);
803 resStr
= ures_getStringByIndex(currentBundle
, 0, &resStrLen
, &status
);
804 ovrStr
= ures_getStringByIndex(currentBundle
, 1, &ovrStrLen
, &status
);
805 fTimeOverride
.setTo(TRUE
, ovrStr
, ovrStrLen
);
809 status
= U_INVALID_FORMAT_ERROR
;
810 ures_close(currentBundle
);
814 ures_close(currentBundle
);
816 UnicodeString
tempus1(TRUE
, resStr
, resStrLen
);
818 currentBundle
= ures_getByIndex(dateTimePatterns
, (int32_t)dateStyle
, NULL
, &status
);
819 if (U_FAILURE(status
)) {
820 status
= U_INVALID_FORMAT_ERROR
;
823 switch (ures_getType(currentBundle
)) {
825 resStr
= ures_getString(currentBundle
, &resStrLen
, &status
);
829 resStr
= ures_getStringByIndex(currentBundle
, 0, &resStrLen
, &status
);
830 ovrStr
= ures_getStringByIndex(currentBundle
, 1, &ovrStrLen
, &status
);
831 fDateOverride
.setTo(TRUE
, ovrStr
, ovrStrLen
);
835 status
= U_INVALID_FORMAT_ERROR
;
836 ures_close(currentBundle
);
840 ures_close(currentBundle
);
842 UnicodeString
tempus2(TRUE
, resStr
, resStrLen
);
844 int32_t glueIndex
= kDateTime
;
845 int32_t patternsSize
= ures_getSize(dateTimePatterns
);
846 if (patternsSize
>= (kDateTimeOffset
+ kShort
+ 1)) {
847 // Get proper date time format
848 glueIndex
= (int32_t)(kDateTimeOffset
+ (dateStyle
- kDateOffset
));
851 resStr
= ures_getStringByIndex(dateTimePatterns
, glueIndex
, &resStrLen
, &status
);
852 SimpleFormatter(UnicodeString(TRUE
, resStr
, resStrLen
), 2, 2, status
).
853 format(tempus1
, tempus2
, fPattern
, status
);
855 // if the pattern includes just time data or just date date, load the appropriate
856 // pattern string from the resources
857 // setTo() - see DateFormatSymbols::assignArray comments
858 else if (timeStyle
!= kNone
) {
859 currentBundle
= ures_getByIndex(dateTimePatterns
, (int32_t)timeStyle
, NULL
, &status
);
860 if (U_FAILURE(status
)) {
861 status
= U_INVALID_FORMAT_ERROR
;
864 switch (ures_getType(currentBundle
)) {
866 resStr
= ures_getString(currentBundle
, &resStrLen
, &status
);
870 resStr
= ures_getStringByIndex(currentBundle
, 0, &resStrLen
, &status
);
871 ovrStr
= ures_getStringByIndex(currentBundle
, 1, &ovrStrLen
, &status
);
872 fDateOverride
.setTo(TRUE
, ovrStr
, ovrStrLen
);
876 status
= U_INVALID_FORMAT_ERROR
;
877 ures_close(currentBundle
);
881 fPattern
.setTo(TRUE
, resStr
, resStrLen
);
882 ures_close(currentBundle
);
884 else if (dateStyle
!= kNone
) {
885 currentBundle
= ures_getByIndex(dateTimePatterns
, (int32_t)dateStyle
, NULL
, &status
);
886 if (U_FAILURE(status
)) {
887 status
= U_INVALID_FORMAT_ERROR
;
890 switch (ures_getType(currentBundle
)) {
892 resStr
= ures_getString(currentBundle
, &resStrLen
, &status
);
896 resStr
= ures_getStringByIndex(currentBundle
, 0, &resStrLen
, &status
);
897 ovrStr
= ures_getStringByIndex(currentBundle
, 1, &ovrStrLen
, &status
);
898 fDateOverride
.setTo(TRUE
, ovrStr
, ovrStrLen
);
902 status
= U_INVALID_FORMAT_ERROR
;
903 ures_close(currentBundle
);
907 fPattern
.setTo(TRUE
, resStr
, resStrLen
);
908 ures_close(currentBundle
);
911 // and if it includes _neither_, that's an error
913 status
= U_INVALID_FORMAT_ERROR
;
915 // finally, finish initializing by creating a Calendar and a NumberFormat
916 initialize(locale
, status
);
919 //----------------------------------------------------------------------
922 SimpleDateFormat::initializeCalendar(TimeZone
* adoptZone
, const Locale
& locale
, UErrorCode
& status
)
924 if(!U_FAILURE(status
)) {
925 fCalendar
= Calendar::createInstance(adoptZone
?adoptZone
:TimeZone::createDefault(), locale
, status
);
931 SimpleDateFormat::initialize(const Locale
& locale
,
934 if (U_FAILURE(status
)) return;
936 // If the locale has @[....]numbers=hanidays we want to *delete* that (so it
937 // it is not used for every field) and then set fDateOverride to "d=hanidays"
938 // (as with std formats for zh@calendar=chinese) to use hanidays for d field.
939 static const UChar hanidaysOverride
[] = {0x64,0x3D,0x68,0x61,0x6E,0x69,0x64,0x61,0x79,0x73,0}; // "d=hanidays"
940 char numbersValue
[ULOC_KEYWORDS_CAPACITY
];
941 UErrorCode numbersStatus
= U_ZERO_ERROR
;
942 Locale
localeNoHanidays(locale
);
943 int32_t numbersLen
= localeNoHanidays
.getKeywordValue("numbers", numbersValue
, ULOC_KEYWORDS_CAPACITY
, numbersStatus
);
944 if ( U_SUCCESS(numbersStatus
) && numbersLen
> 0 ) {
945 if ( uprv_strcmp(numbersValue
, "hanidays") == 0 ) {
946 localeNoHanidays
.setKeywordValue("numbers", NULL
, numbersStatus
);
947 fDateOverride
.setTo(hanidaysOverride
,-1);
951 // We don't need to check that the row count is >= 1, since all 2d arrays have at
953 fNumberFormat
= NumberFormat::createInstance(localeNoHanidays
, status
);
954 if (fNumberFormat
!= NULL
&& U_SUCCESS(status
))
956 fixNumberFormatForDates(*fNumberFormat
);
957 //fNumberFormat->setLenient(TRUE); // Java uses a custom DateNumberFormat to format/parse
959 initNumberFormatters(locale
,status
);
962 else if (U_SUCCESS(status
))
964 status
= U_MISSING_RESOURCE_ERROR
;
970 /* Initialize the fields we use to disambiguate ambiguous years. Separate
971 * so we can call it from readObject().
973 void SimpleDateFormat::initializeDefaultCentury()
976 fHaveDefaultCentury
= fCalendar
->haveDefaultCentury();
977 if(fHaveDefaultCentury
) {
978 fDefaultCenturyStart
= fCalendar
->defaultCenturyStart();
979 fDefaultCenturyStartYear
= fCalendar
->defaultCenturyStartYear();
981 fDefaultCenturyStart
= DBL_MIN
;
982 fDefaultCenturyStartYear
= -1;
988 * Initialize the boolean attributes. Separate so we can call it from all constructors.
990 void SimpleDateFormat::initializeBooleanAttributes()
992 UErrorCode status
= U_ZERO_ERROR
;
994 setBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE
, true, status
);
995 setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC
, true, status
);
996 setBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH
, true, status
);
997 setBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, true, status
);
1000 /* Define one-century window into which to disambiguate dates using
1001 * two-digit years. Make public in JDK 1.2.
1003 void SimpleDateFormat::parseAmbiguousDatesAsAfter(UDate startDate
, UErrorCode
& status
)
1005 if(U_FAILURE(status
)) {
1009 status
= U_ILLEGAL_ARGUMENT_ERROR
;
1013 fCalendar
->setTime(startDate
, status
);
1014 if(U_SUCCESS(status
)) {
1015 fHaveDefaultCentury
= TRUE
;
1016 fDefaultCenturyStart
= startDate
;
1017 fDefaultCenturyStartYear
= fCalendar
->get(UCAL_YEAR
, status
);
1021 //----------------------------------------------------------------------
1024 SimpleDateFormat::format(Calendar
& cal
, UnicodeString
& appendTo
, FieldPosition
& pos
) const
1026 UErrorCode status
= U_ZERO_ERROR
;
1027 FieldPositionOnlyHandler
handler(pos
);
1028 return _format(cal
, appendTo
, handler
, status
);
1031 //----------------------------------------------------------------------
1034 SimpleDateFormat::format(Calendar
& cal
, UnicodeString
& appendTo
,
1035 FieldPositionIterator
* posIter
, UErrorCode
& status
) const
1037 FieldPositionIteratorHandler
handler(posIter
, status
);
1038 return _format(cal
, appendTo
, handler
, status
);
1041 //----------------------------------------------------------------------
1044 SimpleDateFormat::_format(Calendar
& cal
, UnicodeString
& appendTo
,
1045 FieldPositionHandler
& handler
, UErrorCode
& status
) const
1047 if ( U_FAILURE(status
) ) {
1050 Calendar
* workCal
= &cal
;
1051 Calendar
* calClone
= NULL
;
1052 if (&cal
!= fCalendar
&& uprv_strcmp(cal
.getType(), fCalendar
->getType()) != 0) {
1053 // Different calendar type
1054 // We use the time and time zone from the input calendar, but
1055 // do not use the input calendar for field calculation.
1056 calClone
= fCalendar
->clone();
1057 if (calClone
!= NULL
) {
1058 UDate t
= cal
.getTime(status
);
1059 calClone
->setTime(t
, status
);
1060 calClone
->setTimeZone(cal
.getTimeZone());
1063 status
= U_MEMORY_ALLOCATION_ERROR
;
1068 UBool inQuote
= FALSE
;
1071 int32_t fieldNum
= 0;
1072 UDisplayContext capitalizationContext
= getContext(UDISPCTX_TYPE_CAPITALIZATION
, status
);
1074 // Create temporary cache of mutable number format objects. This way
1075 // subFormat won't have to clone the const NumberFormat for each field.
1076 // if several fields share the same NumberFormat, which will almost
1077 // always be the case, this is a big save.
1078 SimpleDateFormatMutableNFs mutableNFs
;
1079 // loop through the pattern string character by character
1080 for (int32_t i
= 0; i
< fPattern
.length() && U_SUCCESS(status
); ++i
) {
1081 UChar ch
= fPattern
[i
];
1083 // Use subFormat() to format a repeated pattern character
1084 // when a different pattern or non-pattern character is seen
1085 if (ch
!= prevCh
&& count
> 0) {
1086 subFormat(appendTo
, prevCh
, count
, capitalizationContext
, fieldNum
++, handler
, *workCal
, mutableNFs
, status
);
1090 // Consecutive single quotes are a single quote literal,
1091 // either outside of quotes or between quotes
1092 if ((i
+1) < fPattern
.length() && fPattern
[i
+1] == QUOTE
) {
1093 appendTo
+= (UChar
)QUOTE
;
1096 inQuote
= ! inQuote
;
1099 else if (!inQuote
&& isSyntaxChar(ch
)) {
1100 // ch is a date-time pattern character to be interpreted
1101 // by subFormat(); count the number of times it is repeated
1106 // Append quoted characters and unquoted non-pattern characters
1111 // Format the last item in the pattern, if any
1113 subFormat(appendTo
, prevCh
, count
, capitalizationContext
, fieldNum
++, handler
, *workCal
, mutableNFs
, status
);
1116 if (calClone
!= NULL
) {
1123 //----------------------------------------------------------------------
1125 /* Map calendar field into calendar field level.
1126 * the larger the level, the smaller the field unit.
1127 * For example, UCAL_ERA level is 0, UCAL_YEAR level is 10,
1128 * UCAL_MONTH level is 20.
1129 * NOTE: if new fields adds in, the table needs to update.
1132 SimpleDateFormat::fgCalendarFieldToLevel
[] =
1136 /*dDEF*/ 30, 20, 30, 30,
1137 /*ahHm*/ 40, 50, 50, 60,
1144 int32_t SimpleDateFormat::getLevelFromChar(UChar ch
) {
1145 // Map date field LETTER into calendar field level.
1146 // the larger the level, the smaller the field unit.
1147 // NOTE: if new fields adds in, the table needs to update.
1148 static const int32_t mapCharToLevel
[] = {
1149 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1151 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1152 // ! " # $ % & ' ( ) * + , - . /
1153 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1154 #if UDAT_HAS_PATTERN_CHAR_FOR_TIME_SEPARATOR
1155 // 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
1156 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1,
1158 // 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
1159 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1161 // @ A B C D E F G H I J K L M N O
1162 -1, 40, -1, -1, 20, 30, 30, 0, 50, -1, -1, 50, 20, 20, -1, 0,
1163 // P Q R S T U V W X Y Z [ \ ] ^ _
1164 -1, 20, -1, 80, -1, 10, 0, 30, 0, 10, 0, -1, -1, -1, -1, -1,
1165 // ` a b c d e f g h i j k l m n o
1166 -1, 40, -1, 30, 30, 30, -1, 0, 50, -1, -1, 50, 0, 60, -1, -1,
1167 // p q r s t u v w x y z { | } ~
1168 -1, 20, 10, 70, -1, 10, 0, 20, 0, 10, 0, -1, -1, -1, -1, -1
1171 return ch
< UPRV_LENGTHOF(mapCharToLevel
) ? mapCharToLevel
[ch
] : -1;
1174 UBool
SimpleDateFormat::isSyntaxChar(UChar ch
) {
1175 static const UBool mapCharToIsSyntax
[] = {
1177 FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
,
1179 FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
,
1181 FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
,
1183 FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
,
1185 FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
,
1187 FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
,
1189 FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
,
1190 #if UDAT_HAS_PATTERN_CHAR_FOR_TIME_SEPARATOR
1192 FALSE
, FALSE
, TRUE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
,
1195 FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
,
1198 FALSE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
,
1200 TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
,
1202 TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
,
1204 TRUE
, TRUE
, TRUE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
,
1206 FALSE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
,
1208 TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
,
1210 TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
, TRUE
,
1212 TRUE
, TRUE
, TRUE
, FALSE
, FALSE
, FALSE
, FALSE
, FALSE
1215 return ch
< UPRV_LENGTHOF(mapCharToIsSyntax
) ? mapCharToIsSyntax
[ch
] : FALSE
;
1218 // Map index into pattern character string to Calendar field number.
1219 const UCalendarDateFields
1220 SimpleDateFormat::fgPatternIndexToCalendarField
[] =
1222 /*GyM*/ UCAL_ERA
, UCAL_YEAR
, UCAL_MONTH
,
1223 /*dkH*/ UCAL_DATE
, UCAL_HOUR_OF_DAY
, UCAL_HOUR_OF_DAY
,
1224 /*msS*/ UCAL_MINUTE
, UCAL_SECOND
, UCAL_MILLISECOND
,
1225 /*EDF*/ UCAL_DAY_OF_WEEK
, UCAL_DAY_OF_YEAR
, UCAL_DAY_OF_WEEK_IN_MONTH
,
1226 /*wWa*/ UCAL_WEEK_OF_YEAR
, UCAL_WEEK_OF_MONTH
, UCAL_AM_PM
,
1227 /*hKz*/ UCAL_HOUR
, UCAL_HOUR
, UCAL_ZONE_OFFSET
,
1228 /*Yeu*/ UCAL_YEAR_WOY
, UCAL_DOW_LOCAL
, UCAL_EXTENDED_YEAR
,
1229 /*gAZ*/ UCAL_JULIAN_DAY
, UCAL_MILLISECONDS_IN_DAY
, UCAL_ZONE_OFFSET
,
1230 /*v*/ UCAL_ZONE_OFFSET
,
1231 /*c*/ UCAL_DOW_LOCAL
,
1235 /*V*/ UCAL_ZONE_OFFSET
,
1237 /*O*/ UCAL_ZONE_OFFSET
,
1238 /*Xx*/ UCAL_ZONE_OFFSET
, UCAL_ZONE_OFFSET
,
1239 /*r*/ UCAL_EXTENDED_YEAR
,
1240 /*bB*/ UCAL_FIELD_COUNT
, UCAL_FIELD_COUNT
, // no mappings to calendar fields
1241 #if UDAT_HAS_PATTERN_CHAR_FOR_TIME_SEPARATOR
1242 /*:*/ UCAL_FIELD_COUNT
, /* => no useful mapping to any calendar field */
1244 /*no pattern char for UDAT_TIME_SEPARATOR_FIELD*/ UCAL_FIELD_COUNT
, /* => no useful mapping to any calendar field */
1248 // Map index into pattern character string to DateFormat field number
1249 const UDateFormatField
1250 SimpleDateFormat::fgPatternIndexToDateFormatField
[] = {
1251 /*GyM*/ UDAT_ERA_FIELD
, UDAT_YEAR_FIELD
, UDAT_MONTH_FIELD
,
1252 /*dkH*/ UDAT_DATE_FIELD
, UDAT_HOUR_OF_DAY1_FIELD
, UDAT_HOUR_OF_DAY0_FIELD
,
1253 /*msS*/ UDAT_MINUTE_FIELD
, UDAT_SECOND_FIELD
, UDAT_FRACTIONAL_SECOND_FIELD
,
1254 /*EDF*/ UDAT_DAY_OF_WEEK_FIELD
, UDAT_DAY_OF_YEAR_FIELD
, UDAT_DAY_OF_WEEK_IN_MONTH_FIELD
,
1255 /*wWa*/ UDAT_WEEK_OF_YEAR_FIELD
, UDAT_WEEK_OF_MONTH_FIELD
, UDAT_AM_PM_FIELD
,
1256 /*hKz*/ UDAT_HOUR1_FIELD
, UDAT_HOUR0_FIELD
, UDAT_TIMEZONE_FIELD
,
1257 /*Yeu*/ UDAT_YEAR_WOY_FIELD
, UDAT_DOW_LOCAL_FIELD
, UDAT_EXTENDED_YEAR_FIELD
,
1258 /*gAZ*/ UDAT_JULIAN_DAY_FIELD
, UDAT_MILLISECONDS_IN_DAY_FIELD
, UDAT_TIMEZONE_RFC_FIELD
,
1259 /*v*/ UDAT_TIMEZONE_GENERIC_FIELD
,
1260 /*c*/ UDAT_STANDALONE_DAY_FIELD
,
1261 /*L*/ UDAT_STANDALONE_MONTH_FIELD
,
1262 /*Q*/ UDAT_QUARTER_FIELD
,
1263 /*q*/ UDAT_STANDALONE_QUARTER_FIELD
,
1264 /*V*/ UDAT_TIMEZONE_SPECIAL_FIELD
,
1265 /*U*/ UDAT_YEAR_NAME_FIELD
,
1266 /*O*/ UDAT_TIMEZONE_LOCALIZED_GMT_OFFSET_FIELD
,
1267 /*Xx*/ UDAT_TIMEZONE_ISO_FIELD
, UDAT_TIMEZONE_ISO_LOCAL_FIELD
,
1268 /*r*/ UDAT_RELATED_YEAR_FIELD
,
1269 /*bB*/ UDAT_AM_PM_MIDNIGHT_NOON_FIELD
, UDAT_FLEXIBLE_DAY_PERIOD_FIELD
,
1270 #if UDAT_HAS_PATTERN_CHAR_FOR_TIME_SEPARATOR
1271 /*:*/ UDAT_TIME_SEPARATOR_FIELD
,
1273 /*no pattern char for UDAT_TIME_SEPARATOR_FIELD*/ UDAT_TIME_SEPARATOR_FIELD
,
1277 //----------------------------------------------------------------------
1280 * Append symbols[value] to dst. Make sure the array index is not out
1284 _appendSymbol(UnicodeString
& dst
,
1286 const UnicodeString
* symbols
,
1287 int32_t symbolsCount
) {
1288 U_ASSERT(0 <= value
&& value
< symbolsCount
);
1289 if (0 <= value
&& value
< symbolsCount
) {
1290 dst
+= symbols
[value
];
1295 _appendSymbolWithMonthPattern(UnicodeString
& dst
, int32_t value
, const UnicodeString
* symbols
, int32_t symbolsCount
,
1296 const UnicodeString
* monthPattern
, UErrorCode
& status
) {
1297 U_ASSERT(0 <= value
&& value
< symbolsCount
);
1298 if (0 <= value
&& value
< symbolsCount
) {
1299 if (monthPattern
== NULL
) {
1300 dst
+= symbols
[value
];
1302 SimpleFormatter(*monthPattern
, 1, 1, status
).format(symbols
[value
], dst
, status
);
1307 //----------------------------------------------------------------------
1309 SimpleDateFormat::initNumberFormatters(const Locale
&locale
,UErrorCode
&status
) {
1310 if (U_FAILURE(status
)) {
1313 if ( fDateOverride
.isBogus() && fTimeOverride
.isBogus() ) {
1317 if (fSharedNumberFormatters
== NULL
) {
1318 fSharedNumberFormatters
= allocSharedNumberFormatters();
1319 if (fSharedNumberFormatters
== NULL
) {
1320 status
= U_MEMORY_ALLOCATION_ERROR
;
1325 if (U_FAILURE(status
)) {
1329 processOverrideString(locale
,fDateOverride
,kOvrStrDate
,status
);
1330 processOverrideString(locale
,fTimeOverride
,kOvrStrTime
,status
);
1334 SimpleDateFormat::processOverrideString(const Locale
&locale
, const UnicodeString
&str
, int8_t type
, UErrorCode
&status
) {
1335 if (str
.isBogus() || U_FAILURE(status
)) {
1341 UnicodeString nsName
;
1342 UnicodeString ovrField
;
1343 UBool moreToProcess
= TRUE
;
1344 NSOverride
*overrideList
= NULL
;
1346 while (moreToProcess
) {
1347 int32_t delimiterPosition
= str
.indexOf((UChar
)ULOC_KEYWORD_ITEM_SEPARATOR_UNICODE
,start
);
1348 if (delimiterPosition
== -1) {
1349 moreToProcess
= FALSE
;
1350 len
= str
.length() - start
;
1352 len
= delimiterPosition
- start
;
1354 UnicodeString
currentString(str
,start
,len
);
1355 int32_t equalSignPosition
= currentString
.indexOf((UChar
)ULOC_KEYWORD_ASSIGN_UNICODE
,0);
1356 if (equalSignPosition
== -1) { // Simple override string such as "hebrew"
1357 nsName
.setTo(currentString
);
1358 ovrField
.setToBogus();
1359 } else { // Field specific override string such as "y=hebrew"
1360 nsName
.setTo(currentString
,equalSignPosition
+1);
1361 ovrField
.setTo(currentString
,0,1); // We just need the first character.
1364 int32_t nsNameHash
= nsName
.hashCode();
1365 // See if the numbering system is in the override list, if not, then add it.
1366 NSOverride
*cur
= overrideList
;
1367 const SharedNumberFormat
*snf
= NULL
;
1368 UBool found
= FALSE
;
1369 while ( cur
&& !found
) {
1370 if ( cur
->hash
== nsNameHash
) {
1378 LocalPointer
<NSOverride
> cur(new NSOverride
);
1379 if (!cur
.isNull()) {
1380 char kw
[ULOC_KEYWORD_AND_VALUES_CAPACITY
];
1381 uprv_strcpy(kw
,"numbers=");
1382 nsName
.extract(0,len
,kw
+8,ULOC_KEYWORD_AND_VALUES_CAPACITY
-8,US_INV
);
1384 Locale
ovrLoc(locale
.getLanguage(),locale
.getCountry(),locale
.getVariant(),kw
);
1385 cur
->hash
= nsNameHash
;
1386 cur
->next
= overrideList
;
1387 SharedObject::copyPtr(
1388 createSharedNumberFormat(ovrLoc
, status
), cur
->snf
);
1389 if (U_FAILURE(status
)) {
1391 overrideList
->free();
1396 overrideList
= cur
.orphan();
1398 status
= U_MEMORY_ALLOCATION_ERROR
;
1400 overrideList
->free();
1406 // Now that we have an appropriate number formatter, fill in the appropriate spaces in the
1407 // number formatters table.
1408 if (ovrField
.isBogus()) {
1412 for ( int8_t i
=0 ; i
<kDateFieldsCount
; i
++ ) {
1413 SharedObject::copyPtr(snf
, fSharedNumberFormatters
[kDateFields
[i
]]);
1415 if (type
==kOvrStrDate
) {
1420 case kOvrStrTime
: {
1421 for ( int8_t i
=0 ; i
<kTimeFieldsCount
; i
++ ) {
1422 SharedObject::copyPtr(snf
, fSharedNumberFormatters
[kTimeFields
[i
]]);
1428 // if the pattern character is unrecognized, signal an error and bail out
1429 UDateFormatField patternCharIndex
=
1430 DateFormatSymbols::getPatternCharIndex(ovrField
.charAt(0));
1431 if (patternCharIndex
== UDAT_FIELD_COUNT
) {
1432 status
= U_INVALID_FORMAT_ERROR
;
1434 overrideList
->free();
1438 SharedObject::copyPtr(snf
, fSharedNumberFormatters
[patternCharIndex
]);
1441 start
= delimiterPosition
+ 1;
1444 overrideList
->free();
1448 //---------------------------------------------------------------------
1450 SimpleDateFormat::subFormat(UnicodeString
&appendTo
,
1453 UDisplayContext capitalizationContext
,
1455 FieldPositionHandler
& handler
,
1457 SimpleDateFormatMutableNFs
&mutableNFs
,
1458 UErrorCode
& status
) const
1460 if (U_FAILURE(status
)) {
1464 // this function gets called by format() to produce the appropriate substitution
1465 // text for an individual pattern symbol (e.g., "HH" or "yyyy")
1467 UDateFormatField patternCharIndex
= DateFormatSymbols::getPatternCharIndex(ch
);
1468 const int32_t maxIntCount
= 10;
1469 int32_t beginOffset
= appendTo
.length();
1470 NumberFormat
*currentNumberFormat
;
1471 DateFormatSymbols::ECapitalizationContextUsageType capContextUsageType
= DateFormatSymbols::kCapContextUsageOther
;
1473 UBool isHebrewCalendar
= (uprv_strcmp(cal
.getType(),"hebrew") == 0);
1474 UBool isChineseCalendar
= (uprv_strcmp(cal
.getType(),"chinese") == 0 || uprv_strcmp(cal
.getType(),"dangi") == 0);
1476 // if the pattern character is unrecognized, signal an error and dump out
1477 if (patternCharIndex
== UDAT_FIELD_COUNT
)
1479 if (ch
!= 0x6C) { // pattern char 'l' (SMALL LETTER L) just gets ignored
1480 status
= U_INVALID_FORMAT_ERROR
;
1485 UCalendarDateFields field
= fgPatternIndexToCalendarField
[patternCharIndex
];
1487 // Don't get value unless it is useful
1488 if (field
< UCAL_FIELD_COUNT
) {
1489 value
= (patternCharIndex
!= UDAT_RELATED_YEAR_FIELD
)? cal
.get(field
, status
): cal
.getRelatedYear(status
);
1491 if (U_FAILURE(status
)) {
1495 currentNumberFormat
= mutableNFs
.get(getNumberFormatByIndex(patternCharIndex
));
1496 if (currentNumberFormat
== NULL
) {
1497 status
= U_MEMORY_ALLOCATION_ERROR
;
1500 UnicodeString
hebr("hebr", 4, US_INV
);
1502 switch (patternCharIndex
) {
1504 // for any "G" symbol, write out the appropriate era string
1505 // "GGGG" is wide era name, "GGGGG" is narrow era name, anything else is abbreviated name
1506 case UDAT_ERA_FIELD
:
1507 if (isChineseCalendar
) {
1508 zeroPaddingNumber(currentNumberFormat
,appendTo
, value
, 1, 9); // as in ICU4J
1511 _appendSymbol(appendTo
, value
, fSymbols
->fNarrowEras
, fSymbols
->fNarrowErasCount
);
1512 capContextUsageType
= DateFormatSymbols::kCapContextUsageEraNarrow
;
1513 } else if (count
== 4) {
1514 _appendSymbol(appendTo
, value
, fSymbols
->fEraNames
, fSymbols
->fEraNamesCount
);
1515 capContextUsageType
= DateFormatSymbols::kCapContextUsageEraWide
;
1517 _appendSymbol(appendTo
, value
, fSymbols
->fEras
, fSymbols
->fErasCount
);
1518 capContextUsageType
= DateFormatSymbols::kCapContextUsageEraAbbrev
;
1523 case UDAT_YEAR_NAME_FIELD
:
1524 if (fSymbols
->fShortYearNames
!= NULL
&& value
<= fSymbols
->fShortYearNamesCount
) {
1525 // the Calendar YEAR field runs 1 through 60 for cyclic years
1526 _appendSymbol(appendTo
, value
- 1, fSymbols
->fShortYearNames
, fSymbols
->fShortYearNamesCount
);
1529 // else fall through to numeric year handling, do not break here
1532 // OLD: for "yyyy", write out the whole year; for "yy", write out the last 2 digits
1534 //Year y yy yyy yyyy yyyyy
1535 //AD 1 1 01 001 0001 00001
1536 //AD 12 12 12 012 0012 00012
1537 //AD 123 123 23 123 0123 00123
1538 //AD 1234 1234 34 1234 1234 01234
1539 //AD 12345 12345 45 12345 12345 12345
1540 case UDAT_YEAR_FIELD
:
1541 case UDAT_YEAR_WOY_FIELD
:
1542 if (fDateOverride
.compare(hebr
)==0 && value
>HEBREW_CAL_CUR_MILLENIUM_START_YEAR
&& value
<HEBREW_CAL_CUR_MILLENIUM_END_YEAR
) {
1543 value
-=HEBREW_CAL_CUR_MILLENIUM_START_YEAR
;
1546 zeroPaddingNumber(currentNumberFormat
, appendTo
, value
, 2, 2);
1548 zeroPaddingNumber(currentNumberFormat
, appendTo
, value
, count
, maxIntCount
);
1551 // for "MMMM"/"LLLL", write out the whole month name, for "MMM"/"LLL", write out the month
1552 // abbreviation, for "M"/"L" or "MM"/"LL", write out the month as a number with the
1553 // appropriate number of digits
1554 // for "MMMMM"/"LLLLL", use the narrow form
1555 case UDAT_MONTH_FIELD
:
1556 case UDAT_STANDALONE_MONTH_FIELD
:
1557 if ( isHebrewCalendar
) {
1558 HebrewCalendar
*hc
= (HebrewCalendar
*)&cal
;
1559 if (hc
->isLeapYear(hc
->get(UCAL_YEAR
,status
)) && value
== 6 && count
>= 3 )
1560 value
= 13; // Show alternate form for Adar II in leap years in Hebrew calendar.
1561 if (!hc
->isLeapYear(hc
->get(UCAL_YEAR
,status
)) && value
>= 6 && count
< 3 )
1562 value
--; // Adjust the month number down 1 in Hebrew non-leap years, i.e. Adar is 6, not 7.
1565 int32_t isLeapMonth
= (fSymbols
->fLeapMonthPatterns
!= NULL
&& fSymbols
->fLeapMonthPatternsCount
>= DateFormatSymbols::kMonthPatternsCount
)?
1566 cal
.get(UCAL_IS_LEAP_MONTH
, status
): 0;
1567 // should consolidate the next section by using arrays of pointers & counts for the right symbols...
1569 if (patternCharIndex
== UDAT_MONTH_FIELD
) {
1570 _appendSymbolWithMonthPattern(appendTo
, value
, fSymbols
->fNarrowMonths
, fSymbols
->fNarrowMonthsCount
,
1571 (isLeapMonth
!=0)? &(fSymbols
->fLeapMonthPatterns
[DateFormatSymbols::kLeapMonthPatternFormatNarrow
]): NULL
, status
);
1573 _appendSymbolWithMonthPattern(appendTo
, value
, fSymbols
->fStandaloneNarrowMonths
, fSymbols
->fStandaloneNarrowMonthsCount
,
1574 (isLeapMonth
!=0)? &(fSymbols
->fLeapMonthPatterns
[DateFormatSymbols::kLeapMonthPatternStandaloneNarrow
]): NULL
, status
);
1576 capContextUsageType
= DateFormatSymbols::kCapContextUsageMonthNarrow
;
1577 } else if (count
== 4) {
1578 if (patternCharIndex
== UDAT_MONTH_FIELD
) {
1579 _appendSymbolWithMonthPattern(appendTo
, value
, fSymbols
->fMonths
, fSymbols
->fMonthsCount
,
1580 (isLeapMonth
!=0)? &(fSymbols
->fLeapMonthPatterns
[DateFormatSymbols::kLeapMonthPatternFormatWide
]): NULL
, status
);
1581 capContextUsageType
= DateFormatSymbols::kCapContextUsageMonthFormat
;
1583 _appendSymbolWithMonthPattern(appendTo
, value
, fSymbols
->fStandaloneMonths
, fSymbols
->fStandaloneMonthsCount
,
1584 (isLeapMonth
!=0)? &(fSymbols
->fLeapMonthPatterns
[DateFormatSymbols::kLeapMonthPatternStandaloneWide
]): NULL
, status
);
1585 capContextUsageType
= DateFormatSymbols::kCapContextUsageMonthStandalone
;
1587 } else if (count
== 3) {
1588 if (patternCharIndex
== UDAT_MONTH_FIELD
) {
1589 _appendSymbolWithMonthPattern(appendTo
, value
, fSymbols
->fShortMonths
, fSymbols
->fShortMonthsCount
,
1590 (isLeapMonth
!=0)? &(fSymbols
->fLeapMonthPatterns
[DateFormatSymbols::kLeapMonthPatternFormatAbbrev
]): NULL
, status
);
1591 capContextUsageType
= DateFormatSymbols::kCapContextUsageMonthFormat
;
1593 _appendSymbolWithMonthPattern(appendTo
, value
, fSymbols
->fStandaloneShortMonths
, fSymbols
->fStandaloneShortMonthsCount
,
1594 (isLeapMonth
!=0)? &(fSymbols
->fLeapMonthPatterns
[DateFormatSymbols::kLeapMonthPatternStandaloneAbbrev
]): NULL
, status
);
1595 capContextUsageType
= DateFormatSymbols::kCapContextUsageMonthStandalone
;
1598 UnicodeString monthNumber
;
1599 zeroPaddingNumber(currentNumberFormat
,monthNumber
, value
+ 1, count
, maxIntCount
);
1600 _appendSymbolWithMonthPattern(appendTo
, 0, &monthNumber
, 1,
1601 (isLeapMonth
!=0)? &(fSymbols
->fLeapMonthPatterns
[DateFormatSymbols::kLeapMonthPatternNumeric
]): NULL
, status
);
1606 // for "k" and "kk", write out the hour, adjusting midnight to appear as "24"
1607 case UDAT_HOUR_OF_DAY1_FIELD
:
1609 zeroPaddingNumber(currentNumberFormat
,appendTo
, cal
.getMaximum(UCAL_HOUR_OF_DAY
) + 1, count
, maxIntCount
);
1611 zeroPaddingNumber(currentNumberFormat
,appendTo
, value
, count
, maxIntCount
);
1614 case UDAT_FRACTIONAL_SECOND_FIELD
:
1615 // Fractional seconds left-justify
1617 currentNumberFormat
->setMinimumIntegerDigits((count
> 3) ? 3 : count
);
1618 currentNumberFormat
->setMaximumIntegerDigits(maxIntCount
);
1621 } else if (count
== 2) {
1625 currentNumberFormat
->format(value
, appendTo
, p
);
1627 currentNumberFormat
->setMinimumIntegerDigits(count
- 3);
1628 currentNumberFormat
->format((int32_t)0, appendTo
, p
);
1633 // for "ee" or "e", use local numeric day-of-the-week
1634 // for "EEEEEE" or "eeeeee", write out the short day-of-the-week name
1635 // for "EEEEE" or "eeeee", write out the narrow day-of-the-week name
1636 // for "EEEE" or "eeee", write out the wide day-of-the-week name
1637 // for "EEE" or "EE" or "E" or "eee", write out the abbreviated day-of-the-week name
1638 case UDAT_DOW_LOCAL_FIELD
:
1640 zeroPaddingNumber(currentNumberFormat
,appendTo
, value
, count
, maxIntCount
);
1643 // fall through to EEEEE-EEE handling, but for that we don't want local day-of-week,
1644 // we want standard day-of-week, so first fix value to work for EEEEE-EEE.
1645 value
= cal
.get(UCAL_DAY_OF_WEEK
, status
);
1646 if (U_FAILURE(status
)) {
1649 // fall through, do not break here
1651 case UDAT_DAY_OF_WEEK_FIELD
:
1653 _appendSymbol(appendTo
, value
, fSymbols
->fNarrowWeekdays
,
1654 fSymbols
->fNarrowWeekdaysCount
);
1655 capContextUsageType
= DateFormatSymbols::kCapContextUsageDayNarrow
;
1656 } else if (count
== 4) {
1657 _appendSymbol(appendTo
, value
, fSymbols
->fWeekdays
,
1658 fSymbols
->fWeekdaysCount
);
1659 capContextUsageType
= DateFormatSymbols::kCapContextUsageDayFormat
;
1660 } else if (count
== 6) {
1661 _appendSymbol(appendTo
, value
, fSymbols
->fShorterWeekdays
,
1662 fSymbols
->fShorterWeekdaysCount
);
1663 capContextUsageType
= DateFormatSymbols::kCapContextUsageDayFormat
;
1665 _appendSymbol(appendTo
, value
, fSymbols
->fShortWeekdays
,
1666 fSymbols
->fShortWeekdaysCount
);
1667 capContextUsageType
= DateFormatSymbols::kCapContextUsageDayFormat
;
1671 // for "ccc", write out the abbreviated day-of-the-week name
1672 // for "cccc", write out the wide day-of-the-week name
1673 // for "ccccc", use the narrow day-of-the-week name
1674 // for "ccccc", use the short day-of-the-week name
1675 case UDAT_STANDALONE_DAY_FIELD
:
1677 zeroPaddingNumber(currentNumberFormat
,appendTo
, value
, 1, maxIntCount
);
1680 // fall through to alpha DOW handling, but for that we don't want local day-of-week,
1681 // we want standard day-of-week, so first fix value.
1682 value
= cal
.get(UCAL_DAY_OF_WEEK
, status
);
1683 if (U_FAILURE(status
)) {
1687 _appendSymbol(appendTo
, value
, fSymbols
->fStandaloneNarrowWeekdays
,
1688 fSymbols
->fStandaloneNarrowWeekdaysCount
);
1689 capContextUsageType
= DateFormatSymbols::kCapContextUsageDayNarrow
;
1690 } else if (count
== 4) {
1691 _appendSymbol(appendTo
, value
, fSymbols
->fStandaloneWeekdays
,
1692 fSymbols
->fStandaloneWeekdaysCount
);
1693 capContextUsageType
= DateFormatSymbols::kCapContextUsageDayStandalone
;
1694 } else if (count
== 6) {
1695 _appendSymbol(appendTo
, value
, fSymbols
->fStandaloneShorterWeekdays
,
1696 fSymbols
->fStandaloneShorterWeekdaysCount
);
1697 capContextUsageType
= DateFormatSymbols::kCapContextUsageDayStandalone
;
1698 } else { // count == 3
1699 _appendSymbol(appendTo
, value
, fSymbols
->fStandaloneShortWeekdays
,
1700 fSymbols
->fStandaloneShortWeekdaysCount
);
1701 capContextUsageType
= DateFormatSymbols::kCapContextUsageDayStandalone
;
1705 // for "a" symbol, write out the whole AM/PM string
1706 case UDAT_AM_PM_FIELD
:
1708 _appendSymbol(appendTo
, value
, fSymbols
->fAmPms
,
1709 fSymbols
->fAmPmsCount
);
1711 _appendSymbol(appendTo
, value
, fSymbols
->fNarrowAmPms
,
1712 fSymbols
->fNarrowAmPmsCount
);
1716 // if we see pattern character for UDAT_TIME_SEPARATOR_FIELD (none currently defined),
1717 // write out the time separator string. Leave support in for future definition.
1718 case UDAT_TIME_SEPARATOR_FIELD
:
1720 UnicodeString separator
;
1721 appendTo
+= fSymbols
->getTimeSeparatorString(separator
);
1725 // for "h" and "hh", write out the hour, adjusting noon and midnight to show up
1727 case UDAT_HOUR1_FIELD
:
1729 zeroPaddingNumber(currentNumberFormat
,appendTo
, cal
.getLeastMaximum(UCAL_HOUR
) + 1, count
, maxIntCount
);
1731 zeroPaddingNumber(currentNumberFormat
,appendTo
, value
, count
, maxIntCount
);
1734 case UDAT_TIMEZONE_FIELD
: // 'z'
1735 case UDAT_TIMEZONE_RFC_FIELD
: // 'Z'
1736 case UDAT_TIMEZONE_GENERIC_FIELD
: // 'v'
1737 case UDAT_TIMEZONE_SPECIAL_FIELD
: // 'V'
1738 case UDAT_TIMEZONE_LOCALIZED_GMT_OFFSET_FIELD
: // 'O'
1739 case UDAT_TIMEZONE_ISO_FIELD
: // 'X'
1740 case UDAT_TIMEZONE_ISO_LOCAL_FIELD
: // 'x'
1743 UnicodeString
zoneString(zsbuf
, 0, UPRV_LENGTHOF(zsbuf
));
1744 const TimeZone
& tz
= cal
.getTimeZone();
1745 UDate date
= cal
.getTime(status
);
1746 if (U_SUCCESS(status
)) {
1747 if (patternCharIndex
== UDAT_TIMEZONE_FIELD
) {
1750 tzFormat()->format(UTZFMT_STYLE_SPECIFIC_SHORT
, tz
, date
, zoneString
);
1751 capContextUsageType
= DateFormatSymbols::kCapContextUsageMetazoneShort
;
1754 tzFormat()->format(UTZFMT_STYLE_SPECIFIC_LONG
, tz
, date
, zoneString
);
1755 capContextUsageType
= DateFormatSymbols::kCapContextUsageMetazoneLong
;
1758 else if (patternCharIndex
== UDAT_TIMEZONE_RFC_FIELD
) {
1761 tzFormat()->format(UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL
, tz
, date
, zoneString
);
1762 } else if (count
== 5) {
1764 tzFormat()->format(UTZFMT_STYLE_ISO_EXTENDED_FULL
, tz
, date
, zoneString
);
1766 // "ZZ", "ZZZ", "ZZZZ"
1767 tzFormat()->format(UTZFMT_STYLE_LOCALIZED_GMT
, tz
, date
, zoneString
);
1770 else if (patternCharIndex
== UDAT_TIMEZONE_GENERIC_FIELD
) {
1773 tzFormat()->format(UTZFMT_STYLE_GENERIC_SHORT
, tz
, date
, zoneString
);
1774 capContextUsageType
= DateFormatSymbols::kCapContextUsageMetazoneShort
;
1775 } else if (count
== 4) {
1777 tzFormat()->format(UTZFMT_STYLE_GENERIC_LONG
, tz
, date
, zoneString
);
1778 capContextUsageType
= DateFormatSymbols::kCapContextUsageMetazoneLong
;
1781 else if (patternCharIndex
== UDAT_TIMEZONE_SPECIAL_FIELD
) {
1784 tzFormat()->format(UTZFMT_STYLE_ZONE_ID_SHORT
, tz
, date
, zoneString
);
1785 } else if (count
== 2) {
1787 tzFormat()->format(UTZFMT_STYLE_ZONE_ID
, tz
, date
, zoneString
);
1788 } else if (count
== 3) {
1790 tzFormat()->format(UTZFMT_STYLE_EXEMPLAR_LOCATION
, tz
, date
, zoneString
);
1791 } else if (count
== 4) {
1793 tzFormat()->format(UTZFMT_STYLE_GENERIC_LOCATION
, tz
, date
, zoneString
);
1794 capContextUsageType
= DateFormatSymbols::kCapContextUsageZoneLong
;
1797 else if (patternCharIndex
== UDAT_TIMEZONE_LOCALIZED_GMT_OFFSET_FIELD
) {
1800 tzFormat()->format(UTZFMT_STYLE_LOCALIZED_GMT_SHORT
, tz
, date
, zoneString
);
1801 } else if (count
== 4) {
1803 tzFormat()->format(UTZFMT_STYLE_LOCALIZED_GMT
, tz
, date
, zoneString
);
1806 else if (patternCharIndex
== UDAT_TIMEZONE_ISO_FIELD
) {
1809 tzFormat()->format(UTZFMT_STYLE_ISO_BASIC_SHORT
, tz
, date
, zoneString
);
1810 } else if (count
== 2) {
1812 tzFormat()->format(UTZFMT_STYLE_ISO_BASIC_FIXED
, tz
, date
, zoneString
);
1813 } else if (count
== 3) {
1815 tzFormat()->format(UTZFMT_STYLE_ISO_EXTENDED_FIXED
, tz
, date
, zoneString
);
1816 } else if (count
== 4) {
1818 tzFormat()->format(UTZFMT_STYLE_ISO_BASIC_FULL
, tz
, date
, zoneString
);
1819 } else if (count
== 5) {
1821 tzFormat()->format(UTZFMT_STYLE_ISO_EXTENDED_FULL
, tz
, date
, zoneString
);
1824 else if (patternCharIndex
== UDAT_TIMEZONE_ISO_LOCAL_FIELD
) {
1827 tzFormat()->format(UTZFMT_STYLE_ISO_BASIC_LOCAL_SHORT
, tz
, date
, zoneString
);
1828 } else if (count
== 2) {
1830 tzFormat()->format(UTZFMT_STYLE_ISO_BASIC_LOCAL_FIXED
, tz
, date
, zoneString
);
1831 } else if (count
== 3) {
1833 tzFormat()->format(UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FIXED
, tz
, date
, zoneString
);
1834 } else if (count
== 4) {
1836 tzFormat()->format(UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL
, tz
, date
, zoneString
);
1837 } else if (count
== 5) {
1839 tzFormat()->format(UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FULL
, tz
, date
, zoneString
);
1846 appendTo
+= zoneString
;
1850 case UDAT_QUARTER_FIELD
:
1852 _appendSymbol(appendTo
, value
/3, fSymbols
->fQuarters
,
1853 fSymbols
->fQuartersCount
);
1854 else if (count
== 3)
1855 _appendSymbol(appendTo
, value
/3, fSymbols
->fShortQuarters
,
1856 fSymbols
->fShortQuartersCount
);
1858 zeroPaddingNumber(currentNumberFormat
,appendTo
, (value
/3) + 1, count
, maxIntCount
);
1861 case UDAT_STANDALONE_QUARTER_FIELD
:
1863 _appendSymbol(appendTo
, value
/3, fSymbols
->fStandaloneQuarters
,
1864 fSymbols
->fStandaloneQuartersCount
);
1865 else if (count
== 3)
1866 _appendSymbol(appendTo
, value
/3, fSymbols
->fStandaloneShortQuarters
,
1867 fSymbols
->fStandaloneShortQuartersCount
);
1869 zeroPaddingNumber(currentNumberFormat
,appendTo
, (value
/3) + 1, count
, maxIntCount
);
1872 case UDAT_AM_PM_MIDNIGHT_NOON_FIELD
:
1874 const UnicodeString
*toAppend
= NULL
;
1875 int32_t hour
= cal
.get(UCAL_HOUR_OF_DAY
, status
);
1877 // Note: "midnight" can be ambiguous as to whether it refers to beginning of day or end of day.
1878 // For ICU 57 output of "midnight" is temporarily suppressed.
1880 // For "midnight" and "noon":
1881 // Time, as displayed, must be exactly noon or midnight.
1882 // This means minutes and seconds, if present, must be zero.
1883 if ((/*hour == 0 ||*/ hour
== 12) &&
1884 (!fHasMinute
|| cal
.get(UCAL_MINUTE
, status
) == 0) &&
1885 (!fHasSecond
|| cal
.get(UCAL_SECOND
, status
) == 0)) {
1886 // Stealing am/pm value to use as our array index.
1887 // It works out: am/midnight are both 0, pm/noon are both 1,
1888 // 12 am is 12 midnight, and 12 pm is 12 noon.
1889 int32_t value
= cal
.get(UCAL_AM_PM
, status
);
1892 toAppend
= &fSymbols
->fAbbreviatedDayPeriods
[value
];
1893 } else if (count
== 4 || count
> 5) {
1894 toAppend
= &fSymbols
->fWideDayPeriods
[value
];
1895 } else { // count == 5
1896 toAppend
= &fSymbols
->fNarrowDayPeriods
[value
];
1900 // toAppend is NULL if time isn't exactly midnight or noon (as displayed).
1901 // toAppend is bogus if time is midnight or noon, but no localized string exists.
1902 // In either case, fall back to am/pm.
1903 if (toAppend
== NULL
|| toAppend
->isBogus()) {
1904 // Reformat with identical arguments except ch, now changed to 'a'.
1905 subFormat(appendTo
, 0x61, count
, capitalizationContext
, fieldNum
,
1906 handler
, cal
, mutableNFs
, status
);
1908 appendTo
+= *toAppend
;
1914 case UDAT_FLEXIBLE_DAY_PERIOD_FIELD
:
1916 // TODO: Maybe fetch the DayperiodRules during initialization (instead of at the first
1917 // loading of an instance) if a relevant pattern character (b or B) is used.
1918 const DayPeriodRules
*ruleSet
= DayPeriodRules::getInstance(this->getSmpFmtLocale(), status
);
1919 if (U_FAILURE(status
)) {
1920 // Data doesn't conform to spec, therefore loading failed.
1923 if (ruleSet
== NULL
) {
1924 // Data doesn't exist for the locale we're looking for.
1925 // Falling back to am/pm.
1926 subFormat(appendTo
, 0x61, count
, capitalizationContext
, fieldNum
,
1927 handler
, cal
, mutableNFs
, status
);
1931 // Get current display time.
1932 int32_t hour
= cal
.get(UCAL_HOUR_OF_DAY
, status
);
1935 minute
= cal
.get(UCAL_MINUTE
, status
);
1939 second
= cal
.get(UCAL_SECOND
, status
);
1942 // Determine day period.
1943 DayPeriodRules::DayPeriod periodType
;
1944 if (hour
== 0 && minute
== 0 && second
== 0 && ruleSet
->hasMidnight()) {
1945 periodType
= DayPeriodRules::DAYPERIOD_MIDNIGHT
;
1946 } else if (hour
== 12 && minute
== 0 && second
== 0 && ruleSet
->hasNoon()) {
1947 periodType
= DayPeriodRules::DAYPERIOD_NOON
;
1949 periodType
= ruleSet
->getDayPeriodForHour(hour
);
1952 // Rule set exists, therefore periodType can't be UNKNOWN.
1953 // Get localized string.
1954 U_ASSERT(periodType
!= DayPeriodRules::DAYPERIOD_UNKNOWN
);
1955 UnicodeString
*toAppend
= NULL
;
1958 // Note: "midnight" can be ambiguous as to whether it refers to beginning of day or end of day.
1959 // For ICU 57 output of "midnight" is temporarily suppressed.
1961 if (periodType
!= DayPeriodRules::DAYPERIOD_AM
&&
1962 periodType
!= DayPeriodRules::DAYPERIOD_PM
&&
1963 periodType
!= DayPeriodRules::DAYPERIOD_MIDNIGHT
) {
1964 index
= (int32_t)periodType
;
1966 toAppend
= &fSymbols
->fAbbreviatedDayPeriods
[index
]; // i.e. short
1967 } else if (count
== 4 || count
> 5) {
1968 toAppend
= &fSymbols
->fWideDayPeriods
[index
];
1969 } else { // count == 5
1970 toAppend
= &fSymbols
->fNarrowDayPeriods
[index
];
1974 // Fallback schedule:
1975 // Midnight/Noon -> General Periods -> AM/PM.
1977 // Midnight/Noon -> General Periods.
1978 if ((toAppend
== NULL
|| toAppend
->isBogus()) &&
1979 (periodType
== DayPeriodRules::DAYPERIOD_MIDNIGHT
||
1980 periodType
== DayPeriodRules::DAYPERIOD_NOON
)) {
1981 periodType
= ruleSet
->getDayPeriodForHour(hour
);
1982 index
= (int32_t)periodType
;
1985 toAppend
= &fSymbols
->fAbbreviatedDayPeriods
[index
]; // i.e. short
1986 } else if (count
== 4 || count
> 5) {
1987 toAppend
= &fSymbols
->fWideDayPeriods
[index
];
1988 } else { // count == 5
1989 toAppend
= &fSymbols
->fNarrowDayPeriods
[index
];
1993 // General Periods -> AM/PM.
1994 if (periodType
== DayPeriodRules::DAYPERIOD_AM
||
1995 periodType
== DayPeriodRules::DAYPERIOD_PM
||
1996 toAppend
->isBogus()) {
1997 subFormat(appendTo
, 0x61, count
, capitalizationContext
, fieldNum
,
1998 handler
, cal
, mutableNFs
, status
);
2001 appendTo
+= *toAppend
;
2007 // all of the other pattern symbols can be formatted as simple numbers with
2008 // appropriate zero padding
2010 zeroPaddingNumber(currentNumberFormat
,appendTo
, value
, count
, maxIntCount
);
2013 #if !UCONFIG_NO_BREAK_ITERATION
2014 // if first field, check to see whether we need to and are able to titlecase it
2015 if (fieldNum
== 0 && u_islower(appendTo
.char32At(beginOffset
)) && fCapitalizationBrkIter
!= NULL
) {
2016 UBool titlecase
= FALSE
;
2017 switch (capitalizationContext
) {
2018 case UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE
:
2021 case UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU
:
2022 titlecase
= fSymbols
->fCapitalization
[capContextUsageType
][0];
2024 case UDISPCTX_CAPITALIZATION_FOR_STANDALONE
:
2025 titlecase
= fSymbols
->fCapitalization
[capContextUsageType
][1];
2028 // titlecase = FALSE;
2032 UnicodeString
firstField(appendTo
, beginOffset
);
2033 firstField
.toTitle(fCapitalizationBrkIter
, fLocale
, U_TITLECASE_NO_LOWERCASE
| U_TITLECASE_NO_BREAK_ADJUSTMENT
);
2034 appendTo
.replaceBetween(beginOffset
, appendTo
.length(), firstField
);
2039 handler
.addAttribute(fgPatternIndexToDateFormatField
[patternCharIndex
], beginOffset
, appendTo
.length());
2042 //----------------------------------------------------------------------
2044 void SimpleDateFormat::adoptNumberFormat(NumberFormat
*formatToAdopt
) {
2045 fixNumberFormatForDates(*formatToAdopt
);
2046 delete fNumberFormat
;
2047 fNumberFormat
= formatToAdopt
;
2049 // We successfully set the default number format. Now delete the overrides
2051 if (fSharedNumberFormatters
) {
2052 freeSharedNumberFormatters(fSharedNumberFormatters
);
2053 fSharedNumberFormatters
= NULL
;
2057 void SimpleDateFormat::adoptNumberFormat(const UnicodeString
& fields
, NumberFormat
*formatToAdopt
, UErrorCode
&status
){
2058 fixNumberFormatForDates(*formatToAdopt
);
2059 LocalPointer
<NumberFormat
> fmt(formatToAdopt
);
2060 if (U_FAILURE(status
)) {
2064 // We must ensure fSharedNumberFormatters is allocated.
2065 if (fSharedNumberFormatters
== NULL
) {
2066 fSharedNumberFormatters
= allocSharedNumberFormatters();
2067 if (fSharedNumberFormatters
== NULL
) {
2068 status
= U_MEMORY_ALLOCATION_ERROR
;
2072 const SharedNumberFormat
*newFormat
= createSharedNumberFormat(fmt
.orphan());
2073 if (newFormat
== NULL
) {
2074 status
= U_MEMORY_ALLOCATION_ERROR
;
2077 for (int i
=0; i
<fields
.length(); i
++) {
2078 UChar field
= fields
.charAt(i
);
2079 // if the pattern character is unrecognized, signal an error and bail out
2080 UDateFormatField patternCharIndex
= DateFormatSymbols::getPatternCharIndex(field
);
2081 if (patternCharIndex
== UDAT_FIELD_COUNT
) {
2082 status
= U_INVALID_FORMAT_ERROR
;
2083 newFormat
->deleteIfZeroRefCount();
2087 // Set the number formatter in the table
2088 SharedObject::copyPtr(
2089 newFormat
, fSharedNumberFormatters
[patternCharIndex
]);
2091 newFormat
->deleteIfZeroRefCount();
2094 const NumberFormat
*
2095 SimpleDateFormat::getNumberFormatForField(UChar field
) const {
2096 UDateFormatField index
= DateFormatSymbols::getPatternCharIndex(field
);
2097 if (index
== UDAT_FIELD_COUNT
) {
2100 return getNumberFormatByIndex(index
);
2103 //----------------------------------------------------------------------
2105 SimpleDateFormat::zeroPaddingNumber(
2106 NumberFormat
*currentNumberFormat
,
2107 UnicodeString
&appendTo
,
2108 int32_t value
, int32_t minDigits
, int32_t maxDigits
) const
2110 if (currentNumberFormat
!=NULL
) {
2111 FieldPosition
pos(0);
2113 currentNumberFormat
->setMinimumIntegerDigits(minDigits
);
2114 currentNumberFormat
->setMaximumIntegerDigits(maxDigits
);
2115 currentNumberFormat
->format(value
, appendTo
, pos
); // 3rd arg is there to speed up processing
2119 //----------------------------------------------------------------------
2122 * Return true if the given format character, occuring count
2123 * times, represents a numeric field.
2125 UBool
SimpleDateFormat::isNumeric(UChar formatChar
, int32_t count
) {
2126 return DateFormatSymbols::isNumericPatternChar(formatChar
, count
);
2130 SimpleDateFormat::isAtNumericField(const UnicodeString
&pattern
, int32_t patternOffset
) {
2131 if (patternOffset
>= pattern
.length()) {
2135 UChar ch
= pattern
.charAt(patternOffset
);
2136 UDateFormatField f
= DateFormatSymbols::getPatternCharIndex(ch
);
2137 if (f
== UDAT_FIELD_COUNT
) {
2141 int32_t i
= patternOffset
;
2142 while (pattern
.charAt(++i
) == ch
) {}
2143 return DateFormatSymbols::isNumericField(f
, i
- patternOffset
);
2147 SimpleDateFormat::isAfterNonNumericField(const UnicodeString
&pattern
, int32_t patternOffset
) {
2148 if (patternOffset
<= 0) {
2149 // not after any field
2152 UChar ch
= pattern
.charAt(--patternOffset
);
2153 UDateFormatField f
= DateFormatSymbols::getPatternCharIndex(ch
);
2154 if (f
== UDAT_FIELD_COUNT
) {
2155 // not after any field
2158 int32_t i
= patternOffset
;
2159 while (pattern
.charAt(--i
) == ch
) {}
2160 return !DateFormatSymbols::isNumericField(f
, patternOffset
- i
);
2164 SimpleDateFormat::parse(const UnicodeString
& text
, Calendar
& cal
, ParsePosition
& parsePos
) const
2166 UErrorCode status
= U_ZERO_ERROR
;
2167 int32_t pos
= parsePos
.getIndex();
2168 if(parsePos
.getIndex() < 0) {
2169 parsePos
.setErrorIndex(0);
2172 int32_t start
= pos
;
2174 // Hold the day period until everything else is parsed, because we need
2175 // the hour to interpret time correctly.
2176 int32_t dayPeriodInt
= -1;
2178 UBool ambiguousYear
[] = { FALSE
};
2179 int32_t saveHebrewMonth
= -1;
2181 UTimeZoneFormatTimeType tzTimeType
= UTZFMT_TIME_TYPE_UNKNOWN
;
2182 SimpleDateFormatMutableNFs mutableNFs
;
2184 // For parsing abutting numeric fields. 'abutPat' is the
2185 // offset into 'pattern' of the first of 2 or more abutting
2186 // numeric fields. 'abutStart' is the offset into 'text'
2187 // where parsing the fields begins. 'abutPass' starts off as 0
2188 // and increments each time we try to parse the fields.
2189 int32_t abutPat
= -1; // If >=0, we are in a run of abutting numeric fields
2190 int32_t abutStart
= 0;
2191 int32_t abutPass
= 0;
2192 UBool inQuote
= FALSE
;
2194 MessageFormat
* numericLeapMonthFormatter
= NULL
;
2196 Calendar
* calClone
= NULL
;
2197 Calendar
*workCal
= &cal
;
2198 if (&cal
!= fCalendar
&& uprv_strcmp(cal
.getType(), fCalendar
->getType()) != 0) {
2199 // Different calendar type
2200 // We use the time/zone from the input calendar, but
2201 // do not use the input calendar for field calculation.
2202 calClone
= fCalendar
->clone();
2203 if (calClone
!= NULL
) {
2204 calClone
->setTime(cal
.getTime(status
),status
);
2205 if (U_FAILURE(status
)) {
2208 calClone
->setTimeZone(cal
.getTimeZone());
2211 status
= U_MEMORY_ALLOCATION_ERROR
;
2216 if (fSymbols
->fLeapMonthPatterns
!= NULL
&& fSymbols
->fLeapMonthPatternsCount
>= DateFormatSymbols::kMonthPatternsCount
) {
2217 numericLeapMonthFormatter
= new MessageFormat(fSymbols
->fLeapMonthPatterns
[DateFormatSymbols::kLeapMonthPatternNumeric
], fLocale
, status
);
2218 if (numericLeapMonthFormatter
== NULL
) {
2219 status
= U_MEMORY_ALLOCATION_ERROR
;
2221 } else if (U_FAILURE(status
)) {
2222 goto ExitParse
; // this will delete numericLeapMonthFormatter
2226 for (int32_t i
=0; i
<fPattern
.length(); ++i
) {
2227 UChar ch
= fPattern
.charAt(i
);
2229 // Handle alphabetic field characters.
2230 if (!inQuote
&& isSyntaxChar(ch
)) {
2231 int32_t fieldPat
= i
;
2233 // Count the length of this field specifier
2235 while ((i
+1)<fPattern
.length() &&
2236 fPattern
.charAt(i
+1) == ch
) {
2241 if (isNumeric(ch
, count
)) {
2243 // Determine if there is an abutting numeric field.
2244 // Record the start of a set of abutting numeric fields.
2245 if (isAtNumericField(fPattern
, i
+ 1)) {
2252 abutPat
= -1; // End of any abutting fields
2255 // Handle fields within a run of abutting numeric fields. Take
2256 // the pattern "HHmmss" as an example. We will try to parse
2257 // 2/2/2 characters of the input text, then if that fails,
2258 // 1/2/2. We only adjust the width of the leftmost field; the
2259 // others remain fixed. This allows "123456" => 12:34:56, but
2260 // "12345" => 1:23:45. Likewise, for the pattern "yyyyMMdd" we
2261 // try 4/2/2, 3/2/2, 2/2/2, and finally 1/2/2.
2263 // If we are at the start of a run of abutting fields, then
2264 // shorten this field in each pass. If we can't shorten
2265 // this field any more, then the parse of this set of
2266 // abutting numeric fields has failed.
2267 if (fieldPat
== abutPat
) {
2268 count
-= abutPass
++;
2270 status
= U_PARSE_ERROR
;
2275 pos
= subParse(text
, pos
, ch
, count
,
2276 TRUE
, FALSE
, ambiguousYear
, saveHebrewMonth
, *workCal
, i
, numericLeapMonthFormatter
, &tzTimeType
, mutableNFs
);
2278 // If the parse fails anywhere in the run, back up to the
2279 // start of the run and retry.
2287 // Handle non-numeric fields and non-abutting numeric
2289 else if (ch
!= 0x6C) { // pattern char 'l' (SMALL LETTER L) just gets ignored
2290 int32_t s
= subParse(text
, pos
, ch
, count
,
2291 FALSE
, TRUE
, ambiguousYear
, saveHebrewMonth
, *workCal
, i
, numericLeapMonthFormatter
, &tzTimeType
, mutableNFs
, &dayPeriodInt
);
2294 // era not present, in special cases allow this to continue
2295 // from the position where the era was expected
2298 if (i
+1 < fPattern
.length()) {
2299 // move to next pattern character
2300 UChar ch
= fPattern
.charAt(i
+1);
2302 // check for whitespace
2303 if (PatternProps::isWhiteSpace(ch
)) {
2305 // Advance over run in pattern
2306 while ((i
+1)<fPattern
.length() &&
2307 PatternProps::isWhiteSpace(fPattern
.charAt(i
+1))) {
2314 status
= U_PARSE_ERROR
;
2321 // Handle literal pattern characters. These are any
2322 // quoted characters and non-alphabetic unquoted
2326 abutPat
= -1; // End of any abutting fields
2328 if (! matchLiterals(fPattern
, i
, text
, pos
, getBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE
, status
), getBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH
, status
), isLenient())) {
2329 status
= U_PARSE_ERROR
;
2335 // Special hack for trailing "." after non-numeric field.
2336 if (text
.charAt(pos
) == 0x2e && getBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE
, status
)) {
2337 // only do if the last field is not numeric
2338 if (isAfterNonNumericField(fPattern
, fPattern
.length())) {
2339 pos
++; // skip the extra "."
2343 // If dayPeriod is set, use it in conjunction with hour-of-day to determine am/pm.
2344 if (dayPeriodInt
>= 0) {
2345 DayPeriodRules::DayPeriod dayPeriod
= (DayPeriodRules::DayPeriod
)dayPeriodInt
;
2346 const DayPeriodRules
*ruleSet
= DayPeriodRules::getInstance(this->getSmpFmtLocale(), status
);
2348 if (!cal
.isSet(UCAL_HOUR
) && !cal
.isSet(UCAL_HOUR_OF_DAY
)) {
2349 // If hour is not set, set time to the midpoint of current day period, overwriting
2350 // minutes if it's set.
2351 double midPoint
= ruleSet
->getMidPointForDayPeriod(dayPeriod
, status
);
2353 // If we can't get midPoint we do nothing.
2354 if (U_SUCCESS(status
)) {
2355 // Truncate midPoint toward zero to get the hour.
2356 // Any leftover means it was a half-hour.
2357 int32_t midPointHour
= (int32_t) midPoint
;
2358 int32_t midPointMinute
= (midPoint
- midPointHour
) > 0 ? 30 : 0;
2360 // No need to set am/pm because hour-of-day is set last therefore takes precedence.
2361 cal
.set(UCAL_HOUR_OF_DAY
, midPointHour
);
2362 cal
.set(UCAL_MINUTE
, midPointMinute
);
2367 if (cal
.isSet(UCAL_HOUR_OF_DAY
)) { // Hour is parsed in 24-hour format.
2368 hourOfDay
= cal
.get(UCAL_HOUR_OF_DAY
, status
);
2369 } else { // Hour is parsed in 12-hour format.
2370 hourOfDay
= cal
.get(UCAL_HOUR
, status
);
2371 // cal.get() turns 12 to 0 for 12-hour time; change 0 to 12
2372 // so 0 unambiguously means a 24-hour time from above.
2373 if (hourOfDay
== 0) { hourOfDay
= 12; }
2375 U_ASSERT(0 <= hourOfDay
&& hourOfDay
<= 23);
2378 // If hour-of-day is 0 or 13 thru 23 then input time in unambiguously in 24-hour format.
2379 if (hourOfDay
== 0 || (13 <= hourOfDay
&& hourOfDay
<= 23)) {
2380 // Make hour-of-day take precedence over (hour + am/pm) by setting it again.
2381 cal
.set(UCAL_HOUR_OF_DAY
, hourOfDay
);
2383 // We have a 12-hour time and need to choose between am and pm.
2384 // Behave as if dayPeriod spanned 6 hours each way from its center point.
2385 // This will parse correctly for consistent time + period (e.g. 10 at night) as
2386 // well as provide a reasonable recovery for inconsistent time + period (e.g.
2387 // 9 in the afternoon).
2389 // Assume current time is in the AM.
2390 // - Change 12 back to 0 for easier handling of 12am.
2391 // - Append minutes as fractional hours because e.g. 8:15 and 8:45 could be parsed
2392 // into different half-days if center of dayPeriod is at 14:30.
2393 // - cal.get(MINUTE) will return 0 if MINUTE is unset, which works.
2394 if (hourOfDay
== 12) { hourOfDay
= 0; }
2395 double currentHour
= hourOfDay
+ (cal
.get(UCAL_MINUTE
, status
)) / 60.0;
2396 double midPointHour
= ruleSet
->getMidPointForDayPeriod(dayPeriod
, status
);
2398 if (U_SUCCESS(status
)) {
2399 double hoursAheadMidPoint
= currentHour
- midPointHour
;
2401 // Assume current time is in the AM.
2402 if (-6 <= hoursAheadMidPoint
&& hoursAheadMidPoint
< 6) {
2403 // Assumption holds; set time as such.
2404 cal
.set(UCAL_AM_PM
, 0);
2406 cal
.set(UCAL_AM_PM
, 1);
2413 // At this point the fields of Calendar have been set. Calendar
2414 // will fill in default values for missing fields when the time
2417 parsePos
.setIndex(pos
);
2419 // This part is a problem: When we call parsedDate.after, we compute the time.
2420 // Take the date April 3 2004 at 2:30 am. When this is first set up, the year
2421 // will be wrong if we're parsing a 2-digit year pattern. It will be 1904.
2422 // April 3 1904 is a Sunday (unlike 2004) so it is the DST onset day. 2:30 am
2423 // is therefore an "impossible" time, since the time goes from 1:59 to 3:00 am
2424 // on that day. It is therefore parsed out to fields as 3:30 am. Then we
2425 // add 100 years, and get April 3 2004 at 3:30 am. Note that April 3 2004 is
2426 // a Saturday, so it can have a 2:30 am -- and it should. [LIU]
2428 UDate parsedDate = calendar.getTime();
2429 if( ambiguousYear[0] && !parsedDate.after(fDefaultCenturyStart) ) {
2430 calendar.add(Calendar.YEAR, 100);
2431 parsedDate = calendar.getTime();
2434 // Because of the above condition, save off the fields in case we need to readjust.
2435 // The procedure we use here is not particularly efficient, but there is no other
2436 // way to do this given the API restrictions present in Calendar. We minimize
2437 // inefficiency by only performing this computation when it might apply, that is,
2438 // when the two-digit year is equal to the start year, and thus might fall at the
2439 // front or the back of the default century. This only works because we adjust
2440 // the year correctly to start with in other cases -- see subParse().
2441 if (ambiguousYear
[0] || tzTimeType
!= UTZFMT_TIME_TYPE_UNKNOWN
) // If this is true then the two-digit year == the default start year
2443 // We need a copy of the fields, and we need to avoid triggering a call to
2444 // complete(), which will recalculate the fields. Since we can't access
2445 // the fields[] array in Calendar, we clone the entire object. This will
2446 // stop working if Calendar.clone() is ever rewritten to call complete().
2448 if (ambiguousYear
[0]) {
2450 // Check for failed cloning.
2452 status
= U_MEMORY_ALLOCATION_ERROR
;
2455 UDate parsedDate
= copy
->getTime(status
);
2456 // {sfb} check internalGetDefaultCenturyStart
2457 if (fHaveDefaultCentury
&& (parsedDate
< fDefaultCenturyStart
)) {
2458 // We can't use add here because that does a complete() first.
2459 cal
.set(UCAL_YEAR
, fDefaultCenturyStartYear
+ 100);
2464 if (tzTimeType
!= UTZFMT_TIME_TYPE_UNKNOWN
) {
2466 // Check for failed cloning.
2468 status
= U_MEMORY_ALLOCATION_ERROR
;
2471 const TimeZone
& tz
= cal
.getTimeZone();
2472 BasicTimeZone
*btz
= NULL
;
2474 if (dynamic_cast<const OlsonTimeZone
*>(&tz
) != NULL
2475 || dynamic_cast<const SimpleTimeZone
*>(&tz
) != NULL
2476 || dynamic_cast<const RuleBasedTimeZone
*>(&tz
) != NULL
2477 || dynamic_cast<const VTimeZone
*>(&tz
) != NULL
) {
2478 btz
= (BasicTimeZone
*)&tz
;
2482 copy
->set(UCAL_ZONE_OFFSET
, 0);
2483 copy
->set(UCAL_DST_OFFSET
, 0);
2484 UDate localMillis
= copy
->getTime(status
);
2486 // Make sure parsed time zone type (Standard or Daylight)
2487 // matches the rule used by the parsed time zone.
2490 if (tzTimeType
== UTZFMT_TIME_TYPE_STANDARD
) {
2491 btz
->getOffsetFromLocal(localMillis
,
2492 BasicTimeZone::kStandard
, BasicTimeZone::kStandard
, raw
, dst
, status
);
2494 btz
->getOffsetFromLocal(localMillis
,
2495 BasicTimeZone::kDaylight
, BasicTimeZone::kDaylight
, raw
, dst
, status
);
2498 // No good way to resolve ambiguous time at transition,
2499 // but following code work in most case.
2500 tz
.getOffset(localMillis
, TRUE
, raw
, dst
, status
);
2503 // Now, compare the results with parsed type, either standard or daylight saving time
2504 int32_t resolvedSavings
= dst
;
2505 if (tzTimeType
== UTZFMT_TIME_TYPE_STANDARD
) {
2507 // Override DST_OFFSET = 0 in the result calendar
2508 resolvedSavings
= 0;
2510 } else { // tztype == TZTYPE_DST
2513 UDate time
= localMillis
+ raw
;
2514 // We use the nearest daylight saving time rule.
2515 TimeZoneTransition beforeTrs
, afterTrs
;
2516 UDate beforeT
= time
, afterT
= time
;
2517 int32_t beforeSav
= 0, afterSav
= 0;
2518 UBool beforeTrsAvail
, afterTrsAvail
;
2520 // Search for DST rule before or on the time
2522 beforeTrsAvail
= btz
->getPreviousTransition(beforeT
, TRUE
, beforeTrs
);
2523 if (!beforeTrsAvail
) {
2526 beforeT
= beforeTrs
.getTime() - 1;
2527 beforeSav
= beforeTrs
.getFrom()->getDSTSavings();
2528 if (beforeSav
!= 0) {
2533 // Search for DST rule after the time
2535 afterTrsAvail
= btz
->getNextTransition(afterT
, FALSE
, afterTrs
);
2536 if (!afterTrsAvail
) {
2539 afterT
= afterTrs
.getTime();
2540 afterSav
= afterTrs
.getTo()->getDSTSavings();
2541 if (afterSav
!= 0) {
2546 if (beforeTrsAvail
&& afterTrsAvail
) {
2547 if (time
- beforeT
> afterT
- time
) {
2548 resolvedSavings
= afterSav
;
2550 resolvedSavings
= beforeSav
;
2552 } else if (beforeTrsAvail
&& beforeSav
!= 0) {
2553 resolvedSavings
= beforeSav
;
2554 } else if (afterTrsAvail
&& afterSav
!= 0) {
2555 resolvedSavings
= afterSav
;
2557 resolvedSavings
= btz
->getDSTSavings();
2560 resolvedSavings
= tz
.getDSTSavings();
2562 if (resolvedSavings
== 0) {
2564 resolvedSavings
= U_MILLIS_PER_HOUR
;
2568 cal
.set(UCAL_ZONE_OFFSET
, raw
);
2569 cal
.set(UCAL_DST_OFFSET
, resolvedSavings
);
2574 // Set the parsed result if local calendar is used
2575 // instead of the input calendar
2576 if (U_SUCCESS(status
) && workCal
!= &cal
) {
2577 cal
.setTimeZone(workCal
->getTimeZone());
2578 cal
.setTime(workCal
->getTime(status
), status
);
2581 if (numericLeapMonthFormatter
!= NULL
) {
2582 delete numericLeapMonthFormatter
;
2584 if (calClone
!= NULL
) {
2588 // If any Calendar calls failed, we pretend that we
2589 // couldn't parse the string, when in reality this isn't quite accurate--
2590 // we did parse it; the Calendar calls just failed.
2591 if (U_FAILURE(status
)) {
2592 parsePos
.setErrorIndex(pos
);
2593 parsePos
.setIndex(start
);
2597 //----------------------------------------------------------------------
2600 matchStringWithOptionalDot(const UnicodeString
&text
,
2602 const UnicodeString
&data
);
2604 int32_t SimpleDateFormat::matchQuarterString(const UnicodeString
& text
,
2606 UCalendarDateFields field
,
2607 const UnicodeString
* data
,
2609 Calendar
& cal
) const
2612 int32_t count
= dataCount
;
2614 // There may be multiple strings in the data[] array which begin with
2615 // the same prefix (e.g., Cerven and Cervenec (June and July) in Czech).
2616 // We keep track of the longest match, and return that. Note that this
2617 // unfortunately requires us to test all array elements.
2618 int32_t bestMatchLength
= 0, bestMatch
= -1;
2619 UnicodeString bestMatchName
;
2621 for (; i
< count
; ++i
) {
2622 int32_t matchLength
= 0;
2623 if ((matchLength
= matchStringWithOptionalDot(text
, start
, data
[i
])) > bestMatchLength
) {
2624 bestMatchLength
= matchLength
;
2629 if (bestMatch
>= 0) {
2630 cal
.set(field
, bestMatch
* 3);
2631 return start
+ bestMatchLength
;
2637 int32_t SimpleDateFormat::matchDayPeriodStrings(const UnicodeString
& text
, int32_t start
,
2638 const UnicodeString
* data
, int32_t dataCount
,
2639 int32_t &dayPeriod
) const
2642 int32_t bestMatchLength
= 0, bestMatch
= -1;
2644 for (int32_t i
= 0; i
< dataCount
; ++i
) {
2645 int32_t matchLength
= 0;
2646 if ((matchLength
= matchStringWithOptionalDot(text
, start
, data
[i
])) > bestMatchLength
) {
2647 bestMatchLength
= matchLength
;
2652 if (bestMatch
>= 0) {
2653 dayPeriod
= bestMatch
;
2654 return start
+ bestMatchLength
;
2660 //----------------------------------------------------------------------
2661 #define IS_BIDI_MARK(c) (c==0x200E || c==0x200F || c==0x061C)
2663 UBool
SimpleDateFormat::matchLiterals(const UnicodeString
&pattern
,
2664 int32_t &patternOffset
,
2665 const UnicodeString
&text
,
2666 int32_t &textOffset
,
2667 UBool whitespaceLenient
,
2668 UBool partialMatchLenient
,
2671 UBool inQuote
= FALSE
;
2672 UnicodeString literal
;
2673 int32_t i
= patternOffset
;
2675 // scan pattern looking for contiguous literal characters
2676 for ( ; i
< pattern
.length(); i
+= 1) {
2677 UChar ch
= pattern
.charAt(i
);
2679 if (!inQuote
&& isSyntaxChar(ch
)) {
2684 // Match a quote literal ('') inside OR outside of quotes
2685 if ((i
+ 1) < pattern
.length() && pattern
.charAt(i
+ 1) == QUOTE
) {
2693 if (!IS_BIDI_MARK(ch
)) {
2698 // at this point, literal contains the pattern literal text (without bidi marks)
2699 // and i is the index of the next non-literal pattern character.
2701 int32_t t
= textOffset
;
2703 if (whitespaceLenient
) {
2704 // trim leading, trailing whitespace from the pattern literal
2707 // ignore any leading whitespace (or bidi marks) in the text
2708 while (t
< text
.length()) {
2709 UChar ch
= text
.charAt(t
);
2710 if (!u_isWhitespace(ch
) && !IS_BIDI_MARK(ch
)) {
2717 // Get ignorables, move up here
2718 const UnicodeSet
*ignorables
= NULL
;
2719 UDateFormatField patternCharIndex
= DateFormatSymbols::getPatternCharIndex(pattern
.charAt(i
));
2720 if (patternCharIndex
!= UDAT_FIELD_COUNT
) {
2721 ignorables
= SimpleDateFormatStaticSets::getIgnorables(patternCharIndex
);
2724 for (p
= 0; p
< literal
.length() && t
< text
.length();) {
2725 UBool needWhitespace
= FALSE
;
2727 // Skip any whitespace at current position in pattern,
2728 // but remember whether we found whitespace in the pattern
2729 // (we already deleted any bidi marks in the pattern).
2730 while (p
< literal
.length() && PatternProps::isWhiteSpace(literal
.charAt(p
))) {
2731 needWhitespace
= TRUE
;
2735 // If the pattern has whitespace at this point, skip it in text as well
2736 // (if the text does not have any, that may be an error for strict parsing)
2737 if (needWhitespace
) {
2738 UBool whitespaceInText
= FALSE
;
2740 // Skip any whitespace (or bidi marks) at current position in text,
2741 // but remember whether we found whitespace in the text at this point.
2742 while (t
< text
.length()) {
2743 UChar tch
= text
.charAt(t
);
2744 if (u_isUWhiteSpace(tch
) || PatternProps::isWhiteSpace(tch
)) {
2745 whitespaceInText
= TRUE
;
2746 } else if (!IS_BIDI_MARK(tch
)) {
2753 // TODO: should we require internal spaces
2754 // in lenient mode? (There won't be any
2755 // leading or trailing spaces)
2756 if (!whitespaceLenient
&& !whitespaceInText
) {
2757 // didn't find matching whitespace:
2758 // an error in strict mode
2762 // In strict mode, this run of whitespace
2763 // may have been at the end.
2764 if (p
>= literal
.length()) {
2768 // Still need to skip any bidi marks in the text
2769 while (t
< text
.length() && IS_BIDI_MARK(text
.charAt(t
))) {
2773 if (t
>= text
.length() || literal
.charAt(p
) != text
.charAt(t
)) {
2774 // Ran out of text, or found a non-matching character:
2775 // OK in lenient mode, an error in strict mode.
2776 if (whitespaceLenient
) {
2777 if (t
== textOffset
&& text
.charAt(t
) == 0x2e &&
2778 isAfterNonNumericField(pattern
, patternOffset
)) {
2779 // Lenient mode and the literal input text begins with a "." and
2780 // we are after a non-numeric field: We skip the "."
2782 continue; // Do not update p.
2784 // if it is actual whitespace and we're whitespace lenient it's OK
2786 UChar wsc
= text
.charAt(t
);
2787 if(PatternProps::isWhiteSpace(wsc
)) {
2788 // Lenient mode and it's just whitespace we skip it
2790 continue; // Do not update p.
2793 // hack around oldleniency being a bit of a catch-all bucket and we're just adding support specifically for paritial matches
2794 // This fix is for http://bugs.icu-project.org/trac/ticket/10855 and adds "&& oldLeniency"
2795 //if(partialMatchLenient && oldLeniency) {
2796 // However this causes problems for Apple, see <rdar://problem/20692829> regressions in Chinese date parsing
2797 // We don't want to go back to just "if(partialMatchLenient)" as in ICU 53, that is too lenient for strict mode.
2798 // So if the pattern character is in the separator set, we allow the text character to be in that set or be an alpha char.
2799 if( partialMatchLenient
&& ( oldLeniency
||
2800 ( ignorables
!= NULL
&& ignorables
->contains(literal
.charAt(p
)) && (ignorables
->contains(text
.charAt(t
)) || u_isalpha(text
.charAt(t
))) ) )
2811 // At this point if we're in strict mode we have a complete match.
2812 // If we're in lenient mode we may have a partial match, or no
2815 // no match. Pretend it matched a run of whitespace
2816 // and ignorables in the text.
2818 for (t
= textOffset
; t
< text
.length(); t
+= 1) {
2819 UChar ch
= text
.charAt(t
);
2821 if (!IS_BIDI_MARK(ch
) && (ignorables
== NULL
|| !ignorables
->contains(ch
))) {
2827 // if we get here, we've got a complete match.
2828 patternOffset
= i
- 1;
2834 //----------------------------------------------------------------------
2836 int32_t SimpleDateFormat::matchString(const UnicodeString
& text
,
2838 UCalendarDateFields field
,
2839 const UnicodeString
* data
,
2841 const UnicodeString
* monthPattern
,
2842 Calendar
& cal
) const
2845 int32_t count
= dataCount
;
2847 if (field
== UCAL_DAY_OF_WEEK
) i
= 1;
2849 // There may be multiple strings in the data[] array which begin with
2850 // the same prefix (e.g., Cerven and Cervenec (June and July) in Czech).
2851 // We keep track of the longest match, and return that. Note that this
2852 // unfortunately requires us to test all array elements.
2853 int32_t bestMatchLength
= 0, bestMatch
= -1;
2854 UnicodeString bestMatchName
;
2855 int32_t isLeapMonth
= 0;
2857 for (; i
< count
; ++i
) {
2858 int32_t matchLen
= 0;
2859 if ((matchLen
= matchStringWithOptionalDot(text
, start
, data
[i
])) > bestMatchLength
) {
2861 bestMatchLength
= matchLen
;
2864 if (monthPattern
!= NULL
) {
2865 UErrorCode status
= U_ZERO_ERROR
;
2866 UnicodeString leapMonthName
;
2867 SimpleFormatter(*monthPattern
, 1, 1, status
).format(data
[i
], leapMonthName
, status
);
2868 if (U_SUCCESS(status
)) {
2869 if ((matchLen
= matchStringWithOptionalDot(text
, start
, leapMonthName
)) > bestMatchLength
) {
2871 bestMatchLength
= matchLen
;
2878 if (bestMatch
>= 0) {
2879 if (field
< UCAL_FIELD_COUNT
) {
2880 // Adjustment for Hebrew Calendar month Adar II
2881 if (!strcmp(cal
.getType(),"hebrew") && field
==UCAL_MONTH
&& bestMatch
==13) {
2884 if (field
== UCAL_YEAR
) {
2885 bestMatch
++; // only get here for cyclic year names, which match 1-based years 1-60
2887 cal
.set(field
, bestMatch
);
2889 if (monthPattern
!= NULL
) {
2890 cal
.set(UCAL_IS_LEAP_MONTH
, isLeapMonth
);
2894 return start
+ bestMatchLength
;
2901 matchStringWithOptionalDot(const UnicodeString
&text
,
2903 const UnicodeString
&data
) {
2904 UErrorCode sts
= U_ZERO_ERROR
;
2905 int32_t matchLenText
= 0;
2906 int32_t matchLenData
= 0;
2908 u_caseInsensitivePrefixMatch(text
.getBuffer() + index
, text
.length() - index
,
2909 data
.getBuffer(), data
.length(),
2910 0 /* default case option */,
2911 &matchLenText
, &matchLenData
,
2913 U_ASSERT (U_SUCCESS(sts
));
2915 if (matchLenData
== data
.length() /* normal match */
2916 || (data
.charAt(data
.length() - 1) == 0x2e
2917 && matchLenData
== data
.length() - 1 /* match without trailing dot */)) {
2918 return matchLenText
;
2924 //----------------------------------------------------------------------
2927 SimpleDateFormat::set2DigitYearStart(UDate d
, UErrorCode
& status
)
2929 parseAmbiguousDatesAsAfter(d
, status
);
2933 * Private member function that converts the parsed date strings into
2934 * timeFields. Returns -start (for ParsePosition) if failed.
2936 int32_t SimpleDateFormat::subParse(const UnicodeString
& text
, int32_t& start
, UChar ch
, int32_t count
,
2937 UBool obeyCount
, UBool allowNegative
, UBool ambiguousYear
[], int32_t& saveHebrewMonth
, Calendar
& cal
,
2938 int32_t patLoc
, MessageFormat
* numericLeapMonthFormatter
, UTimeZoneFormatTimeType
*tzTimeType
, SimpleDateFormatMutableNFs
&mutableNFs
,
2939 int32_t *dayPeriod
) const
2945 UErrorCode status
= U_ZERO_ERROR
;
2946 ParsePosition
pos(0);
2947 UDateFormatField patternCharIndex
= DateFormatSymbols::getPatternCharIndex(ch
);
2948 NumberFormat
*currentNumberFormat
;
2950 int32_t tzParseOptions
= (isLenient())? UTZFMT_PARSE_OPTION_ALL_STYLES
: UTZFMT_PARSE_OPTION_NONE
;
2951 UBool gotNumber
= FALSE
;
2953 #if defined (U_DEBUG_CAL)
2954 //fprintf(stderr, "%s:%d - [%c] st=%d \n", __FILE__, __LINE__, (char) ch, start);
2957 if (patternCharIndex
== UDAT_FIELD_COUNT
) {
2961 currentNumberFormat
= mutableNFs
.get(getNumberFormatByIndex(patternCharIndex
));
2962 if (currentNumberFormat
== NULL
) {
2965 UCalendarDateFields field
= fgPatternIndexToCalendarField
[patternCharIndex
]; // UCAL_FIELD_COUNT if irrelevant
2966 UnicodeString
hebr("hebr", 4, US_INV
);
2968 if (numericLeapMonthFormatter
!= NULL
) {
2969 numericLeapMonthFormatter
->setFormats((const Format
**)¤tNumberFormat
, 1);
2971 UBool isChineseCalendar
= (uprv_strcmp(cal
.getType(),"chinese") == 0 || uprv_strcmp(cal
.getType(),"dangi") == 0);
2973 // If there are any spaces here, skip over them. If we hit the end
2974 // of the string, then fail.
2976 if (start
>= text
.length()) {
2979 UChar32 c
= text
.char32At(start
);
2980 if (!u_isUWhiteSpace(c
) /*||*/ && !PatternProps::isWhiteSpace(c
)) {
2983 start
+= U16_LENGTH(c
);
2985 pos
.setIndex(start
);
2987 // We handle a few special cases here where we need to parse
2988 // a number value. We handle further, more generic cases below. We need
2989 // to handle some of them here because some fields require extra processing on
2990 // the parsed value.
2991 if (patternCharIndex
== UDAT_HOUR_OF_DAY1_FIELD
|| // k
2992 patternCharIndex
== UDAT_HOUR_OF_DAY0_FIELD
|| // H
2993 patternCharIndex
== UDAT_HOUR1_FIELD
|| // h
2994 patternCharIndex
== UDAT_HOUR0_FIELD
|| // K
2995 (patternCharIndex
== UDAT_DOW_LOCAL_FIELD
&& count
<= 2) || // e
2996 (patternCharIndex
== UDAT_STANDALONE_DAY_FIELD
&& count
<= 2) || // c
2997 (patternCharIndex
== UDAT_MONTH_FIELD
&& count
<= 2) || // M
2998 (patternCharIndex
== UDAT_STANDALONE_MONTH_FIELD
&& count
<= 2) || // L
2999 (patternCharIndex
== UDAT_QUARTER_FIELD
&& count
<= 2) || // Q
3000 (patternCharIndex
== UDAT_STANDALONE_QUARTER_FIELD
&& count
<= 2) || // q
3001 patternCharIndex
== UDAT_YEAR_FIELD
|| // y
3002 patternCharIndex
== UDAT_YEAR_WOY_FIELD
|| // Y
3003 patternCharIndex
== UDAT_YEAR_NAME_FIELD
|| // U (falls back to numeric)
3004 (patternCharIndex
== UDAT_ERA_FIELD
&& isChineseCalendar
) || // G
3005 patternCharIndex
== UDAT_FRACTIONAL_SECOND_FIELD
) // S
3007 int32_t parseStart
= pos
.getIndex();
3008 // It would be good to unify this with the obeyCount logic below,
3009 // but that's going to be difficult.
3010 const UnicodeString
* src
;
3012 UBool parsedNumericLeapMonth
= FALSE
;
3013 if (numericLeapMonthFormatter
!= NULL
&& (patternCharIndex
== UDAT_MONTH_FIELD
|| patternCharIndex
== UDAT_STANDALONE_MONTH_FIELD
)) {
3015 Formattable
* args
= numericLeapMonthFormatter
->parse(text
, pos
, argCount
);
3016 if (args
!= NULL
&& argCount
== 1 && pos
.getIndex() > parseStart
&& args
[0].isNumeric()) {
3017 parsedNumericLeapMonth
= TRUE
;
3018 number
.setLong(args
[0].getLong());
3019 cal
.set(UCAL_IS_LEAP_MONTH
, 1);
3022 pos
.setIndex(parseStart
);
3023 cal
.set(UCAL_IS_LEAP_MONTH
, 0);
3027 if (!parsedNumericLeapMonth
) {
3029 if ((start
+count
) > text
.length()) {
3033 text
.extractBetween(0, start
+ count
, temp
);
3039 parseInt(*src
, number
, pos
, allowNegative
,currentNumberFormat
);
3042 int32_t txtLoc
= pos
.getIndex();
3044 if (txtLoc
> parseStart
) {
3045 value
= number
.getLong();
3048 // suffix processing
3050 txtLoc
= checkIntSuffix(text
, txtLoc
, patLoc
+1, TRUE
);
3051 if (txtLoc
!= pos
.getIndex()) {
3056 txtLoc
= checkIntSuffix(text
, txtLoc
, patLoc
+1, FALSE
);
3059 // Check the range of the value
3060 if (!getBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE
, status
)) {
3061 int32_t bias
= gFieldRangeBias
[patternCharIndex
];
3062 if (bias
>= 0 && (value
> cal
.getMaximum(field
) + bias
|| value
< cal
.getMinimum(field
) + bias
)) {
3066 int32_t bias
= gFieldRangeBiasLenient
[patternCharIndex
];
3067 if (bias
>= 0 && (value
> cal
.getMaximum(field
) + bias
)) {
3072 pos
.setIndex(txtLoc
);
3076 // Make sure that we got a number if
3077 // we want one, and didn't get one
3078 // if we don't want one.
3079 switch (patternCharIndex
) {
3080 case UDAT_HOUR_OF_DAY1_FIELD
:
3081 case UDAT_HOUR_OF_DAY0_FIELD
:
3082 case UDAT_HOUR1_FIELD
:
3083 case UDAT_HOUR0_FIELD
:
3084 // special range check for hours:
3085 if (value
< 0 || value
> 24) {
3089 // fall through to gotNumber check
3091 case UDAT_YEAR_FIELD
:
3092 case UDAT_YEAR_WOY_FIELD
:
3093 case UDAT_FRACTIONAL_SECOND_FIELD
:
3094 // these must be a number
3102 // we check the rest of the fields below.
3106 switch (patternCharIndex
) {
3107 case UDAT_ERA_FIELD
:
3108 if (isChineseCalendar
) {
3112 cal
.set(UCAL_ERA
, value
);
3113 return pos
.getIndex();
3116 ps
= matchString(text
, start
, UCAL_ERA
, fSymbols
->fNarrowEras
, fSymbols
->fNarrowErasCount
, NULL
, cal
);
3117 } else if (count
== 4) {
3118 ps
= matchString(text
, start
, UCAL_ERA
, fSymbols
->fEraNames
, fSymbols
->fEraNamesCount
, NULL
, cal
);
3120 ps
= matchString(text
, start
, UCAL_ERA
, fSymbols
->fEras
, fSymbols
->fErasCount
, NULL
, cal
);
3123 // check return position, if it equals -start, then matchString error
3124 // special case the return code so we don't necessarily fail out until we
3125 // verify no year information also
3131 case UDAT_YEAR_FIELD
:
3132 // If there are 3 or more YEAR pattern characters, this indicates
3133 // that the year value is to be treated literally, without any
3134 // two-digit year adjustments (e.g., from "01" to 2001). Otherwise
3135 // we made adjustments to place the 2-digit year in the proper
3136 // century, for parsed strings from "00" to "99". Any other string
3137 // is treated literally: "2250", "-1", "1", "002".
3138 if (fDateOverride
.compare(hebr
)==0 && value
< 1000) {
3139 value
+= HEBREW_CAL_CUR_MILLENIUM_START_YEAR
;
3140 } else if ((pos
.getIndex() - start
) == 2 && !isChineseCalendar
3141 && u_isdigit(text
.charAt(start
))
3142 && u_isdigit(text
.charAt(start
+1)))
3144 // only adjust year for patterns less than 3.
3146 // Assume for example that the defaultCenturyStart is 6/18/1903.
3147 // This means that two-digit years will be forced into the range
3148 // 6/18/1903 to 6/17/2003. As a result, years 00, 01, and 02
3149 // correspond to 2000, 2001, and 2002. Years 04, 05, etc. correspond
3150 // to 1904, 1905, etc. If the year is 03, then it is 2003 if the
3151 // other fields specify a date before 6/18, or 1903 if they specify a
3152 // date afterwards. As a result, 03 is an ambiguous year. All other
3153 // two-digit years are unambiguous.
3154 if(fHaveDefaultCentury
) { // check if this formatter even has a pivot year
3155 int32_t ambiguousTwoDigitYear
= fDefaultCenturyStartYear
% 100;
3156 ambiguousYear
[0] = (value
== ambiguousTwoDigitYear
);
3157 value
+= (fDefaultCenturyStartYear
/100)*100 +
3158 (value
< ambiguousTwoDigitYear
? 100 : 0);
3162 cal
.set(UCAL_YEAR
, value
);
3164 // Delayed checking for adjustment of Hebrew month numbers in non-leap years.
3165 if (saveHebrewMonth
>= 0) {
3166 HebrewCalendar
*hc
= (HebrewCalendar
*)&cal
;
3167 if (!hc
->isLeapYear(value
) && saveHebrewMonth
>= 6) {
3168 cal
.set(UCAL_MONTH
,saveHebrewMonth
);
3170 cal
.set(UCAL_MONTH
,saveHebrewMonth
-1);
3172 saveHebrewMonth
= -1;
3174 return pos
.getIndex();
3176 case UDAT_YEAR_WOY_FIELD
:
3177 // Comment is the same as for UDAT_Year_FIELDs - look above
3178 if (fDateOverride
.compare(hebr
)==0 && value
< 1000) {
3179 value
+= HEBREW_CAL_CUR_MILLENIUM_START_YEAR
;
3180 } else if ((pos
.getIndex() - start
) == 2
3181 && u_isdigit(text
.charAt(start
))
3182 && u_isdigit(text
.charAt(start
+1))
3183 && fHaveDefaultCentury
)
3185 int32_t ambiguousTwoDigitYear
= fDefaultCenturyStartYear
% 100;
3186 ambiguousYear
[0] = (value
== ambiguousTwoDigitYear
);
3187 value
+= (fDefaultCenturyStartYear
/100)*100 +
3188 (value
< ambiguousTwoDigitYear
? 100 : 0);
3190 cal
.set(UCAL_YEAR_WOY
, value
);
3191 return pos
.getIndex();
3193 case UDAT_YEAR_NAME_FIELD
:
3194 if (fSymbols
->fShortYearNames
!= NULL
) {
3195 int32_t newStart
= matchString(text
, start
, UCAL_YEAR
, fSymbols
->fShortYearNames
, fSymbols
->fShortYearNamesCount
, NULL
, cal
);
3200 if (gotNumber
&& (getBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC
,status
) || value
> fSymbols
->fShortYearNamesCount
)) {
3201 cal
.set(UCAL_YEAR
, value
);
3202 return pos
.getIndex();
3206 case UDAT_MONTH_FIELD
:
3207 case UDAT_STANDALONE_MONTH_FIELD
:
3208 if (gotNumber
) // i.e., M or MM.
3210 // When parsing month numbers from the Hebrew Calendar, we might need to adjust the month depending on whether
3211 // 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
3212 // the year is parsed.
3213 if (!strcmp(cal
.getType(),"hebrew")) {
3214 HebrewCalendar
*hc
= (HebrewCalendar
*)&cal
;
3215 if (cal
.isSet(UCAL_YEAR
)) {
3216 UErrorCode status
= U_ZERO_ERROR
;
3217 if (!hc
->isLeapYear(hc
->get(UCAL_YEAR
,status
)) && value
>= 6) {
3218 cal
.set(UCAL_MONTH
, value
);
3220 cal
.set(UCAL_MONTH
, value
- 1);
3223 saveHebrewMonth
= value
;
3226 // Don't want to parse the month if it is a string
3227 // while pattern uses numeric style: M/MM, L/LL
3228 // [We computed 'value' above.]
3229 cal
.set(UCAL_MONTH
, value
- 1);
3231 return pos
.getIndex();
3233 // count >= 3 // i.e., MMM/MMMM, LLL/LLLL
3234 // Want to be able to parse both short and long forms.
3235 // Try count == 4 first:
3236 UnicodeString
* wideMonthPat
= NULL
;
3237 UnicodeString
* shortMonthPat
= NULL
;
3238 if (fSymbols
->fLeapMonthPatterns
!= NULL
&& fSymbols
->fLeapMonthPatternsCount
>= DateFormatSymbols::kMonthPatternsCount
) {
3239 if (patternCharIndex
==UDAT_MONTH_FIELD
) {
3240 wideMonthPat
= &fSymbols
->fLeapMonthPatterns
[DateFormatSymbols::kLeapMonthPatternFormatWide
];
3241 shortMonthPat
= &fSymbols
->fLeapMonthPatterns
[DateFormatSymbols::kLeapMonthPatternFormatAbbrev
];
3243 wideMonthPat
= &fSymbols
->fLeapMonthPatterns
[DateFormatSymbols::kLeapMonthPatternStandaloneWide
];
3244 shortMonthPat
= &fSymbols
->fLeapMonthPatterns
[DateFormatSymbols::kLeapMonthPatternStandaloneAbbrev
];
3247 int32_t newStart
= 0;
3248 if (patternCharIndex
==UDAT_MONTH_FIELD
) {
3249 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
== 4) {
3250 newStart
= matchString(text
, start
, UCAL_MONTH
, fSymbols
->fMonths
, fSymbols
->fMonthsCount
, wideMonthPat
, cal
); // try MMMM
3255 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
== 3) {
3256 newStart
= matchString(text
, start
, UCAL_MONTH
, fSymbols
->fShortMonths
, fSymbols
->fShortMonthsCount
, shortMonthPat
, cal
); // try MMM
3259 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
== 4) {
3260 newStart
= matchString(text
, start
, UCAL_MONTH
, fSymbols
->fStandaloneMonths
, fSymbols
->fStandaloneMonthsCount
, wideMonthPat
, cal
); // try LLLL
3265 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
== 3) {
3266 newStart
= matchString(text
, start
, UCAL_MONTH
, fSymbols
->fStandaloneShortMonths
, fSymbols
->fStandaloneShortMonthsCount
, shortMonthPat
, cal
); // try LLL
3269 if (newStart
> 0 || !getBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC
, status
)) // currently we do not try to parse MMMMM/LLLLL: #8860
3271 // else we allowing parsing as number, below
3275 case UDAT_HOUR_OF_DAY1_FIELD
:
3276 // [We computed 'value' above.]
3277 if (value
== cal
.getMaximum(UCAL_HOUR_OF_DAY
) + 1)
3280 // fall through to set field
3282 case UDAT_HOUR_OF_DAY0_FIELD
:
3283 cal
.set(UCAL_HOUR_OF_DAY
, value
);
3284 return pos
.getIndex();
3286 case UDAT_FRACTIONAL_SECOND_FIELD
:
3287 // Fractional seconds left-justify
3288 i
= pos
.getIndex() - start
;
3302 cal
.set(UCAL_MILLISECOND
, value
);
3303 return pos
.getIndex();
3305 case UDAT_DOW_LOCAL_FIELD
:
3306 if (gotNumber
) // i.e., e or ee
3308 // [We computed 'value' above.]
3309 cal
.set(UCAL_DOW_LOCAL
, value
);
3310 return pos
.getIndex();
3312 // else for eee-eeeee fall through to handling of EEE-EEEEE
3313 // fall through, do not break here
3315 case UDAT_DAY_OF_WEEK_FIELD
:
3317 // Want to be able to parse both short and long forms.
3318 // Try count == 4 (EEEE) wide first:
3319 int32_t newStart
= 0;
3320 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
== 4) {
3321 if ((newStart
= matchString(text
, start
, UCAL_DAY_OF_WEEK
,
3322 fSymbols
->fWeekdays
, fSymbols
->fWeekdaysCount
, NULL
, cal
)) > 0)
3325 // EEEE wide failed, now try EEE abbreviated
3326 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
== 3) {
3327 if ((newStart
= matchString(text
, start
, UCAL_DAY_OF_WEEK
,
3328 fSymbols
->fShortWeekdays
, fSymbols
->fShortWeekdaysCount
, NULL
, cal
)) > 0)
3331 // EEE abbreviated failed, now try EEEEEE short
3332 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
== 6) {
3333 if ((newStart
= matchString(text
, start
, UCAL_DAY_OF_WEEK
,
3334 fSymbols
->fShorterWeekdays
, fSymbols
->fShorterWeekdaysCount
, NULL
, cal
)) > 0)
3337 // EEEEEE short failed, now try EEEEE narrow
3338 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
== 5) {
3339 if ((newStart
= matchString(text
, start
, UCAL_DAY_OF_WEEK
,
3340 fSymbols
->fNarrowWeekdays
, fSymbols
->fNarrowWeekdaysCount
, NULL
, cal
)) > 0)
3343 if (!getBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC
, status
) || patternCharIndex
== UDAT_DAY_OF_WEEK_FIELD
)
3345 // else we allowing parsing as number, below
3349 case UDAT_STANDALONE_DAY_FIELD
:
3351 if (gotNumber
) // c or cc
3353 // [We computed 'value' above.]
3354 cal
.set(UCAL_DOW_LOCAL
, value
);
3355 return pos
.getIndex();
3357 // Want to be able to parse both short and long forms.
3358 // Try count == 4 (cccc) first:
3359 int32_t newStart
= 0;
3360 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
== 4) {
3361 if ((newStart
= matchString(text
, start
, UCAL_DAY_OF_WEEK
,
3362 fSymbols
->fStandaloneWeekdays
, fSymbols
->fStandaloneWeekdaysCount
, NULL
, cal
)) > 0)
3365 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
== 3) {
3366 if ((newStart
= matchString(text
, start
, UCAL_DAY_OF_WEEK
,
3367 fSymbols
->fStandaloneShortWeekdays
, fSymbols
->fStandaloneShortWeekdaysCount
, NULL
, cal
)) > 0)
3370 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
== 6) {
3371 if ((newStart
= matchString(text
, start
, UCAL_DAY_OF_WEEK
,
3372 fSymbols
->fStandaloneShorterWeekdays
, fSymbols
->fStandaloneShorterWeekdaysCount
, NULL
, cal
)) > 0)
3375 if (!getBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC
, status
))
3377 // else we allowing parsing as number, below
3381 case UDAT_AM_PM_FIELD
:
3383 // optionally try both wide/abbrev and narrow forms
3384 int32_t newStart
= 0;
3386 if( getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
< 5 ) {
3387 if ((newStart
= matchString(text
, start
, UCAL_AM_PM
, fSymbols
->fAmPms
, fSymbols
->fAmPmsCount
, NULL
, cal
)) > 0) {
3392 if( getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
>= 5 ) {
3393 if ((newStart
= matchString(text
, start
, UCAL_AM_PM
, fSymbols
->fNarrowAmPms
, fSymbols
->fNarrowAmPmsCount
, NULL
, cal
)) > 0) {
3397 // no matches for given options
3401 case UDAT_HOUR1_FIELD
:
3402 // [We computed 'value' above.]
3403 if (value
== cal
.getLeastMaximum(UCAL_HOUR
)+1)
3406 // fall through to set field
3408 case UDAT_HOUR0_FIELD
:
3409 cal
.set(UCAL_HOUR
, value
);
3410 return pos
.getIndex();
3412 case UDAT_QUARTER_FIELD
:
3413 if (gotNumber
) // i.e., Q or QQ.
3415 // Don't want to parse the month if it is a string
3416 // while pattern uses numeric style: Q or QQ.
3417 // [We computed 'value' above.]
3418 cal
.set(UCAL_MONTH
, (value
- 1) * 3);
3419 return pos
.getIndex();
3421 // count >= 3 // i.e., QQQ or QQQQ
3422 // Want to be able to parse both short and long forms.
3423 // Try count == 4 first:
3424 int32_t newStart
= 0;
3426 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
== 4) {
3427 if ((newStart
= matchQuarterString(text
, start
, UCAL_MONTH
,
3428 fSymbols
->fQuarters
, fSymbols
->fQuartersCount
, cal
)) > 0)
3431 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
== 3) {
3432 if ((newStart
= matchQuarterString(text
, start
, UCAL_MONTH
,
3433 fSymbols
->fShortQuarters
, fSymbols
->fShortQuartersCount
, cal
)) > 0)
3436 if (!getBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC
, status
))
3438 // else we allowing parsing as number, below
3439 if(!getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
))
3444 case UDAT_STANDALONE_QUARTER_FIELD
:
3445 if (gotNumber
) // i.e., q or qq.
3447 // Don't want to parse the month if it is a string
3448 // while pattern uses numeric style: q or q.
3449 // [We computed 'value' above.]
3450 cal
.set(UCAL_MONTH
, (value
- 1) * 3);
3451 return pos
.getIndex();
3453 // count >= 3 // i.e., qqq or qqqq
3454 // Want to be able to parse both short and long forms.
3455 // Try count == 4 first:
3456 int32_t newStart
= 0;
3458 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
== 4) {
3459 if ((newStart
= matchQuarterString(text
, start
, UCAL_MONTH
,
3460 fSymbols
->fStandaloneQuarters
, fSymbols
->fStandaloneQuartersCount
, cal
)) > 0)
3463 if(getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
== 3) {
3464 if ((newStart
= matchQuarterString(text
, start
, UCAL_MONTH
,
3465 fSymbols
->fStandaloneShortQuarters
, fSymbols
->fStandaloneShortQuartersCount
, cal
)) > 0)
3468 if (!getBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC
, status
))
3470 // else we allowing parsing as number, below
3471 if(!getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
))
3476 case UDAT_TIMEZONE_FIELD
: // 'z'
3478 UTimeZoneFormatStyle style
= (count
< 4) ? UTZFMT_STYLE_SPECIFIC_SHORT
: UTZFMT_STYLE_SPECIFIC_LONG
;
3479 TimeZone
*tz
= tzFormat()->parse(style
, text
, pos
, tzParseOptions
, tzTimeType
);
3481 cal
.adoptTimeZone(tz
);
3482 return pos
.getIndex();
3486 case UDAT_TIMEZONE_RFC_FIELD
: // 'Z'
3488 UTimeZoneFormatStyle style
= (count
< 4) ?
3489 UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL
: ((count
== 5) ? UTZFMT_STYLE_ISO_EXTENDED_FULL
: UTZFMT_STYLE_LOCALIZED_GMT
);
3490 TimeZone
*tz
= tzFormat()->parse(style
, text
, pos
, tzTimeType
);
3492 cal
.adoptTimeZone(tz
);
3493 return pos
.getIndex();
3497 case UDAT_TIMEZONE_GENERIC_FIELD
: // 'v'
3499 UTimeZoneFormatStyle style
= (count
< 4) ? UTZFMT_STYLE_GENERIC_SHORT
: UTZFMT_STYLE_GENERIC_LONG
;
3500 TimeZone
*tz
= tzFormat()->parse(style
, text
, pos
, tzParseOptions
, tzTimeType
);
3502 cal
.adoptTimeZone(tz
);
3503 return pos
.getIndex();
3507 case UDAT_TIMEZONE_SPECIAL_FIELD
: // 'V'
3509 UTimeZoneFormatStyle style
;
3512 style
= UTZFMT_STYLE_ZONE_ID_SHORT
;
3515 style
= UTZFMT_STYLE_ZONE_ID
;
3518 style
= UTZFMT_STYLE_EXEMPLAR_LOCATION
;
3521 style
= UTZFMT_STYLE_GENERIC_LOCATION
;
3524 TimeZone
*tz
= tzFormat()->parse(style
, text
, pos
, tzTimeType
);
3526 cal
.adoptTimeZone(tz
);
3527 return pos
.getIndex();
3531 case UDAT_TIMEZONE_LOCALIZED_GMT_OFFSET_FIELD
: // 'O'
3533 UTimeZoneFormatStyle style
= (count
< 4) ? UTZFMT_STYLE_LOCALIZED_GMT_SHORT
: UTZFMT_STYLE_LOCALIZED_GMT
;
3534 TimeZone
*tz
= tzFormat()->parse(style
, text
, pos
, tzTimeType
);
3536 cal
.adoptTimeZone(tz
);
3537 return pos
.getIndex();
3541 case UDAT_TIMEZONE_ISO_FIELD
: // 'X'
3543 UTimeZoneFormatStyle style
;
3546 style
= UTZFMT_STYLE_ISO_BASIC_SHORT
;
3549 style
= UTZFMT_STYLE_ISO_BASIC_FIXED
;
3552 style
= UTZFMT_STYLE_ISO_EXTENDED_FIXED
;
3555 style
= UTZFMT_STYLE_ISO_BASIC_FULL
;
3558 style
= UTZFMT_STYLE_ISO_EXTENDED_FULL
;
3561 TimeZone
*tz
= tzFormat()->parse(style
, text
, pos
, tzTimeType
);
3563 cal
.adoptTimeZone(tz
);
3564 return pos
.getIndex();
3568 case UDAT_TIMEZONE_ISO_LOCAL_FIELD
: // 'x'
3570 UTimeZoneFormatStyle style
;
3573 style
= UTZFMT_STYLE_ISO_BASIC_LOCAL_SHORT
;
3576 style
= UTZFMT_STYLE_ISO_BASIC_LOCAL_FIXED
;
3579 style
= UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FIXED
;
3582 style
= UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL
;
3585 style
= UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FULL
;
3588 TimeZone
*tz
= tzFormat()->parse(style
, text
, pos
, tzTimeType
);
3590 cal
.adoptTimeZone(tz
);
3591 return pos
.getIndex();
3595 // currently no pattern character is defined for UDAT_TIME_SEPARATOR_FIELD
3596 // so we should not get here. Leave support in for future definition.
3597 case UDAT_TIME_SEPARATOR_FIELD
:
3599 static const UChar def_sep
= DateFormatSymbols::DEFAULT_TIME_SEPARATOR
;
3600 static const UChar alt_sep
= DateFormatSymbols::ALTERNATE_TIME_SEPARATOR
;
3602 // Try matching a time separator.
3604 UnicodeString data
[3];
3605 fSymbols
->getTimeSeparatorString(data
[0]);
3607 // Add the default, if different from the locale.
3608 if (data
[0].compare(&def_sep
, 1) != 0) {
3609 data
[count
++].setTo(def_sep
);
3612 // If lenient, add also the alternate, if different from the locale.
3613 if (isLenient() && data
[0].compare(&alt_sep
, 1) != 0) {
3614 data
[count
++].setTo(alt_sep
);
3617 return matchString(text
, start
, UCAL_FIELD_COUNT
/* => nothing to set */, data
, count
, NULL
, cal
);
3620 case UDAT_AM_PM_MIDNIGHT_NOON_FIELD
:
3622 U_ASSERT(dayPeriod
!= NULL
);
3623 int32_t ampmStart
= subParse(text
, start
, 0x61, count
,
3624 obeyCount
, allowNegative
, ambiguousYear
, saveHebrewMonth
, cal
,
3625 patLoc
, numericLeapMonthFormatter
, tzTimeType
, mutableNFs
);
3627 if (ampmStart
> 0) {
3630 int32_t newStart
= 0;
3632 // Only match the first two strings from the day period strings array.
3633 if (getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
== 3) {
3634 if ((newStart
= matchDayPeriodStrings(text
, start
, fSymbols
->fAbbreviatedDayPeriods
,
3635 2, *dayPeriod
)) > 0) {
3639 if (getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
== 5) {
3640 if ((newStart
= matchDayPeriodStrings(text
, start
, fSymbols
->fNarrowDayPeriods
,
3641 2, *dayPeriod
)) > 0) {
3645 // count == 4, but allow other counts
3646 if (getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
)) {
3647 if ((newStart
= matchDayPeriodStrings(text
, start
, fSymbols
->fWideDayPeriods
,
3648 2, *dayPeriod
)) > 0) {
3657 case UDAT_FLEXIBLE_DAY_PERIOD_FIELD
:
3659 U_ASSERT(dayPeriod
!= NULL
);
3660 int32_t newStart
= 0;
3662 if (getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
== 3) {
3663 if ((newStart
= matchDayPeriodStrings(text
, start
, fSymbols
->fAbbreviatedDayPeriods
,
3664 fSymbols
->fAbbreviatedDayPeriodsCount
, *dayPeriod
)) > 0) {
3668 if (getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
== 5) {
3669 if ((newStart
= matchDayPeriodStrings(text
, start
, fSymbols
->fNarrowDayPeriods
,
3670 fSymbols
->fNarrowDayPeriodsCount
, *dayPeriod
)) > 0) {
3674 if (getBooleanAttribute(UDAT_PARSE_MULTIPLE_PATTERNS_FOR_MATCH
, status
) || count
== 4) {
3675 if ((newStart
= matchDayPeriodStrings(text
, start
, fSymbols
->fWideDayPeriods
,
3676 fSymbols
->fWideDayPeriodsCount
, *dayPeriod
)) > 0) {
3685 // Handle "generic" fields
3686 // this is now handled below, outside the switch block
3689 // Handle "generic" fields:
3690 // switch default case now handled here (outside switch block) to allow
3691 // parsing of some string fields as digits for lenient case
3693 int32_t parseStart
= pos
.getIndex();
3694 const UnicodeString
* src
;
3696 if ((start
+count
) > text
.length()) {
3699 text
.extractBetween(0, start
+ count
, temp
);
3704 parseInt(*src
, number
, pos
, allowNegative
,currentNumberFormat
);
3705 if (pos
.getIndex() != parseStart
) {
3706 int32_t value
= number
.getLong();
3708 // Don't need suffix processing here (as in number processing at the beginning of the function);
3709 // the new fields being handled as numeric values (month, weekdays, quarters) should not have suffixes.
3711 // Check the range of the value
3712 if (!getBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC
, status
)) {
3713 int32_t bias
= gFieldRangeBias
[patternCharIndex
];
3714 if (bias
>= 0 && (value
> cal
.getMaximum(field
) + bias
|| value
< cal
.getMinimum(field
) + bias
)) {
3718 int32_t bias
= gFieldRangeBiasLenient
[patternCharIndex
];
3719 if (bias
>= 0 && (value
> cal
.getMaximum(field
) + bias
)) {
3724 // For the following, need to repeat some of the "if (gotNumber)" code above:
3725 // UDAT_[STANDALONE_]MONTH_FIELD, UDAT_DOW_LOCAL_FIELD, UDAT_STANDALONE_DAY_FIELD,
3726 // UDAT_[STANDALONE_]QUARTER_FIELD
3727 switch (patternCharIndex
) {
3728 case UDAT_MONTH_FIELD
:
3729 // See notes under UDAT_MONTH_FIELD case above
3730 if (!strcmp(cal
.getType(),"hebrew")) {
3731 HebrewCalendar
*hc
= (HebrewCalendar
*)&cal
;
3732 if (cal
.isSet(UCAL_YEAR
)) {
3733 UErrorCode status
= U_ZERO_ERROR
;
3734 if (!hc
->isLeapYear(hc
->get(UCAL_YEAR
,status
)) && value
>= 6) {
3735 cal
.set(UCAL_MONTH
, value
);
3737 cal
.set(UCAL_MONTH
, value
- 1);
3740 saveHebrewMonth
= value
;
3743 cal
.set(UCAL_MONTH
, value
- 1);
3746 case UDAT_STANDALONE_MONTH_FIELD
:
3747 cal
.set(UCAL_MONTH
, value
- 1);
3749 case UDAT_DOW_LOCAL_FIELD
:
3750 case UDAT_STANDALONE_DAY_FIELD
:
3751 cal
.set(UCAL_DOW_LOCAL
, value
);
3753 case UDAT_QUARTER_FIELD
:
3754 case UDAT_STANDALONE_QUARTER_FIELD
:
3755 cal
.set(UCAL_MONTH
, (value
- 1) * 3);
3757 case UDAT_RELATED_YEAR_FIELD
:
3758 cal
.setRelatedYear(value
);
3761 cal
.set(field
, value
);
3764 return pos
.getIndex();
3770 * Parse an integer using fNumberFormat. This method is semantically
3771 * const, but actually may modify fNumberFormat.
3773 void SimpleDateFormat::parseInt(const UnicodeString
& text
,
3774 Formattable
& number
,
3776 UBool allowNegative
,
3777 NumberFormat
*fmt
) const {
3778 parseInt(text
, number
, -1, pos
, allowNegative
,fmt
);
3782 * Parse an integer using fNumberFormat up to maxDigits.
3784 void SimpleDateFormat::parseInt(const UnicodeString
& text
,
3785 Formattable
& number
,
3788 UBool allowNegative
,
3789 NumberFormat
*fmt
) const {
3790 UnicodeString oldPrefix
;
3791 DecimalFormat
* df
= NULL
;
3792 if (!allowNegative
&& (df
= dynamic_cast<DecimalFormat
*>(fmt
)) != NULL
) {
3793 df
->getNegativePrefix(oldPrefix
);
3794 df
->setNegativePrefix(UnicodeString(TRUE
, SUPPRESS_NEGATIVE_PREFIX
, -1));
3796 int32_t oldPos
= pos
.getIndex();
3797 fmt
->parse(text
, number
, pos
);
3799 df
->setNegativePrefix(oldPrefix
);
3802 if (maxDigits
> 0) {
3803 // adjust the result to fit into
3804 // the maxDigits and move the position back
3805 int32_t nDigits
= pos
.getIndex() - oldPos
;
3806 if (nDigits
> maxDigits
) {
3807 int32_t val
= number
.getLong();
3808 nDigits
-= maxDigits
;
3809 while (nDigits
> 0) {
3813 pos
.setIndex(oldPos
+ maxDigits
);
3814 number
.setLong(val
);
3819 //----------------------------------------------------------------------
3821 void SimpleDateFormat::translatePattern(const UnicodeString
& originalPattern
,
3822 UnicodeString
& translatedPattern
,
3823 const UnicodeString
& from
,
3824 const UnicodeString
& to
,
3827 // run through the pattern and convert any pattern symbols from the version
3828 // in "from" to the corresponding character in "to". This code takes
3829 // quoted strings into account (it doesn't try to translate them), and it signals
3830 // an error if a particular "pattern character" doesn't appear in "from".
3831 // Depending on the values of "from" and "to" this can convert from generic
3832 // to localized patterns or localized to generic.
3833 if (U_FAILURE(status
)) {
3837 translatedPattern
.remove();
3838 UBool inQuote
= FALSE
;
3839 for (int32_t i
= 0; i
< originalPattern
.length(); ++i
) {
3840 UChar c
= originalPattern
[i
];
3848 } else if (isSyntaxChar(c
)) {
3849 int32_t ci
= from
.indexOf(c
);
3851 status
= U_INVALID_FORMAT_ERROR
;
3857 translatedPattern
+= c
;
3860 status
= U_INVALID_FORMAT_ERROR
;
3865 //----------------------------------------------------------------------
3868 SimpleDateFormat::toPattern(UnicodeString
& result
) const
3874 //----------------------------------------------------------------------
3877 SimpleDateFormat::toLocalizedPattern(UnicodeString
& result
,
3878 UErrorCode
& status
) const
3880 translatePattern(fPattern
, result
,
3881 UnicodeString(DateFormatSymbols::getPatternUChars()),
3882 fSymbols
->fLocalPatternChars
, status
);
3886 //----------------------------------------------------------------------
3889 SimpleDateFormat::applyPattern(const UnicodeString
& pattern
)
3895 //----------------------------------------------------------------------
3898 SimpleDateFormat::applyLocalizedPattern(const UnicodeString
& pattern
,
3901 translatePattern(pattern
, fPattern
,
3902 fSymbols
->fLocalPatternChars
,
3903 UnicodeString(DateFormatSymbols::getPatternUChars()), status
);
3906 //----------------------------------------------------------------------
3908 const DateFormatSymbols
*
3909 SimpleDateFormat::getDateFormatSymbols() const
3914 //----------------------------------------------------------------------
3917 SimpleDateFormat::adoptDateFormatSymbols(DateFormatSymbols
* newFormatSymbols
)
3920 fSymbols
= newFormatSymbols
;
3923 //----------------------------------------------------------------------
3925 SimpleDateFormat::setDateFormatSymbols(const DateFormatSymbols
& newFormatSymbols
)
3928 fSymbols
= new DateFormatSymbols(newFormatSymbols
);
3931 //----------------------------------------------------------------------
3932 const TimeZoneFormat
*
3933 SimpleDateFormat::getTimeZoneFormat(void) const {
3934 return (const TimeZoneFormat
*)tzFormat();
3937 //----------------------------------------------------------------------
3939 SimpleDateFormat::adoptTimeZoneFormat(TimeZoneFormat
* timeZoneFormatToAdopt
)
3941 delete fTimeZoneFormat
;
3942 fTimeZoneFormat
= timeZoneFormatToAdopt
;
3945 //----------------------------------------------------------------------
3947 SimpleDateFormat::setTimeZoneFormat(const TimeZoneFormat
& newTimeZoneFormat
)
3949 delete fTimeZoneFormat
;
3950 fTimeZoneFormat
= new TimeZoneFormat(newTimeZoneFormat
);
3953 //----------------------------------------------------------------------
3956 void SimpleDateFormat::adoptCalendar(Calendar
* calendarToAdopt
)
3958 UErrorCode status
= U_ZERO_ERROR
;
3959 Locale
calLocale(fLocale
);
3960 calLocale
.setKeywordValue("calendar", calendarToAdopt
->getType(), status
);
3961 DateFormatSymbols
*newSymbols
=
3962 DateFormatSymbols::createForLocale(calLocale
, status
);
3963 if (U_FAILURE(status
)) {
3966 DateFormat::adoptCalendar(calendarToAdopt
);
3968 fSymbols
= newSymbols
;
3969 initializeDefaultCentury(); // we need a new century (possibly)
3973 //----------------------------------------------------------------------
3976 // override the DateFormat implementation in order to
3977 // lazily initialize fCapitalizationBrkIter
3979 SimpleDateFormat::setContext(UDisplayContext value
, UErrorCode
& status
)
3981 DateFormat::setContext(value
, status
);
3982 #if !UCONFIG_NO_BREAK_ITERATION
3983 if (U_SUCCESS(status
)) {
3984 if ( fCapitalizationBrkIter
== NULL
&& (value
==UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE
||
3985 value
==UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU
|| value
==UDISPCTX_CAPITALIZATION_FOR_STANDALONE
) ) {
3986 UErrorCode status
= U_ZERO_ERROR
;
3987 fCapitalizationBrkIter
= BreakIterator::createSentenceInstance(fLocale
, status
);
3988 if (U_FAILURE(status
)) {
3989 delete fCapitalizationBrkIter
;
3990 fCapitalizationBrkIter
= NULL
;
3998 //----------------------------------------------------------------------
4002 SimpleDateFormat::isFieldUnitIgnored(UCalendarDateFields field
) const {
4003 return isFieldUnitIgnored(fPattern
, field
);
4008 SimpleDateFormat::isFieldUnitIgnored(const UnicodeString
& pattern
,
4009 UCalendarDateFields field
) {
4010 int32_t fieldLevel
= fgCalendarFieldToLevel
[field
];
4013 UBool inQuote
= FALSE
;
4017 for (int32_t i
= 0; i
< pattern
.length(); ++i
) {
4019 if (ch
!= prevCh
&& count
> 0) {
4020 level
= getLevelFromChar(prevCh
);
4021 // the larger the level, the smaller the field unit.
4022 if (fieldLevel
<= level
) {
4028 if ((i
+1) < pattern
.length() && pattern
[i
+1] == QUOTE
) {
4031 inQuote
= ! inQuote
;
4034 else if (!inQuote
&& isSyntaxChar(ch
)) {
4041 level
= getLevelFromChar(prevCh
);
4042 if (fieldLevel
<= level
) {
4049 //----------------------------------------------------------------------
4052 SimpleDateFormat::getSmpFmtLocale(void) const {
4056 //----------------------------------------------------------------------
4059 SimpleDateFormat::checkIntSuffix(const UnicodeString
& text
, int32_t start
,
4060 int32_t patLoc
, UBool isNegative
) const {
4063 int32_t patternMatch
;
4064 int32_t textPreMatch
;
4065 int32_t textPostMatch
;
4067 // check that we are still in range
4068 if ( (start
> text
.length()) ||
4071 (patLoc
> fPattern
.length())) {
4072 // out of range, don't advance location in text
4077 DecimalFormat
* decfmt
= dynamic_cast<DecimalFormat
*>(fNumberFormat
);
4078 if (decfmt
!= NULL
) {
4080 suf
= decfmt
->getNegativeSuffix(suf
);
4083 suf
= decfmt
->getPositiveSuffix(suf
);
4088 if (suf
.length() <= 0) {
4092 // check suffix will be encountered in the pattern
4093 patternMatch
= compareSimpleAffix(suf
,fPattern
,patLoc
);
4095 // check if a suffix will be encountered in the text
4096 textPreMatch
= compareSimpleAffix(suf
,text
,start
);
4098 // check if a suffix was encountered in the text
4099 textPostMatch
= compareSimpleAffix(suf
,text
,start
-suf
.length());
4101 // check for suffix match
4102 if ((textPreMatch
>= 0) && (patternMatch
>= 0) && (textPreMatch
== patternMatch
)) {
4105 else if ((textPostMatch
>= 0) && (patternMatch
>= 0) && (textPostMatch
== patternMatch
)) {
4106 return start
- suf
.length();
4109 // should not get here
4113 //----------------------------------------------------------------------
4116 SimpleDateFormat::compareSimpleAffix(const UnicodeString
& affix
,
4117 const UnicodeString
& input
,
4118 int32_t pos
) const {
4119 int32_t start
= pos
;
4120 for (int32_t i
=0; i
<affix
.length(); ) {
4121 UChar32 c
= affix
.char32At(i
);
4122 int32_t len
= U16_LENGTH(c
);
4123 if (PatternProps::isWhiteSpace(c
)) {
4124 // We may have a pattern like: \u200F \u0020
4125 // and input text like: \u200F \u0020
4126 // Note that U+200F and U+0020 are Pattern_White_Space but only
4127 // U+0020 is UWhiteSpace. So we have to first do a direct
4128 // match of the run of Pattern_White_Space in the pattern,
4129 // then match any extra characters.
4130 UBool literalMatch
= FALSE
;
4131 while (pos
< input
.length() &&
4132 input
.char32At(pos
) == c
) {
4133 literalMatch
= TRUE
;
4136 if (i
== affix
.length()) {
4139 c
= affix
.char32At(i
);
4140 len
= U16_LENGTH(c
);
4141 if (!PatternProps::isWhiteSpace(c
)) {
4146 // Advance over run in pattern
4147 i
= skipPatternWhiteSpace(affix
, i
);
4149 // Advance over run in input text
4150 // Must see at least one white space char in input,
4151 // unless we've already matched some characters literally.
4153 pos
= skipUWhiteSpace(input
, pos
);
4154 if (pos
== s
&& !literalMatch
) {
4158 // If we skip UWhiteSpace in the input text, we need to skip it in the pattern.
4159 // Otherwise, the previous lines may have skipped over text (such as U+00A0) that
4160 // is also in the affix.
4161 i
= skipUWhiteSpace(affix
, i
);
4163 if (pos
< input
.length() &&
4164 input
.char32At(pos
) == c
) {
4175 //----------------------------------------------------------------------
4178 SimpleDateFormat::skipPatternWhiteSpace(const UnicodeString
& text
, int32_t pos
) const {
4179 const UChar
* s
= text
.getBuffer();
4180 return (int32_t)(PatternProps::skipWhiteSpace(s
+ pos
, text
.length() - pos
) - s
);
4183 //----------------------------------------------------------------------
4186 SimpleDateFormat::skipUWhiteSpace(const UnicodeString
& text
, int32_t pos
) const {
4187 while (pos
< text
.length()) {
4188 UChar32 c
= text
.char32At(pos
);
4189 if (!u_isUWhiteSpace(c
)) {
4192 pos
+= U16_LENGTH(c
);
4197 //----------------------------------------------------------------------
4199 // Lazy TimeZoneFormat instantiation, semantically const.
4201 SimpleDateFormat::tzFormat() const {
4202 if (fTimeZoneFormat
== NULL
) {
4205 if (fTimeZoneFormat
== NULL
) {
4206 UErrorCode status
= U_ZERO_ERROR
;
4207 TimeZoneFormat
*tzfmt
= TimeZoneFormat::createInstance(fLocale
, status
);
4208 if (U_FAILURE(status
)) {
4212 const_cast<SimpleDateFormat
*>(this)->fTimeZoneFormat
= tzfmt
;
4217 return fTimeZoneFormat
;
4220 void SimpleDateFormat::parsePattern() {
4224 int len
= fPattern
.length();
4225 UBool inQuote
= FALSE
;
4226 for (int32_t i
= 0; i
< len
; ++i
) {
4227 UChar ch
= fPattern
[i
];
4232 if (ch
== 0x6D) { // 0x6D == 'm'
4235 if (ch
== 0x73) { // 0x73 == 's'
4244 #endif /* #if !UCONFIG_NO_FORMATTING */