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