1 /******************************************************************** 2 * COPYRIGHT: 3 * Copyright (c) 1997-2015, International Business Machines Corporation and 4 * others. All Rights Reserved. 5 ********************************************************************/ 6 7 #include "unicode/utypes.h" 8 9 #if !UCONFIG_NO_FORMATTING 10 #include "dtfmrgts.h" 11 12 #include "unicode/timezone.h" 13 #include "unicode/gregocal.h" 14 #include "unicode/smpdtfmt.h" 15 #include "unicode/datefmt.h" 16 #include "unicode/simpletz.h" 17 #include "unicode/resbund.h" 18 19 // ***************************************************************************** 20 // class DateFormatRegressionTest 21 // ***************************************************************************** 22 23 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break; 24 25 void 26 DateFormatRegressionTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ ) 27 { 28 // if (exec) logln((UnicodeString)"TestSuite DateFormatRegressionTest"); 29 switch (index) { 30 CASE(0,Test4029195) 31 CASE(1,Test4052408) 32 CASE(2,Test4056591) 33 CASE(3,Test4059917) 34 CASE(4,Test4060212) 35 CASE(5,Test4061287) 36 CASE(6,Test4065240) 37 CASE(7,Test4071441) 38 CASE(8,Test4073003) 39 CASE(9,Test4089106) 40 CASE(10,Test4100302) 41 CASE(11,Test4101483) 42 CASE(12,Test4103340) 43 CASE(13,Test4103341) 44 CASE(14,Test4104136) 45 CASE(15,Test4104522) 46 CASE(16,Test4106807) 47 CASE(17,Test4108407) 48 CASE(18,Test4134203) 49 CASE(19,Test4151631) 50 CASE(20,Test4151706) 51 CASE(21,Test4162071) 52 CASE(22,Test4182066) 53 CASE(23,Test4210209) 54 CASE(24,Test714) 55 CASE(25,Test1684) 56 CASE(26,Test5554) 57 CASE(27,Test9237) 58 CASE(28,TestParsing) 59 CASE(29,TestT10334) 60 CASE(30,TestT10619) 61 CASE(31,TestT10855) 62 CASE(32,TestT10906) 63 default: name = ""; break; 64 } 65 } 66 67 /** 68 * @bug 4029195 69 */ 70 void DateFormatRegressionTest::Test4029195(void) 71 { 72 UErrorCode status = U_ZERO_ERROR; 73 74 UDate today = Calendar::getNow(); 75 logln((UnicodeString) "today: " + today); 76 77 SimpleDateFormat *sdf = (SimpleDateFormat*) DateFormat::createDateInstance(); 78 if (failure(status, "SimpleDateFormat::createDateInstance")) { 79 return; 80 } 81 UnicodeString pat; 82 if(sdf == NULL){ 83 dataerrln("Error calling DateFormat::createDateTimeInstance"); 84 return; 85 } 86 87 pat = sdf->toPattern(pat); 88 logln("pattern: " + pat); 89 UnicodeString fmtd; 90 91 FieldPosition pos(FieldPosition::DONT_CARE); 92 fmtd = sdf->format(today, fmtd, pos); 93 logln("today: " + fmtd); 94 95 sdf->applyPattern("G yyyy DDD"); 96 UnicodeString todayS; 97 todayS = sdf->format(today, todayS, pos); 98 logln("today: " + todayS); 99 //try { 100 today = sdf->parse(todayS, status); 101 failure(status, "sdf->parse"); 102 logln((UnicodeString)"today date: " + today); 103 /*} catch(Exception e) { 104 logln("Error reparsing date: " + e.getMessage()); 105 }*/ 106 107 //try { 108 UnicodeString rt; 109 rt = sdf->format(sdf->parse(todayS, status), rt, pos); 110 failure(status, "sdf->parse"); 111 logln("round trip: " + rt); 112 if(rt != todayS) 113 errln("Fail: Want " + todayS + " Got " + rt); 114 /*} 115 catch (ParseException e) { 116 errln("Fail: " + e); 117 e.printStackTrace(); 118 }*/ 119 120 delete sdf; 121 } 122 123 /** 124 * @bug 4052408 125 */ 126 void DateFormatRegressionTest::Test4052408(void) 127 { 128 129 DateFormat *fmt = DateFormat::createDateTimeInstance(DateFormat::SHORT, 130 DateFormat::SHORT, Locale::getUS()); 131 if (fmt == NULL) { 132 dataerrln("Error calling DateFormat::createDateTimeInstance"); 133 return; 134 } 135 136 UDate dt = date(97, UCAL_MAY, 3, 8, 55); 137 UnicodeString str; 138 str = fmt->format(dt, str); 139 logln(str); 140 141 if(str != "5/3/97, 8:55 AM") 142 errln("Fail: Test broken; Want 5/3/97 8:55 AM Got " + str); 143 144 UnicodeString expected[] = { 145 (UnicodeString) "", //"ERA_FIELD", 146 (UnicodeString) "97", //"YEAR_FIELD", 147 (UnicodeString) "5", //"MONTH_FIELD", 148 (UnicodeString) "3", //"DATE_FIELD", 149 (UnicodeString) "", //"HOUR_OF_DAY1_FIELD", 150 (UnicodeString) "", //"HOUR_OF_DAY0_FIELD", 151 (UnicodeString) "55", //"MINUTE_FIELD", 152 (UnicodeString) "", //"SECOND_FIELD", 153 (UnicodeString) "", //"MILLISECOND_FIELD", 154 (UnicodeString) "", //"DAY_OF_WEEK_FIELD", 155 (UnicodeString) "", //"DAY_OF_YEAR_FIELD", 156 (UnicodeString) "", //"DAY_OF_WEEK_IN_MONTH_FIELD", 157 (UnicodeString) "", //"WEEK_OF_YEAR_FIELD", 158 (UnicodeString) "", //"WEEK_OF_MONTH_FIELD", 159 (UnicodeString) "AM", //"AM_PM_FIELD", 160 (UnicodeString) "8", //"HOUR1_FIELD", 161 (UnicodeString) "", //"HOUR0_FIELD", 162 (UnicodeString) "" //"TIMEZONE_FIELD" 163 }; 164 165 //Hashtable expected;// = new Hashtable(); 166 //expected.put(new LongKey(DateFormat.MONTH_FIELD), "5"); 167 //expected.put(new LongKey(DateFormat.DATE_FIELD), "3"); 168 //expected.put(new LongKey(DateFormat.YEAR_FIELD), "97"); 169 //expected.put(new LongKey(DateFormat.HOUR1_FIELD), "8"); 170 //expected.put(new LongKey(DateFormat.MINUTE_FIELD), "55"); 171 //expected.put(new LongKey(DateFormat.AM_PM_FIELD), "AM"); 172 173 //StringBuffer buf = new StringBuffer(); 174 UnicodeString fieldNames[] = { 175 (UnicodeString) "ERA_FIELD", 176 (UnicodeString) "YEAR_FIELD", 177 (UnicodeString) "MONTH_FIELD", 178 (UnicodeString) "DATE_FIELD", 179 (UnicodeString) "HOUR_OF_DAY1_FIELD", 180 (UnicodeString) "HOUR_OF_DAY0_FIELD", 181 (UnicodeString) "MINUTE_FIELD", 182 (UnicodeString) "SECOND_FIELD", 183 (UnicodeString) "MILLISECOND_FIELD", 184 (UnicodeString) "DAY_OF_WEEK_FIELD", 185 (UnicodeString) "DAY_OF_YEAR_FIELD", 186 (UnicodeString) "DAY_OF_WEEK_IN_MONTH_FIELD", 187 (UnicodeString) "WEEK_OF_YEAR_FIELD", 188 (UnicodeString) "WEEK_OF_MONTH_FIELD", 189 (UnicodeString) "AM_PM_FIELD", 190 (UnicodeString) "HOUR1_FIELD", 191 (UnicodeString) "HOUR0_FIELD", 192 (UnicodeString) "TIMEZONE_FIELD" 193 }; 194 195 UBool pass = TRUE; 196 for(int i = 0; i <= 17; ++i) { 197 FieldPosition pos(i); 198 UnicodeString buf; 199 fmt->format(dt, buf, pos); 200 //char[] dst = new char[pos.getEndIndex() - pos.getBeginIndex()]; 201 UnicodeString dst; 202 buf.extractBetween(pos.getBeginIndex(), pos.getEndIndex(), dst); 203 UnicodeString str(dst); 204 logln((UnicodeString)"" + i + (UnicodeString)": " + fieldNames[i] + 205 (UnicodeString)", \"" + str + (UnicodeString)"\", " + 206 pos.getBeginIndex() + (UnicodeString)", " + 207 pos.getEndIndex()); 208 UnicodeString exp = expected[i]; 209 if((exp.length() == 0 && str.length() == 0) || str == exp) 210 logln(" ok"); 211 else { 212 errln(UnicodeString(" expected ") + exp); 213 pass = FALSE; 214 } 215 216 } 217 if( ! pass) 218 errln("Fail: FieldPosition not set right by DateFormat"); 219 220 delete fmt; 221 } 222 223 /** 224 * @bug 4056591 225 * Verify the function of the [s|g]et2DigitYearStart() API. 226 */ 227 void DateFormatRegressionTest::Test4056591(void) 228 { 229 UErrorCode status = U_ZERO_ERROR; 230 231 //try { 232 SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("yyMMdd"), Locale::getUS(), status); 233 if (failure(status, "new SimpleDateFormat", TRUE)) { 234 delete fmt; 235 return; 236 } 237 UDate start = date(1809-1900, UCAL_DECEMBER, 25); 238 fmt->set2DigitYearStart(start, status); 239 failure(status, "fmt->setTwoDigitStartDate"); 240 if( (fmt->get2DigitYearStart(status) != start) || failure(status, "get2DigitStartDate")) 241 errln("get2DigitYearStart broken"); 242 UDate dates [] = { 243 date(1809-1900, UCAL_DECEMBER, 25), 244 date(1909-1900, UCAL_DECEMBER, 24), 245 date(1809-1900, UCAL_DECEMBER, 26), 246 date(1861-1900, UCAL_DECEMBER, 25), 247 }; 248 249 UnicodeString strings [] = { 250 (UnicodeString) "091225", 251 (UnicodeString) "091224", 252 (UnicodeString) "091226", 253 (UnicodeString) "611225" 254 }; 255 256 /*Object[] DATA = { 257 "091225", new Date(1809-1900, Calendar.DECEMBER, 25), 258 "091224", new Date(1909-1900, Calendar.DECEMBER, 24), 259 "091226", new Date(1809-1900, Calendar.DECEMBER, 26), 260 "611225", new Date(1861-1900, Calendar.DECEMBER, 25), 261 };*/ 262 263 for(int i = 0; i < 4; i++) { 264 UnicodeString s = strings[i]; 265 UDate exp = dates[i]; 266 UDate got = fmt->parse(s, status); 267 failure(status, "fmt->parse"); 268 logln(s + " -> " + got + "; exp " + exp); 269 if(got != exp) 270 errln("set2DigitYearStart broken"); 271 } 272 /*} 273 catch (ParseException e) { 274 errln("Fail: " + e); 275 e.printStackTrace(); 276 }*/ 277 278 delete fmt; 279 } 280 281 /** 282 * @bug 4059917 283 */ 284 void DateFormatRegressionTest::Test4059917(void) 285 { 286 UErrorCode status = U_ZERO_ERROR; 287 288 SimpleDateFormat *fmt; 289 UnicodeString myDate; 290 291 fmt = new SimpleDateFormat( UnicodeString("yyyy/MM/dd"), status ); 292 if (failure(status, "new SimpleDateFormat", TRUE)) return; 293 myDate = "1997/01/01"; 294 aux917( fmt, myDate ); 295 296 delete fmt; 297 fmt = NULL; 298 299 fmt = new SimpleDateFormat( UnicodeString("yyyyMMdd"), status ); 300 if(failure(status, "new SimpleDateFormat")) return; 301 myDate = "19970101"; 302 aux917( fmt, myDate ); 303 304 delete fmt; 305 } 306 307 void DateFormatRegressionTest::aux917( SimpleDateFormat *fmt, UnicodeString& str ) { 308 //try { 309 UnicodeString pat; 310 pat = fmt->toPattern(pat); 311 logln( "==================" ); 312 logln( "testIt: pattern=" + pat + 313 " string=" + str ); 314 315 316 Formattable o; 317 //Object o; 318 ParsePosition pos(0); 319 fmt->parseObject( str, o, pos ); 320 //logln( UnicodeString("Parsed object: ") + o ); 321 322 UErrorCode status = U_ZERO_ERROR; 323 UnicodeString formatted; 324 FieldPosition poss(FieldPosition::DONT_CARE); 325 formatted = fmt->format( o, formatted, poss, status ); 326 failure(status, "fmt->format"); 327 logln( "Formatted string: " + formatted ); 328 if( formatted != str) 329 errln("Fail: Want " + str + " Got " + formatted); 330 /*} 331 catch (ParseException e) { 332 errln("Fail: " + e); 333 e.printStackTrace(); 334 }*/ 335 } 336 337 /** 338 * @bug 4060212 339 */ 340 void DateFormatRegressionTest::Test4060212(void) 341 { 342 UnicodeString dateString = "1995-040.05:01:29"; 343 344 logln( "dateString= " + dateString ); 345 logln("Using yyyy-DDD.hh:mm:ss"); 346 UErrorCode status = U_ZERO_ERROR; 347 SimpleDateFormat *formatter = new SimpleDateFormat(UnicodeString("yyyy-DDD.hh:mm:ss"), status); 348 if (failure(status, "new SimpleDateFormat", TRUE)) return; 349 ParsePosition pos(0); 350 UDate myDate = formatter->parse( dateString, pos ); 351 UnicodeString myString; 352 DateFormat *fmt = DateFormat::createDateTimeInstance( DateFormat::FULL, 353 DateFormat::LONG); 354 if (fmt == NULL) { 355 dataerrln("Error calling DateFormat::createDateTimeInstance"); 356 delete formatter; 357 return; 358 } 359 360 myString = fmt->format( myDate, myString); 361 logln( myString ); 362 363 Calendar *cal = new GregorianCalendar(status); 364 failure(status, "new GregorianCalendar"); 365 cal->setTime(myDate, status); 366 failure(status, "cal->setTime"); 367 if ((cal->get(UCAL_DAY_OF_YEAR, status) != 40) || failure(status, "cal->get")) 368 errln((UnicodeString) "Fail: Got " + cal->get(UCAL_DAY_OF_YEAR, status) + 369 " Want 40"); 370 371 // this is an odd usage of "ddd" and it doesn't 372 // work now that date values are range checked per #3579. 373 logln("Using yyyy-ddd.hh:mm:ss"); 374 delete formatter; 375 formatter = NULL; 376 formatter = new SimpleDateFormat(UnicodeString("yyyy-ddd.hh:mm:ss"), status); 377 if(failure(status, "new SimpleDateFormat")) return; 378 pos.setIndex(0); 379 myDate = formatter->parse( dateString, pos ); 380 myString = fmt->format( myDate, myString ); 381 logln( myString ); 382 cal->setTime(myDate, status); 383 failure(status, "cal->setTime"); 384 if ((cal->get(UCAL_DAY_OF_YEAR, status) != 40) || failure(status, "cal->get")) 385 errln((UnicodeString) "Fail: Got " + cal->get(UCAL_DAY_OF_YEAR, status) + 386 " Want 40"); 387 388 delete formatter; 389 delete fmt; 390 delete cal; 391 } 392 393 /** 394 * @bug 4061287 395 */ 396 void DateFormatRegressionTest::Test4061287(void) 397 { 398 UErrorCode status = U_ZERO_ERROR; 399 400 SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("dd/MM/yyyy"), status); 401 if (U_FAILURE(status)) { 402 dataerrln("Fail new SimpleDateFormat: %s", u_errorName(status)); 403 delete df; 404 return; 405 } 406 failure(status, "new SimpleDateFormat"); 407 //try { 408 logln(UnicodeString("") + df->parse("35/01/1971", status)); 409 failure(status, "df->parse(\"35/01/1971\")"); 410 //logln(df.parse("35/01/1971").toString()); 411 //} 412 /*catch (ParseException e) { 413 errln("Fail: " + e); 414 e.printStackTrace(); 415 }*/ 416 df->setLenient(FALSE); 417 UBool ok = FALSE; 418 //try { 419 logln(UnicodeString("") + df->parse("35/01/1971", status)); 420 if(U_FAILURE(status)) 421 ok = TRUE; 422 //logln(df.parse("35/01/1971").toString()); 423 //} catch (ParseException e) {ok=TRUE;} 424 if(!ok) 425 errln("Fail: Lenient not working"); 426 delete df; 427 } 428 429 /** 430 * @bug 4065240 431 */ 432 void DateFormatRegressionTest::Test4065240(void) 433 { 434 UDate curDate; 435 DateFormat *shortdate, *fulldate; 436 UnicodeString strShortDate, strFullDate; 437 Locale saveLocale = Locale::getDefault(); 438 TimeZone *saveZone = TimeZone::createDefault(); 439 440 UErrorCode status = U_ZERO_ERROR; 441 //try { 442 Locale *curLocale = new Locale("de","DE"); 443 Locale::setDefault(*curLocale, status); 444 failure(status, "Locale::setDefault"); 445 // {sfb} adoptDefault instead of setDefault 446 //TimeZone::setDefault(TimeZone::createTimeZone("EST")); 447 TimeZone::adoptDefault(TimeZone::createTimeZone("EST")); 448 curDate = date(98, 0, 1); 449 shortdate = DateFormat::createDateInstance(DateFormat::SHORT); 450 if (shortdate == NULL){ 451 dataerrln("Error calling DateFormat::createDateInstance"); 452 return; 453 } 454 455 fulldate = DateFormat::createDateTimeInstance(DateFormat::LONG, DateFormat::LONG); 456 if (fulldate == NULL){ 457 dataerrln("Error calling DateFormat::createDateTimeInstance"); 458 return; 459 } 460 strShortDate = "The current date (short form) is "; 461 UnicodeString temp; 462 temp = shortdate->format(curDate, temp); 463 strShortDate += temp; 464 strFullDate = "The current date (long form) is "; 465 UnicodeString temp2; 466 fulldate->format(curDate, temp2); 467 strFullDate += temp2; 468 469 logln(strShortDate); 470 logln(strFullDate); 471 472 // {sfb} What to do with resource bundle stuff????? 473 474 // Check to see if the resource is present; if not, we can't test 475 ResourceBundle *bundle = new ResourceBundle( 476 NULL, *curLocale, status); 477 failure(status, "new ResourceBundle"); 478 //(UnicodeString) "java.text.resources.DateFormatZoneData", curLocale); 479 480 // {sfb} API change to ResourceBundle -- add getLocale() 481 /*if (bundle->getLocale().getLanguage(temp) == UnicodeString("de")) { 482 // UPDATE THIS AS ZONE NAME RESOURCE FOR <EST> in de_DE is updated 483 if (!strFullDate.endsWith(UnicodeString("GMT-05:00"))) 484 errln("Fail: Want GMT-05:00"); 485 } 486 else { 487 logln("*** TEST COULD NOT BE COMPLETED BECAUSE DateFormatZoneData ***"); 488 logln("*** FOR LOCALE de OR de_DE IS MISSING ***"); 489 }*/ 490 //} 491 //finally { 492 Locale::setDefault(saveLocale, status); 493 failure(status, "Locale::setDefault"); 494 TimeZone::setDefault(*saveZone); 495 //} 496 delete shortdate; 497 delete fulldate; 498 delete saveZone; 499 delete curLocale; 500 delete bundle; 501 } 502 503 /* 504 DateFormat.equals is too narrowly defined. As a result, MessageFormat 505 does not work correctly. DateFormat.equals needs to be written so 506 that the Calendar sub-object is not compared using Calendar.equals, 507 but rather compared for equivalency. This may necessitate adding a 508 (package private) method to Calendar to test for equivalency. 509 510 Currently this bug breaks MessageFormat.toPattern 511 */ 512 /** 513 * @bug 4071441 514 */ 515 void DateFormatRegressionTest::Test4071441(void) 516 { 517 DateFormat *fmtA = DateFormat::createInstance(); 518 DateFormat *fmtB = DateFormat::createInstance(); 519 520 if (fmtA == NULL || fmtB == NULL){ 521 dataerrln("Error calling DateFormat::createInstance"); 522 delete fmtA; 523 delete fmtB; 524 return; 525 } 526 527 // {sfb} Is it OK to cast away const here? 528 Calendar *calA = (Calendar*) fmtA->getCalendar(); 529 Calendar *calB = (Calendar*) fmtB->getCalendar(); 530 if(!calA || !calB) { 531 errln("Couldn't get proper calendars, exiting"); 532 delete fmtA; 533 delete fmtB; 534 return; 535 } 536 UDate epoch = date(0, 0, 0); 537 UDate xmas = date(61, UCAL_DECEMBER, 25); 538 539 UErrorCode status = U_ZERO_ERROR; 540 calA->setTime(epoch, status); 541 failure(status, "calA->setTime"); 542 calB->setTime(epoch, status); 543 failure(status, "calB->setTime"); 544 if (*calA != *calB) 545 errln("Fail: Can't complete test; Calendar instances unequal"); 546 if (*fmtA != *fmtB) 547 errln("Fail: DateFormat unequal when Calendars equal"); 548 calB->setTime(xmas, status); 549 failure(status, "calB->setTime"); 550 if (*calA == *calB) 551 errln("Fail: Can't complete test; Calendar instances equal"); 552 if (*fmtA != *fmtB) 553 errln("Fail: DateFormat unequal when Calendars equivalent"); 554 555 logln("DateFormat.equals ok"); 556 557 delete fmtA; 558 delete fmtB; 559 } 560 561 /* The java.text.DateFormat.parse(String) method expects for the 562 US locale a string formatted according to mm/dd/yy and parses it 563 correctly. 564 565 When given a string mm/dd/yyyy [sic] it only parses up to the first 566 two y's, typically resulting in a date in the year 1919. 567 568 Please extend the parsing method(s) to handle strings with 569 four-digit year values (probably also applicable to various 570 other locales. */ 571 /** 572 * @bug 4073003 573 */ 574 void DateFormatRegressionTest::Test4073003(void) 575 { 576 //try { 577 UErrorCode ec = U_ZERO_ERROR; 578 SimpleDateFormat fmt("MM/dd/yy", Locale::getUK(), ec); 579 if (U_FAILURE(ec)) { 580 dataerrln("FAIL: SimpleDateFormat constructor - %s", u_errorName(ec)); 581 return; 582 } 583 UnicodeString tests [] = { 584 (UnicodeString) "12/25/61", 585 (UnicodeString) "12/25/1961", 586 (UnicodeString) "4/3/2010", 587 (UnicodeString) "4/3/10" 588 }; 589 UErrorCode status = U_ZERO_ERROR; 590 for(int i= 0; i < 4; i+=2) { 591 UDate d = fmt.parse(tests[i], status); 592 failure(status, "fmt.parse"); 593 UDate dd = fmt.parse(tests[i+1], status); 594 failure(status, "fmt.parse"); 595 UnicodeString s; 596 s = fmt.format(d, s); 597 UnicodeString ss; 598 ss = fmt.format(dd, ss); 599 if (d != dd) 600 errln((UnicodeString) "Fail: " + d + " != " + dd); 601 if (s != ss) 602 errln((UnicodeString)"Fail: " + s + " != " + ss); 603 logln("Ok: " + s + " " + d); 604 } 605 } 606 607 /** 608 * @bug 4089106 609 */ 610 void DateFormatRegressionTest::Test4089106(void) 611 { 612 TimeZone *def = TimeZone::createDefault(); 613 //try { 614 TimeZone *z = new SimpleTimeZone((int)(1.25 * 3600000), "FAKEZONE"); 615 TimeZone::setDefault(*z); 616 UErrorCode status = U_ZERO_ERROR; 617 SimpleDateFormat *f = new SimpleDateFormat(status); 618 if(U_FAILURE(status)) { 619 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status)); 620 delete f; 621 delete def; 622 delete z; 623 return; 624 } 625 failure(status, "new SimpleDateFormat"); 626 if (f->getTimeZone()!= *z) 627 errln("Fail: SimpleTimeZone should use TimeZone.getDefault()"); 628 629 //} 630 //finally { 631 TimeZone::setDefault(*def); 632 //} 633 634 delete z; 635 delete f; 636 delete def; 637 } 638 639 /** 640 * @bug 4100302 641 */ 642 643 // {sfb} not applicable in C++?? 644 645 void DateFormatRegressionTest::Test4100302(void) 646 { 647 /* Locale locales [] = { 648 Locale::CANADA, 649 Locale::CANADA_FRENCH, 650 Locale::CHINA, 651 Locale::CHINESE, 652 Locale::ENGLISH, 653 Locale::FRANCE, 654 Locale::FRENCH, 655 Locale::GERMAN, 656 Locale::GERMANY, 657 Locale::ITALIAN, 658 Locale::ITALY, 659 Locale::JAPAN, 660 Locale::JAPANESE, 661 Locale::KOREA, 662 Locale::KOREAN, 663 Locale::PRC, 664 Locale::SIMPLIFIED_CHINESE, 665 Locale::TAIWAN, 666 Locale::TRADITIONAL_CHINESE, 667 Locale::UK, 668 Locale::US 669 }; 670 //try { 671 UBool pass = TRUE; 672 for(int i = 0; i < 21; i++) { 673 674 Format *format = DateFormat::createDateTimeInstance(DateFormat::FULL, 675 DateFormat::FULL, locales[i]); 676 byte[] bytes; 677 678 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 679 ObjectOutputStream oos = new ObjectOutputStream(baos); 680 681 oos.writeObject(format); 682 oos.flush(); 683 684 baos.close(); 685 bytes = baos.toByteArray(); 686 687 ObjectInputStream ois = 688 new ObjectInputStream(new ByteArrayInputStream(bytes)); 689 690 if (!format.equals(ois.readObject())) { 691 pass = FALSE; 692 logln("DateFormat instance for locale " + 693 locales[i] + " is incorrectly serialized/deserialized."); 694 } else { 695 logln("DateFormat instance for locale " + 696 locales[i] + " is OKAY."); 697 } 698 } 699 if (!pass) errln("Fail: DateFormat serialization/equality bug"); 700 } 701 catch (IOException e) { 702 errln("Fail: " + e); 703 e.printStackTrace(); 704 } 705 catch (ClassNotFoundException e) { 706 errln("Fail: " + e); 707 e.printStackTrace(); 708 } 709 */} 710 711 /** 712 * @bug 4101483 713 */ 714 void DateFormatRegressionTest::Test4101483(void) 715 { 716 UErrorCode status = U_ZERO_ERROR; 717 SimpleDateFormat *sdf = new SimpleDateFormat(UnicodeString("z"), Locale::getUS(), status); 718 if (failure(status, "new SimpleDateFormat", TRUE)) return; 719 FieldPosition fp(UDAT_TIMEZONE_FIELD); 720 //Date d = date(9234567890L); 721 UDate d = 9234567890.0; 722 //StringBuffer buf = new StringBuffer(""); 723 UnicodeString buf; 724 sdf->format(d, buf, fp); 725 //logln(sdf.format(d, buf, fp).toString()); 726 logln(dateToString(d) + " => " + buf); 727 logln(UnicodeString("beginIndex = ") + fp.getBeginIndex()); 728 logln(UnicodeString("endIndex = ") + fp.getEndIndex()); 729 if (fp.getBeginIndex() == fp.getEndIndex()) 730 errln("Fail: Empty field"); 731 732 delete sdf; 733 } 734 735 /** 736 * @bug 4103340 737 * @bug 4138203 738 * This bug really only works in Locale.US, since that's what the locale 739 * used for Date.toString() is. Bug 4138203 reports that it fails on Korean 740 * NT; it would actually have failed on any non-US locale. Now it should 741 * work on all locales. 742 */ 743 void DateFormatRegressionTest::Test4103340(void) 744 { 745 UErrorCode status = U_ZERO_ERROR; 746 747 // choose a date that is the FIRST of some month 748 // and some arbitrary time 749 UDate d = date(97, 3, 1, 1, 1, 1); 750 SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("MMMM"), Locale::getUS(), status); 751 if (failure(status, "new SimpleDateFormat", TRUE)) return; 752 753 UnicodeString s; 754 s = dateToString(d, s); 755 UnicodeString s2; 756 FieldPosition pos(FieldPosition::DONT_CARE); 757 s2 = df->format(d, s2, pos); 758 logln("Date=" + s); 759 logln("DF=" + s2); 760 UnicodeString substr; 761 s2.extract(0,2, substr); 762 if (s.indexOf(substr) == -1) 763 errln("Months should match"); 764 765 delete df; 766 } 767 768 /** 769 * @bug 4103341 770 */ 771 void DateFormatRegressionTest::Test4103341(void) 772 { 773 TimeZone *saveZone =TimeZone::createDefault(); 774 //try { 775 776 // {sfb} changed from setDefault to adoptDefault 777 TimeZone::adoptDefault(TimeZone::createTimeZone("CST")); 778 UErrorCode status = U_ZERO_ERROR; 779 SimpleDateFormat *simple = new SimpleDateFormat(UnicodeString("MM/dd/yyyy HH:mm"), status); 780 if(U_FAILURE(status)) { 781 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status)); 782 delete simple; 783 return; 784 } 785 failure(status, "new SimpleDateFormat"); 786 TimeZone *temp = TimeZone::createDefault(); 787 if(simple->getTimeZone() != *temp) 788 errln("Fail: SimpleDateFormat not using default zone"); 789 //} 790 //finally { 791 TimeZone::adoptDefault(saveZone); 792 //} 793 794 delete temp; 795 delete simple; 796 } 797 798 /** 799 * @bug 4104136 800 */ 801 void DateFormatRegressionTest::Test4104136(void) 802 { 803 UErrorCode status = U_ZERO_ERROR; 804 SimpleDateFormat *sdf = new SimpleDateFormat(status); 805 if(U_FAILURE(status)) { 806 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status)); 807 delete sdf; 808 return; 809 } 810 if(failure(status, "new SimpleDateFormat")) return; 811 UnicodeString pattern = "'time' hh:mm"; 812 sdf->applyPattern(pattern); 813 logln("pattern: \"" + pattern + "\""); 814 815 UnicodeString strings [] = { 816 (UnicodeString)"time 10:30", 817 (UnicodeString) "time 10:x", 818 (UnicodeString) "time 10x" 819 }; 820 821 ParsePosition ppos [] = { 822 ParsePosition(10), 823 ParsePosition(0), 824 ParsePosition(0) 825 }; 826 827 UDate dates [] = { 828 date(70, UCAL_JANUARY, 1, 10, 30), 829 -1, 830 -1 831 }; 832 833 /*Object[] DATA = { 834 "time 10:30", new ParsePosition(10), new Date(70, Calendar.JANUARY, 1, 10, 30), 835 "time 10:x", new ParsePosition(0), null, 836 "time 10x", new ParsePosition(0), null, 837 };*/ 838 839 for(int i = 0; i < 3; i++) { 840 UnicodeString text = strings[i]; 841 ParsePosition finish = ppos[i]; 842 UDate exp = dates[i]; 843 844 ParsePosition pos(0); 845 UDate d = sdf->parse(text, pos); 846 logln(" text: \"" + text + "\""); 847 logln(" index: %d", pos.getIndex()); 848 logln((UnicodeString) " result: " + d); 849 if(pos.getIndex() != finish.getIndex()) 850 errln(UnicodeString("Fail: Expected pos ") + finish.getIndex()); 851 if (! ((d == 0 && exp == -1) || (d == exp))) 852 errln((UnicodeString) "Fail: Expected result " + exp); 853 } 854 855 delete sdf; 856 } 857 858 /** 859 * @bug 4104522 860 * CANNOT REPRODUCE 861 * According to the bug report, this test should throw a 862 * StringIndexOutOfBoundsException during the second parse. However, 863 * this is not seen. 864 */ 865 void DateFormatRegressionTest::Test4104522(void) 866 { 867 UErrorCode status = U_ZERO_ERROR; 868 869 SimpleDateFormat *sdf = new SimpleDateFormat(status); 870 if(U_FAILURE(status)) { 871 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status)); 872 delete sdf; 873 return; 874 } 875 failure(status, "new SimpleDateFormat"); 876 UnicodeString pattern = "'time' hh:mm"; 877 sdf->applyPattern(pattern); 878 logln("pattern: \"" + pattern + "\""); 879 880 // works correctly 881 ParsePosition pp(0); 882 UnicodeString text = "time "; 883 UDate dt = sdf->parse(text, pp); 884 logln(" text: \"" + text + "\"" + 885 " date: " + dt); 886 887 // works wrong 888 pp.setIndex(0); 889 text = "time"; 890 dt = sdf->parse(text, pp); 891 logln(" text: \"" + text + "\"" + 892 " date: " + dt); 893 894 delete sdf; 895 } 896 897 /** 898 * @bug 4106807 899 */ 900 void DateFormatRegressionTest::Test4106807(void) 901 { 902 UDate dt; 903 DateFormat *df = DateFormat::createDateTimeInstance(); 904 905 UErrorCode status = U_ZERO_ERROR; 906 SimpleDateFormat *sdfs [] = { 907 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss"), status), 908 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'Z'"), status), 909 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss''"), status), 910 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'a''a'"), status), 911 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss %"), status) 912 }; 913 if(U_FAILURE(status)) { 914 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status)); 915 delete sdfs[0]; 916 delete sdfs[1]; 917 delete sdfs[2]; 918 delete sdfs[3]; 919 delete sdfs[4]; 920 return; 921 } 922 923 failure(status, "new SimpleDateFormat"); 924 925 UnicodeString strings [] = { 926 (UnicodeString) "19980211140000", 927 (UnicodeString) "19980211140000", 928 (UnicodeString) "19980211140000", 929 (UnicodeString) "19980211140000a", 930 (UnicodeString) "19980211140000 " 931 }; 932 933 /*Object[] data = { 934 new SimpleDateFormat("yyyyMMddHHmmss"), "19980211140000", 935 new SimpleDateFormat("yyyyMMddHHmmss'Z'"), "19980211140000", 936 new SimpleDateFormat("yyyyMMddHHmmss''"), "19980211140000", 937 new SimpleDateFormat("yyyyMMddHHmmss'a''a'"), "19980211140000a", 938 new SimpleDateFormat("yyyyMMddHHmmss %"), "19980211140000 ", 939 };*/ 940 GregorianCalendar *gc = new GregorianCalendar(status); 941 failure(status, "new GregorianCalendar"); 942 TimeZone *timeZone = TimeZone::createDefault(); 943 944 TimeZone *gmt = timeZone->clone(); 945 946 gmt->setRawOffset(0); 947 948 for(int32_t i = 0; i < 5; i++) { 949 SimpleDateFormat *format = sdfs[i]; 950 UnicodeString dateString = strings[i]; 951 //try { 952 format->setTimeZone(*gmt); 953 dt = format->parse(dateString, status); 954 // {sfb} some of these parses will fail purposely 955 if(U_FAILURE(status)) 956 break; 957 status = U_ZERO_ERROR; 958 UnicodeString fmtd; 959 FieldPosition pos(FieldPosition::DONT_CARE); 960 fmtd = df->format(dt, fmtd, pos); 961 logln(fmtd); 962 //logln(df->format(dt)); 963 gc->setTime(dt, status); 964 failure(status, "gc->getTime"); 965 logln(UnicodeString("") + gc->get(UCAL_ZONE_OFFSET, status)); 966 failure(status, "gc->get"); 967 UnicodeString s; 968 s = format->format(dt, s, pos); 969 logln(s); 970 /*} 971 catch (ParseException e) { 972 logln("No way Jose"); 973 }*/ 974 } 975 976 delete timeZone; 977 delete df; 978 for(int32_t j = 0; j < 5; j++) 979 delete sdfs [j]; 980 delete gc; 981 delete gmt; 982 } 983 984 /* 985 Synopsis: Chinese time zone CTT is not recogonized correctly. 986 Description: Platform Chinese Windows 95 - ** Time zone set to CST ** 987 */ 988 /** 989 * @bug 4108407 990 */ 991 992 // {sfb} what to do with this one ?? 993 void DateFormatRegressionTest::Test4108407(void) 994 { 995 /*long l = System.currentTimeMillis(); 996 logln("user.timezone = " + System.getProperty("user.timezone", "?")); 997 logln("Time Zone :" + 998 DateFormat.getDateInstance().getTimeZone().getID()); 999 logln("Default format :" + 1000 DateFormat.getDateInstance().format(new Date(l))); 1001 logln("Full format :" + 1002 DateFormat.getDateInstance(DateFormat.FULL).format(new 1003 Date(l))); 1004 logln("*** Set host TZ to CST ***"); 1005 logln("*** THE RESULTS OF THIS TEST MUST BE VERIFIED MANUALLY ***");*/ 1006 } 1007 1008 /** 1009 * @bug 4134203 1010 * SimpleDateFormat won't parse "GMT" 1011 */ 1012 void DateFormatRegressionTest::Test4134203(void) 1013 { 1014 UErrorCode status = U_ZERO_ERROR; 1015 UnicodeString dateFormat = "MM/dd/yy HH:mm:ss zzz"; 1016 SimpleDateFormat *fmt = new SimpleDateFormat(dateFormat, status); 1017 if (failure(status, "new SimpleDateFormat", TRUE)) return; 1018 ParsePosition p0(0); 1019 UDate d = fmt->parse("01/22/92 04:52:00 GMT", p0); 1020 logln(dateToString(d)); 1021 if(p0 == ParsePosition(0)) 1022 errln("Fail: failed to parse 'GMT'"); 1023 // In the failure case an exception is thrown by parse(); 1024 // if no exception is thrown, the test passes. 1025 1026 delete fmt; 1027 } 1028 1029 /** 1030 * @bug 4151631 1031 * SimpleDateFormat incorrect handling of 2 single quotes in format() 1032 */ 1033 void DateFormatRegressionTest::Test4151631(void) 1034 { 1035 UnicodeString pattern = "'TO_DATE('''dd'-'MM'-'yyyy HH:mm:ss''' , ''DD-MM-YYYY HH:MI:SS'')'"; 1036 logln("pattern=" + pattern); 1037 UErrorCode status = U_ZERO_ERROR; 1038 SimpleDateFormat *format = new SimpleDateFormat(pattern, Locale::getUS(), status); 1039 if (failure(status, "new SimpleDateFormat", TRUE)) return; 1040 UnicodeString result; 1041 FieldPosition pos(FieldPosition::DONT_CARE); 1042 result = format->format(date(1998-1900, UCAL_JUNE, 30, 13, 30, 0), result, pos); 1043 if (result != "TO_DATE('30-06-1998 13:30:00' , 'DD-MM-YYYY HH:MI:SS')") { 1044 errln("Fail: result=" + result); 1045 } 1046 else { 1047 logln("Pass: result=" + result); 1048 } 1049 1050 delete format; 1051 } 1052 1053 /** 1054 * @bug 4151706 1055 * 'z' at end of date format throws index exception in SimpleDateFormat 1056 * CANNOT REPRODUCE THIS BUG ON 1.2FCS 1057 */ 1058 void DateFormatRegressionTest::Test4151706(void) 1059 { 1060 UnicodeString dateString("Thursday, 31-Dec-98 23:00:00 GMT"); 1061 UErrorCode status = U_ZERO_ERROR; 1062 SimpleDateFormat fmt(UnicodeString("EEEE, dd-MMM-yy HH:mm:ss z"), Locale::getUS(), status); 1063 if (failure(status, "new SimpleDateFormat", TRUE)) return; 1064 //try { 1065 UDate d = fmt.parse(dateString, status); 1066 failure(status, "fmt->parse"); 1067 // {sfb} what about next two lines? 1068 //if (d.getTime() != Date.UTC(1998-1900, Calendar.DECEMBER, 31, 23, 0, 0)) 1069 // errln("Incorrect value: " + d); 1070 /*} catch (Exception e) { 1071 errln("Fail: " + e); 1072 }*/ 1073 UnicodeString temp; 1074 FieldPosition pos(0); 1075 logln(dateString + " -> " + fmt.format(d, temp, pos)); 1076 } 1077 1078 /** 1079 * @bug 4162071 1080 * Cannot reproduce this bug under 1.2 FCS -- it may be a convoluted duplicate 1081 * of some other bug that has been fixed. 1082 */ 1083 void 1084 DateFormatRegressionTest::Test4162071(void) 1085 { 1086 UnicodeString dateString("Thu, 30-Jul-1999 11:51:14 GMT"); 1087 UnicodeString format("EEE', 'dd-MMM-yyyy HH:mm:ss z"); // RFC 822/1123 1088 UErrorCode status = U_ZERO_ERROR; 1089 SimpleDateFormat df(format, Locale::getUS(), status); 1090 if(U_FAILURE(status)) { 1091 dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status)); 1092 return; 1093 } 1094 1095 //try { 1096 UDate x = df.parse(dateString, status); 1097 if(U_SUCCESS(status)) 1098 logln("Parse format \"" + format + "\" ok"); 1099 else 1100 errln("Parse format \"" + format + "\" failed."); 1101 UnicodeString temp; 1102 FieldPosition pos(0); 1103 logln(dateString + " -> " + df.format(x, temp, pos)); 1104 //} catch (Exception e) { 1105 // errln("Parse format \"" + format + "\" failed."); 1106 //} 1107 } 1108 1109 /** 1110 * DateFormat shouldn't parse year "-1" as a two-digit year (e.g., "-1" -> 1999). 1111 */ 1112 void DateFormatRegressionTest::Test4182066(void) { 1113 UErrorCode status = U_ZERO_ERROR; 1114 SimpleDateFormat fmt("MM/dd/yy", Locale::getUS(), status); 1115 SimpleDateFormat dispFmt("MMM dd yyyy GG", Locale::getUS(), status); 1116 if (U_FAILURE(status)) { 1117 dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status)); 1118 return; 1119 } 1120 1121 /* We expect 2-digit year formats to put 2-digit years in the right 1122 * window. Out of range years, that is, anything less than "00" or 1123 * greater than "99", are treated as literal years. So "1/2/3456" 1124 * becomes 3456 AD. Likewise, "1/2/-3" becomes -3 AD == 2 BC. 1125 */ 1126 const char* STRINGS[] = { 1127 "02/29/00", 1128 "01/23/01", 1129 "04/05/-1", 1130 "01/23/-9", 1131 "11/12/1314", 1132 "10/31/1", 1133 "09/12/+1", 1134 "09/12/001", 1135 }; 1136 int32_t STRINGS_COUNT = (int32_t)(sizeof(STRINGS) / sizeof(STRINGS[0])); 1137 UDate FAIL_DATE = (UDate) 0; 1138 UDate DATES[] = { 1139 date(2000-1900, UCAL_FEBRUARY, 29), 1140 date(2001-1900, UCAL_JANUARY, 23), 1141 date( -1-1900, UCAL_APRIL, 5), 1142 date( -9-1900, UCAL_JANUARY, 23), 1143 date(1314-1900, UCAL_NOVEMBER, 12), 1144 date( 1-1900, UCAL_OCTOBER, 31), 1145 FAIL_DATE, // "+1" isn't recognized by US NumberFormat 1146 date( 1-1900, UCAL_SEPTEMBER,12), 1147 }; 1148 1149 UnicodeString out; 1150 UBool pass = TRUE; 1151 for (int32_t i=0; i<STRINGS_COUNT; ++i) { 1152 UnicodeString str(STRINGS[i]); 1153 UDate expected = DATES[i]; 1154 status = U_ZERO_ERROR; 1155 UDate actual = fmt.parse(str, status); 1156 if (U_FAILURE(status)) { 1157 actual = FAIL_DATE; 1158 } 1159 UnicodeString actStr; 1160 if (actual == FAIL_DATE) { 1161 actStr.append("null"); 1162 } else { 1163 // Yuck: See j25 1164 ((DateFormat*)&dispFmt)->format(actual, actStr); 1165 } 1166 1167 if (expected == actual) { 1168 out.append(str + " => " + actStr + "\n"); 1169 } else { 1170 UnicodeString expStr; 1171 if (expected == FAIL_DATE) { 1172 expStr.append("null"); 1173 } else { 1174 // Yuck: See j25 1175 ((DateFormat*)&dispFmt)->format(expected, expStr); 1176 } 1177 out.append("FAIL: " + str + " => " + actStr 1178 + ", expected " + expStr + "\n"); 1179 pass = FALSE; 1180 } 1181 } 1182 if (pass) { 1183 log(out); 1184 } else { 1185 err(out); 1186 } 1187 } 1188 1189 /** 1190 * j32 {JDK Bug 4210209 4209272} 1191 * DateFormat cannot parse Feb 29 2000 when setLenient(false) 1192 */ 1193 void 1194 DateFormatRegressionTest::Test4210209(void) { 1195 UErrorCode status = U_ZERO_ERROR; 1196 UnicodeString pattern("MMM d, yyyy"); 1197 SimpleDateFormat sfmt(pattern, Locale::getUS(), status); 1198 SimpleDateFormat sdisp("MMM dd yyyy GG", Locale::getUS(), status); 1199 DateFormat& fmt = *(DateFormat*)&sfmt; // Yuck: See j25 1200 DateFormat& disp = *(DateFormat*)&sdisp; // Yuck: See j25 1201 if (U_FAILURE(status)) { 1202 dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status)); 1203 return; 1204 } 1205 Calendar* calx = (Calendar*)fmt.getCalendar(); // cast away const! 1206 calx->setLenient(FALSE); 1207 UDate d = date(2000-1900, UCAL_FEBRUARY, 29); 1208 UnicodeString s, ss; 1209 fmt.format(d, s); 1210 logln(disp.format(d, ss.remove()) + " f> " + pattern + 1211 " => \"" + s + "\""); 1212 ParsePosition pos(0); 1213 d = fmt.parse(s, pos); 1214 logln(UnicodeString("\"") + s + "\" p> " + pattern + 1215 " => " + disp.format(d, ss.remove())); 1216 logln(UnicodeString("Parse pos = ") + pos.getIndex() + 1217 ", error pos = " + pos.getErrorIndex()); 1218 if (pos.getErrorIndex() != -1) { 1219 errln(UnicodeString("FAIL: Error index should be -1")); 1220 } 1221 1222 // The underlying bug is in GregorianCalendar. If the following lines 1223 // succeed, the bug is fixed. If the bug isn't fixed, they will throw 1224 // an exception. 1225 GregorianCalendar cal(status); 1226 if (U_FAILURE(status)) { 1227 errln("FAIL: Unable to create Calendar"); 1228 return; 1229 } 1230 cal.clear(); 1231 cal.setLenient(FALSE); 1232 cal.set(2000, UCAL_FEBRUARY, 29); // This should work! 1233 logln(UnicodeString("Attempt to set Calendar to Feb 29 2000: ") + 1234 disp.format(cal.getTime(status), ss.remove())); 1235 if (U_FAILURE(status)) { 1236 errln("FAIL: Unable to set Calendar to Feb 29 2000"); 1237 } 1238 } 1239 1240 void DateFormatRegressionTest::Test714(void) 1241 { 1242 //try { 1243 UDate d(978103543000.); 1244 DateFormat *fmt = DateFormat::createDateTimeInstance(DateFormat::NONE, 1245 DateFormat::MEDIUM, 1246 Locale::getUS()); 1247 if (fmt == NULL) { 1248 dataerrln("Error calling DateFormat::createDateTimeInstance"); 1249 return; 1250 } 1251 1252 UnicodeString s; 1253 UnicodeString tests = 1254 (UnicodeString) "7:25:43 AM" ; 1255 UErrorCode status = U_ZERO_ERROR; 1256 fmt->format (d,s); 1257 if(U_FAILURE(status)) 1258 { 1259 errln((UnicodeString) "Fail, errmsg " + u_errorName(status)); 1260 return; 1261 } 1262 1263 if(s != tests) 1264 { 1265 errln((UnicodeString) "Fail: " + s + " != " + tests); 1266 } 1267 else 1268 { 1269 logln("OK: " + s + " == " + tests); 1270 } 1271 1272 delete fmt; 1273 } 1274 1275 class Test1684Data { 1276 public: 1277 int32_t year; 1278 int32_t month; 1279 int32_t date; 1280 int32_t womyear; 1281 int32_t wommon; 1282 int32_t wom; 1283 int32_t dow; 1284 UnicodeString data; 1285 UnicodeString normalized; 1286 1287 Test1684Data(int32_t xyear, int32_t xmonth, int32_t xdate, 1288 int32_t xwomyear, int32_t xwommon, int32_t xwom, int32_t xdow, 1289 const char *xdata, const char *xnormalized) : 1290 year(xyear), 1291 month(xmonth-1), 1292 date(xdate), 1293 womyear(xwomyear), 1294 wommon(xwommon-1), 1295 wom(xwom), 1296 dow(xdow), 1297 data(xdata,""), 1298 normalized((xnormalized==NULL)?xdata:xnormalized,"") 1299 { } 1300 }; 1301 1302 void DateFormatRegressionTest::Test1684(void) 1303 { 1304 // July 2001 August 2001 January 2002 1305 // Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1306 // 1 2 3 4 5 6 7 1 2 3 4 1 2 3 4 5 1307 // 8 9 10 11 12 13 14 5 6 7 8 9 10 11 6 7 8 9 10 11 12 1308 // 15 16 17 18 19 20 21 12 13 14 15 16 17 18 13 14 15 16 17 18 19 1309 // 22 23 24 25 26 27 28 19 20 21 22 23 24 25 20 21 22 23 24 25 26 1310 // 29 30 31 26 27 28 29 30 31 27 28 29 30 31 1311 Test1684Data *tests[] = { 1312 new Test1684Data(2001, 8, 6, 2001,8,2,UCAL_MONDAY, "2001 08 02 Mon", NULL), 1313 new Test1684Data(2001, 8, 7, 2001,8,2,UCAL_TUESDAY, "2001 08 02 Tue", NULL), 1314 new Test1684Data(2001, 8, 5,/*12,*/ 2001,8,2,UCAL_SUNDAY, "2001 08 02 Sun", NULL), 1315 new Test1684Data(2001, 8,6, /*7, 30,*/ 2001,7,6,UCAL_MONDAY, "2001 07 06 Mon", "2001 08 02 Mon"), 1316 new Test1684Data(2001, 8,7, /*7, 31,*/ 2001,7,6,UCAL_TUESDAY, "2001 07 06 Tue", "2001 08 02 Tue"), 1317 new Test1684Data(2001, 8, 5, 2001,7,6,UCAL_SUNDAY, "2001 07 06 Sun", "2001 08 02 Sun"), 1318 new Test1684Data(2001, 7, 30, 2001,8,1,UCAL_MONDAY, "2001 08 01 Mon", "2001 07 05 Mon"), 1319 new Test1684Data(2001, 7, 31, 2001,8,1,UCAL_TUESDAY, "2001 08 01 Tue", "2001 07 05 Tue"), 1320 new Test1684Data(2001, 7,29, /*8, 5,*/ 2001,8,1,UCAL_SUNDAY, "2001 08 01 Sun", "2001 07 05 Sun"), 1321 new Test1684Data(2001, 12, 31, 2001,12,6,UCAL_MONDAY, "2001 12 06 Mon", NULL), 1322 new Test1684Data(2002, 1, 1, 2002,1,1,UCAL_TUESDAY, "2002 01 01 Tue", NULL), 1323 new Test1684Data(2002, 1, 2, 2002,1,1,UCAL_WEDNESDAY, "2002 01 01 Wed", NULL), 1324 new Test1684Data(2002, 1, 3, 2002,1,1,UCAL_THURSDAY, "2002 01 01 Thu", NULL), 1325 new Test1684Data(2002, 1, 4, 2002,1,1,UCAL_FRIDAY, "2002 01 01 Fri", NULL), 1326 new Test1684Data(2002, 1, 5, 2002,1,1,UCAL_SATURDAY, "2002 01 01 Sat", NULL), 1327 new Test1684Data(2001,12,30, /*2002, 1, 6,*/ 2002,1,1,UCAL_SUNDAY, "2002 01 01 Sun", "2001 12 06 Sun") 1328 }; 1329 1330 #define kTest1684Count ((int32_t)(sizeof(tests)/sizeof(tests[0]))) 1331 1332 int32_t pass = 0, error = 0, warning = 0; 1333 int32_t i; 1334 1335 UErrorCode status = U_ZERO_ERROR; 1336 UnicodeString pattern("yyyy MM WW EEE",""); 1337 Calendar *cal = new GregorianCalendar(status); 1338 SimpleDateFormat *sdf = new SimpleDateFormat(pattern,status); 1339 if (U_FAILURE(status)) { 1340 dataerrln("Error constructing SimpleDateFormat"); 1341 for(i=0;i<kTest1684Count;i++) { 1342 delete tests[i]; 1343 } 1344 delete cal; 1345 delete sdf; 1346 return; 1347 } 1348 cal->setFirstDayOfWeek(UCAL_SUNDAY); 1349 cal->setMinimalDaysInFirstWeek(1); 1350 1351 sdf->adoptCalendar(cal); 1352 1353 cal = sdf->getCalendar()->clone(); // sdf may have deleted calendar 1354 1355 if(!cal || !sdf || U_FAILURE(status)) { 1356 errln(UnicodeString("Error setting up test: ") + u_errorName(status)); 1357 } 1358 1359 for (i = 0; i < kTest1684Count; ++i) { 1360 Test1684Data &test = *(tests[i]); 1361 logln(UnicodeString("#") + i + UnicodeString("\n-----\nTesting round trip of ") + test.year + 1362 " " + (test.month + 1) + 1363 " " + test.date + 1364 " (written as) " + test.data); 1365 1366 cal->clear(); 1367 cal->set(test.year, test.month, test.date); 1368 UDate ms = cal->getTime(status); 1369 1370 cal->clear(); 1371 cal->set(UCAL_YEAR, test.womyear); 1372 cal->set(UCAL_MONTH, test.wommon); 1373 cal->set(UCAL_WEEK_OF_MONTH, test.wom); 1374 cal->set(UCAL_DAY_OF_WEEK, test.dow); 1375 UDate ms2 = cal->getTime(status); 1376 1377 if (ms2 != ms) { 1378 errln((UnicodeString)"\nError: GregorianUCAL_DOM gave " + ms + 1379 "\n GregorianUCAL_WOM gave " + ms2); 1380 error++; 1381 } else { 1382 pass++; 1383 } 1384 1385 ms2 = sdf->parse(test.data, status); 1386 if(U_FAILURE(status)) { 1387 errln("parse exception: " + UnicodeString(u_errorName(status))); 1388 } 1389 1390 if (ms2!=ms) { 1391 errln((UnicodeString)"\nError: GregorianCalendar gave " + ms + 1392 "\n SimpleDateFormat.parse gave " + ms2); 1393 error++; 1394 } else { 1395 pass++; 1396 } 1397 1398 UnicodeString result; 1399 sdf->format(ms, result); 1400 if (result != test.normalized) { 1401 errln("\nWarning: format of '" + test.data + "' gave" + 1402 "\n '" + result + "'" + 1403 "\n expected '" + test.normalized + "'"); 1404 warning++; 1405 } else { 1406 pass++; 1407 } 1408 1409 UDate ms3; 1410 ms3 = sdf->parse(result, status); 1411 if(U_FAILURE(status)) { 1412 errln("parse exception 2: " + (UnicodeString)u_errorName(status)); 1413 } 1414 1415 if (ms3!=ms) { 1416 error++; 1417 errln((UnicodeString)"\nError: Re-parse of '" + result + "' gave time of " + 1418 "\n " + ms3 + 1419 "\n not " + ms); 1420 } else { 1421 pass++; 1422 } 1423 } 1424 1425 UnicodeString info 1426 = UnicodeString("Passed: ") + pass + ", Warnings: " + warning + ", Errors: " + error; 1427 if (error > 0) { 1428 errln(info); 1429 } else { 1430 logln(info); 1431 } 1432 1433 for(i=0;i<kTest1684Count;i++) { 1434 delete tests[i]; 1435 } 1436 delete cal; 1437 delete sdf; 1438 } 1439 1440 void DateFormatRegressionTest::Test5554(void) 1441 { 1442 UErrorCode status = U_ZERO_ERROR; 1443 UnicodeString pattern("Z",""); 1444 UnicodeString newfoundland("Canada/Newfoundland", ""); 1445 TimeZone *zone = TimeZone::createTimeZone(newfoundland); 1446 Calendar *cal = new GregorianCalendar(zone, status); 1447 SimpleDateFormat *sdf = new SimpleDateFormat(pattern,status); 1448 if (U_FAILURE(status)) { 1449 dataerrln("Error constructing SimpleDateFormat"); 1450 delete cal; 1451 delete sdf; 1452 return; 1453 } 1454 cal->set(2007, 1, 14); 1455 UDate date = cal->getTime(status); 1456 if (U_FAILURE(status)) { 1457 errln("Error getting time to format"); 1458 return; 1459 }; 1460 sdf->adoptCalendar(cal); 1461 UnicodeString result; 1462 UnicodeString correct("-0330", ""); 1463 sdf->format(date, result); 1464 if (result != correct) { 1465 errln("\nError: Newfoundland Z of Jan 14, 2007 gave '" + result + "', expected '" + correct + "'"); 1466 } 1467 delete sdf; 1468 } 1469 1470 void DateFormatRegressionTest::Test9237(void) 1471 { 1472 UErrorCode status = U_ZERO_ERROR; 1473 UnicodeString pattern("VVVV"); 1474 1475 SimpleDateFormat fmt(pattern, status); // default locale 1476 SimpleDateFormat fmtDE(pattern, Locale("de_DE"), status); 1477 if (U_FAILURE(status)) { 1478 dataerrln("Error constructing SimpleDateFormat"); 1479 return; 1480 } 1481 1482 // copy constructor 1483 SimpleDateFormat fmtCopyDE(fmtDE); 1484 UnicodeString resDE, resCopyDE; 1485 1486 fmtDE.format(0.0, resDE); 1487 fmtCopyDE.format(0.0, resCopyDE); 1488 1489 if (resDE != resCopyDE) { 1490 errln(UnicodeString("Error: different result by the copied instance - org:") + resDE + " copy:" + resCopyDE); 1491 } 1492 1493 // test for assignment operator 1494 fmt = fmtDE; 1495 1496 UnicodeString resAssigned; 1497 fmt.format(0.0, resAssigned); 1498 1499 if (resDE != resAssigned) { 1500 errln(UnicodeString("Error: different results by the assigned instance - org:") + resDE + " assigned:" + resAssigned); 1501 } 1502 } 1503 1504 void DateFormatRegressionTest::TestParsing(void) { 1505 UErrorCode status = U_ZERO_ERROR; 1506 UnicodeString pattern("EEE-WW-MMMM-yyyy"); 1507 UnicodeString text("mon-02-march-2011"); 1508 int32_t expectedDay = 7; 1509 1510 SimpleDateFormat format(pattern, status); 1511 if (U_FAILURE(status)) { 1512 dataerrln("Unable to create SimpleDateFormat - %s", u_errorName(status)); 1513 return; 1514 } 1515 1516 Calendar *cal = new GregorianCalendar(status); 1517 if (cal == NULL || U_FAILURE(status)) { 1518 errln("Unable to create calendar - %s", u_errorName(status)); 1519 return; 1520 } 1521 1522 ParsePosition pos(0); 1523 format.parse(text, *cal, pos); 1524 1525 if (cal->get(UCAL_DAY_OF_MONTH, status) != expectedDay) { 1526 errln("Parsing failed: day of month should be '7' with pattern: \"" + pattern + "\" for text: \"" + text + "\""); 1527 } 1528 1529 delete cal; 1530 } 1531 1532 void DateFormatRegressionTest::TestT10334(void) { 1533 UErrorCode status = U_ZERO_ERROR; 1534 UnicodeString pattern("'--: 'EEE-WW-MMMM-yyyy"); 1535 UnicodeString text("--mon-02-march-2011"); 1536 SimpleDateFormat format(pattern, status); 1537 1538 logln("pattern["+pattern+"] text["+text+"]"); 1539 1540 if (U_FAILURE(status)) { 1541 dataerrln("Fail creating SimpleDateFormat object - %s", u_errorName(status)); 1542 return; 1543 } 1544 1545 format.setBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH, FALSE, status); 1546 format.parse(text, status); 1547 if (!U_FAILURE(status)) { 1548 errln("parse partial match did NOT fail in strict mode - %s", u_errorName(status)); 1549 } 1550 1551 status = U_ZERO_ERROR; 1552 format.setBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH, TRUE, status); 1553 format.parse(text, status); 1554 if (U_FAILURE(status)) { 1555 errln("parse partial match failure in lenient mode - %s", u_errorName(status)); 1556 } 1557 1558 status = U_ZERO_ERROR; 1559 pattern = UnicodeString("YYYY MM dd"); 1560 text = UnicodeString("2013 12 10"); 1561 format.applyPattern(pattern); 1562 UDate referenceDate = format.parse(text, status); 1563 1564 FieldPosition fp(0); 1565 UnicodeString formattedString(""); 1566 pattern = UnicodeString("YYYY LL dd ee cc qq QQ"); 1567 format.applyPattern(pattern); 1568 format.format(referenceDate, formattedString, fp, status); 1569 logln("ref date: " + formattedString); 1570 1571 1572 char patternArray[] = "YYYY LLL dd eee ccc qqq QQQ"; 1573 pattern = UnicodeString(patternArray); 1574 text = UnicodeString("2013 12 10 03 3 04 04"); 1575 status = U_ZERO_ERROR; 1576 format.setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, TRUE, status); 1577 format.applyPattern(pattern); 1578 ParsePosition pp(0); 1579 format.parse(text, pp); 1580 if (pp.getErrorIndex() != -1) { 1581 errln("numeric parse error"); 1582 } 1583 1584 status = U_ZERO_ERROR; 1585 format.setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, FALSE, status); 1586 format.parse(text, status); 1587 if (!U_FAILURE(status)) { 1588 errln("numeric parse did NOT fail in strict mode", u_errorName(status)); 1589 } 1590 1591 } 1592 1593 1594 typedef struct { 1595 const char * locale; 1596 UBool leniency; 1597 UnicodeString parseString; 1598 UnicodeString pattern; 1599 UnicodeString expectedResult; // null indicates expected error 1600 } TestDateFormatLeniencyItem; 1601 1602 1603 void DateFormatRegressionTest::TestT10619(void) { 1604 const UDate july022008 = 1215000001979.0; 1605 const TestDateFormatLeniencyItem items[] = { 1606 //locale leniency parse String pattern expected result 1607 { "en", true, UnicodeString("2008-07 02"), UnicodeString("yyyy-LLLL dd"), UnicodeString("2008-July 02") }, 1608 { "en", false, UnicodeString("2008-07 03"), UnicodeString("yyyy-LLLL dd"), UnicodeString("") }, 1609 { "en", true, UnicodeString("2008-Jan. 04"), UnicodeString("yyyy-LLL dd"), UnicodeString("2008-Jan 04") }, 1610 { "en", false, UnicodeString("2008-Jan. 05"), UnicodeString("yyyy-LLL dd"), UnicodeString("") }, 1611 { "en", true, UnicodeString("2008-Jan--06"), UnicodeString("yyyy-MMM -- dd"), UnicodeString("2008-Jan 06") }, 1612 { "en", false, UnicodeString("2008-Jan--07"), UnicodeString("yyyy-MMM -- dd"), UnicodeString("") }, 1613 { "en", true, UnicodeString("6 Jan 08 2008"), UnicodeString("eee MMM dd yyyy"), UnicodeString("Sat Jan 08 2008") }, 1614 { "en", false, UnicodeString("6 Jan 09 2008"), UnicodeString("eee MMM dd yyyy"), UnicodeString("") }, 1615 // terminator 1616 { NULL, true, UnicodeString(""), UnicodeString(""), UnicodeString("") } 1617 }; 1618 UErrorCode status = U_ZERO_ERROR; 1619 Calendar* cal = Calendar::createInstance(status); 1620 if (U_FAILURE(status)) { 1621 dataerrln(UnicodeString("FAIL: Unable to create Calendar for default timezone and locale.")); 1622 } else { 1623 cal->setTime(july022008, status); 1624 const TestDateFormatLeniencyItem * itemPtr; 1625 for (itemPtr = items; itemPtr->locale != NULL; itemPtr++ ) { 1626 1627 Locale locale = Locale::createFromName(itemPtr->locale); 1628 status = U_ZERO_ERROR; 1629 ParsePosition pos(0); 1630 SimpleDateFormat * sdmft = new SimpleDateFormat(itemPtr->pattern, locale, status); 1631 if (U_FAILURE(status)) { 1632 dataerrln("Unable to create SimpleDateFormat - %s", u_errorName(status)); 1633 continue; 1634 } 1635 logln("parsing " + itemPtr->parseString); 1636 sdmft->setLenient(itemPtr->leniency); 1637 sdmft->setBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE, itemPtr->leniency, status); 1638 sdmft->setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, itemPtr->leniency, status); 1639 sdmft->setBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH, itemPtr->leniency, status); 1640 sdmft->parse(itemPtr->parseString, pos); 1641 1642 delete sdmft; 1643 if(pos.getErrorIndex() > -1) { 1644 if(itemPtr->expectedResult.length() != 0) { 1645 errln("error: unexpected error - " + itemPtr->parseString + " - error index " + pos.getErrorIndex() + 1646 " - leniency " + itemPtr->leniency); 1647 continue; 1648 } else { 1649 continue; 1650 } 1651 } 1652 } 1653 } 1654 delete cal; 1655 1656 } 1657 1658 1659 typedef struct { 1660 UnicodeString text; 1661 UnicodeString pattern; 1662 int initialParsePos; 1663 } T10855Data; 1664 1665 void DateFormatRegressionTest::TestT10855(void) { 1666 // NOTE: these should NOT parse 1667 const T10855Data items[] = { 1668 //parse String pattern initial parse pos 1669 { UnicodeString("September 30, 1998"), UnicodeString("MM-dd-yyyy"), 0}, 1670 { UnicodeString("123-73-1950"), UnicodeString("MM-dd-yyyy"), -1}, 1671 { UnicodeString("12-23-1950"), UnicodeString("MM-dd-yyyy"), -1}, 1672 // terminator 1673 { UnicodeString(""), UnicodeString(""), 0} 1674 }; 1675 UErrorCode status = U_ZERO_ERROR; 1676 1677 int x = 0; 1678 while(items[x].pattern.length() > 0) 1679 { 1680 status = U_ZERO_ERROR; 1681 logln("Date to parse: \""+items[x].text+"\""); 1682 logln("Starting Index: %d", items[x].initialParsePos); 1683 1684 SimpleDateFormat dateFmt(items[x].pattern, status); 1685 if(U_FAILURE(status)) { 1686 errcheckln(status, "Failed dateFmt: %s", u_errorName(status)); 1687 ++x; 1688 continue; 1689 } 1690 status = U_ZERO_ERROR; 1691 1692 dateFmt.setLenient(false); 1693 dateFmt.setTimeZone(*TimeZone::getGMT()); 1694 1695 ParsePosition position(items[x].initialParsePos); 1696 logln("set position is now: %d", position.getIndex()); 1697 UDate d = dateFmt.parse(items[x].text, position); 1698 if (position.getErrorIndex() != -1 || position.getIndex() == items[x].initialParsePos) { 1699 logln("Parse Failed. ErrorIndex is %d - Index is %d", position.getErrorIndex(), position.getIndex()); 1700 } else { 1701 errln("Parse Succeeded...should have failed. Index is %d - ErrorIndex is %d", position.getIndex(), position.getErrorIndex()); 1702 } 1703 logln("Parsed date returns %d\n", d); 1704 1705 ++x; 1706 } 1707 } 1708 1709 void DateFormatRegressionTest::TestT10906(void) { 1710 1711 UErrorCode status = U_ZERO_ERROR; 1712 UnicodeString pattern = "MM-dd-yyyy"; 1713 UnicodeString text = "06-10-2014"; 1714 SimpleDateFormat format(pattern, status); 1715 int32_t errorIdx = 0; 1716 ParsePosition pp(-1); 1717 format.parse(text, pp); 1718 errorIdx = pp.getErrorIndex(); 1719 if (errorIdx == -1) { 1720 errln("failed to report invalid (negative) starting parse position"); 1721 } 1722 } 1723 1724 #endif /* #if !UCONFIG_NO_FORMATTING */ 1725 1726 //eof 1727