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