1 /********************************************************************
3 * Copyright (c) 1997-2006, 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");
57 default: name
= ""; break;
64 void DateFormatRegressionTest::Test4029195(void)
66 UErrorCode status
= U_ZERO_ERROR
;
68 UDate today
= Calendar::getNow();
69 logln((UnicodeString
) "today: " + today
);
71 SimpleDateFormat
*sdf
= (SimpleDateFormat
*) DateFormat::createDateInstance();
72 if (failure(status
, "SimpleDateFormat::createDateInstance")) {
77 dataerrln("Error calling DateFormat::createDateTimeInstance");
81 pat
= sdf
->toPattern(pat
);
82 logln("pattern: " + pat
);
84 FieldPosition
pos(FieldPosition::DONT_CARE
);
85 fmtd
= sdf
->format(today
, fmtd
, pos
);
86 logln("today: " + fmtd
);
88 sdf
->applyPattern("G yyyy DDD");
90 todayS
= sdf
->format(today
, todayS
, pos
);
91 logln("today: " + todayS
);
93 today
= sdf
->parse(todayS
, status
);
94 failure(status
, "sdf->parse");
95 logln((UnicodeString
)"today date: " + today
);
96 /*} catch(Exception e) {
97 logln("Error reparsing date: " + e.getMessage());
102 rt
= sdf
->format(sdf
->parse(todayS
, status
), rt
, pos
);
103 failure(status
, "sdf->parse");
104 logln("round trip: " + rt
);
106 errln("Fail: Want " + todayS
+ " Got " + rt
);
108 catch (ParseException e) {
119 void DateFormatRegressionTest::Test4052408(void)
122 DateFormat
*fmt
= DateFormat::createDateTimeInstance(DateFormat::SHORT
,
123 DateFormat::SHORT
, Locale::getUS());
125 dataerrln("Error calling DateFormat::createDateTimeInstance");
129 UDate dt
= date(97, UCAL_MAY
, 3, 8, 55);
131 str
= fmt
->format(dt
, str
);
134 if(str
!= "5/3/97 8:55 AM")
135 errln("Fail: Test broken; Want 5/3/97 8:55 AM Got " + str
);
137 UnicodeString expected
[] = {
138 (UnicodeString
) "", //"ERA_FIELD",
139 (UnicodeString
) "97", //"YEAR_FIELD",
140 (UnicodeString
) "5", //"MONTH_FIELD",
141 (UnicodeString
) "3", //"DATE_FIELD",
142 (UnicodeString
) "", //"HOUR_OF_DAY1_FIELD",
143 (UnicodeString
) "", //"HOUR_OF_DAY0_FIELD",
144 (UnicodeString
) "55", //"MINUTE_FIELD",
145 (UnicodeString
) "", //"SECOND_FIELD",
146 (UnicodeString
) "", //"MILLISECOND_FIELD",
147 (UnicodeString
) "", //"DAY_OF_WEEK_FIELD",
148 (UnicodeString
) "", //"DAY_OF_YEAR_FIELD",
149 (UnicodeString
) "", //"DAY_OF_WEEK_IN_MONTH_FIELD",
150 (UnicodeString
) "", //"WEEK_OF_YEAR_FIELD",
151 (UnicodeString
) "", //"WEEK_OF_MONTH_FIELD",
152 (UnicodeString
) "AM", //"AM_PM_FIELD",
153 (UnicodeString
) "8", //"HOUR1_FIELD",
154 (UnicodeString
) "", //"HOUR0_FIELD",
155 (UnicodeString
) "" //"TIMEZONE_FIELD"
158 //Hashtable expected;// = new Hashtable();
159 //expected.put(new LongKey(DateFormat.MONTH_FIELD), "5");
160 //expected.put(new LongKey(DateFormat.DATE_FIELD), "3");
161 //expected.put(new LongKey(DateFormat.YEAR_FIELD), "97");
162 //expected.put(new LongKey(DateFormat.HOUR1_FIELD), "8");
163 //expected.put(new LongKey(DateFormat.MINUTE_FIELD), "55");
164 //expected.put(new LongKey(DateFormat.AM_PM_FIELD), "AM");
166 //StringBuffer buf = new StringBuffer();
167 UnicodeString fieldNames
[] = {
168 (UnicodeString
) "ERA_FIELD",
169 (UnicodeString
) "YEAR_FIELD",
170 (UnicodeString
) "MONTH_FIELD",
171 (UnicodeString
) "DATE_FIELD",
172 (UnicodeString
) "HOUR_OF_DAY1_FIELD",
173 (UnicodeString
) "HOUR_OF_DAY0_FIELD",
174 (UnicodeString
) "MINUTE_FIELD",
175 (UnicodeString
) "SECOND_FIELD",
176 (UnicodeString
) "MILLISECOND_FIELD",
177 (UnicodeString
) "DAY_OF_WEEK_FIELD",
178 (UnicodeString
) "DAY_OF_YEAR_FIELD",
179 (UnicodeString
) "DAY_OF_WEEK_IN_MONTH_FIELD",
180 (UnicodeString
) "WEEK_OF_YEAR_FIELD",
181 (UnicodeString
) "WEEK_OF_MONTH_FIELD",
182 (UnicodeString
) "AM_PM_FIELD",
183 (UnicodeString
) "HOUR1_FIELD",
184 (UnicodeString
) "HOUR0_FIELD",
185 (UnicodeString
) "TIMEZONE_FIELD"
189 for(int i
= 0; i
<= 17; ++i
) {
190 FieldPosition
pos(i
);
192 fmt
->format(dt
, buf
, pos
);
193 //char[] dst = new char[pos.getEndIndex() - pos.getBeginIndex()];
195 buf
.extractBetween(pos
.getBeginIndex(), pos
.getEndIndex(), dst
);
196 UnicodeString
str(dst
);
197 logln((UnicodeString
)"" + i
+ (UnicodeString
)": " + fieldNames
[i
] +
198 (UnicodeString
)", \"" + str
+ (UnicodeString
)"\", " +
199 pos
.getBeginIndex() + (UnicodeString
)", " +
201 UnicodeString exp
= expected
[i
];
202 if((exp
.length() == 0 && str
.length() == 0) || str
== exp
)
205 errln(UnicodeString(" expected ") + exp
);
211 errln("Fail: FieldPosition not set right by DateFormat");
218 * Verify the function of the [s|g]et2DigitYearStart() API.
220 void DateFormatRegressionTest::Test4056591(void)
222 UErrorCode status
= U_ZERO_ERROR
;
225 SimpleDateFormat
*fmt
= new SimpleDateFormat(UnicodeString("yyMMdd"), Locale::getUS(), status
);
226 failure(status
, "new SimpleDateFormat");
227 UDate start
= date(1809-1900, UCAL_DECEMBER
, 25);
228 fmt
->set2DigitYearStart(start
, status
);
229 failure(status
, "fmt->setTwoDigitStartDate");
230 if( (fmt
->get2DigitYearStart(status
) != start
) || failure(status
, "get2DigitStartDate"))
231 errln("get2DigitYearStart broken");
233 date(1809-1900, UCAL_DECEMBER
, 25),
234 date(1909-1900, UCAL_DECEMBER
, 24),
235 date(1809-1900, UCAL_DECEMBER
, 26),
236 date(1861-1900, UCAL_DECEMBER
, 25),
239 UnicodeString strings
[] = {
240 (UnicodeString
) "091225",
241 (UnicodeString
) "091224",
242 (UnicodeString
) "091226",
243 (UnicodeString
) "611225"
247 "091225", new Date(1809-1900, Calendar.DECEMBER, 25),
248 "091224", new Date(1909-1900, Calendar.DECEMBER, 24),
249 "091226", new Date(1809-1900, Calendar.DECEMBER, 26),
250 "611225", new Date(1861-1900, Calendar.DECEMBER, 25),
253 for(int i
= 0; i
< 4; i
++) {
254 UnicodeString s
= strings
[i
];
255 UDate exp
= dates
[i
];
256 UDate got
= fmt
->parse(s
, status
);
257 failure(status
, "fmt->parse");
258 logln(s
+ " -> " + got
+ "; exp " + exp
);
260 errln("set2DigitYearStart broken");
263 catch (ParseException e) {
274 void DateFormatRegressionTest::Test4059917(void)
276 UErrorCode status
= U_ZERO_ERROR
;
278 SimpleDateFormat
*fmt
;
279 UnicodeString myDate
;
281 fmt
= new SimpleDateFormat( UnicodeString("yyyy/MM/dd"), status
);
282 if(failure(status
, "new SimpleDateFormat")) return;
283 myDate
= "1997/01/01";
284 aux917( fmt
, myDate
);
289 fmt
= new SimpleDateFormat( UnicodeString("yyyyMMdd"), status
);
290 if(failure(status
, "new SimpleDateFormat")) return;
292 aux917( fmt
, myDate
);
297 void DateFormatRegressionTest::aux917( SimpleDateFormat
*fmt
, UnicodeString
& str
) {
300 pat
= fmt
->toPattern(pat
);
301 logln( "==================" );
302 logln( "testIt: pattern=" + pat
+
308 ParsePosition
pos(0);
309 fmt
->parseObject( str
, o
, pos
);
310 //logln( UnicodeString("Parsed object: ") + o );
312 UErrorCode status
= U_ZERO_ERROR
;
313 UnicodeString formatted
;
314 FieldPosition
poss(FieldPosition::DONT_CARE
);
315 formatted
= fmt
->format( o
, formatted
, poss
, status
);
316 failure(status
, "fmt->format");
317 logln( "Formatted string: " + formatted
);
318 if( formatted
!= str
)
319 errln("Fail: Want " + str
+ " Got " + formatted
);
321 catch (ParseException e) {
330 void DateFormatRegressionTest::Test4060212(void)
332 UnicodeString dateString
= "1995-040.05:01:29";
334 logln( "dateString= " + dateString
);
335 logln("Using yyyy-DDD.hh:mm:ss");
336 UErrorCode status
= U_ZERO_ERROR
;
337 SimpleDateFormat
*formatter
= new SimpleDateFormat(UnicodeString("yyyy-DDD.hh:mm:ss"), status
);
338 if(failure(status
, "new SimpleDateFormat")) return;
339 ParsePosition
pos(0);
340 UDate myDate
= formatter
->parse( dateString
, pos
);
341 UnicodeString myString
;
342 DateFormat
*fmt
= DateFormat::createDateTimeInstance( DateFormat::FULL
,
345 dataerrln("Error calling DateFormat::createDateTimeInstance");
350 myString
= fmt
->format( myDate
, myString
);
353 Calendar
*cal
= new GregorianCalendar(status
);
354 failure(status
, "new GregorianCalendar");
355 cal
->setTime(myDate
, status
);
356 failure(status
, "cal->setTime");
357 if ((cal
->get(UCAL_DAY_OF_YEAR
, status
) != 40) || failure(status
, "cal->get"))
358 errln((UnicodeString
) "Fail: Got " + cal
->get(UCAL_DAY_OF_YEAR
, status
) +
361 logln("Using yyyy-ddd.hh:mm:ss");
364 formatter
= new SimpleDateFormat(UnicodeString("yyyy-ddd.hh:mm:ss"), status
);
365 if(failure(status
, "new SimpleDateFormat")) return;
367 myDate
= formatter
->parse( dateString
, pos
);
368 myString
= fmt
->format( myDate
, myString
);
370 cal
->setTime(myDate
, status
);
371 failure(status
, "cal->setTime");
372 if ((cal
->get(UCAL_DAY_OF_YEAR
, status
) != 40) || failure(status
, "cal->get"))
373 errln((UnicodeString
) "Fail: Got " + cal
->get(UCAL_DAY_OF_YEAR
, status
) +
384 void DateFormatRegressionTest::Test4061287(void)
386 UErrorCode status
= U_ZERO_ERROR
;
388 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("dd/MM/yyyy"), status
);
389 if(U_FAILURE(status
)) {
390 errln("Couldn't create SimpleDateFormat, error: %s", u_errorName(status
));
394 failure(status
, "new SimpleDateFormat");
396 logln(UnicodeString("") + df
->parse("35/01/1971", status
));
397 failure(status
, "df->parse");
398 //logln(df.parse("35/01/1971").toString());
400 /*catch (ParseException e) {
404 df
->setLenient(FALSE
);
407 logln(UnicodeString("") + df
->parse("35/01/1971", status
));
408 if(U_FAILURE(status
))
410 //logln(df.parse("35/01/1971").toString());
411 //} catch (ParseException e) {ok=TRUE;}
413 errln("Fail: Lenient not working");
420 void DateFormatRegressionTest::Test4065240(void)
423 DateFormat
*shortdate
, *fulldate
;
424 UnicodeString strShortDate
, strFullDate
;
425 Locale saveLocale
= Locale::getDefault();
426 TimeZone
*saveZone
= TimeZone::createDefault();
428 UErrorCode status
= U_ZERO_ERROR
;
430 Locale
*curLocale
= new Locale("de","DE");
431 Locale::setDefault(*curLocale
, status
);
432 failure(status
, "Locale::setDefault");
433 // {sfb} adoptDefault instead of setDefault
434 //TimeZone::setDefault(TimeZone::createTimeZone("EST"));
435 TimeZone::adoptDefault(TimeZone::createTimeZone("EST"));
436 curDate
= date(98, 0, 1);
437 shortdate
= DateFormat::createDateInstance(DateFormat::SHORT
);
438 if (shortdate
== NULL
){
439 dataerrln("Error calling DateFormat::createDateInstance");
443 fulldate
= DateFormat::createDateTimeInstance(DateFormat::LONG
, DateFormat::LONG
);
444 if (fulldate
== NULL
){
445 dataerrln("Error calling DateFormat::createDateTimeInstance");
448 strShortDate
= "The current date (short form) is ";
450 temp
= shortdate
->format(curDate
, temp
);
451 strShortDate
+= temp
;
452 strFullDate
= "The current date (long form) is ";
454 fulldate
->format(curDate
, temp2
);
455 strFullDate
+= temp2
;
460 // {sfb} What to do with resource bundle stuff?????
462 // Check to see if the resource is present; if not, we can't test
463 ResourceBundle
*bundle
= new ResourceBundle(
464 NULL
, *curLocale
, status
);
465 failure(status
, "new ResourceBundle");
466 //(UnicodeString) "java.text.resources.DateFormatZoneData", curLocale);
468 // {sfb} API change to ResourceBundle -- add getLocale()
469 /*if (bundle->getLocale().getLanguage(temp) == UnicodeString("de")) {
470 // UPDATE THIS AS ZONE NAME RESOURCE FOR <EST> in de_DE is updated
471 if (!strFullDate.endsWith(UnicodeString("GMT-05:00")))
472 errln("Fail: Want GMT-05:00");
475 logln("*** TEST COULD NOT BE COMPLETED BECAUSE DateFormatZoneData ***");
476 logln("*** FOR LOCALE de OR de_DE IS MISSING ***");
480 Locale::setDefault(saveLocale
, status
);
481 failure(status
, "Locale::setDefault");
482 TimeZone::setDefault(*saveZone
);
492 DateFormat.equals is too narrowly defined. As a result, MessageFormat
493 does not work correctly. DateFormat.equals needs to be written so
494 that the Calendar sub-object is not compared using Calendar.equals,
495 but rather compared for equivalency. This may necessitate adding a
496 (package private) method to Calendar to test for equivalency.
498 Currently this bug breaks MessageFormat.toPattern
503 void DateFormatRegressionTest::Test4071441(void)
505 DateFormat
*fmtA
= DateFormat::createInstance();
506 DateFormat
*fmtB
= DateFormat::createInstance();
508 if (fmtA
== NULL
|| fmtB
== NULL
){
509 dataerrln("Error calling DateFormat::createInstance");
515 // {sfb} Is it OK to cast away const here?
516 Calendar
*calA
= (Calendar
*) fmtA
->getCalendar();
517 Calendar
*calB
= (Calendar
*) fmtB
->getCalendar();
519 errln("Couldn't get proper calendars, exiting");
524 UDate epoch
= date(0, 0, 0);
525 UDate xmas
= date(61, UCAL_DECEMBER
, 25);
527 UErrorCode status
= U_ZERO_ERROR
;
528 calA
->setTime(epoch
, status
);
529 failure(status
, "calA->setTime");
530 calB
->setTime(epoch
, status
);
531 failure(status
, "calB->setTime");
533 errln("Fail: Can't complete test; Calendar instances unequal");
535 errln("Fail: DateFormat unequal when Calendars equal");
536 calB
->setTime(xmas
, status
);
537 failure(status
, "calB->setTime");
539 errln("Fail: Can't complete test; Calendar instances equal");
541 errln("Fail: DateFormat unequal when Calendars equivalent");
543 logln("DateFormat.equals ok");
549 /* The java.text.DateFormat.parse(String) method expects for the
550 US locale a string formatted according to mm/dd/yy and parses it
553 When given a string mm/dd/yyyy it only parses up to the first
554 two y's, typically resulting in a date in the year 1919.
556 Please extend the parsing method(s) to handle strings with
557 four-digit year values (probably also applicable to various
562 void DateFormatRegressionTest::Test4073003(void)
565 UErrorCode ec
= U_ZERO_ERROR
;
566 SimpleDateFormat
fmt("dd/MM/yy", Locale::getUK(), ec
);
568 errln("FAIL: SimpleDateFormat constructor");
571 UnicodeString tests
[] = {
572 (UnicodeString
) "12/25/61",
573 (UnicodeString
) "12/25/1961",
574 (UnicodeString
) "4/3/2010",
575 (UnicodeString
) "4/3/10"
577 UErrorCode status
= U_ZERO_ERROR
;
578 for(int i
= 0; i
< 4; i
+=2) {
579 UDate d
= fmt
.parse(tests
[i
], status
);
580 failure(status
, "fmt.parse");
581 UDate dd
= fmt
.parse(tests
[i
+1], status
);
582 failure(status
, "fmt.parse");
584 s
= fmt
.format(d
, s
);
586 ss
= fmt
.format(dd
, ss
);
588 errln((UnicodeString
) "Fail: " + d
+ " != " + dd
);
590 errln((UnicodeString
)"Fail: " + s
+ " != " + ss
);
591 logln("Ok: " + s
+ " " + d
);
598 void DateFormatRegressionTest::Test4089106(void)
600 TimeZone
*def
= TimeZone::createDefault();
602 TimeZone
*z
= new SimpleTimeZone((int)(1.25 * 3600000), "FAKEZONE");
603 TimeZone::setDefault(*z
);
604 UErrorCode status
= U_ZERO_ERROR
;
605 SimpleDateFormat
*f
= new SimpleDateFormat(status
);
606 if(U_FAILURE(status
)) {
607 errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
613 failure(status
, "new SimpleDateFormat");
614 if (f
->getTimeZone()!= *z
)
615 errln("Fail: SimpleTimeZone should use TimeZone.getDefault()");
619 TimeZone::setDefault(*def
);
631 // {sfb} not applicable in C++??
633 void DateFormatRegressionTest::Test4100302(void)
635 /* Locale locales [] = {
637 Locale::CANADA_FRENCH,
652 Locale::SIMPLIFIED_CHINESE,
654 Locale::TRADITIONAL_CHINESE,
660 for(int i = 0; i < 21; i++) {
662 Format *format = DateFormat::createDateTimeInstance(DateFormat::FULL,
663 DateFormat::FULL, locales[i]);
666 ByteArrayOutputStream baos = new ByteArrayOutputStream();
667 ObjectOutputStream oos = new ObjectOutputStream(baos);
669 oos.writeObject(format);
673 bytes = baos.toByteArray();
675 ObjectInputStream ois =
676 new ObjectInputStream(new ByteArrayInputStream(bytes));
678 if (!format.equals(ois.readObject())) {
680 logln("DateFormat instance for locale " +
681 locales[i] + " is incorrectly serialized/deserialized.");
683 logln("DateFormat instance for locale " +
684 locales[i] + " is OKAY.");
687 if (!pass) errln("Fail: DateFormat serialization/equality bug");
689 catch (IOException e) {
693 catch (ClassNotFoundException e) {
702 void DateFormatRegressionTest::Test4101483(void)
704 UErrorCode status
= U_ZERO_ERROR
;
705 SimpleDateFormat
*sdf
= new SimpleDateFormat(UnicodeString("z"), Locale::getUS(), status
);
706 if(failure(status
, "new SimpleDateFormat")) return;
707 FieldPosition
fp(UDAT_TIMEZONE_FIELD
);
708 //Date d = date(9234567890L);
709 UDate d
= 9234567890.0;
710 //StringBuffer buf = new StringBuffer("");
712 sdf
->format(d
, buf
, fp
);
713 //logln(sdf.format(d, buf, fp).toString());
714 logln(dateToString(d
) + " => " + buf
);
715 logln("beginIndex = " + fp
.getBeginIndex());
716 logln("endIndex = " + fp
.getEndIndex());
717 if (fp
.getBeginIndex() == fp
.getEndIndex())
718 errln("Fail: Empty field");
726 * This bug really only works in Locale.US, since that's what the locale
727 * used for Date.toString() is. Bug 4138203 reports that it fails on Korean
728 * NT; it would actually have failed on any non-US locale. Now it should
729 * work on all locales.
731 void DateFormatRegressionTest::Test4103340(void)
733 UErrorCode status
= U_ZERO_ERROR
;
735 // choose a date that is the FIRST of some month
736 // and some arbitrary time
737 UDate d
= date(97, 3, 1, 1, 1, 1);
738 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("MMMM"), Locale::getUS(), status
);
739 if(failure(status
, "new SimpleDateFormat")) return;
742 s
= dateToString(d
, s
);
744 FieldPosition
pos(FieldPosition::DONT_CARE
);
745 s2
= df
->format(d
, s2
, pos
);
748 UnicodeString substr
;
749 s2
.extract(0,2, substr
);
750 if (s
.indexOf(substr
) == -1)
751 errln("Months should match");
759 void DateFormatRegressionTest::Test4103341(void)
761 TimeZone
*saveZone
=TimeZone::createDefault();
764 // {sfb} changed from setDefault to adoptDefault
765 TimeZone::adoptDefault(TimeZone::createTimeZone("CST"));
766 UErrorCode status
= U_ZERO_ERROR
;
767 SimpleDateFormat
*simple
= new SimpleDateFormat(UnicodeString("MM/dd/yyyy HH:mm"), status
);
768 if(U_FAILURE(status
)) {
769 errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
773 failure(status
, "new SimpleDateFormat");
774 TimeZone
*temp
= TimeZone::createDefault();
775 if(simple
->getTimeZone() != *temp
)
776 errln("Fail: SimpleDateFormat not using default zone");
779 TimeZone::adoptDefault(saveZone
);
789 void DateFormatRegressionTest::Test4104136(void)
791 UErrorCode status
= U_ZERO_ERROR
;
792 SimpleDateFormat
*sdf
= new SimpleDateFormat(status
);
793 if(U_FAILURE(status
)) {
794 errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
798 if(failure(status
, "new SimpleDateFormat")) return;
799 UnicodeString pattern
= "'time' hh:mm";
800 sdf
->applyPattern(pattern
);
801 logln("pattern: \"" + pattern
+ "\"");
803 UnicodeString strings
[] = {
804 (UnicodeString
)"time 10:30",
805 (UnicodeString
) "time 10:x",
806 (UnicodeString
) "time 10x"
809 ParsePosition ppos
[] = {
816 date(70, UCAL_JANUARY
, 1, 10, 30),
822 "time 10:30", new ParsePosition(10), new Date(70, Calendar.JANUARY, 1, 10, 30),
823 "time 10:x", new ParsePosition(0), null,
824 "time 10x", new ParsePosition(0), null,
827 for(int i
= 0; i
< 3; i
++) {
828 UnicodeString text
= strings
[i
];
829 ParsePosition finish
= ppos
[i
];
830 UDate exp
= dates
[i
];
832 ParsePosition
pos(0);
833 UDate d
= sdf
->parse(text
, pos
);
834 logln(" text: \"" + text
+ "\"");
835 logln(" index: %d", pos
.getIndex());
836 logln((UnicodeString
) " result: " + d
);
837 if(pos
.getIndex() != finish
.getIndex())
838 errln("Fail: Expected pos " + finish
.getIndex());
839 if (! ((d
== 0 && exp
== -1) || (d
== exp
)))
840 errln((UnicodeString
) "Fail: Expected result " + exp
);
849 * According to the bug report, this test should throw a
850 * StringIndexOutOfBoundsException during the second parse. However,
853 void DateFormatRegressionTest::Test4104522(void)
855 UErrorCode status
= U_ZERO_ERROR
;
857 SimpleDateFormat
*sdf
= new SimpleDateFormat(status
);
858 if(U_FAILURE(status
)) {
859 errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
863 failure(status
, "new SimpleDateFormat");
864 UnicodeString pattern
= "'time' hh:mm";
865 sdf
->applyPattern(pattern
);
866 logln("pattern: \"" + pattern
+ "\"");
870 UnicodeString text
= "time ";
871 UDate dt
= sdf
->parse(text
, pp
);
872 logln(" text: \"" + text
+ "\"" +
878 dt
= sdf
->parse(text
, pp
);
879 logln(" text: \"" + text
+ "\"" +
888 void DateFormatRegressionTest::Test4106807(void)
891 DateFormat
*df
= DateFormat::createDateTimeInstance();
893 UErrorCode status
= U_ZERO_ERROR
;
894 SimpleDateFormat
*sdfs
[] = {
895 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss"), status
),
896 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'Z'"), status
),
897 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss''"), status
),
898 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'a''a'"), status
),
899 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss %"), status
)
901 if(U_FAILURE(status
)) {
902 errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
911 failure(status
, "new SimpleDateFormat");
913 UnicodeString strings
[] = {
914 (UnicodeString
) "19980211140000",
915 (UnicodeString
) "19980211140000",
916 (UnicodeString
) "19980211140000",
917 (UnicodeString
) "19980211140000a",
918 (UnicodeString
) "19980211140000 "
922 new SimpleDateFormat("yyyyMMddHHmmss"), "19980211140000",
923 new SimpleDateFormat("yyyyMMddHHmmss'Z'"), "19980211140000",
924 new SimpleDateFormat("yyyyMMddHHmmss''"), "19980211140000",
925 new SimpleDateFormat("yyyyMMddHHmmss'a''a'"), "19980211140000a",
926 new SimpleDateFormat("yyyyMMddHHmmss %"), "19980211140000 ",
928 GregorianCalendar
*gc
= new GregorianCalendar(status
);
929 failure(status
, "new GregorianCalendar");
930 TimeZone
*timeZone
= TimeZone::createDefault();
932 TimeZone
*gmt
= timeZone
->clone();
934 gmt
->setRawOffset(0);
936 for(int32_t i
= 0; i
< 5; i
++) {
937 SimpleDateFormat
*format
= sdfs
[i
];
938 UnicodeString dateString
= strings
[i
];
940 format
->setTimeZone(*gmt
);
941 dt
= format
->parse(dateString
, status
);
942 // {sfb} some of these parses will fail purposely
943 if(U_FAILURE(status
))
945 status
= U_ZERO_ERROR
;
947 FieldPosition
pos(FieldPosition::DONT_CARE
);
948 fmtd
= df
->format(dt
, fmtd
, pos
);
950 //logln(df->format(dt));
951 gc
->setTime(dt
, status
);
952 failure(status
, "gc->getTime");
953 logln(UnicodeString("") + gc
->get(UCAL_ZONE_OFFSET
, status
));
954 failure(status
, "gc->get");
956 s
= format
->format(dt
, s
, pos
);
959 catch (ParseException e) {
960 logln("No way Jose");
966 for(int32_t j
= 0; j
< 5; j
++)
973 Synopsis: Chinese time zone CTT is not recogonized correctly.
974 Description: Platform Chinese Windows 95 - ** Time zone set to CST **
980 // {sfb} what to do with this one ??
981 void DateFormatRegressionTest::Test4108407(void)
983 /*long l = System.currentTimeMillis();
984 logln("user.timezone = " + System.getProperty("user.timezone", "?"));
985 logln("Time Zone :" +
986 DateFormat.getDateInstance().getTimeZone().getID());
987 logln("Default format :" +
988 DateFormat.getDateInstance().format(new Date(l)));
989 logln("Full format :" +
990 DateFormat.getDateInstance(DateFormat.FULL).format(new
992 logln("*** Set host TZ to CST ***");
993 logln("*** THE RESULTS OF THIS TEST MUST BE VERIFIED MANUALLY ***");*/
998 * SimpleDateFormat won't parse "GMT"
1000 void DateFormatRegressionTest::Test4134203(void)
1002 UErrorCode status
= U_ZERO_ERROR
;
1003 UnicodeString dateFormat
= "MM/dd/yy HH:mm:ss zzz";
1004 SimpleDateFormat
*fmt
= new SimpleDateFormat(dateFormat
, status
);
1005 if(failure(status
, "new SimpleDateFormat")) return;
1006 ParsePosition
p0(0);
1007 UDate d
= fmt
->parse("01/22/92 04:52:00 GMT", p0
);
1008 logln(dateToString(d
));
1009 if(p0
== ParsePosition(0))
1010 errln("Fail: failed to parse 'GMT'");
1011 // In the failure case an exception is thrown by parse();
1012 // if no exception is thrown, the test passes.
1019 * SimpleDateFormat incorrect handling of 2 single quotes in format()
1021 void DateFormatRegressionTest::Test4151631(void)
1023 UnicodeString pattern
= "'TO_DATE('''dd'-'MM'-'yyyy HH:mm:ss''' , ''DD-MM-YYYY HH:MI:SS'')'";
1024 logln("pattern=" + pattern
);
1025 UErrorCode status
= U_ZERO_ERROR
;
1026 SimpleDateFormat
*format
= new SimpleDateFormat(pattern
, Locale::getUS(), status
);
1027 if(failure(status
, "new SimpleDateFormat")) return;
1028 UnicodeString result
;
1029 FieldPosition
pos(FieldPosition::DONT_CARE
);
1030 result
= format
->format(date(1998-1900, UCAL_JUNE
, 30, 13, 30, 0), result
, pos
);
1031 if (result
!= "TO_DATE('30-06-1998 13:30:00' , 'DD-MM-YYYY HH:MI:SS')") {
1032 errln("Fail: result=" + result
);
1035 logln("Pass: result=" + result
);
1043 * 'z' at end of date format throws index exception in SimpleDateFormat
1044 * CANNOT REPRODUCE THIS BUG ON 1.2FCS
1046 void DateFormatRegressionTest::Test4151706(void)
1048 UnicodeString
dateString("Thursday, 31-Dec-98 23:00:00 GMT");
1049 UErrorCode status
= U_ZERO_ERROR
;
1050 SimpleDateFormat
fmt(UnicodeString("EEEE, dd-MMM-yy HH:mm:ss z"), Locale::getUS(), status
);
1051 if(failure(status
, "new SimpleDateFormat")) return;
1053 UDate d
= fmt
.parse(dateString
, status
);
1054 failure(status
, "fmt->parse");
1055 // {sfb} what about next two lines?
1056 //if (d.getTime() != Date.UTC(1998-1900, Calendar.DECEMBER, 31, 23, 0, 0))
1057 // errln("Incorrect value: " + d);
1058 /*} catch (Exception e) {
1059 errln("Fail: " + e);
1062 FieldPosition
pos(0);
1063 logln(dateString
+ " -> " + fmt
.format(d
, temp
, pos
));
1068 * Cannot reproduce this bug under 1.2 FCS -- it may be a convoluted duplicate
1069 * of some other bug that has been fixed.
1072 DateFormatRegressionTest::Test4162071(void)
1074 UnicodeString
dateString("Thu, 30-Jul-1999 11:51:14 GMT");
1075 UnicodeString
format("EEE', 'dd-MMM-yyyy HH:mm:ss z"); // RFC 822/1123
1076 UErrorCode status
= U_ZERO_ERROR
;
1077 SimpleDateFormat
df(format
, Locale::getUS(), status
);
1078 if(U_FAILURE(status
)) {
1079 errln("Couldn't create SimpleDateFormat");
1084 UDate x
= df
.parse(dateString
, status
);
1085 if(U_SUCCESS(status
))
1086 logln("Parse format \"" + format
+ "\" ok");
1088 errln("Parse format \"" + format
+ "\" failed.");
1090 FieldPosition
pos(0);
1091 logln(dateString
+ " -> " + df
.format(x
, temp
, pos
));
1092 //} catch (Exception e) {
1093 // errln("Parse format \"" + format + "\" failed.");
1098 * DateFormat shouldn't parse year "-1" as a two-digit year (e.g., "-1" -> 1999).
1100 void DateFormatRegressionTest::Test4182066(void) {
1101 UErrorCode status
= U_ZERO_ERROR
;
1102 SimpleDateFormat
fmt("MM/dd/yy", Locale::getUS(), status
);
1103 SimpleDateFormat
dispFmt("MMM dd yyyy GG", Locale::getUS(), status
);
1104 if (U_FAILURE(status
)) {
1105 errln("Couldn't create SimpleDateFormat");
1109 /* We expect 2-digit year formats to put 2-digit years in the right
1110 * window. Out of range years, that is, anything less than "00" or
1111 * greater than "99", are treated as literal years. So "1/2/3456"
1112 * becomes 3456 AD. Likewise, "1/2/-3" becomes -3 AD == 2 BC.
1114 const char* STRINGS
[] = {
1124 int32_t STRINGS_COUNT
= (int32_t)(sizeof(STRINGS
) / sizeof(STRINGS
[0]));
1125 UDate FAIL_DATE
= (UDate
) 0;
1127 date(2000-1900, UCAL_FEBRUARY
, 29),
1128 date(2001-1900, UCAL_JANUARY
, 23),
1129 date( -1-1900, UCAL_APRIL
, 5),
1130 date( -9-1900, UCAL_JANUARY
, 23),
1131 date(1314-1900, UCAL_NOVEMBER
, 12),
1132 date( 1-1900, UCAL_OCTOBER
, 31),
1133 FAIL_DATE
, // "+1" isn't recognized by US NumberFormat
1134 date( 1-1900, UCAL_SEPTEMBER
,12),
1139 for (int32_t i
=0; i
<STRINGS_COUNT
; ++i
) {
1140 UnicodeString
str(STRINGS
[i
]);
1141 UDate expected
= DATES
[i
];
1142 status
= U_ZERO_ERROR
;
1143 UDate actual
= fmt
.parse(str
, status
);
1144 if (U_FAILURE(status
)) {
1147 UnicodeString actStr
;
1148 if (actual
== FAIL_DATE
) {
1149 actStr
.append("null");
1152 ((DateFormat
*)&dispFmt
)->format(actual
, actStr
);
1155 if (expected
== actual
) {
1156 out
.append(str
+ " => " + actStr
+ "\n");
1158 UnicodeString expStr
;
1159 if (expected
== FAIL_DATE
) {
1160 expStr
.append("null");
1163 ((DateFormat
*)&dispFmt
)->format(expected
, expStr
);
1165 out
.append("FAIL: " + str
+ " => " + actStr
1166 + ", expected " + expStr
+ "\n");
1178 * j32 {JDK Bug 4210209 4209272}
1179 * DateFormat cannot parse Feb 29 2000 when setLenient(false)
1182 DateFormatRegressionTest::Test4210209(void) {
1183 UErrorCode status
= U_ZERO_ERROR
;
1184 UnicodeString
pattern("MMM d, yyyy");
1185 SimpleDateFormat
sfmt(pattern
, Locale::getUS(), status
);
1186 SimpleDateFormat
sdisp("MMM dd yyyy GG", Locale::getUS(), status
);
1187 DateFormat
& fmt
= *(DateFormat
*)&sfmt
; // Yuck: See j25
1188 DateFormat
& disp
= *(DateFormat
*)&sdisp
; // Yuck: See j25
1189 if (U_FAILURE(status
)) {
1190 errln("Couldn't create SimpleDateFormat");
1193 Calendar
* calx
= (Calendar
*)fmt
.getCalendar(); // cast away const!
1194 calx
->setLenient(FALSE
);
1195 UDate d
= date(2000-1900, UCAL_FEBRUARY
, 29);
1196 UnicodeString s
, ss
;
1198 logln(disp
.format(d
, ss
.remove()) + " f> " + pattern
+
1199 " => \"" + s
+ "\"");
1200 ParsePosition
pos(0);
1201 d
= fmt
.parse(s
, pos
);
1202 logln(UnicodeString("\"") + s
+ "\" p> " + pattern
+
1203 " => " + disp
.format(d
, ss
.remove()));
1204 logln(UnicodeString("Parse pos = ") + pos
.getIndex() +
1205 ", error pos = " + pos
.getErrorIndex());
1206 if (pos
.getErrorIndex() != -1) {
1207 errln(UnicodeString("FAIL: Error index should be -1"));
1210 // The underlying bug is in GregorianCalendar. If the following lines
1211 // succeed, the bug is fixed. If the bug isn't fixed, they will throw
1213 GregorianCalendar
cal(status
);
1214 if (U_FAILURE(status
)) {
1215 errln("FAIL: Unable to create Calendar");
1219 cal
.setLenient(FALSE
);
1220 cal
.set(2000, UCAL_FEBRUARY
, 29); // This should work!
1221 logln(UnicodeString("Attempt to set Calendar to Feb 29 2000: ") +
1222 disp
.format(cal
.getTime(status
), ss
.remove()));
1223 if (U_FAILURE(status
)) {
1224 errln("FAIL: Unable to set Calendar to Feb 29 2000");
1228 void DateFormatRegressionTest::Test714(void)
1231 UDate
d(978103543000.);
1232 DateFormat
*fmt
= DateFormat::createDateTimeInstance(DateFormat::NONE
,
1236 dataerrln("Error calling DateFormat::createDateTimeInstance");
1241 UnicodeString tests
=
1242 (UnicodeString
) "7:25:43 AM" ;
1243 UErrorCode status
= U_ZERO_ERROR
;
1245 if(U_FAILURE(status
))
1247 errln((UnicodeString
) "Fail, errmsg " + u_errorName(status
));
1253 errln((UnicodeString
) "Fail: " + s
+ " != " + tests
);
1257 logln("OK: " + s
+ " == " + tests
);
1263 class Test1684Data
{
1273 UnicodeString normalized
;
1275 Test1684Data(int32_t xyear
, int32_t xmonth
, int32_t xdate
,
1276 int32_t xwomyear
, int32_t xwommon
, int32_t xwom
, int32_t xdow
,
1277 const char *xdata
, const char *xnormalized
) :
1286 normalized((xnormalized
==NULL
)?xdata
:xnormalized
,"")
1290 void DateFormatRegressionTest::Test1684(void)
1292 // July 2001 August 2001 January 2002
1293 // Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1294 // 1 2 3 4 5 6 7 1 2 3 4 1 2 3 4 5
1295 // 8 9 10 11 12 13 14 5 6 7 8 9 10 11 6 7 8 9 10 11 12
1296 // 15 16 17 18 19 20 21 12 13 14 15 16 17 18 13 14 15 16 17 18 19
1297 // 22 23 24 25 26 27 28 19 20 21 22 23 24 25 20 21 22 23 24 25 26
1298 // 29 30 31 26 27 28 29 30 31 27 28 29 30 31
1299 Test1684Data
*tests
[] = {
1300 new Test1684Data(2001, 8, 6, 2001,8,2,UCAL_MONDAY
, "2001 08 02 Mon", NULL
),
1301 new Test1684Data(2001, 8, 7, 2001,8,2,UCAL_TUESDAY
, "2001 08 02 Tue", NULL
),
1302 new Test1684Data(2001, 8, 5,/*12,*/ 2001,8,2,UCAL_SUNDAY
, "2001 08 02 Sun", NULL
),
1303 new Test1684Data(2001, 8,6, /*7, 30,*/ 2001,7,6,UCAL_MONDAY
, "2001 07 06 Mon", "2001 08 02 Mon"),
1304 new Test1684Data(2001, 8,7, /*7, 31,*/ 2001,7,6,UCAL_TUESDAY
, "2001 07 06 Tue", "2001 08 02 Tue"),
1305 new Test1684Data(2001, 8, 5, 2001,7,6,UCAL_SUNDAY
, "2001 07 06 Sun", "2001 08 02 Sun"),
1306 new Test1684Data(2001, 7, 30, 2001,8,1,UCAL_MONDAY
, "2001 08 01 Mon", "2001 07 05 Mon"),
1307 new Test1684Data(2001, 7, 31, 2001,8,1,UCAL_TUESDAY
, "2001 08 01 Tue", "2001 07 05 Tue"),
1308 new Test1684Data(2001, 7,29, /*8, 5,*/ 2001,8,1,UCAL_SUNDAY
, "2001 08 01 Sun", "2001 07 05 Sun"),
1309 new Test1684Data(2001, 12, 31, 2001,12,6,UCAL_MONDAY
, "2001 12 06 Mon", NULL
),
1310 new Test1684Data(2002, 1, 1, 2002,1,1,UCAL_TUESDAY
, "2002 01 01 Tue", NULL
),
1311 new Test1684Data(2002, 1, 2, 2002,1,1,UCAL_WEDNESDAY
, "2002 01 01 Wed", NULL
),
1312 new Test1684Data(2002, 1, 3, 2002,1,1,UCAL_THURSDAY
, "2002 01 01 Thu", NULL
),
1313 new Test1684Data(2002, 1, 4, 2002,1,1,UCAL_FRIDAY
, "2002 01 01 Fri", NULL
),
1314 new Test1684Data(2002, 1, 5, 2002,1,1,UCAL_SATURDAY
, "2002 01 01 Sat", NULL
),
1315 new Test1684Data(2001,12,30, /*2002, 1, 6,*/ 2002,1,1,UCAL_SUNDAY
, "2002 01 01 Sun", "2001 12 06 Sun")
1318 #define kTest1684Count ((int32_t)(sizeof(tests)/sizeof(tests[0])))
1320 int32_t pass
= 0, error
= 0, warning
= 0;
1323 UErrorCode status
= U_ZERO_ERROR
;
1324 UnicodeString
pattern("yyyy MM WW EEE","");
1325 Calendar
*cal
= new GregorianCalendar(status
);
1326 SimpleDateFormat
*sdf
= new SimpleDateFormat(pattern
,status
);
1327 if (U_FAILURE(status
)) {
1328 dataerrln("Error constructing SimpleDateFormat");
1329 for(i
=0;i
<kTest1684Count
;i
++) {
1336 cal
->setFirstDayOfWeek(UCAL_SUNDAY
);
1337 cal
->setMinimalDaysInFirstWeek(1);
1339 sdf
->adoptCalendar(cal
);
1341 cal
= sdf
->getCalendar()->clone(); // sdf may have deleted calendar
1343 if(!cal
|| !sdf
|| U_FAILURE(status
)) {
1344 errln(UnicodeString("Error setting up test: ") + u_errorName(status
));
1347 for (i
= 0; i
< kTest1684Count
; ++i
) {
1348 Test1684Data
&test
= *(tests
[i
]);
1349 logln(UnicodeString("#") + i
+ UnicodeString("\n-----\nTesting round trip of ") + test
.year
+
1350 " " + (test
.month
+ 1) +
1352 " (written as) " + test
.data
);
1355 cal
->set(test
.year
, test
.month
, test
.date
);
1356 UDate ms
= cal
->getTime(status
);
1359 cal
->set(UCAL_YEAR
, test
.womyear
);
1360 cal
->set(UCAL_MONTH
, test
.wommon
);
1361 cal
->set(UCAL_WEEK_OF_MONTH
, test
.wom
);
1362 cal
->set(UCAL_DAY_OF_WEEK
, test
.dow
);
1363 UDate ms2
= cal
->getTime(status
);
1366 errln((UnicodeString
)"\nError: GregorianUCAL_DOM gave " + ms
+
1367 "\n GregorianUCAL_WOM gave " + ms2
);
1373 ms2
= sdf
->parse(test
.data
, status
);
1374 if(U_FAILURE(status
)) {
1375 errln("parse exception: " + UnicodeString(u_errorName(status
)));
1379 errln((UnicodeString
)"\nError: GregorianCalendar gave " + ms
+
1380 "\n SimpleDateFormat.parse gave " + ms2
);
1386 UnicodeString result
;
1387 sdf
->format(ms
, result
);
1388 if (result
!= test
.normalized
) {
1389 errln("\nWarning: format of '" + test
.data
+ "' gave" +
1390 "\n '" + result
+ "'" +
1391 "\n expected '" + test
.normalized
+ "'");
1398 ms3
= sdf
->parse(result
, status
);
1399 if(U_FAILURE(status
)) {
1400 errln("parse exception 2: " + (UnicodeString
)u_errorName(status
));
1405 errln((UnicodeString
)"\nError: Re-parse of '" + result
+ "' gave time of " +
1414 = UnicodeString("Passed: ") + pass
+ ", Warnings: " + warning
+ ", Errors: " + error
;
1421 for(i
=0;i
<kTest1684Count
;i
++) {
1428 #endif /* #if !UCONFIG_NO_FORMATTING */