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