intl/icu/source/i18n/smpdtfmt.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial