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