1 /********************************************************************
3 * Copyright (c) 1997-2004, 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 failure(status
, "SimpleDateFormat::createDateInstance");
74 pat
= sdf
->toPattern(pat
);
75 logln("pattern: " + pat
);
77 FieldPosition
pos(FieldPosition::DONT_CARE
);
78 fmtd
= sdf
->format(today
, fmtd
, pos
);
79 logln("today: " + fmtd
);
81 sdf
->applyPattern("G yyyy DDD");
83 todayS
= sdf
->format(today
, todayS
, pos
);
84 logln("today: " + todayS
);
86 today
= sdf
->parse(todayS
, status
);
87 failure(status
, "sdf->parse");
88 logln((UnicodeString
)"today date: " + today
);
89 /*} catch(Exception e) {
90 logln("Error reparsing date: " + e.getMessage());
95 rt
= sdf
->format(sdf
->parse(todayS
, status
), rt
, pos
);
96 failure(status
, "sdf->parse");
97 logln("round trip: " + rt
);
99 errln("Fail: Want " + todayS
+ " Got " + rt
);
101 catch (ParseException e) {
112 void DateFormatRegressionTest::Test4052408(void)
115 DateFormat
*fmt
= DateFormat::createDateTimeInstance(DateFormat::SHORT
,
116 DateFormat::SHORT
, Locale::getUS());
117 UDate dt
= date(97, UCAL_MAY
, 3, 8, 55);
119 str
= fmt
->format(dt
, str
);
122 if(str
!= "5/3/97 8:55 AM")
123 errln("Fail: Test broken; Want 5/3/97 8:55 AM Got " + str
);
125 UnicodeString expected
[] = {
126 (UnicodeString
) "", //"ERA_FIELD",
127 (UnicodeString
) "97", //"YEAR_FIELD",
128 (UnicodeString
) "5", //"MONTH_FIELD",
129 (UnicodeString
) "3", //"DATE_FIELD",
130 (UnicodeString
) "", //"HOUR_OF_DAY1_FIELD",
131 (UnicodeString
) "", //"HOUR_OF_DAY0_FIELD",
132 (UnicodeString
) "55", //"MINUTE_FIELD",
133 (UnicodeString
) "", //"SECOND_FIELD",
134 (UnicodeString
) "", //"MILLISECOND_FIELD",
135 (UnicodeString
) "", //"DAY_OF_WEEK_FIELD",
136 (UnicodeString
) "", //"DAY_OF_YEAR_FIELD",
137 (UnicodeString
) "", //"DAY_OF_WEEK_IN_MONTH_FIELD",
138 (UnicodeString
) "", //"WEEK_OF_YEAR_FIELD",
139 (UnicodeString
) "", //"WEEK_OF_MONTH_FIELD",
140 (UnicodeString
) "AM", //"AM_PM_FIELD",
141 (UnicodeString
) "8", //"HOUR1_FIELD",
142 (UnicodeString
) "", //"HOUR0_FIELD",
143 (UnicodeString
) "" //"TIMEZONE_FIELD"
146 //Hashtable expected;// = new Hashtable();
147 //expected.put(new LongKey(DateFormat.MONTH_FIELD), "5");
148 //expected.put(new LongKey(DateFormat.DATE_FIELD), "3");
149 //expected.put(new LongKey(DateFormat.YEAR_FIELD), "97");
150 //expected.put(new LongKey(DateFormat.HOUR1_FIELD), "8");
151 //expected.put(new LongKey(DateFormat.MINUTE_FIELD), "55");
152 //expected.put(new LongKey(DateFormat.AM_PM_FIELD), "AM");
154 //StringBuffer buf = new StringBuffer();
155 UnicodeString fieldNames
[] = {
156 (UnicodeString
) "ERA_FIELD",
157 (UnicodeString
) "YEAR_FIELD",
158 (UnicodeString
) "MONTH_FIELD",
159 (UnicodeString
) "DATE_FIELD",
160 (UnicodeString
) "HOUR_OF_DAY1_FIELD",
161 (UnicodeString
) "HOUR_OF_DAY0_FIELD",
162 (UnicodeString
) "MINUTE_FIELD",
163 (UnicodeString
) "SECOND_FIELD",
164 (UnicodeString
) "MILLISECOND_FIELD",
165 (UnicodeString
) "DAY_OF_WEEK_FIELD",
166 (UnicodeString
) "DAY_OF_YEAR_FIELD",
167 (UnicodeString
) "DAY_OF_WEEK_IN_MONTH_FIELD",
168 (UnicodeString
) "WEEK_OF_YEAR_FIELD",
169 (UnicodeString
) "WEEK_OF_MONTH_FIELD",
170 (UnicodeString
) "AM_PM_FIELD",
171 (UnicodeString
) "HOUR1_FIELD",
172 (UnicodeString
) "HOUR0_FIELD",
173 (UnicodeString
) "TIMEZONE_FIELD"
177 for(int i
= 0; i
<= 17; ++i
) {
178 FieldPosition
pos(i
);
180 fmt
->format(dt
, buf
, pos
);
181 //char[] dst = new char[pos.getEndIndex() - pos.getBeginIndex()];
183 buf
.extractBetween(pos
.getBeginIndex(), pos
.getEndIndex(), dst
);
184 UnicodeString
str(dst
);
185 logln((UnicodeString
)"" + i
+ (UnicodeString
)": " + fieldNames
[i
] +
186 (UnicodeString
)", \"" + str
+ (UnicodeString
)"\", " +
187 pos
.getBeginIndex() + (UnicodeString
)", " +
189 UnicodeString exp
= expected
[i
];
190 if((exp
.length() == 0 && str
.length() == 0) || str
== exp
)
193 errln(UnicodeString(" expected ") + exp
);
199 errln("Fail: FieldPosition not set right by DateFormat");
206 * Verify the function of the [s|g]et2DigitYearStart() API.
208 void DateFormatRegressionTest::Test4056591(void)
210 UErrorCode status
= U_ZERO_ERROR
;
213 SimpleDateFormat
*fmt
= new SimpleDateFormat(UnicodeString("yyMMdd"), Locale::getUS(), status
);
214 failure(status
, "new SimpleDateFormat");
215 UDate start
= date(1809-1900, UCAL_DECEMBER
, 25);
216 fmt
->set2DigitYearStart(start
, status
);
217 failure(status
, "fmt->setTwoDigitStartDate");
218 if( (fmt
->get2DigitYearStart(status
) != start
) || failure(status
, "get2DigitStartDate"))
219 errln("get2DigitYearStart broken");
221 date(1809-1900, UCAL_DECEMBER
, 25),
222 date(1909-1900, UCAL_DECEMBER
, 24),
223 date(1809-1900, UCAL_DECEMBER
, 26),
224 date(1861-1900, UCAL_DECEMBER
, 25),
227 UnicodeString strings
[] = {
228 (UnicodeString
) "091225",
229 (UnicodeString
) "091224",
230 (UnicodeString
) "091226",
231 (UnicodeString
) "611225"
235 "091225", new Date(1809-1900, Calendar.DECEMBER, 25),
236 "091224", new Date(1909-1900, Calendar.DECEMBER, 24),
237 "091226", new Date(1809-1900, Calendar.DECEMBER, 26),
238 "611225", new Date(1861-1900, Calendar.DECEMBER, 25),
241 for(int i
= 0; i
< 4; i
++) {
242 UnicodeString s
= strings
[i
];
243 UDate exp
= dates
[i
];
244 UDate got
= fmt
->parse(s
, status
);
245 failure(status
, "fmt->parse");
246 logln(s
+ " -> " + got
+ "; exp " + exp
);
248 errln("set2DigitYearStart broken");
251 catch (ParseException e) {
262 void DateFormatRegressionTest::Test4059917(void)
264 UErrorCode status
= U_ZERO_ERROR
;
266 SimpleDateFormat
*fmt
;
267 UnicodeString myDate
;
269 fmt
= new SimpleDateFormat( UnicodeString("yyyy/MM/dd"), status
);
270 failure(status
, "new SimpleDateFormat");
271 myDate
= "1997/01/01";
272 aux917( fmt
, myDate
);
277 fmt
= new SimpleDateFormat( UnicodeString("yyyyMMdd"), status
);
278 failure(status
, "new SimpleDateFormat");
280 aux917( fmt
, myDate
);
285 void DateFormatRegressionTest::aux917( SimpleDateFormat
*fmt
, UnicodeString
& str
) {
288 pat
= fmt
->toPattern(pat
);
289 logln( "==================" );
290 logln( "testIt: pattern=" + pat
+
296 ParsePosition
pos(0);
297 fmt
->parseObject( str
, o
, pos
);
298 //logln( UnicodeString("Parsed object: ") + o );
300 UErrorCode status
= U_ZERO_ERROR
;
301 UnicodeString formatted
;
302 FieldPosition
poss(FieldPosition::DONT_CARE
);
303 formatted
= fmt
->format( o
, formatted
, poss
, status
);
304 failure(status
, "fmt->format");
305 logln( "Formatted string: " + formatted
);
306 if( formatted
!= str
)
307 errln("Fail: Want " + str
+ " Got " + formatted
);
309 catch (ParseException e) {
318 void DateFormatRegressionTest::Test4060212(void)
320 UnicodeString dateString
= "1995-040.05:01:29";
322 logln( "dateString= " + dateString
);
323 logln("Using yyyy-DDD.hh:mm:ss");
324 UErrorCode status
= U_ZERO_ERROR
;
325 SimpleDateFormat
*formatter
= new SimpleDateFormat(UnicodeString("yyyy-DDD.hh:mm:ss"), status
);
326 failure(status
, "new SimpleDateFormat");
327 ParsePosition
pos(0);
328 UDate myDate
= formatter
->parse( dateString
, pos
);
329 UnicodeString myString
;
330 DateFormat
*fmt
= DateFormat::createDateTimeInstance( DateFormat::FULL
,
332 myString
= fmt
->format( myDate
, myString
);
335 Calendar
*cal
= new GregorianCalendar(status
);
336 failure(status
, "new GregorianCalendar");
337 cal
->setTime(myDate
, status
);
338 failure(status
, "cal->setTime");
339 if ((cal
->get(UCAL_DAY_OF_YEAR
, status
) != 40) || failure(status
, "cal->get"))
340 errln((UnicodeString
) "Fail: Got " + cal
->get(UCAL_DAY_OF_YEAR
, status
) +
343 logln("Using yyyy-ddd.hh:mm:ss");
346 formatter
= new SimpleDateFormat(UnicodeString("yyyy-ddd.hh:mm:ss"), status
);
347 failure(status
, "new SimpleDateFormat");
349 myDate
= formatter
->parse( dateString
, pos
);
350 myString
= fmt
->format( myDate
, myString
);
352 cal
->setTime(myDate
, status
);
353 failure(status
, "cal->setTime");
354 if ((cal
->get(UCAL_DAY_OF_YEAR
, status
) != 40) || failure(status
, "cal->get"))
355 errln((UnicodeString
) "Fail: Got " + cal
->get(UCAL_DAY_OF_YEAR
, status
) +
366 void DateFormatRegressionTest::Test4061287(void)
368 UErrorCode status
= U_ZERO_ERROR
;
370 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("dd/MM/yyyy"), status
);
371 if(U_FAILURE(status
)) {
372 errln("Couldn't create SimpleDateFormat, error: %s", u_errorName(status
));
376 failure(status
, "new SimpleDateFormat");
378 logln(UnicodeString("") + df
->parse("35/01/1971", status
));
379 failure(status
, "df->parse");
380 //logln(df.parse("35/01/1971").toString());
382 /*catch (ParseException e) {
386 df
->setLenient(FALSE
);
389 logln(UnicodeString("") + df
->parse("35/01/1971", status
));
390 if(U_FAILURE(status
))
392 //logln(df.parse("35/01/1971").toString());
393 //} catch (ParseException e) {ok=TRUE;}
395 errln("Fail: Lenient not working");
402 void DateFormatRegressionTest::Test4065240(void)
405 DateFormat
*shortdate
, *fulldate
;
406 UnicodeString strShortDate
, strFullDate
;
407 Locale saveLocale
= Locale::getDefault();
408 TimeZone
*saveZone
= TimeZone::createDefault();
410 UErrorCode status
= U_ZERO_ERROR
;
412 Locale
*curLocale
= new Locale("de","DE");
413 Locale::setDefault(*curLocale
, status
);
414 failure(status
, "Locale::setDefault");
415 // {sfb} adoptDefault instead of setDefault
416 //TimeZone::setDefault(TimeZone::createTimeZone("EST"));
417 TimeZone::adoptDefault(TimeZone::createTimeZone("EST"));
418 curDate
= date(98, 0, 1);
419 shortdate
= DateFormat::createDateInstance(DateFormat::SHORT
);
420 fulldate
= DateFormat::createDateTimeInstance(DateFormat::LONG
, DateFormat::LONG
);
421 strShortDate
= "The current date (short form) is ";
423 temp
= shortdate
->format(curDate
, temp
);
424 strShortDate
+= temp
;
425 strFullDate
= "The current date (long form) is ";
427 fulldate
->format(curDate
, temp2
);
428 strFullDate
+= temp2
;
433 // {sfb} What to do with resource bundle stuff?????
435 // Check to see if the resource is present; if not, we can't test
436 ResourceBundle
*bundle
= new ResourceBundle(
437 NULL
, *curLocale
, status
);
438 failure(status
, "new ResourceBundle");
439 //(UnicodeString) "java.text.resources.DateFormatZoneData", curLocale);
441 // {sfb} API change to ResourceBundle -- add getLocale()
442 /*if (bundle->getLocale().getLanguage(temp) == UnicodeString("de")) {
443 // UPDATE THIS AS ZONE NAME RESOURCE FOR <EST> in de_DE is updated
444 if (!strFullDate.endsWith(UnicodeString("GMT-05:00")))
445 errln("Fail: Want GMT-05:00");
448 logln("*** TEST COULD NOT BE COMPLETED BECAUSE DateFormatZoneData ***");
449 logln("*** FOR LOCALE de OR de_DE IS MISSING ***");
453 Locale::setDefault(saveLocale
, status
);
454 failure(status
, "Locale::setDefault");
455 TimeZone::setDefault(*saveZone
);
466 DateFormat.equals is too narrowly defined. As a result, MessageFormat
467 does not work correctly. DateFormat.equals needs to be written so
468 that the Calendar sub-object is not compared using Calendar.equals,
469 but rather compared for equivalency. This may necessitate adding a
470 (package private) method to Calendar to test for equivalency.
472 Currently this bug breaks MessageFormat.toPattern
477 void DateFormatRegressionTest::Test4071441(void)
479 DateFormat
*fmtA
= DateFormat::createInstance();
480 DateFormat
*fmtB
= DateFormat::createInstance();
482 // {sfb} Is it OK to cast away const here?
483 Calendar
*calA
= (Calendar
*) fmtA
->getCalendar();
484 Calendar
*calB
= (Calendar
*) fmtB
->getCalendar();
486 errln("Couldn't get proper calendars, exiting");
491 UDate epoch
= date(0, 0, 0);
492 UDate xmas
= date(61, UCAL_DECEMBER
, 25);
494 UErrorCode status
= U_ZERO_ERROR
;
495 calA
->setTime(epoch
, status
);
496 failure(status
, "calA->setTime");
497 calB
->setTime(epoch
, status
);
498 failure(status
, "calB->setTime");
500 errln("Fail: Can't complete test; Calendar instances unequal");
502 errln("Fail: DateFormat unequal when Calendars equal");
503 calB
->setTime(xmas
, status
);
504 failure(status
, "calB->setTime");
506 errln("Fail: Can't complete test; Calendar instances equal");
508 errln("Fail: DateFormat unequal when Calendars equivalent");
510 logln("DateFormat.equals ok");
516 /* The java.text.DateFormat.parse(String) method expects for the
517 US locale a string formatted according to mm/dd/yy and parses it
520 When given a string mm/dd/yyyy it only parses up to the first
521 two y's, typically resulting in a date in the year 1919.
523 Please extend the parsing method(s) to handle strings with
524 four-digit year values (probably also applicable to various
529 void DateFormatRegressionTest::Test4073003(void)
532 UErrorCode ec
= U_ZERO_ERROR
;
533 SimpleDateFormat
fmt("dd/MM/yy", Locale::getUK(), ec
);
535 errln("FAIL: SimpleDateFormat constructor");
538 UnicodeString tests
[] = {
539 (UnicodeString
) "12/25/61",
540 (UnicodeString
) "12/25/1961",
541 (UnicodeString
) "4/3/2010",
542 (UnicodeString
) "4/3/10"
544 UErrorCode status
= U_ZERO_ERROR
;
545 for(int i
= 0; i
< 4; i
+=2) {
546 UDate d
= fmt
.parse(tests
[i
], status
);
547 failure(status
, "fmt.parse");
548 UDate dd
= fmt
.parse(tests
[i
+1], status
);
549 failure(status
, "fmt.parse");
551 s
= fmt
.format(d
, s
);
553 ss
= fmt
.format(dd
, ss
);
555 errln((UnicodeString
) "Fail: " + d
+ " != " + dd
);
557 errln((UnicodeString
)"Fail: " + s
+ " != " + ss
);
558 logln("Ok: " + s
+ " " + d
);
565 void DateFormatRegressionTest::Test4089106(void)
567 TimeZone
*def
= TimeZone::createDefault();
569 TimeZone
*z
= new SimpleTimeZone((int)(1.25 * 3600000), "FAKEZONE");
570 TimeZone::setDefault(*z
);
571 UErrorCode status
= U_ZERO_ERROR
;
572 SimpleDateFormat
*f
= new SimpleDateFormat(status
);
573 if(U_FAILURE(status
)) {
574 errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
580 failure(status
, "new SimpleDateFormat");
581 if (f
->getTimeZone()!= *z
)
582 errln("Fail: SimpleTimeZone should use TimeZone.getDefault()");
586 TimeZone::setDefault(*def
);
598 // {sfb} not applicable in C++??
600 void DateFormatRegressionTest::Test4100302(void)
602 /* Locale locales [] = {
604 Locale::CANADA_FRENCH,
619 Locale::SIMPLIFIED_CHINESE,
621 Locale::TRADITIONAL_CHINESE,
627 for(int i = 0; i < 21; i++) {
629 Format *format = DateFormat::createDateTimeInstance(DateFormat::FULL,
630 DateFormat::FULL, locales[i]);
633 ByteArrayOutputStream baos = new ByteArrayOutputStream();
634 ObjectOutputStream oos = new ObjectOutputStream(baos);
636 oos.writeObject(format);
640 bytes = baos.toByteArray();
642 ObjectInputStream ois =
643 new ObjectInputStream(new ByteArrayInputStream(bytes));
645 if (!format.equals(ois.readObject())) {
647 logln("DateFormat instance for locale " +
648 locales[i] + " is incorrectly serialized/deserialized.");
650 logln("DateFormat instance for locale " +
651 locales[i] + " is OKAY.");
654 if (!pass) errln("Fail: DateFormat serialization/equality bug");
656 catch (IOException e) {
660 catch (ClassNotFoundException e) {
669 void DateFormatRegressionTest::Test4101483(void)
671 UErrorCode status
= U_ZERO_ERROR
;
672 SimpleDateFormat
*sdf
= new SimpleDateFormat(UnicodeString("z"), Locale::getUS(), status
);
673 failure(status
, "new SimpleDateFormat");
674 FieldPosition
fp(UDAT_TIMEZONE_FIELD
);
675 //Date d = date(9234567890L);
676 UDate d
= 9234567890.0;
677 //StringBuffer buf = new StringBuffer("");
679 sdf
->format(d
, buf
, fp
);
680 //logln(sdf.format(d, buf, fp).toString());
681 logln(dateToString(d
) + " => " + buf
);
682 logln("beginIndex = " + fp
.getBeginIndex());
683 logln("endIndex = " + fp
.getEndIndex());
684 if (fp
.getBeginIndex() == fp
.getEndIndex())
685 errln("Fail: Empty field");
693 * This bug really only works in Locale.US, since that's what the locale
694 * used for Date.toString() is. Bug 4138203 reports that it fails on Korean
695 * NT; it would actually have failed on any non-US locale. Now it should
696 * work on all locales.
698 void DateFormatRegressionTest::Test4103340(void)
700 UErrorCode status
= U_ZERO_ERROR
;
702 // choose a date that is the FIRST of some month
703 // and some arbitrary time
704 UDate d
= date(97, 3, 1, 1, 1, 1);
705 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("MMMM"), Locale::getUS(), status
);
706 failure(status
, "new SimpleDateFormat");
709 s
= dateToString(d
, s
);
711 FieldPosition
pos(FieldPosition::DONT_CARE
);
712 s2
= df
->format(d
, s2
, pos
);
715 UnicodeString substr
;
716 s2
.extract(0,2, substr
);
717 if (s
.indexOf(substr
) == -1)
718 errln("Months should match");
726 void DateFormatRegressionTest::Test4103341(void)
728 TimeZone
*saveZone
=TimeZone::createDefault();
731 // {sfb} changed from setDefault to adoptDefault
732 TimeZone::adoptDefault(TimeZone::createTimeZone("CST"));
733 UErrorCode status
= U_ZERO_ERROR
;
734 SimpleDateFormat
*simple
= new SimpleDateFormat(UnicodeString("MM/dd/yyyy HH:mm"), status
);
735 if(U_FAILURE(status
)) {
736 errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
740 failure(status
, "new SimpleDateFormat");
741 TimeZone
*temp
= TimeZone::createDefault();
742 if(simple
->getTimeZone() != *temp
)
743 errln("Fail: SimpleDateFormat not using default zone");
746 TimeZone::adoptDefault(saveZone
);
756 void DateFormatRegressionTest::Test4104136(void)
758 UErrorCode status
= U_ZERO_ERROR
;
759 SimpleDateFormat
*sdf
= new SimpleDateFormat(status
);
760 if(U_FAILURE(status
)) {
761 errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
765 failure(status
, "new SimpleDateFormat");
766 UnicodeString pattern
= "'time' hh:mm";
767 sdf
->applyPattern(pattern
);
768 logln("pattern: \"" + pattern
+ "\"");
770 UnicodeString strings
[] = {
771 (UnicodeString
)"time 10:30",
772 (UnicodeString
) "time 10:x",
773 (UnicodeString
) "time 10x"
776 ParsePosition ppos
[] = {
783 date(70, UCAL_JANUARY
, 1, 10, 30),
789 "time 10:30", new ParsePosition(10), new Date(70, Calendar.JANUARY, 1, 10, 30),
790 "time 10:x", new ParsePosition(0), null,
791 "time 10x", new ParsePosition(0), null,
794 for(int i
= 0; i
< 3; i
++) {
795 UnicodeString text
= strings
[i
];
796 ParsePosition finish
= ppos
[i
];
797 UDate exp
= dates
[i
];
799 ParsePosition
pos(0);
800 UDate d
= sdf
->parse(text
, pos
);
801 logln(" text: \"" + text
+ "\"");
802 logln(" index: %d", pos
.getIndex());
803 logln((UnicodeString
) " result: " + d
);
804 if(pos
.getIndex() != finish
.getIndex())
805 errln("Fail: Expected pos " + finish
.getIndex());
806 if (! ((d
== 0 && exp
== -1) || (d
== exp
)))
807 errln((UnicodeString
) "Fail: Expected result " + exp
);
816 * According to the bug report, this test should throw a
817 * StringIndexOutOfBoundsException during the second parse. However,
820 void DateFormatRegressionTest::Test4104522(void)
822 UErrorCode status
= U_ZERO_ERROR
;
824 SimpleDateFormat
*sdf
= new SimpleDateFormat(status
);
825 if(U_FAILURE(status
)) {
826 errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
830 failure(status
, "new SimpleDateFormat");
831 UnicodeString pattern
= "'time' hh:mm";
832 sdf
->applyPattern(pattern
);
833 logln("pattern: \"" + pattern
+ "\"");
837 UnicodeString text
= "time ";
838 UDate dt
= sdf
->parse(text
, pp
);
839 logln(" text: \"" + text
+ "\"" +
845 dt
= sdf
->parse(text
, pp
);
846 logln(" text: \"" + text
+ "\"" +
855 void DateFormatRegressionTest::Test4106807(void)
858 DateFormat
*df
= DateFormat::createDateTimeInstance();
860 UErrorCode status
= U_ZERO_ERROR
;
861 SimpleDateFormat
*sdfs
[] = {
862 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss"), status
),
863 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'Z'"), status
),
864 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss''"), status
),
865 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'a''a'"), status
),
866 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss %"), status
)
868 if(U_FAILURE(status
)) {
869 errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
878 failure(status
, "new SimpleDateFormat");
880 UnicodeString strings
[] = {
881 (UnicodeString
) "19980211140000",
882 (UnicodeString
) "19980211140000",
883 (UnicodeString
) "19980211140000",
884 (UnicodeString
) "19980211140000a",
885 (UnicodeString
) "19980211140000 "
889 new SimpleDateFormat("yyyyMMddHHmmss"), "19980211140000",
890 new SimpleDateFormat("yyyyMMddHHmmss'Z'"), "19980211140000",
891 new SimpleDateFormat("yyyyMMddHHmmss''"), "19980211140000",
892 new SimpleDateFormat("yyyyMMddHHmmss'a''a'"), "19980211140000a",
893 new SimpleDateFormat("yyyyMMddHHmmss %"), "19980211140000 ",
895 GregorianCalendar
*gc
= new GregorianCalendar(status
);
896 failure(status
, "new GregorianCalendar");
897 TimeZone
*timeZone
= TimeZone::createDefault();
899 TimeZone
*gmt
= timeZone
->clone();
901 gmt
->setRawOffset(0);
903 for(int32_t i
= 0; i
< 5; i
++) {
904 SimpleDateFormat
*format
= sdfs
[i
];
905 UnicodeString dateString
= strings
[i
];
907 format
->setTimeZone(*gmt
);
908 dt
= format
->parse(dateString
, status
);
909 // {sfb} some of these parses will fail purposely
910 if(U_FAILURE(status
))
912 status
= U_ZERO_ERROR
;
914 FieldPosition
pos(FieldPosition::DONT_CARE
);
915 fmtd
= df
->format(dt
, fmtd
, pos
);
917 //logln(df->format(dt));
918 gc
->setTime(dt
, status
);
919 failure(status
, "gc->getTime");
920 logln(UnicodeString("") + gc
->get(UCAL_ZONE_OFFSET
, status
));
921 failure(status
, "gc->get");
923 s
= format
->format(dt
, s
, pos
);
926 catch (ParseException e) {
927 logln("No way Jose");
933 for(int32_t j
= 0; j
< 5; j
++)
940 Synopsis: Chinese time zone CTT is not recogonized correctly.
941 Description: Platform Chinese Windows 95 - ** Time zone set to CST **
947 // {sfb} what to do with this one ??
948 void DateFormatRegressionTest::Test4108407(void)
950 /*long l = System.currentTimeMillis();
951 logln("user.timezone = " + System.getProperty("user.timezone", "?"));
952 logln("Time Zone :" +
953 DateFormat.getDateInstance().getTimeZone().getID());
954 logln("Default format :" +
955 DateFormat.getDateInstance().format(new Date(l)));
956 logln("Full format :" +
957 DateFormat.getDateInstance(DateFormat.FULL).format(new
959 logln("*** Set host TZ to CST ***");
960 logln("*** THE RESULTS OF THIS TEST MUST BE VERIFIED MANUALLY ***");*/
965 * SimpleDateFormat won't parse "GMT"
967 void DateFormatRegressionTest::Test4134203(void)
969 UErrorCode status
= U_ZERO_ERROR
;
970 UnicodeString dateFormat
= "MM/dd/yy HH:mm:ss zzz";
971 SimpleDateFormat
*fmt
= new SimpleDateFormat(dateFormat
, status
);
972 failure(status
, "new SimpleDateFormat");
974 UDate d
= fmt
->parse("01/22/92 04:52:00 GMT", p0
);
975 logln(dateToString(d
));
976 if(p0
== ParsePosition(0))
977 errln("Fail: failed to parse 'GMT'");
978 // In the failure case an exception is thrown by parse();
979 // if no exception is thrown, the test passes.
986 * SimpleDateFormat incorrect handling of 2 single quotes in format()
988 void DateFormatRegressionTest::Test4151631(void)
990 UnicodeString pattern
= "'TO_DATE('''dd'-'MM'-'yyyy HH:mm:ss''' , ''DD-MM-YYYY HH:MI:SS'')'";
991 logln("pattern=" + pattern
);
992 UErrorCode status
= U_ZERO_ERROR
;
993 SimpleDateFormat
*format
= new SimpleDateFormat(pattern
, Locale::getUS(), status
);
994 failure(status
, "new SimpleDateFormat");
995 UnicodeString result
;
996 FieldPosition
pos(FieldPosition::DONT_CARE
);
997 result
= format
->format(date(1998-1900, UCAL_JUNE
, 30, 13, 30, 0), result
, pos
);
998 if (result
!= "TO_DATE('30-06-1998 13:30:00' , 'DD-MM-YYYY HH:MI:SS')") {
999 errln("Fail: result=" + result
);
1002 logln("Pass: result=" + result
);
1010 * 'z' at end of date format throws index exception in SimpleDateFormat
1011 * CANNOT REPRODUCE THIS BUG ON 1.2FCS
1013 void DateFormatRegressionTest::Test4151706(void)
1015 UnicodeString
dateString("Thursday, 31-Dec-98 23:00:00 GMT");
1016 UErrorCode status
= U_ZERO_ERROR
;
1017 SimpleDateFormat
fmt(UnicodeString("EEEE, dd-MMM-yy HH:mm:ss z"), Locale::getUS(), status
);
1018 failure(status
, "new SimpleDateFormat");
1020 UDate d
= fmt
.parse(dateString
, status
);
1021 failure(status
, "fmt->parse");
1022 // {sfb} what about next two lines?
1023 //if (d.getTime() != Date.UTC(1998-1900, Calendar.DECEMBER, 31, 23, 0, 0))
1024 // errln("Incorrect value: " + d);
1025 /*} catch (Exception e) {
1026 errln("Fail: " + e);
1029 FieldPosition
pos(0);
1030 logln(dateString
+ " -> " + fmt
.format(d
, temp
, pos
));
1035 * Cannot reproduce this bug under 1.2 FCS -- it may be a convoluted duplicate
1036 * of some other bug that has been fixed.
1039 DateFormatRegressionTest::Test4162071(void)
1041 UnicodeString
dateString("Thu, 30-Jul-1999 11:51:14 GMT");
1042 UnicodeString
format("EEE', 'dd-MMM-yyyy HH:mm:ss z"); // RFC 822/1123
1043 UErrorCode status
= U_ZERO_ERROR
;
1044 SimpleDateFormat
df(format
, Locale::getUS(), status
);
1045 if(U_FAILURE(status
))
1046 errln("Couldn't create SimpleDateFormat");
1049 UDate x
= df
.parse(dateString
, status
);
1050 if(U_SUCCESS(status
))
1051 logln("Parse format \"" + format
+ "\" ok");
1053 errln("Parse format \"" + format
+ "\" failed.");
1055 FieldPosition
pos(0);
1056 logln(dateString
+ " -> " + df
.format(x
, temp
, pos
));
1057 //} catch (Exception e) {
1058 // errln("Parse format \"" + format + "\" failed.");
1063 * DateFormat shouldn't parse year "-1" as a two-digit year (e.g., "-1" -> 1999).
1065 void DateFormatRegressionTest::Test4182066(void) {
1066 UErrorCode status
= U_ZERO_ERROR
;
1067 SimpleDateFormat
fmt("MM/dd/yy", Locale::getUS(), status
);
1068 SimpleDateFormat
dispFmt("MMM dd yyyy GG", Locale::getUS(), status
);
1069 if (U_FAILURE(status
)) {
1070 errln("Couldn't create SimpleDateFormat");
1074 /* We expect 2-digit year formats to put 2-digit years in the right
1075 * window. Out of range years, that is, anything less than "00" or
1076 * greater than "99", are treated as literal years. So "1/2/3456"
1077 * becomes 3456 AD. Likewise, "1/2/-3" becomes -3 AD == 2 BC.
1079 const char* STRINGS
[] = {
1089 int32_t STRINGS_COUNT
= (int32_t)(sizeof(STRINGS
) / sizeof(STRINGS
[0]));
1090 UDate FAIL_DATE
= (UDate
) 0;
1092 date(2000-1900, UCAL_FEBRUARY
, 29),
1093 date(2001-1900, UCAL_JANUARY
, 23),
1094 date( -1-1900, UCAL_APRIL
, 5),
1095 date( -9-1900, UCAL_JANUARY
, 23),
1096 date(1314-1900, UCAL_NOVEMBER
, 12),
1097 date( 1-1900, UCAL_OCTOBER
, 31),
1098 FAIL_DATE
, // "+1" isn't recognized by US NumberFormat
1099 date( 1-1900, UCAL_SEPTEMBER
,12),
1104 for (int32_t i
=0; i
<STRINGS_COUNT
; ++i
) {
1105 UnicodeString
str(STRINGS
[i
]);
1106 UDate expected
= DATES
[i
];
1107 status
= U_ZERO_ERROR
;
1108 UDate actual
= fmt
.parse(str
, status
);
1109 if (U_FAILURE(status
)) {
1112 UnicodeString actStr
;
1113 if (actual
== FAIL_DATE
) {
1114 actStr
.append("null");
1117 ((DateFormat
*)&dispFmt
)->format(actual
, actStr
);
1120 if (expected
== actual
) {
1121 out
.append(str
+ " => " + actStr
+ "\n");
1123 UnicodeString expStr
;
1124 if (expected
== FAIL_DATE
) {
1125 expStr
.append("null");
1128 ((DateFormat
*)&dispFmt
)->format(expected
, expStr
);
1130 out
.append("FAIL: " + str
+ " => " + actStr
1131 + ", expected " + expStr
+ "\n");
1143 * j32 {JDK Bug 4210209 4209272}
1144 * DateFormat cannot parse Feb 29 2000 when setLenient(false)
1147 DateFormatRegressionTest::Test4210209(void) {
1148 UErrorCode status
= U_ZERO_ERROR
;
1149 UnicodeString
pattern("MMM d, yyyy");
1150 SimpleDateFormat
sfmt(pattern
, Locale::getUS(), status
);
1151 SimpleDateFormat
sdisp("MMM dd yyyy GG", Locale::getUS(), status
);
1152 DateFormat
& fmt
= *(DateFormat
*)&sfmt
; // Yuck: See j25
1153 DateFormat
& disp
= *(DateFormat
*)&sdisp
; // Yuck: See j25
1154 if (U_FAILURE(status
)) {
1155 errln("Couldn't create SimpleDateFormat");
1158 Calendar
* calx
= (Calendar
*)fmt
.getCalendar(); // cast away const!
1159 calx
->setLenient(FALSE
);
1160 UDate d
= date(2000-1900, UCAL_FEBRUARY
, 29);
1161 UnicodeString s
, ss
;
1163 logln(disp
.format(d
, ss
.remove()) + " f> " + pattern
+
1164 " => \"" + s
+ "\"");
1165 ParsePosition
pos(0);
1166 d
= fmt
.parse(s
, pos
);
1167 logln(UnicodeString("\"") + s
+ "\" p> " + pattern
+
1168 " => " + disp
.format(d
, ss
.remove()));
1169 logln(UnicodeString("Parse pos = ") + pos
.getIndex() +
1170 ", error pos = " + pos
.getErrorIndex());
1171 if (pos
.getErrorIndex() != -1) {
1172 errln(UnicodeString("FAIL: Error index should be -1"));
1175 // The underlying bug is in GregorianCalendar. If the following lines
1176 // succeed, the bug is fixed. If the bug isn't fixed, they will throw
1178 GregorianCalendar
cal(status
);
1179 if (U_FAILURE(status
)) {
1180 errln("FAIL: Unable to create Calendar");
1184 cal
.setLenient(FALSE
);
1185 cal
.set(2000, UCAL_FEBRUARY
, 29); // This should work!
1186 logln(UnicodeString("Attempt to set Calendar to Feb 29 2000: ") +
1187 disp
.format(cal
.getTime(status
), ss
.remove()));
1188 if (U_FAILURE(status
)) {
1189 errln("FAIL: Unable to set Calendar to Feb 29 2000");
1193 void DateFormatRegressionTest::Test714(void)
1196 UDate
d(978103543000.);
1197 DateFormat
*fmt
= DateFormat::createDateTimeInstance(DateFormat::NONE
,
1201 UnicodeString tests
=
1202 (UnicodeString
) "7:25:43 AM" ;
1203 UErrorCode status
= U_ZERO_ERROR
;
1205 if(U_FAILURE(status
))
1207 errln((UnicodeString
) "Fail, errmsg " + u_errorName(status
));
1213 errln((UnicodeString
) "Fail: " + s
+ " != " + tests
);
1217 logln("OK: " + s
+ " == " + tests
);
1223 class Test1684Data
{
1233 UnicodeString normalized
;
1235 Test1684Data(int32_t xyear
, int32_t xmonth
, int32_t xdate
,
1236 int32_t xwomyear
, int32_t xwommon
, int32_t xwom
, int32_t xdow
,
1237 const char *xdata
, const char *xnormalized
) :
1246 normalized((xnormalized
==NULL
)?xdata
:xnormalized
,"")
1250 void DateFormatRegressionTest::Test1684(void)
1252 // July 2001 August 2001 January 2002
1253 // Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1254 // 1 2 3 4 5 6 7 1 2 3 4 1 2 3 4 5
1255 // 8 9 10 11 12 13 14 5 6 7 8 9 10 11 6 7 8 9 10 11 12
1256 // 15 16 17 18 19 20 21 12 13 14 15 16 17 18 13 14 15 16 17 18 19
1257 // 22 23 24 25 26 27 28 19 20 21 22 23 24 25 20 21 22 23 24 25 26
1258 // 29 30 31 26 27 28 29 30 31 27 28 29 30 31
1259 Test1684Data
*tests
[] = {
1260 new Test1684Data(2001, 8, 6, 2001,8,2,UCAL_MONDAY
, "2001 08 02 Mon", NULL
),
1261 new Test1684Data(2001, 8, 7, 2001,8,2,UCAL_TUESDAY
, "2001 08 02 Tue", NULL
),
1262 new Test1684Data(2001, 8, 5,/*12,*/ 2001,8,2,UCAL_SUNDAY
, "2001 08 02 Sun", NULL
),
1263 new Test1684Data(2001, 8,6, /*7, 30,*/ 2001,7,6,UCAL_MONDAY
, "2001 07 06 Mon", "2001 08 02 Mon"),
1264 new Test1684Data(2001, 8,7, /*7, 31,*/ 2001,7,6,UCAL_TUESDAY
, "2001 07 06 Tue", "2001 08 02 Tue"),
1265 new Test1684Data(2001, 8, 5, 2001,7,6,UCAL_SUNDAY
, "2001 07 06 Sun", "2001 08 02 Sun"),
1266 new Test1684Data(2001, 7, 30, 2001,8,1,UCAL_MONDAY
, "2001 08 01 Mon", "2001 07 05 Mon"),
1267 new Test1684Data(2001, 7, 31, 2001,8,1,UCAL_TUESDAY
, "2001 08 01 Tue", "2001 07 05 Tue"),
1268 new Test1684Data(2001, 7,29, /*8, 5,*/ 2001,8,1,UCAL_SUNDAY
, "2001 08 01 Sun", "2001 07 05 Sun"),
1269 new Test1684Data(2001, 12, 31, 2001,12,6,UCAL_MONDAY
, "2001 12 06 Mon", NULL
),
1270 new Test1684Data(2002, 1, 1, 2002,1,1,UCAL_TUESDAY
, "2002 01 01 Tue", NULL
),
1271 new Test1684Data(2002, 1, 2, 2002,1,1,UCAL_WEDNESDAY
, "2002 01 01 Wed", NULL
),
1272 new Test1684Data(2002, 1, 3, 2002,1,1,UCAL_THURSDAY
, "2002 01 01 Thu", NULL
),
1273 new Test1684Data(2002, 1, 4, 2002,1,1,UCAL_FRIDAY
, "2002 01 01 Fri", NULL
),
1274 new Test1684Data(2002, 1, 5, 2002,1,1,UCAL_SATURDAY
, "2002 01 01 Sat", NULL
),
1275 new Test1684Data(2001,12,30, /*2002, 1, 6,*/ 2002,1,1,UCAL_SUNDAY
, "2002 01 01 Sun", "2001 12 06 Sun")
1278 #define kTest1684Count ((int32_t)(sizeof(tests)/sizeof(tests[0])))
1280 int32_t pass
= 0, error
= 0, warning
= 0;
1283 UErrorCode status
= U_ZERO_ERROR
;
1284 UnicodeString
pattern("yyyy MM WW EEE","");
1285 Calendar
*cal
= new GregorianCalendar(status
);
1286 SimpleDateFormat
*sdf
= new SimpleDateFormat(pattern
,status
);
1287 cal
->setFirstDayOfWeek(UCAL_SUNDAY
);
1288 cal
->setMinimalDaysInFirstWeek(1);
1290 sdf
->adoptCalendar(cal
);
1292 cal
= sdf
->getCalendar()->clone(); // sdf may have deleted calendar
1294 if(!cal
|| !sdf
|| U_FAILURE(status
)) {
1295 errln(UnicodeString("Error setting up test: ") + u_errorName(status
));
1298 for (i
= 0; i
< kTest1684Count
; ++i
) {
1299 Test1684Data
&test
= *(tests
[i
]);
1300 logln(UnicodeString("#") + i
+ UnicodeString("\n-----\nTesting round trip of ") + test
.year
+
1301 " " + (test
.month
+ 1) +
1303 " (written as) " + test
.data
);
1306 cal
->set(test
.year
, test
.month
, test
.date
);
1307 UDate ms
= cal
->getTime(status
);
1310 cal
->set(UCAL_YEAR
, test
.womyear
);
1311 cal
->set(UCAL_MONTH
, test
.wommon
);
1312 cal
->set(UCAL_WEEK_OF_MONTH
, test
.wom
);
1313 cal
->set(UCAL_DAY_OF_WEEK
, test
.dow
);
1314 UDate ms2
= cal
->getTime(status
);
1317 errln((UnicodeString
)"\nError: GregorianUCAL_DOM gave " + ms
+
1318 "\n GregorianUCAL_WOM gave " + ms2
);
1324 ms2
= sdf
->parse(test
.data
, status
);
1325 if(U_FAILURE(status
)) {
1326 errln("parse exception: " + UnicodeString(u_errorName(status
)));
1330 errln((UnicodeString
)"\nError: GregorianCalendar gave " + ms
+
1331 "\n SimpleDateFormat.parse gave " + ms2
);
1337 UnicodeString result
;
1338 sdf
->format(ms
, result
);
1339 if (result
!= test
.normalized
) {
1340 errln("\nWarning: format of '" + test
.data
+ "' gave" +
1341 "\n '" + result
+ "'" +
1342 "\n expected '" + test
.normalized
+ "'");
1349 ms3
= sdf
->parse(result
, status
);
1350 if(U_FAILURE(status
)) {
1351 errln("parse exception 2: " + (UnicodeString
)u_errorName(status
));
1356 errln((UnicodeString
)"\nError: Re-parse of '" + result
+ "' gave time of " +
1365 = UnicodeString("Passed: ") + pass
+ ", Warnings: " + warning
+ ", Errors: " + error
;
1372 for(i
=0;i
<kTest1684Count
;i
++) {
1379 #endif /* #if !UCONFIG_NO_FORMATTING */