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