1 /********************************************************************
3 * Copyright (c) 1997-2012, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
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"
20 // *****************************************************************************
21 // class DateFormatRegressionTest
22 // *****************************************************************************
24 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
27 DateFormatRegressionTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
29 // if (exec) logln((UnicodeString)"TestSuite DateFormatRegressionTest");
59 default: name
= ""; break;
66 void DateFormatRegressionTest::Test4029195(void)
68 UErrorCode status
= U_ZERO_ERROR
;
70 UDate today
= Calendar::getNow();
71 logln((UnicodeString
) "today: " + today
);
73 SimpleDateFormat
*sdf
= (SimpleDateFormat
*) DateFormat::createDateInstance();
74 if (failure(status
, "SimpleDateFormat::createDateInstance")) {
79 dataerrln("Error calling DateFormat::createDateTimeInstance");
83 pat
= sdf
->toPattern(pat
);
84 logln("pattern: " + pat
);
86 FieldPosition
pos(FieldPosition::DONT_CARE
);
87 fmtd
= sdf
->format(today
, fmtd
, pos
);
88 logln("today: " + fmtd
);
90 sdf
->applyPattern("G yyyy DDD");
92 todayS
= sdf
->format(today
, todayS
, pos
);
93 logln("today: " + todayS
);
95 today
= sdf
->parse(todayS
, status
);
96 failure(status
, "sdf->parse");
97 logln((UnicodeString
)"today date: " + today
);
98 /*} catch(Exception e) {
99 logln("Error reparsing date: " + e.getMessage());
104 rt
= sdf
->format(sdf
->parse(todayS
, status
), rt
, pos
);
105 failure(status
, "sdf->parse");
106 logln("round trip: " + rt
);
108 errln("Fail: Want " + todayS
+ " Got " + rt
);
110 catch (ParseException e) {
121 void DateFormatRegressionTest::Test4052408(void)
124 DateFormat
*fmt
= DateFormat::createDateTimeInstance(DateFormat::SHORT
,
125 DateFormat::SHORT
, Locale::getUS());
127 dataerrln("Error calling DateFormat::createDateTimeInstance");
131 UDate dt
= date(97, UCAL_MAY
, 3, 8, 55);
133 str
= fmt
->format(dt
, str
);
136 if(str
!= "5/3/97 8:55 AM")
137 errln("Fail: Test broken; Want 5/3/97 8:55 AM Got " + str
);
139 UnicodeString expected
[] = {
140 (UnicodeString
) "", //"ERA_FIELD",
141 (UnicodeString
) "97", //"YEAR_FIELD",
142 (UnicodeString
) "5", //"MONTH_FIELD",
143 (UnicodeString
) "3", //"DATE_FIELD",
144 (UnicodeString
) "", //"HOUR_OF_DAY1_FIELD",
145 (UnicodeString
) "", //"HOUR_OF_DAY0_FIELD",
146 (UnicodeString
) "55", //"MINUTE_FIELD",
147 (UnicodeString
) "", //"SECOND_FIELD",
148 (UnicodeString
) "", //"MILLISECOND_FIELD",
149 (UnicodeString
) "", //"DAY_OF_WEEK_FIELD",
150 (UnicodeString
) "", //"DAY_OF_YEAR_FIELD",
151 (UnicodeString
) "", //"DAY_OF_WEEK_IN_MONTH_FIELD",
152 (UnicodeString
) "", //"WEEK_OF_YEAR_FIELD",
153 (UnicodeString
) "", //"WEEK_OF_MONTH_FIELD",
154 (UnicodeString
) "AM", //"AM_PM_FIELD",
155 (UnicodeString
) "8", //"HOUR1_FIELD",
156 (UnicodeString
) "", //"HOUR0_FIELD",
157 (UnicodeString
) "" //"TIMEZONE_FIELD"
160 //Hashtable expected;// = new Hashtable();
161 //expected.put(new LongKey(DateFormat.MONTH_FIELD), "5");
162 //expected.put(new LongKey(DateFormat.DATE_FIELD), "3");
163 //expected.put(new LongKey(DateFormat.YEAR_FIELD), "97");
164 //expected.put(new LongKey(DateFormat.HOUR1_FIELD), "8");
165 //expected.put(new LongKey(DateFormat.MINUTE_FIELD), "55");
166 //expected.put(new LongKey(DateFormat.AM_PM_FIELD), "AM");
168 //StringBuffer buf = new StringBuffer();
169 UnicodeString fieldNames
[] = {
170 (UnicodeString
) "ERA_FIELD",
171 (UnicodeString
) "YEAR_FIELD",
172 (UnicodeString
) "MONTH_FIELD",
173 (UnicodeString
) "DATE_FIELD",
174 (UnicodeString
) "HOUR_OF_DAY1_FIELD",
175 (UnicodeString
) "HOUR_OF_DAY0_FIELD",
176 (UnicodeString
) "MINUTE_FIELD",
177 (UnicodeString
) "SECOND_FIELD",
178 (UnicodeString
) "MILLISECOND_FIELD",
179 (UnicodeString
) "DAY_OF_WEEK_FIELD",
180 (UnicodeString
) "DAY_OF_YEAR_FIELD",
181 (UnicodeString
) "DAY_OF_WEEK_IN_MONTH_FIELD",
182 (UnicodeString
) "WEEK_OF_YEAR_FIELD",
183 (UnicodeString
) "WEEK_OF_MONTH_FIELD",
184 (UnicodeString
) "AM_PM_FIELD",
185 (UnicodeString
) "HOUR1_FIELD",
186 (UnicodeString
) "HOUR0_FIELD",
187 (UnicodeString
) "TIMEZONE_FIELD"
191 for(int i
= 0; i
<= 17; ++i
) {
192 FieldPosition
pos(i
);
194 fmt
->format(dt
, buf
, pos
);
195 //char[] dst = new char[pos.getEndIndex() - pos.getBeginIndex()];
197 buf
.extractBetween(pos
.getBeginIndex(), pos
.getEndIndex(), dst
);
198 UnicodeString
str(dst
);
199 logln((UnicodeString
)"" + i
+ (UnicodeString
)": " + fieldNames
[i
] +
200 (UnicodeString
)", \"" + str
+ (UnicodeString
)"\", " +
201 pos
.getBeginIndex() + (UnicodeString
)", " +
203 UnicodeString exp
= expected
[i
];
204 if((exp
.length() == 0 && str
.length() == 0) || str
== exp
)
207 errln(UnicodeString(" expected ") + exp
);
213 errln("Fail: FieldPosition not set right by DateFormat");
220 * Verify the function of the [s|g]et2DigitYearStart() API.
222 void DateFormatRegressionTest::Test4056591(void)
224 UErrorCode status
= U_ZERO_ERROR
;
227 SimpleDateFormat
*fmt
= new SimpleDateFormat(UnicodeString("yyMMdd"), Locale::getUS(), status
);
228 if (failure(status
, "new SimpleDateFormat", TRUE
)) {
232 UDate start
= date(1809-1900, UCAL_DECEMBER
, 25);
233 fmt
->set2DigitYearStart(start
, status
);
234 failure(status
, "fmt->setTwoDigitStartDate");
235 if( (fmt
->get2DigitYearStart(status
) != start
) || failure(status
, "get2DigitStartDate"))
236 errln("get2DigitYearStart broken");
238 date(1809-1900, UCAL_DECEMBER
, 25),
239 date(1909-1900, UCAL_DECEMBER
, 24),
240 date(1809-1900, UCAL_DECEMBER
, 26),
241 date(1861-1900, UCAL_DECEMBER
, 25),
244 UnicodeString strings
[] = {
245 (UnicodeString
) "091225",
246 (UnicodeString
) "091224",
247 (UnicodeString
) "091226",
248 (UnicodeString
) "611225"
252 "091225", new Date(1809-1900, Calendar.DECEMBER, 25),
253 "091224", new Date(1909-1900, Calendar.DECEMBER, 24),
254 "091226", new Date(1809-1900, Calendar.DECEMBER, 26),
255 "611225", new Date(1861-1900, Calendar.DECEMBER, 25),
258 for(int i
= 0; i
< 4; i
++) {
259 UnicodeString s
= strings
[i
];
260 UDate exp
= dates
[i
];
261 UDate got
= fmt
->parse(s
, status
);
262 failure(status
, "fmt->parse");
263 logln(s
+ " -> " + got
+ "; exp " + exp
);
265 errln("set2DigitYearStart broken");
268 catch (ParseException e) {
279 void DateFormatRegressionTest::Test4059917(void)
281 UErrorCode status
= U_ZERO_ERROR
;
283 SimpleDateFormat
*fmt
;
284 UnicodeString myDate
;
286 fmt
= new SimpleDateFormat( UnicodeString("yyyy/MM/dd"), status
);
287 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
288 myDate
= "1997/01/01";
289 aux917( fmt
, myDate
);
294 fmt
= new SimpleDateFormat( UnicodeString("yyyyMMdd"), status
);
295 if(failure(status
, "new SimpleDateFormat")) return;
297 aux917( fmt
, myDate
);
302 void DateFormatRegressionTest::aux917( SimpleDateFormat
*fmt
, UnicodeString
& str
) {
305 pat
= fmt
->toPattern(pat
);
306 logln( "==================" );
307 logln( "testIt: pattern=" + pat
+
313 ParsePosition
pos(0);
314 fmt
->parseObject( str
, o
, pos
);
315 //logln( UnicodeString("Parsed object: ") + o );
317 UErrorCode status
= U_ZERO_ERROR
;
318 UnicodeString formatted
;
319 FieldPosition
poss(FieldPosition::DONT_CARE
);
320 formatted
= fmt
->format( o
, formatted
, poss
, status
);
321 failure(status
, "fmt->format");
322 logln( "Formatted string: " + formatted
);
323 if( formatted
!= str
)
324 errln("Fail: Want " + str
+ " Got " + formatted
);
326 catch (ParseException e) {
335 void DateFormatRegressionTest::Test4060212(void)
337 UnicodeString dateString
= "1995-040.05:01:29";
339 logln( "dateString= " + dateString
);
340 logln("Using yyyy-DDD.hh:mm:ss");
341 UErrorCode status
= U_ZERO_ERROR
;
342 SimpleDateFormat
*formatter
= new SimpleDateFormat(UnicodeString("yyyy-DDD.hh:mm:ss"), status
);
343 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
344 ParsePosition
pos(0);
345 UDate myDate
= formatter
->parse( dateString
, pos
);
346 UnicodeString myString
;
347 DateFormat
*fmt
= DateFormat::createDateTimeInstance( DateFormat::FULL
,
350 dataerrln("Error calling DateFormat::createDateTimeInstance");
355 myString
= fmt
->format( myDate
, myString
);
358 Calendar
*cal
= new GregorianCalendar(status
);
359 failure(status
, "new GregorianCalendar");
360 cal
->setTime(myDate
, status
);
361 failure(status
, "cal->setTime");
362 if ((cal
->get(UCAL_DAY_OF_YEAR
, status
) != 40) || failure(status
, "cal->get"))
363 errln((UnicodeString
) "Fail: Got " + cal
->get(UCAL_DAY_OF_YEAR
, status
) +
366 // this is an odd usage of "ddd" and it doesn't
367 // work now that date values are range checked per #3579.
368 logln("Using yyyy-ddd.hh:mm:ss");
371 formatter
= new SimpleDateFormat(UnicodeString("yyyy-ddd.hh:mm:ss"), status
);
372 if(failure(status
, "new SimpleDateFormat")) return;
374 myDate
= formatter
->parse( dateString
, pos
);
375 myString
= fmt
->format( myDate
, myString
);
377 cal
->setTime(myDate
, status
);
378 failure(status
, "cal->setTime");
379 if ((cal
->get(UCAL_DAY_OF_YEAR
, status
) != 40) || failure(status
, "cal->get"))
380 errln((UnicodeString
) "Fail: Got " + cal
->get(UCAL_DAY_OF_YEAR
, status
) +
391 void DateFormatRegressionTest::Test4061287(void)
393 UErrorCode status
= U_ZERO_ERROR
;
395 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("dd/MM/yyyy"), status
);
396 if (U_FAILURE(status
)) {
397 dataerrln("Fail new SimpleDateFormat: %s", u_errorName(status
));
401 failure(status
, "new SimpleDateFormat");
403 logln(UnicodeString("") + df
->parse("35/01/1971", status
));
404 failure(status
, "df->parse(\"35/01/1971\")");
405 //logln(df.parse("35/01/1971").toString());
407 /*catch (ParseException e) {
411 df
->setLenient(FALSE
);
414 logln(UnicodeString("") + df
->parse("35/01/1971", status
));
415 if(U_FAILURE(status
))
417 //logln(df.parse("35/01/1971").toString());
418 //} catch (ParseException e) {ok=TRUE;}
420 errln("Fail: Lenient not working");
427 void DateFormatRegressionTest::Test4065240(void)
430 DateFormat
*shortdate
, *fulldate
;
431 UnicodeString strShortDate
, strFullDate
;
432 Locale saveLocale
= Locale::getDefault();
433 TimeZone
*saveZone
= TimeZone::createDefault();
435 UErrorCode status
= U_ZERO_ERROR
;
437 Locale
*curLocale
= new Locale("de","DE");
438 Locale::setDefault(*curLocale
, status
);
439 failure(status
, "Locale::setDefault");
440 // {sfb} adoptDefault instead of setDefault
441 //TimeZone::setDefault(TimeZone::createTimeZone("EST"));
442 TimeZone::adoptDefault(TimeZone::createTimeZone("EST"));
443 curDate
= date(98, 0, 1);
444 shortdate
= DateFormat::createDateInstance(DateFormat::SHORT
);
445 if (shortdate
== NULL
){
446 dataerrln("Error calling DateFormat::createDateInstance");
450 fulldate
= DateFormat::createDateTimeInstance(DateFormat::LONG
, DateFormat::LONG
);
451 if (fulldate
== NULL
){
452 dataerrln("Error calling DateFormat::createDateTimeInstance");
455 strShortDate
= "The current date (short form) is ";
457 temp
= shortdate
->format(curDate
, temp
);
458 strShortDate
+= temp
;
459 strFullDate
= "The current date (long form) is ";
461 fulldate
->format(curDate
, temp2
);
462 strFullDate
+= temp2
;
467 // {sfb} What to do with resource bundle stuff?????
469 // Check to see if the resource is present; if not, we can't test
470 ResourceBundle
*bundle
= new ResourceBundle(
471 NULL
, *curLocale
, status
);
472 failure(status
, "new ResourceBundle");
473 //(UnicodeString) "java.text.resources.DateFormatZoneData", curLocale);
475 // {sfb} API change to ResourceBundle -- add getLocale()
476 /*if (bundle->getLocale().getLanguage(temp) == UnicodeString("de")) {
477 // UPDATE THIS AS ZONE NAME RESOURCE FOR <EST> in de_DE is updated
478 if (!strFullDate.endsWith(UnicodeString("GMT-05:00")))
479 errln("Fail: Want GMT-05:00");
482 logln("*** TEST COULD NOT BE COMPLETED BECAUSE DateFormatZoneData ***");
483 logln("*** FOR LOCALE de OR de_DE IS MISSING ***");
487 Locale::setDefault(saveLocale
, status
);
488 failure(status
, "Locale::setDefault");
489 TimeZone::setDefault(*saveZone
);
499 DateFormat.equals is too narrowly defined. As a result, MessageFormat
500 does not work correctly. DateFormat.equals needs to be written so
501 that the Calendar sub-object is not compared using Calendar.equals,
502 but rather compared for equivalency. This may necessitate adding a
503 (package private) method to Calendar to test for equivalency.
505 Currently this bug breaks MessageFormat.toPattern
510 void DateFormatRegressionTest::Test4071441(void)
512 DateFormat
*fmtA
= DateFormat::createInstance();
513 DateFormat
*fmtB
= DateFormat::createInstance();
515 if (fmtA
== NULL
|| fmtB
== NULL
){
516 dataerrln("Error calling DateFormat::createInstance");
522 // {sfb} Is it OK to cast away const here?
523 Calendar
*calA
= (Calendar
*) fmtA
->getCalendar();
524 Calendar
*calB
= (Calendar
*) fmtB
->getCalendar();
526 errln("Couldn't get proper calendars, exiting");
531 UDate epoch
= date(0, 0, 0);
532 UDate xmas
= date(61, UCAL_DECEMBER
, 25);
534 UErrorCode status
= U_ZERO_ERROR
;
535 calA
->setTime(epoch
, status
);
536 failure(status
, "calA->setTime");
537 calB
->setTime(epoch
, status
);
538 failure(status
, "calB->setTime");
540 errln("Fail: Can't complete test; Calendar instances unequal");
542 errln("Fail: DateFormat unequal when Calendars equal");
543 calB
->setTime(xmas
, status
);
544 failure(status
, "calB->setTime");
546 errln("Fail: Can't complete test; Calendar instances equal");
548 errln("Fail: DateFormat unequal when Calendars equivalent");
550 logln("DateFormat.equals ok");
556 /* The java.text.DateFormat.parse(String) method expects for the
557 US locale a string formatted according to mm/dd/yy and parses it
560 When given a string mm/dd/yyyy [sic] it only parses up to the first
561 two y's, typically resulting in a date in the year 1919.
563 Please extend the parsing method(s) to handle strings with
564 four-digit year values (probably also applicable to various
569 void DateFormatRegressionTest::Test4073003(void)
572 UErrorCode ec
= U_ZERO_ERROR
;
573 SimpleDateFormat
fmt("MM/dd/yy", Locale::getUK(), ec
);
575 dataerrln("FAIL: SimpleDateFormat constructor - %s", u_errorName(ec
));
578 UnicodeString tests
[] = {
579 (UnicodeString
) "12/25/61",
580 (UnicodeString
) "12/25/1961",
581 (UnicodeString
) "4/3/2010",
582 (UnicodeString
) "4/3/10"
584 UErrorCode status
= U_ZERO_ERROR
;
585 for(int i
= 0; i
< 4; i
+=2) {
586 UDate d
= fmt
.parse(tests
[i
], status
);
587 failure(status
, "fmt.parse");
588 UDate dd
= fmt
.parse(tests
[i
+1], status
);
589 failure(status
, "fmt.parse");
591 s
= fmt
.format(d
, s
);
593 ss
= fmt
.format(dd
, ss
);
595 errln((UnicodeString
) "Fail: " + d
+ " != " + dd
);
597 errln((UnicodeString
)"Fail: " + s
+ " != " + ss
);
598 logln("Ok: " + s
+ " " + d
);
605 void DateFormatRegressionTest::Test4089106(void)
607 TimeZone
*def
= TimeZone::createDefault();
609 TimeZone
*z
= new SimpleTimeZone((int)(1.25 * 3600000), "FAKEZONE");
610 TimeZone::setDefault(*z
);
611 UErrorCode status
= U_ZERO_ERROR
;
612 SimpleDateFormat
*f
= new SimpleDateFormat(status
);
613 if(U_FAILURE(status
)) {
614 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
620 failure(status
, "new SimpleDateFormat");
621 if (f
->getTimeZone()!= *z
)
622 errln("Fail: SimpleTimeZone should use TimeZone.getDefault()");
626 TimeZone::setDefault(*def
);
638 // {sfb} not applicable in C++??
640 void DateFormatRegressionTest::Test4100302(void)
642 /* Locale locales [] = {
644 Locale::CANADA_FRENCH,
659 Locale::SIMPLIFIED_CHINESE,
661 Locale::TRADITIONAL_CHINESE,
667 for(int i = 0; i < 21; i++) {
669 Format *format = DateFormat::createDateTimeInstance(DateFormat::FULL,
670 DateFormat::FULL, locales[i]);
673 ByteArrayOutputStream baos = new ByteArrayOutputStream();
674 ObjectOutputStream oos = new ObjectOutputStream(baos);
676 oos.writeObject(format);
680 bytes = baos.toByteArray();
682 ObjectInputStream ois =
683 new ObjectInputStream(new ByteArrayInputStream(bytes));
685 if (!format.equals(ois.readObject())) {
687 logln("DateFormat instance for locale " +
688 locales[i] + " is incorrectly serialized/deserialized.");
690 logln("DateFormat instance for locale " +
691 locales[i] + " is OKAY.");
694 if (!pass) errln("Fail: DateFormat serialization/equality bug");
696 catch (IOException e) {
700 catch (ClassNotFoundException e) {
709 void DateFormatRegressionTest::Test4101483(void)
711 UErrorCode status
= U_ZERO_ERROR
;
712 SimpleDateFormat
*sdf
= new SimpleDateFormat(UnicodeString("z"), Locale::getUS(), status
);
713 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
714 FieldPosition
fp(UDAT_TIMEZONE_FIELD
);
715 //Date d = date(9234567890L);
716 UDate d
= 9234567890.0;
717 //StringBuffer buf = new StringBuffer("");
719 sdf
->format(d
, buf
, fp
);
720 //logln(sdf.format(d, buf, fp).toString());
721 logln(dateToString(d
) + " => " + buf
);
722 logln("beginIndex = " + fp
.getBeginIndex());
723 logln("endIndex = " + fp
.getEndIndex());
724 if (fp
.getBeginIndex() == fp
.getEndIndex())
725 errln("Fail: Empty field");
733 * This bug really only works in Locale.US, since that's what the locale
734 * used for Date.toString() is. Bug 4138203 reports that it fails on Korean
735 * NT; it would actually have failed on any non-US locale. Now it should
736 * work on all locales.
738 void DateFormatRegressionTest::Test4103340(void)
740 UErrorCode status
= U_ZERO_ERROR
;
742 // choose a date that is the FIRST of some month
743 // and some arbitrary time
744 UDate d
= date(97, 3, 1, 1, 1, 1);
745 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("MMMM"), Locale::getUS(), status
);
746 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
749 s
= dateToString(d
, s
);
751 FieldPosition
pos(FieldPosition::DONT_CARE
);
752 s2
= df
->format(d
, s2
, pos
);
755 UnicodeString substr
;
756 s2
.extract(0,2, substr
);
757 if (s
.indexOf(substr
) == -1)
758 errln("Months should match");
766 void DateFormatRegressionTest::Test4103341(void)
768 TimeZone
*saveZone
=TimeZone::createDefault();
771 // {sfb} changed from setDefault to adoptDefault
772 TimeZone::adoptDefault(TimeZone::createTimeZone("CST"));
773 UErrorCode status
= U_ZERO_ERROR
;
774 SimpleDateFormat
*simple
= new SimpleDateFormat(UnicodeString("MM/dd/yyyy HH:mm"), status
);
775 if(U_FAILURE(status
)) {
776 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
780 failure(status
, "new SimpleDateFormat");
781 TimeZone
*temp
= TimeZone::createDefault();
782 if(simple
->getTimeZone() != *temp
)
783 errln("Fail: SimpleDateFormat not using default zone");
786 TimeZone::adoptDefault(saveZone
);
796 void DateFormatRegressionTest::Test4104136(void)
798 UErrorCode status
= U_ZERO_ERROR
;
799 SimpleDateFormat
*sdf
= new SimpleDateFormat(status
);
800 if(U_FAILURE(status
)) {
801 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
805 if(failure(status
, "new SimpleDateFormat")) return;
806 UnicodeString pattern
= "'time' hh:mm";
807 sdf
->applyPattern(pattern
);
808 logln("pattern: \"" + pattern
+ "\"");
810 UnicodeString strings
[] = {
811 (UnicodeString
)"time 10:30",
812 (UnicodeString
) "time 10:x",
813 (UnicodeString
) "time 10x"
816 ParsePosition ppos
[] = {
823 date(70, UCAL_JANUARY
, 1, 10, 30),
829 "time 10:30", new ParsePosition(10), new Date(70, Calendar.JANUARY, 1, 10, 30),
830 "time 10:x", new ParsePosition(0), null,
831 "time 10x", new ParsePosition(0), null,
834 for(int i
= 0; i
< 3; i
++) {
835 UnicodeString text
= strings
[i
];
836 ParsePosition finish
= ppos
[i
];
837 UDate exp
= dates
[i
];
839 ParsePosition
pos(0);
840 UDate d
= sdf
->parse(text
, pos
);
841 logln(" text: \"" + text
+ "\"");
842 logln(" index: %d", pos
.getIndex());
843 logln((UnicodeString
) " result: " + d
);
844 if(pos
.getIndex() != finish
.getIndex())
845 errln("Fail: Expected pos " + finish
.getIndex());
846 if (! ((d
== 0 && exp
== -1) || (d
== exp
)))
847 errln((UnicodeString
) "Fail: Expected result " + exp
);
856 * According to the bug report, this test should throw a
857 * StringIndexOutOfBoundsException during the second parse. However,
860 void DateFormatRegressionTest::Test4104522(void)
862 UErrorCode status
= U_ZERO_ERROR
;
864 SimpleDateFormat
*sdf
= new SimpleDateFormat(status
);
865 if(U_FAILURE(status
)) {
866 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
870 failure(status
, "new SimpleDateFormat");
871 UnicodeString pattern
= "'time' hh:mm";
872 sdf
->applyPattern(pattern
);
873 logln("pattern: \"" + pattern
+ "\"");
877 UnicodeString text
= "time ";
878 UDate dt
= sdf
->parse(text
, pp
);
879 logln(" text: \"" + text
+ "\"" +
885 dt
= sdf
->parse(text
, pp
);
886 logln(" text: \"" + text
+ "\"" +
895 void DateFormatRegressionTest::Test4106807(void)
898 DateFormat
*df
= DateFormat::createDateTimeInstance();
900 UErrorCode status
= U_ZERO_ERROR
;
901 SimpleDateFormat
*sdfs
[] = {
902 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss"), status
),
903 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'Z'"), status
),
904 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss''"), status
),
905 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'a''a'"), status
),
906 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss %"), status
)
908 if(U_FAILURE(status
)) {
909 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
918 failure(status
, "new SimpleDateFormat");
920 UnicodeString strings
[] = {
921 (UnicodeString
) "19980211140000",
922 (UnicodeString
) "19980211140000",
923 (UnicodeString
) "19980211140000",
924 (UnicodeString
) "19980211140000a",
925 (UnicodeString
) "19980211140000 "
929 new SimpleDateFormat("yyyyMMddHHmmss"), "19980211140000",
930 new SimpleDateFormat("yyyyMMddHHmmss'Z'"), "19980211140000",
931 new SimpleDateFormat("yyyyMMddHHmmss''"), "19980211140000",
932 new SimpleDateFormat("yyyyMMddHHmmss'a''a'"), "19980211140000a",
933 new SimpleDateFormat("yyyyMMddHHmmss %"), "19980211140000 ",
935 GregorianCalendar
*gc
= new GregorianCalendar(status
);
936 failure(status
, "new GregorianCalendar");
937 TimeZone
*timeZone
= TimeZone::createDefault();
939 TimeZone
*gmt
= timeZone
->clone();
941 gmt
->setRawOffset(0);
943 for(int32_t i
= 0; i
< 5; i
++) {
944 SimpleDateFormat
*format
= sdfs
[i
];
945 UnicodeString dateString
= strings
[i
];
947 format
->setTimeZone(*gmt
);
948 dt
= format
->parse(dateString
, status
);
949 // {sfb} some of these parses will fail purposely
950 if(U_FAILURE(status
))
952 status
= U_ZERO_ERROR
;
954 FieldPosition
pos(FieldPosition::DONT_CARE
);
955 fmtd
= df
->format(dt
, fmtd
, pos
);
957 //logln(df->format(dt));
958 gc
->setTime(dt
, status
);
959 failure(status
, "gc->getTime");
960 logln(UnicodeString("") + gc
->get(UCAL_ZONE_OFFSET
, status
));
961 failure(status
, "gc->get");
963 s
= format
->format(dt
, s
, pos
);
966 catch (ParseException e) {
967 logln("No way Jose");
973 for(int32_t j
= 0; j
< 5; j
++)
980 Synopsis: Chinese time zone CTT is not recogonized correctly.
981 Description: Platform Chinese Windows 95 - ** Time zone set to CST **
987 // {sfb} what to do with this one ??
988 void DateFormatRegressionTest::Test4108407(void)
990 /*long l = System.currentTimeMillis();
991 logln("user.timezone = " + System.getProperty("user.timezone", "?"));
992 logln("Time Zone :" +
993 DateFormat.getDateInstance().getTimeZone().getID());
994 logln("Default format :" +
995 DateFormat.getDateInstance().format(new Date(l)));
996 logln("Full format :" +
997 DateFormat.getDateInstance(DateFormat.FULL).format(new
999 logln("*** Set host TZ to CST ***");
1000 logln("*** THE RESULTS OF THIS TEST MUST BE VERIFIED MANUALLY ***");*/
1005 * SimpleDateFormat won't parse "GMT"
1007 void DateFormatRegressionTest::Test4134203(void)
1009 UErrorCode status
= U_ZERO_ERROR
;
1010 UnicodeString dateFormat
= "MM/dd/yy HH:mm:ss zzz";
1011 SimpleDateFormat
*fmt
= new SimpleDateFormat(dateFormat
, status
);
1012 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
1013 ParsePosition
p0(0);
1014 UDate d
= fmt
->parse("01/22/92 04:52:00 GMT", p0
);
1015 logln(dateToString(d
));
1016 if(p0
== ParsePosition(0))
1017 errln("Fail: failed to parse 'GMT'");
1018 // In the failure case an exception is thrown by parse();
1019 // if no exception is thrown, the test passes.
1026 * SimpleDateFormat incorrect handling of 2 single quotes in format()
1028 void DateFormatRegressionTest::Test4151631(void)
1030 UnicodeString pattern
= "'TO_DATE('''dd'-'MM'-'yyyy HH:mm:ss''' , ''DD-MM-YYYY HH:MI:SS'')'";
1031 logln("pattern=" + pattern
);
1032 UErrorCode status
= U_ZERO_ERROR
;
1033 SimpleDateFormat
*format
= new SimpleDateFormat(pattern
, Locale::getUS(), status
);
1034 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
1035 UnicodeString result
;
1036 FieldPosition
pos(FieldPosition::DONT_CARE
);
1037 result
= format
->format(date(1998-1900, UCAL_JUNE
, 30, 13, 30, 0), result
, pos
);
1038 if (result
!= "TO_DATE('30-06-1998 13:30:00' , 'DD-MM-YYYY HH:MI:SS')") {
1039 errln("Fail: result=" + result
);
1042 logln("Pass: result=" + result
);
1050 * 'z' at end of date format throws index exception in SimpleDateFormat
1051 * CANNOT REPRODUCE THIS BUG ON 1.2FCS
1053 void DateFormatRegressionTest::Test4151706(void)
1055 UnicodeString
dateString("Thursday, 31-Dec-98 23:00:00 GMT");
1056 UErrorCode status
= U_ZERO_ERROR
;
1057 SimpleDateFormat
fmt(UnicodeString("EEEE, dd-MMM-yy HH:mm:ss z"), Locale::getUS(), status
);
1058 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
1060 UDate d
= fmt
.parse(dateString
, status
);
1061 failure(status
, "fmt->parse");
1062 // {sfb} what about next two lines?
1063 //if (d.getTime() != Date.UTC(1998-1900, Calendar.DECEMBER, 31, 23, 0, 0))
1064 // errln("Incorrect value: " + d);
1065 /*} catch (Exception e) {
1066 errln("Fail: " + e);
1069 FieldPosition
pos(0);
1070 logln(dateString
+ " -> " + fmt
.format(d
, temp
, pos
));
1075 * Cannot reproduce this bug under 1.2 FCS -- it may be a convoluted duplicate
1076 * of some other bug that has been fixed.
1079 DateFormatRegressionTest::Test4162071(void)
1081 UnicodeString
dateString("Thu, 30-Jul-1999 11:51:14 GMT");
1082 UnicodeString
format("EEE', 'dd-MMM-yyyy HH:mm:ss z"); // RFC 822/1123
1083 UErrorCode status
= U_ZERO_ERROR
;
1084 SimpleDateFormat
df(format
, Locale::getUS(), status
);
1085 if(U_FAILURE(status
)) {
1086 dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status
));
1091 UDate x
= df
.parse(dateString
, status
);
1092 if(U_SUCCESS(status
))
1093 logln("Parse format \"" + format
+ "\" ok");
1095 errln("Parse format \"" + format
+ "\" failed.");
1097 FieldPosition
pos(0);
1098 logln(dateString
+ " -> " + df
.format(x
, temp
, pos
));
1099 //} catch (Exception e) {
1100 // errln("Parse format \"" + format + "\" failed.");
1105 * DateFormat shouldn't parse year "-1" as a two-digit year (e.g., "-1" -> 1999).
1107 void DateFormatRegressionTest::Test4182066(void) {
1108 UErrorCode status
= U_ZERO_ERROR
;
1109 SimpleDateFormat
fmt("MM/dd/yy", Locale::getUS(), status
);
1110 SimpleDateFormat
dispFmt("MMM dd yyyy GG", Locale::getUS(), status
);
1111 if (U_FAILURE(status
)) {
1112 dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status
));
1116 /* We expect 2-digit year formats to put 2-digit years in the right
1117 * window. Out of range years, that is, anything less than "00" or
1118 * greater than "99", are treated as literal years. So "1/2/3456"
1119 * becomes 3456 AD. Likewise, "1/2/-3" becomes -3 AD == 2 BC.
1121 const char* STRINGS
[] = {
1131 int32_t STRINGS_COUNT
= (int32_t)(sizeof(STRINGS
) / sizeof(STRINGS
[0]));
1132 UDate FAIL_DATE
= (UDate
) 0;
1134 date(2000-1900, UCAL_FEBRUARY
, 29),
1135 date(2001-1900, UCAL_JANUARY
, 23),
1136 date( -1-1900, UCAL_APRIL
, 5),
1137 date( -9-1900, UCAL_JANUARY
, 23),
1138 date(1314-1900, UCAL_NOVEMBER
, 12),
1139 date( 1-1900, UCAL_OCTOBER
, 31),
1140 FAIL_DATE
, // "+1" isn't recognized by US NumberFormat
1141 date( 1-1900, UCAL_SEPTEMBER
,12),
1146 for (int32_t i
=0; i
<STRINGS_COUNT
; ++i
) {
1147 UnicodeString
str(STRINGS
[i
]);
1148 UDate expected
= DATES
[i
];
1149 status
= U_ZERO_ERROR
;
1150 UDate actual
= fmt
.parse(str
, status
);
1151 if (U_FAILURE(status
)) {
1154 UnicodeString actStr
;
1155 if (actual
== FAIL_DATE
) {
1156 actStr
.append("null");
1159 ((DateFormat
*)&dispFmt
)->format(actual
, actStr
);
1162 if (expected
== actual
) {
1163 out
.append(str
+ " => " + actStr
+ "\n");
1165 UnicodeString expStr
;
1166 if (expected
== FAIL_DATE
) {
1167 expStr
.append("null");
1170 ((DateFormat
*)&dispFmt
)->format(expected
, expStr
);
1172 out
.append("FAIL: " + str
+ " => " + actStr
1173 + ", expected " + expStr
+ "\n");
1185 * j32 {JDK Bug 4210209 4209272}
1186 * DateFormat cannot parse Feb 29 2000 when setLenient(false)
1189 DateFormatRegressionTest::Test4210209(void) {
1190 UErrorCode status
= U_ZERO_ERROR
;
1191 UnicodeString
pattern("MMM d, yyyy");
1192 SimpleDateFormat
sfmt(pattern
, Locale::getUS(), status
);
1193 SimpleDateFormat
sdisp("MMM dd yyyy GG", Locale::getUS(), status
);
1194 DateFormat
& fmt
= *(DateFormat
*)&sfmt
; // Yuck: See j25
1195 DateFormat
& disp
= *(DateFormat
*)&sdisp
; // Yuck: See j25
1196 if (U_FAILURE(status
)) {
1197 dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status
));
1200 Calendar
* calx
= (Calendar
*)fmt
.getCalendar(); // cast away const!
1201 calx
->setLenient(FALSE
);
1202 UDate d
= date(2000-1900, UCAL_FEBRUARY
, 29);
1203 UnicodeString s
, ss
;
1205 logln(disp
.format(d
, ss
.remove()) + " f> " + pattern
+
1206 " => \"" + s
+ "\"");
1207 ParsePosition
pos(0);
1208 d
= fmt
.parse(s
, pos
);
1209 logln(UnicodeString("\"") + s
+ "\" p> " + pattern
+
1210 " => " + disp
.format(d
, ss
.remove()));
1211 logln(UnicodeString("Parse pos = ") + pos
.getIndex() +
1212 ", error pos = " + pos
.getErrorIndex());
1213 if (pos
.getErrorIndex() != -1) {
1214 errln(UnicodeString("FAIL: Error index should be -1"));
1217 // The underlying bug is in GregorianCalendar. If the following lines
1218 // succeed, the bug is fixed. If the bug isn't fixed, they will throw
1220 GregorianCalendar
cal(status
);
1221 if (U_FAILURE(status
)) {
1222 errln("FAIL: Unable to create Calendar");
1226 cal
.setLenient(FALSE
);
1227 cal
.set(2000, UCAL_FEBRUARY
, 29); // This should work!
1228 logln(UnicodeString("Attempt to set Calendar to Feb 29 2000: ") +
1229 disp
.format(cal
.getTime(status
), ss
.remove()));
1230 if (U_FAILURE(status
)) {
1231 errln("FAIL: Unable to set Calendar to Feb 29 2000");
1235 void DateFormatRegressionTest::Test714(void)
1238 UDate
d(978103543000.);
1239 DateFormat
*fmt
= DateFormat::createDateTimeInstance(DateFormat::NONE
,
1243 dataerrln("Error calling DateFormat::createDateTimeInstance");
1248 UnicodeString tests
=
1249 (UnicodeString
) "7:25:43 AM" ;
1250 UErrorCode status
= U_ZERO_ERROR
;
1252 if(U_FAILURE(status
))
1254 errln((UnicodeString
) "Fail, errmsg " + u_errorName(status
));
1260 errln((UnicodeString
) "Fail: " + s
+ " != " + tests
);
1264 logln("OK: " + s
+ " == " + tests
);
1270 class Test1684Data
{
1280 UnicodeString normalized
;
1282 Test1684Data(int32_t xyear
, int32_t xmonth
, int32_t xdate
,
1283 int32_t xwomyear
, int32_t xwommon
, int32_t xwom
, int32_t xdow
,
1284 const char *xdata
, const char *xnormalized
) :
1293 normalized((xnormalized
==NULL
)?xdata
:xnormalized
,"")
1297 void DateFormatRegressionTest::Test1684(void)
1299 // July 2001 August 2001 January 2002
1300 // Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1301 // 1 2 3 4 5 6 7 1 2 3 4 1 2 3 4 5
1302 // 8 9 10 11 12 13 14 5 6 7 8 9 10 11 6 7 8 9 10 11 12
1303 // 15 16 17 18 19 20 21 12 13 14 15 16 17 18 13 14 15 16 17 18 19
1304 // 22 23 24 25 26 27 28 19 20 21 22 23 24 25 20 21 22 23 24 25 26
1305 // 29 30 31 26 27 28 29 30 31 27 28 29 30 31
1306 Test1684Data
*tests
[] = {
1307 new Test1684Data(2001, 8, 6, 2001,8,2,UCAL_MONDAY
, "2001 08 02 Mon", NULL
),
1308 new Test1684Data(2001, 8, 7, 2001,8,2,UCAL_TUESDAY
, "2001 08 02 Tue", NULL
),
1309 new Test1684Data(2001, 8, 5,/*12,*/ 2001,8,2,UCAL_SUNDAY
, "2001 08 02 Sun", NULL
),
1310 new Test1684Data(2001, 8,6, /*7, 30,*/ 2001,7,6,UCAL_MONDAY
, "2001 07 06 Mon", "2001 08 02 Mon"),
1311 new Test1684Data(2001, 8,7, /*7, 31,*/ 2001,7,6,UCAL_TUESDAY
, "2001 07 06 Tue", "2001 08 02 Tue"),
1312 new Test1684Data(2001, 8, 5, 2001,7,6,UCAL_SUNDAY
, "2001 07 06 Sun", "2001 08 02 Sun"),
1313 new Test1684Data(2001, 7, 30, 2001,8,1,UCAL_MONDAY
, "2001 08 01 Mon", "2001 07 05 Mon"),
1314 new Test1684Data(2001, 7, 31, 2001,8,1,UCAL_TUESDAY
, "2001 08 01 Tue", "2001 07 05 Tue"),
1315 new Test1684Data(2001, 7,29, /*8, 5,*/ 2001,8,1,UCAL_SUNDAY
, "2001 08 01 Sun", "2001 07 05 Sun"),
1316 new Test1684Data(2001, 12, 31, 2001,12,6,UCAL_MONDAY
, "2001 12 06 Mon", NULL
),
1317 new Test1684Data(2002, 1, 1, 2002,1,1,UCAL_TUESDAY
, "2002 01 01 Tue", NULL
),
1318 new Test1684Data(2002, 1, 2, 2002,1,1,UCAL_WEDNESDAY
, "2002 01 01 Wed", NULL
),
1319 new Test1684Data(2002, 1, 3, 2002,1,1,UCAL_THURSDAY
, "2002 01 01 Thu", NULL
),
1320 new Test1684Data(2002, 1, 4, 2002,1,1,UCAL_FRIDAY
, "2002 01 01 Fri", NULL
),
1321 new Test1684Data(2002, 1, 5, 2002,1,1,UCAL_SATURDAY
, "2002 01 01 Sat", NULL
),
1322 new Test1684Data(2001,12,30, /*2002, 1, 6,*/ 2002,1,1,UCAL_SUNDAY
, "2002 01 01 Sun", "2001 12 06 Sun")
1325 #define kTest1684Count ((int32_t)(sizeof(tests)/sizeof(tests[0])))
1327 int32_t pass
= 0, error
= 0, warning
= 0;
1330 UErrorCode status
= U_ZERO_ERROR
;
1331 UnicodeString
pattern("yyyy MM WW EEE","");
1332 Calendar
*cal
= new GregorianCalendar(status
);
1333 SimpleDateFormat
*sdf
= new SimpleDateFormat(pattern
,status
);
1334 if (U_FAILURE(status
)) {
1335 dataerrln("Error constructing SimpleDateFormat");
1336 for(i
=0;i
<kTest1684Count
;i
++) {
1343 cal
->setFirstDayOfWeek(UCAL_SUNDAY
);
1344 cal
->setMinimalDaysInFirstWeek(1);
1346 sdf
->adoptCalendar(cal
);
1348 cal
= sdf
->getCalendar()->clone(); // sdf may have deleted calendar
1350 if(!cal
|| !sdf
|| U_FAILURE(status
)) {
1351 errln(UnicodeString("Error setting up test: ") + u_errorName(status
));
1354 for (i
= 0; i
< kTest1684Count
; ++i
) {
1355 Test1684Data
&test
= *(tests
[i
]);
1356 logln(UnicodeString("#") + i
+ UnicodeString("\n-----\nTesting round trip of ") + test
.year
+
1357 " " + (test
.month
+ 1) +
1359 " (written as) " + test
.data
);
1362 cal
->set(test
.year
, test
.month
, test
.date
);
1363 UDate ms
= cal
->getTime(status
);
1366 cal
->set(UCAL_YEAR
, test
.womyear
);
1367 cal
->set(UCAL_MONTH
, test
.wommon
);
1368 cal
->set(UCAL_WEEK_OF_MONTH
, test
.wom
);
1369 cal
->set(UCAL_DAY_OF_WEEK
, test
.dow
);
1370 UDate ms2
= cal
->getTime(status
);
1373 errln((UnicodeString
)"\nError: GregorianUCAL_DOM gave " + ms
+
1374 "\n GregorianUCAL_WOM gave " + ms2
);
1380 ms2
= sdf
->parse(test
.data
, status
);
1381 if(U_FAILURE(status
)) {
1382 errln("parse exception: " + UnicodeString(u_errorName(status
)));
1386 errln((UnicodeString
)"\nError: GregorianCalendar gave " + ms
+
1387 "\n SimpleDateFormat.parse gave " + ms2
);
1393 UnicodeString result
;
1394 sdf
->format(ms
, result
);
1395 if (result
!= test
.normalized
) {
1396 errln("\nWarning: format of '" + test
.data
+ "' gave" +
1397 "\n '" + result
+ "'" +
1398 "\n expected '" + test
.normalized
+ "'");
1405 ms3
= sdf
->parse(result
, status
);
1406 if(U_FAILURE(status
)) {
1407 errln("parse exception 2: " + (UnicodeString
)u_errorName(status
));
1412 errln((UnicodeString
)"\nError: Re-parse of '" + result
+ "' gave time of " +
1421 = UnicodeString("Passed: ") + pass
+ ", Warnings: " + warning
+ ", Errors: " + error
;
1428 for(i
=0;i
<kTest1684Count
;i
++) {
1435 void DateFormatRegressionTest::Test5554(void)
1437 UErrorCode status
= U_ZERO_ERROR
;
1438 UnicodeString
pattern("Z","");
1439 UnicodeString
newfoundland("Canada/Newfoundland", "");
1440 TimeZone
*zone
= TimeZone::createTimeZone(newfoundland
);
1441 Calendar
*cal
= new GregorianCalendar(zone
, status
);
1442 SimpleDateFormat
*sdf
= new SimpleDateFormat(pattern
,status
);
1443 if (U_FAILURE(status
)) {
1444 dataerrln("Error constructing SimpleDateFormat");
1449 cal
->set(2007, 1, 14);
1450 UDate date
= cal
->getTime(status
);
1451 if (U_FAILURE(status
)) {
1452 errln("Error getting time to format");
1455 sdf
->adoptCalendar(cal
);
1456 UnicodeString result
;
1457 UnicodeString
correct("-0330", "");
1458 sdf
->format(date
, result
);
1459 if (result
!= correct
) {
1460 errln("\nError: Newfoundland Z of Jan 14, 2007 gave '" + result
+ "', expected '" + correct
+ "'");
1465 void DateFormatRegressionTest::Test9237(void)
1467 UErrorCode status
= U_ZERO_ERROR
;
1468 UnicodeString
pattern("VVVV");
1470 SimpleDateFormat
fmt(pattern
, status
); // default locale
1471 SimpleDateFormat
fmtDE(pattern
, Locale("de_DE"), status
);
1472 if (U_FAILURE(status
)) {
1473 dataerrln("Error constructing SimpleDateFormat");
1478 SimpleDateFormat
fmtCopyDE(fmtDE
);
1479 UnicodeString resDE
, resCopyDE
;
1481 fmtDE
.format(0.0, resDE
);
1482 fmtCopyDE
.format(0.0, resCopyDE
);
1484 if (resDE
!= resCopyDE
) {
1485 errln(UnicodeString("Error: different result by the copied instance - org:") + resDE
+ " copy:" + resCopyDE
);
1488 // test for assignment operator
1491 UnicodeString resAssigned
;
1492 fmt
.format(0.0, resAssigned
);
1494 if (resDE
!= resAssigned
) {
1495 errln(UnicodeString("Error: different results by the assigned instance - org:") + resDE
+ " assigned:" + resAssigned
);
1499 #endif /* #if !UCONFIG_NO_FORMATTING */