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