1 /********************************************************************
3 * Copyright (c) 1997-2009, 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");
58 default: name
= ""; break;
65 void DateFormatRegressionTest::Test4029195(void)
67 UErrorCode status
= U_ZERO_ERROR
;
69 UDate today
= Calendar::getNow();
70 logln((UnicodeString
) "today: " + today
);
72 SimpleDateFormat
*sdf
= (SimpleDateFormat
*) DateFormat::createDateInstance();
73 if (failure(status
, "SimpleDateFormat::createDateInstance")) {
78 dataerrln("Error calling DateFormat::createDateTimeInstance");
82 pat
= sdf
->toPattern(pat
);
83 logln("pattern: " + pat
);
85 FieldPosition
pos(FieldPosition::DONT_CARE
);
86 fmtd
= sdf
->format(today
, fmtd
, pos
);
87 logln("today: " + fmtd
);
89 sdf
->applyPattern("G yyyy DDD");
91 todayS
= sdf
->format(today
, todayS
, pos
);
92 logln("today: " + todayS
);
94 today
= sdf
->parse(todayS
, status
);
95 failure(status
, "sdf->parse");
96 logln((UnicodeString
)"today date: " + today
);
97 /*} catch(Exception e) {
98 logln("Error reparsing date: " + e.getMessage());
103 rt
= sdf
->format(sdf
->parse(todayS
, status
), rt
, pos
);
104 failure(status
, "sdf->parse");
105 logln("round trip: " + rt
);
107 errln("Fail: Want " + todayS
+ " Got " + rt
);
109 catch (ParseException e) {
120 void DateFormatRegressionTest::Test4052408(void)
123 DateFormat
*fmt
= DateFormat::createDateTimeInstance(DateFormat::SHORT
,
124 DateFormat::SHORT
, Locale::getUS());
126 dataerrln("Error calling DateFormat::createDateTimeInstance");
130 UDate dt
= date(97, UCAL_MAY
, 3, 8, 55);
132 str
= fmt
->format(dt
, str
);
135 if(str
!= "5/3/97 8:55 AM")
136 errln("Fail: Test broken; Want 5/3/97 8:55 AM Got " + str
);
138 UnicodeString expected
[] = {
139 (UnicodeString
) "", //"ERA_FIELD",
140 (UnicodeString
) "97", //"YEAR_FIELD",
141 (UnicodeString
) "5", //"MONTH_FIELD",
142 (UnicodeString
) "3", //"DATE_FIELD",
143 (UnicodeString
) "", //"HOUR_OF_DAY1_FIELD",
144 (UnicodeString
) "", //"HOUR_OF_DAY0_FIELD",
145 (UnicodeString
) "55", //"MINUTE_FIELD",
146 (UnicodeString
) "", //"SECOND_FIELD",
147 (UnicodeString
) "", //"MILLISECOND_FIELD",
148 (UnicodeString
) "", //"DAY_OF_WEEK_FIELD",
149 (UnicodeString
) "", //"DAY_OF_YEAR_FIELD",
150 (UnicodeString
) "", //"DAY_OF_WEEK_IN_MONTH_FIELD",
151 (UnicodeString
) "", //"WEEK_OF_YEAR_FIELD",
152 (UnicodeString
) "", //"WEEK_OF_MONTH_FIELD",
153 (UnicodeString
) "AM", //"AM_PM_FIELD",
154 (UnicodeString
) "8", //"HOUR1_FIELD",
155 (UnicodeString
) "", //"HOUR0_FIELD",
156 (UnicodeString
) "" //"TIMEZONE_FIELD"
159 //Hashtable expected;// = new Hashtable();
160 //expected.put(new LongKey(DateFormat.MONTH_FIELD), "5");
161 //expected.put(new LongKey(DateFormat.DATE_FIELD), "3");
162 //expected.put(new LongKey(DateFormat.YEAR_FIELD), "97");
163 //expected.put(new LongKey(DateFormat.HOUR1_FIELD), "8");
164 //expected.put(new LongKey(DateFormat.MINUTE_FIELD), "55");
165 //expected.put(new LongKey(DateFormat.AM_PM_FIELD), "AM");
167 //StringBuffer buf = new StringBuffer();
168 UnicodeString fieldNames
[] = {
169 (UnicodeString
) "ERA_FIELD",
170 (UnicodeString
) "YEAR_FIELD",
171 (UnicodeString
) "MONTH_FIELD",
172 (UnicodeString
) "DATE_FIELD",
173 (UnicodeString
) "HOUR_OF_DAY1_FIELD",
174 (UnicodeString
) "HOUR_OF_DAY0_FIELD",
175 (UnicodeString
) "MINUTE_FIELD",
176 (UnicodeString
) "SECOND_FIELD",
177 (UnicodeString
) "MILLISECOND_FIELD",
178 (UnicodeString
) "DAY_OF_WEEK_FIELD",
179 (UnicodeString
) "DAY_OF_YEAR_FIELD",
180 (UnicodeString
) "DAY_OF_WEEK_IN_MONTH_FIELD",
181 (UnicodeString
) "WEEK_OF_YEAR_FIELD",
182 (UnicodeString
) "WEEK_OF_MONTH_FIELD",
183 (UnicodeString
) "AM_PM_FIELD",
184 (UnicodeString
) "HOUR1_FIELD",
185 (UnicodeString
) "HOUR0_FIELD",
186 (UnicodeString
) "TIMEZONE_FIELD"
190 for(int i
= 0; i
<= 17; ++i
) {
191 FieldPosition
pos(i
);
193 fmt
->format(dt
, buf
, pos
);
194 //char[] dst = new char[pos.getEndIndex() - pos.getBeginIndex()];
196 buf
.extractBetween(pos
.getBeginIndex(), pos
.getEndIndex(), dst
);
197 UnicodeString
str(dst
);
198 logln((UnicodeString
)"" + i
+ (UnicodeString
)": " + fieldNames
[i
] +
199 (UnicodeString
)", \"" + str
+ (UnicodeString
)"\", " +
200 pos
.getBeginIndex() + (UnicodeString
)", " +
202 UnicodeString exp
= expected
[i
];
203 if((exp
.length() == 0 && str
.length() == 0) || str
== exp
)
206 errln(UnicodeString(" expected ") + exp
);
212 errln("Fail: FieldPosition not set right by DateFormat");
219 * Verify the function of the [s|g]et2DigitYearStart() API.
221 void DateFormatRegressionTest::Test4056591(void)
223 UErrorCode status
= U_ZERO_ERROR
;
226 SimpleDateFormat
*fmt
= new SimpleDateFormat(UnicodeString("yyMMdd"), Locale::getUS(), status
);
227 failure(status
, "new SimpleDateFormat");
228 UDate start
= date(1809-1900, UCAL_DECEMBER
, 25);
229 fmt
->set2DigitYearStart(start
, status
);
230 failure(status
, "fmt->setTwoDigitStartDate");
231 if( (fmt
->get2DigitYearStart(status
) != start
) || failure(status
, "get2DigitStartDate"))
232 errln("get2DigitYearStart broken");
234 date(1809-1900, UCAL_DECEMBER
, 25),
235 date(1909-1900, UCAL_DECEMBER
, 24),
236 date(1809-1900, UCAL_DECEMBER
, 26),
237 date(1861-1900, UCAL_DECEMBER
, 25),
240 UnicodeString strings
[] = {
241 (UnicodeString
) "091225",
242 (UnicodeString
) "091224",
243 (UnicodeString
) "091226",
244 (UnicodeString
) "611225"
248 "091225", new Date(1809-1900, Calendar.DECEMBER, 25),
249 "091224", new Date(1909-1900, Calendar.DECEMBER, 24),
250 "091226", new Date(1809-1900, Calendar.DECEMBER, 26),
251 "611225", new Date(1861-1900, Calendar.DECEMBER, 25),
254 for(int i
= 0; i
< 4; i
++) {
255 UnicodeString s
= strings
[i
];
256 UDate exp
= dates
[i
];
257 UDate got
= fmt
->parse(s
, status
);
258 failure(status
, "fmt->parse");
259 logln(s
+ " -> " + got
+ "; exp " + exp
);
261 errln("set2DigitYearStart broken");
264 catch (ParseException e) {
275 void DateFormatRegressionTest::Test4059917(void)
277 UErrorCode status
= U_ZERO_ERROR
;
279 SimpleDateFormat
*fmt
;
280 UnicodeString myDate
;
282 fmt
= new SimpleDateFormat( UnicodeString("yyyy/MM/dd"), status
);
283 if(failure(status
, "new SimpleDateFormat")) return;
284 myDate
= "1997/01/01";
285 aux917( fmt
, myDate
);
290 fmt
= new SimpleDateFormat( UnicodeString("yyyyMMdd"), status
);
291 if(failure(status
, "new SimpleDateFormat")) return;
293 aux917( fmt
, myDate
);
298 void DateFormatRegressionTest::aux917( SimpleDateFormat
*fmt
, UnicodeString
& str
) {
301 pat
= fmt
->toPattern(pat
);
302 logln( "==================" );
303 logln( "testIt: pattern=" + pat
+
309 ParsePosition
pos(0);
310 fmt
->parseObject( str
, o
, pos
);
311 //logln( UnicodeString("Parsed object: ") + o );
313 UErrorCode status
= U_ZERO_ERROR
;
314 UnicodeString formatted
;
315 FieldPosition
poss(FieldPosition::DONT_CARE
);
316 formatted
= fmt
->format( o
, formatted
, poss
, status
);
317 failure(status
, "fmt->format");
318 logln( "Formatted string: " + formatted
);
319 if( formatted
!= str
)
320 errln("Fail: Want " + str
+ " Got " + formatted
);
322 catch (ParseException e) {
331 void DateFormatRegressionTest::Test4060212(void)
333 UnicodeString dateString
= "1995-040.05:01:29";
335 logln( "dateString= " + dateString
);
336 logln("Using yyyy-DDD.hh:mm:ss");
337 UErrorCode status
= U_ZERO_ERROR
;
338 SimpleDateFormat
*formatter
= new SimpleDateFormat(UnicodeString("yyyy-DDD.hh:mm:ss"), status
);
339 if(failure(status
, "new SimpleDateFormat")) return;
340 ParsePosition
pos(0);
341 UDate myDate
= formatter
->parse( dateString
, pos
);
342 UnicodeString myString
;
343 DateFormat
*fmt
= DateFormat::createDateTimeInstance( DateFormat::FULL
,
346 dataerrln("Error calling DateFormat::createDateTimeInstance");
351 myString
= fmt
->format( myDate
, myString
);
354 Calendar
*cal
= new GregorianCalendar(status
);
355 failure(status
, "new GregorianCalendar");
356 cal
->setTime(myDate
, status
);
357 failure(status
, "cal->setTime");
358 if ((cal
->get(UCAL_DAY_OF_YEAR
, status
) != 40) || failure(status
, "cal->get"))
359 errln((UnicodeString
) "Fail: Got " + cal
->get(UCAL_DAY_OF_YEAR
, status
) +
363 // this is an odd usage of "ddd" and it doesn't
364 // work now that date values are range checked.
365 logln("Using yyyy-ddd.hh:mm:ss");
368 formatter
= new SimpleDateFormat(UnicodeString("yyyy-ddd.hh:mm:ss"), status
);
369 if(failure(status
, "new SimpleDateFormat")) return;
371 myDate
= formatter
->parse( dateString
, pos
);
372 myString
= fmt
->format( myDate
, myString
);
374 cal
->setTime(myDate
, status
);
375 failure(status
, "cal->setTime");
376 if ((cal
->get(UCAL_DAY_OF_YEAR
, status
) != 40) || failure(status
, "cal->get"))
377 errln((UnicodeString
) "Fail: Got " + cal
->get(UCAL_DAY_OF_YEAR
, status
) +
389 void DateFormatRegressionTest::Test4061287(void)
391 UErrorCode status
= U_ZERO_ERROR
;
393 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("dd/MM/yyyy"), status
);
394 if(U_FAILURE(status
)) {
395 errln("Couldn't create SimpleDateFormat, error: %s", u_errorName(status
));
399 failure(status
, "new SimpleDateFormat");
401 logln(UnicodeString("") + df
->parse("30/02/1971", status
));
402 failure(status
, "df->parse");
403 //logln(df.parse("35/01/1971").toString());
405 /*catch (ParseException e) {
409 df
->setLenient(FALSE
);
412 logln(UnicodeString("") + df
->parse("30/02/1971", status
));
413 if(U_FAILURE(status
))
415 //logln(df.parse("35/01/1971").toString());
416 //} catch (ParseException e) {ok=TRUE;}
418 errln("Fail: Lenient not working");
425 void DateFormatRegressionTest::Test4065240(void)
428 DateFormat
*shortdate
, *fulldate
;
429 UnicodeString strShortDate
, strFullDate
;
430 Locale saveLocale
= Locale::getDefault();
431 TimeZone
*saveZone
= TimeZone::createDefault();
433 UErrorCode status
= U_ZERO_ERROR
;
435 Locale
*curLocale
= new Locale("de","DE");
436 Locale::setDefault(*curLocale
, status
);
437 failure(status
, "Locale::setDefault");
438 // {sfb} adoptDefault instead of setDefault
439 //TimeZone::setDefault(TimeZone::createTimeZone("EST"));
440 TimeZone::adoptDefault(TimeZone::createTimeZone("EST"));
441 curDate
= date(98, 0, 1);
442 shortdate
= DateFormat::createDateInstance(DateFormat::SHORT
);
443 if (shortdate
== NULL
){
444 dataerrln("Error calling DateFormat::createDateInstance");
448 fulldate
= DateFormat::createDateTimeInstance(DateFormat::LONG
, DateFormat::LONG
);
449 if (fulldate
== NULL
){
450 dataerrln("Error calling DateFormat::createDateTimeInstance");
453 strShortDate
= "The current date (short form) is ";
455 temp
= shortdate
->format(curDate
, temp
);
456 strShortDate
+= temp
;
457 strFullDate
= "The current date (long form) is ";
459 fulldate
->format(curDate
, temp2
);
460 strFullDate
+= temp2
;
465 // {sfb} What to do with resource bundle stuff?????
467 // Check to see if the resource is present; if not, we can't test
468 ResourceBundle
*bundle
= new ResourceBundle(
469 NULL
, *curLocale
, status
);
470 failure(status
, "new ResourceBundle");
471 //(UnicodeString) "java.text.resources.DateFormatZoneData", curLocale);
473 // {sfb} API change to ResourceBundle -- add getLocale()
474 /*if (bundle->getLocale().getLanguage(temp) == UnicodeString("de")) {
475 // UPDATE THIS AS ZONE NAME RESOURCE FOR <EST> in de_DE is updated
476 if (!strFullDate.endsWith(UnicodeString("GMT-05:00")))
477 errln("Fail: Want GMT-05:00");
480 logln("*** TEST COULD NOT BE COMPLETED BECAUSE DateFormatZoneData ***");
481 logln("*** FOR LOCALE de OR de_DE IS MISSING ***");
485 Locale::setDefault(saveLocale
, status
);
486 failure(status
, "Locale::setDefault");
487 TimeZone::setDefault(*saveZone
);
497 DateFormat.equals is too narrowly defined. As a result, MessageFormat
498 does not work correctly. DateFormat.equals needs to be written so
499 that the Calendar sub-object is not compared using Calendar.equals,
500 but rather compared for equivalency. This may necessitate adding a
501 (package private) method to Calendar to test for equivalency.
503 Currently this bug breaks MessageFormat.toPattern
508 void DateFormatRegressionTest::Test4071441(void)
510 DateFormat
*fmtA
= DateFormat::createInstance();
511 DateFormat
*fmtB
= DateFormat::createInstance();
513 if (fmtA
== NULL
|| fmtB
== NULL
){
514 dataerrln("Error calling DateFormat::createInstance");
520 // {sfb} Is it OK to cast away const here?
521 Calendar
*calA
= (Calendar
*) fmtA
->getCalendar();
522 Calendar
*calB
= (Calendar
*) fmtB
->getCalendar();
524 errln("Couldn't get proper calendars, exiting");
529 UDate epoch
= date(0, 0, 0);
530 UDate xmas
= date(61, UCAL_DECEMBER
, 25);
532 UErrorCode status
= U_ZERO_ERROR
;
533 calA
->setTime(epoch
, status
);
534 failure(status
, "calA->setTime");
535 calB
->setTime(epoch
, status
);
536 failure(status
, "calB->setTime");
538 errln("Fail: Can't complete test; Calendar instances unequal");
540 errln("Fail: DateFormat unequal when Calendars equal");
541 calB
->setTime(xmas
, status
);
542 failure(status
, "calB->setTime");
544 errln("Fail: Can't complete test; Calendar instances equal");
546 errln("Fail: DateFormat unequal when Calendars equivalent");
548 logln("DateFormat.equals ok");
554 /* The java.text.DateFormat.parse(String) method expects for the
555 US locale a string formatted according to mm/dd/yy and parses it
558 When given a string mm/dd/yyyy [sic] it only parses up to the first
559 two y's, typically resulting in a date in the year 1919.
561 Please extend the parsing method(s) to handle strings with
562 four-digit year values (probably also applicable to various
567 void DateFormatRegressionTest::Test4073003(void)
570 UErrorCode ec
= U_ZERO_ERROR
;
571 SimpleDateFormat
fmt("MM/dd/yy", Locale::getUK(), ec
);
573 errln("FAIL: SimpleDateFormat constructor");
576 UnicodeString tests
[] = {
577 (UnicodeString
) "12/25/61",
578 (UnicodeString
) "12/25/1961",
579 (UnicodeString
) "4/3/2010",
580 (UnicodeString
) "4/3/10"
582 UErrorCode status
= U_ZERO_ERROR
;
583 for(int i
= 0; i
< 4; i
+=2) {
584 UDate d
= fmt
.parse(tests
[i
], status
);
585 failure(status
, "fmt.parse");
586 UDate dd
= fmt
.parse(tests
[i
+1], status
);
587 failure(status
, "fmt.parse");
589 s
= fmt
.format(d
, s
);
591 ss
= fmt
.format(dd
, ss
);
593 errln((UnicodeString
) "Fail: " + d
+ " != " + dd
);
595 errln((UnicodeString
)"Fail: " + s
+ " != " + ss
);
596 logln("Ok: " + s
+ " " + d
);
603 void DateFormatRegressionTest::Test4089106(void)
605 TimeZone
*def
= TimeZone::createDefault();
607 TimeZone
*z
= new SimpleTimeZone((int)(1.25 * 3600000), "FAKEZONE");
608 TimeZone::setDefault(*z
);
609 UErrorCode status
= U_ZERO_ERROR
;
610 SimpleDateFormat
*f
= new SimpleDateFormat(status
);
611 if(U_FAILURE(status
)) {
612 errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
618 failure(status
, "new SimpleDateFormat");
619 if (f
->getTimeZone()!= *z
)
620 errln("Fail: SimpleTimeZone should use TimeZone.getDefault()");
624 TimeZone::setDefault(*def
);
636 // {sfb} not applicable in C++??
638 void DateFormatRegressionTest::Test4100302(void)
640 /* Locale locales [] = {
642 Locale::CANADA_FRENCH,
657 Locale::SIMPLIFIED_CHINESE,
659 Locale::TRADITIONAL_CHINESE,
665 for(int i = 0; i < 21; i++) {
667 Format *format = DateFormat::createDateTimeInstance(DateFormat::FULL,
668 DateFormat::FULL, locales[i]);
671 ByteArrayOutputStream baos = new ByteArrayOutputStream();
672 ObjectOutputStream oos = new ObjectOutputStream(baos);
674 oos.writeObject(format);
678 bytes = baos.toByteArray();
680 ObjectInputStream ois =
681 new ObjectInputStream(new ByteArrayInputStream(bytes));
683 if (!format.equals(ois.readObject())) {
685 logln("DateFormat instance for locale " +
686 locales[i] + " is incorrectly serialized/deserialized.");
688 logln("DateFormat instance for locale " +
689 locales[i] + " is OKAY.");
692 if (!pass) errln("Fail: DateFormat serialization/equality bug");
694 catch (IOException e) {
698 catch (ClassNotFoundException e) {
707 void DateFormatRegressionTest::Test4101483(void)
709 UErrorCode status
= U_ZERO_ERROR
;
710 SimpleDateFormat
*sdf
= new SimpleDateFormat(UnicodeString("z"), Locale::getUS(), status
);
711 if(failure(status
, "new SimpleDateFormat")) return;
712 FieldPosition
fp(UDAT_TIMEZONE_FIELD
);
713 //Date d = date(9234567890L);
714 UDate d
= 9234567890.0;
715 //StringBuffer buf = new StringBuffer("");
717 sdf
->format(d
, buf
, fp
);
718 //logln(sdf.format(d, buf, fp).toString());
719 logln(dateToString(d
) + " => " + buf
);
720 logln("beginIndex = " + fp
.getBeginIndex());
721 logln("endIndex = " + fp
.getEndIndex());
722 if (fp
.getBeginIndex() == fp
.getEndIndex())
723 errln("Fail: Empty field");
731 * This bug really only works in Locale.US, since that's what the locale
732 * used for Date.toString() is. Bug 4138203 reports that it fails on Korean
733 * NT; it would actually have failed on any non-US locale. Now it should
734 * work on all locales.
736 void DateFormatRegressionTest::Test4103340(void)
738 UErrorCode status
= U_ZERO_ERROR
;
740 // choose a date that is the FIRST of some month
741 // and some arbitrary time
742 UDate d
= date(97, 3, 1, 1, 1, 1);
743 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("MMMM"), Locale::getUS(), status
);
744 if(failure(status
, "new SimpleDateFormat")) return;
747 s
= dateToString(d
, s
);
749 FieldPosition
pos(FieldPosition::DONT_CARE
);
750 s2
= df
->format(d
, s2
, pos
);
753 UnicodeString substr
;
754 s2
.extract(0,2, substr
);
755 if (s
.indexOf(substr
) == -1)
756 errln("Months should match");
764 void DateFormatRegressionTest::Test4103341(void)
766 TimeZone
*saveZone
=TimeZone::createDefault();
769 // {sfb} changed from setDefault to adoptDefault
770 TimeZone::adoptDefault(TimeZone::createTimeZone("CST"));
771 UErrorCode status
= U_ZERO_ERROR
;
772 SimpleDateFormat
*simple
= new SimpleDateFormat(UnicodeString("MM/dd/yyyy HH:mm"), status
);
773 if(U_FAILURE(status
)) {
774 errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
778 failure(status
, "new SimpleDateFormat");
779 TimeZone
*temp
= TimeZone::createDefault();
780 if(simple
->getTimeZone() != *temp
)
781 errln("Fail: SimpleDateFormat not using default zone");
784 TimeZone::adoptDefault(saveZone
);
794 void DateFormatRegressionTest::Test4104136(void)
796 UErrorCode status
= U_ZERO_ERROR
;
797 SimpleDateFormat
*sdf
= new SimpleDateFormat(status
);
798 if(U_FAILURE(status
)) {
799 errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
803 if(failure(status
, "new SimpleDateFormat")) return;
804 UnicodeString pattern
= "'time' hh:mm";
805 sdf
->applyPattern(pattern
);
806 logln("pattern: \"" + pattern
+ "\"");
808 UnicodeString strings
[] = {
809 (UnicodeString
)"time 10:30",
810 (UnicodeString
) "time 10:x",
811 (UnicodeString
) "time 10x"
814 ParsePosition ppos
[] = {
821 date(70, UCAL_JANUARY
, 1, 10, 30),
827 "time 10:30", new ParsePosition(10), new Date(70, Calendar.JANUARY, 1, 10, 30),
828 "time 10:x", new ParsePosition(0), null,
829 "time 10x", new ParsePosition(0), null,
832 for(int i
= 0; i
< 3; i
++) {
833 UnicodeString text
= strings
[i
];
834 ParsePosition finish
= ppos
[i
];
835 UDate exp
= dates
[i
];
837 ParsePosition
pos(0);
838 UDate d
= sdf
->parse(text
, pos
);
839 logln(" text: \"" + text
+ "\"");
840 logln(" index: %d", pos
.getIndex());
841 logln((UnicodeString
) " result: " + d
);
842 if(pos
.getIndex() != finish
.getIndex())
843 errln("Fail: Expected pos " + finish
.getIndex());
844 if (! ((d
== 0 && exp
== -1) || (d
== exp
)))
845 errln((UnicodeString
) "Fail: Expected result " + exp
);
854 * According to the bug report, this test should throw a
855 * StringIndexOutOfBoundsException during the second parse. However,
858 void DateFormatRegressionTest::Test4104522(void)
860 UErrorCode status
= U_ZERO_ERROR
;
862 SimpleDateFormat
*sdf
= new SimpleDateFormat(status
);
863 if(U_FAILURE(status
)) {
864 errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
868 failure(status
, "new SimpleDateFormat");
869 UnicodeString pattern
= "'time' hh:mm";
870 sdf
->applyPattern(pattern
);
871 logln("pattern: \"" + pattern
+ "\"");
875 UnicodeString text
= "time ";
876 UDate dt
= sdf
->parse(text
, pp
);
877 logln(" text: \"" + text
+ "\"" +
883 dt
= sdf
->parse(text
, pp
);
884 logln(" text: \"" + text
+ "\"" +
893 void DateFormatRegressionTest::Test4106807(void)
896 DateFormat
*df
= DateFormat::createDateTimeInstance();
898 UErrorCode status
= U_ZERO_ERROR
;
899 SimpleDateFormat
*sdfs
[] = {
900 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss"), status
),
901 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'Z'"), status
),
902 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss''"), status
),
903 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'a''a'"), status
),
904 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss %"), status
)
906 if(U_FAILURE(status
)) {
907 errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
916 failure(status
, "new SimpleDateFormat");
918 UnicodeString strings
[] = {
919 (UnicodeString
) "19980211140000",
920 (UnicodeString
) "19980211140000",
921 (UnicodeString
) "19980211140000",
922 (UnicodeString
) "19980211140000a",
923 (UnicodeString
) "19980211140000 "
927 new SimpleDateFormat("yyyyMMddHHmmss"), "19980211140000",
928 new SimpleDateFormat("yyyyMMddHHmmss'Z'"), "19980211140000",
929 new SimpleDateFormat("yyyyMMddHHmmss''"), "19980211140000",
930 new SimpleDateFormat("yyyyMMddHHmmss'a''a'"), "19980211140000a",
931 new SimpleDateFormat("yyyyMMddHHmmss %"), "19980211140000 ",
933 GregorianCalendar
*gc
= new GregorianCalendar(status
);
934 failure(status
, "new GregorianCalendar");
935 TimeZone
*timeZone
= TimeZone::createDefault();
937 TimeZone
*gmt
= timeZone
->clone();
939 gmt
->setRawOffset(0);
941 for(int32_t i
= 0; i
< 5; i
++) {
942 SimpleDateFormat
*format
= sdfs
[i
];
943 UnicodeString dateString
= strings
[i
];
945 format
->setTimeZone(*gmt
);
946 dt
= format
->parse(dateString
, status
);
947 // {sfb} some of these parses will fail purposely
948 if(U_FAILURE(status
))
950 status
= U_ZERO_ERROR
;
952 FieldPosition
pos(FieldPosition::DONT_CARE
);
953 fmtd
= df
->format(dt
, fmtd
, pos
);
955 //logln(df->format(dt));
956 gc
->setTime(dt
, status
);
957 failure(status
, "gc->getTime");
958 logln(UnicodeString("") + gc
->get(UCAL_ZONE_OFFSET
, status
));
959 failure(status
, "gc->get");
961 s
= format
->format(dt
, s
, pos
);
964 catch (ParseException e) {
965 logln("No way Jose");
971 for(int32_t j
= 0; j
< 5; j
++)
978 Synopsis: Chinese time zone CTT is not recogonized correctly.
979 Description: Platform Chinese Windows 95 - ** Time zone set to CST **
985 // {sfb} what to do with this one ??
986 void DateFormatRegressionTest::Test4108407(void)
988 /*long l = System.currentTimeMillis();
989 logln("user.timezone = " + System.getProperty("user.timezone", "?"));
990 logln("Time Zone :" +
991 DateFormat.getDateInstance().getTimeZone().getID());
992 logln("Default format :" +
993 DateFormat.getDateInstance().format(new Date(l)));
994 logln("Full format :" +
995 DateFormat.getDateInstance(DateFormat.FULL).format(new
997 logln("*** Set host TZ to CST ***");
998 logln("*** THE RESULTS OF THIS TEST MUST BE VERIFIED MANUALLY ***");*/
1003 * SimpleDateFormat won't parse "GMT"
1005 void DateFormatRegressionTest::Test4134203(void)
1007 UErrorCode status
= U_ZERO_ERROR
;
1008 UnicodeString dateFormat
= "MM/dd/yy HH:mm:ss zzz";
1009 SimpleDateFormat
*fmt
= new SimpleDateFormat(dateFormat
, status
);
1010 if(failure(status
, "new SimpleDateFormat")) return;
1011 ParsePosition
p0(0);
1012 UDate d
= fmt
->parse("01/22/92 04:52:00 GMT", p0
);
1013 logln(dateToString(d
));
1014 if(p0
== ParsePosition(0))
1015 errln("Fail: failed to parse 'GMT'");
1016 // In the failure case an exception is thrown by parse();
1017 // if no exception is thrown, the test passes.
1024 * SimpleDateFormat incorrect handling of 2 single quotes in format()
1026 void DateFormatRegressionTest::Test4151631(void)
1028 UnicodeString pattern
= "'TO_DATE('''dd'-'MM'-'yyyy HH:mm:ss''' , ''DD-MM-YYYY HH:MI:SS'')'";
1029 logln("pattern=" + pattern
);
1030 UErrorCode status
= U_ZERO_ERROR
;
1031 SimpleDateFormat
*format
= new SimpleDateFormat(pattern
, Locale::getUS(), status
);
1032 if(failure(status
, "new SimpleDateFormat")) return;
1033 UnicodeString result
;
1034 FieldPosition
pos(FieldPosition::DONT_CARE
);
1035 result
= format
->format(date(1998-1900, UCAL_JUNE
, 30, 13, 30, 0), result
, pos
);
1036 if (result
!= "TO_DATE('30-06-1998 13:30:00' , 'DD-MM-YYYY HH:MI:SS')") {
1037 errln("Fail: result=" + result
);
1040 logln("Pass: result=" + result
);
1048 * 'z' at end of date format throws index exception in SimpleDateFormat
1049 * CANNOT REPRODUCE THIS BUG ON 1.2FCS
1051 void DateFormatRegressionTest::Test4151706(void)
1053 UnicodeString
dateString("Thursday, 31-Dec-98 23:00:00 GMT");
1054 UErrorCode status
= U_ZERO_ERROR
;
1055 SimpleDateFormat
fmt(UnicodeString("EEEE, dd-MMM-yy HH:mm:ss z"), Locale::getUS(), status
);
1056 if(failure(status
, "new SimpleDateFormat")) return;
1058 UDate d
= fmt
.parse(dateString
, status
);
1059 failure(status
, "fmt->parse");
1060 // {sfb} what about next two lines?
1061 //if (d.getTime() != Date.UTC(1998-1900, Calendar.DECEMBER, 31, 23, 0, 0))
1062 // errln("Incorrect value: " + d);
1063 /*} catch (Exception e) {
1064 errln("Fail: " + e);
1067 FieldPosition
pos(0);
1068 logln(dateString
+ " -> " + fmt
.format(d
, temp
, pos
));
1073 * Cannot reproduce this bug under 1.2 FCS -- it may be a convoluted duplicate
1074 * of some other bug that has been fixed.
1077 DateFormatRegressionTest::Test4162071(void)
1079 UnicodeString
dateString("Thu, 30-Jul-1999 11:51:14 GMT");
1080 UnicodeString
format("EEE', 'dd-MMM-yyyy HH:mm:ss z"); // RFC 822/1123
1081 UErrorCode status
= U_ZERO_ERROR
;
1082 SimpleDateFormat
df(format
, Locale::getUS(), status
);
1083 if(U_FAILURE(status
)) {
1084 errln("Couldn't create SimpleDateFormat");
1089 UDate x
= df
.parse(dateString
, status
);
1090 if(U_SUCCESS(status
))
1091 logln("Parse format \"" + format
+ "\" ok");
1093 errln("Parse format \"" + format
+ "\" failed.");
1095 FieldPosition
pos(0);
1096 logln(dateString
+ " -> " + df
.format(x
, temp
, pos
));
1097 //} catch (Exception e) {
1098 // errln("Parse format \"" + format + "\" failed.");
1103 * DateFormat shouldn't parse year "-1" as a two-digit year (e.g., "-1" -> 1999).
1105 void DateFormatRegressionTest::Test4182066(void) {
1106 UErrorCode status
= U_ZERO_ERROR
;
1107 SimpleDateFormat
fmt("MM/dd/yy", Locale::getUS(), status
);
1108 SimpleDateFormat
dispFmt("MMM dd yyyy GG", Locale::getUS(), status
);
1109 if (U_FAILURE(status
)) {
1110 errln("Couldn't create SimpleDateFormat");
1114 /* We expect 2-digit year formats to put 2-digit years in the right
1115 * window. Out of range years, that is, anything less than "00" or
1116 * greater than "99", are treated as literal years. So "1/2/3456"
1117 * becomes 3456 AD. Likewise, "1/2/-3" becomes -3 AD == 2 BC.
1119 const char* STRINGS
[] = {
1129 int32_t STRINGS_COUNT
= (int32_t)(sizeof(STRINGS
) / sizeof(STRINGS
[0]));
1130 UDate FAIL_DATE
= (UDate
) 0;
1132 date(2000-1900, UCAL_FEBRUARY
, 29),
1133 date(2001-1900, UCAL_JANUARY
, 23),
1134 date( -1-1900, UCAL_APRIL
, 5),
1135 date( -9-1900, UCAL_JANUARY
, 23),
1136 date(1314-1900, UCAL_NOVEMBER
, 12),
1137 date( 1-1900, UCAL_OCTOBER
, 31),
1138 FAIL_DATE
, // "+1" isn't recognized by US NumberFormat
1139 date( 1-1900, UCAL_SEPTEMBER
,12),
1144 for (int32_t i
=0; i
<STRINGS_COUNT
; ++i
) {
1145 UnicodeString
str(STRINGS
[i
]);
1146 UDate expected
= DATES
[i
];
1147 status
= U_ZERO_ERROR
;
1148 UDate actual
= fmt
.parse(str
, status
);
1149 if (U_FAILURE(status
)) {
1152 UnicodeString actStr
;
1153 if (actual
== FAIL_DATE
) {
1154 actStr
.append("null");
1157 ((DateFormat
*)&dispFmt
)->format(actual
, actStr
);
1160 if (expected
== actual
) {
1161 out
.append(str
+ " => " + actStr
+ "\n");
1163 UnicodeString expStr
;
1164 if (expected
== FAIL_DATE
) {
1165 expStr
.append("null");
1168 ((DateFormat
*)&dispFmt
)->format(expected
, expStr
);
1170 out
.append("FAIL: " + str
+ " => " + actStr
1171 + ", expected " + expStr
+ "\n");
1183 * j32 {JDK Bug 4210209 4209272}
1184 * DateFormat cannot parse Feb 29 2000 when setLenient(false)
1187 DateFormatRegressionTest::Test4210209(void) {
1188 UErrorCode status
= U_ZERO_ERROR
;
1189 UnicodeString
pattern("MMM d, yyyy");
1190 SimpleDateFormat
sfmt(pattern
, Locale::getUS(), status
);
1191 SimpleDateFormat
sdisp("MMM dd yyyy GG", Locale::getUS(), status
);
1192 DateFormat
& fmt
= *(DateFormat
*)&sfmt
; // Yuck: See j25
1193 DateFormat
& disp
= *(DateFormat
*)&sdisp
; // Yuck: See j25
1194 if (U_FAILURE(status
)) {
1195 errln("Couldn't create SimpleDateFormat");
1198 Calendar
* calx
= (Calendar
*)fmt
.getCalendar(); // cast away const!
1199 calx
->setLenient(FALSE
);
1200 UDate d
= date(2000-1900, UCAL_FEBRUARY
, 29);
1201 UnicodeString s
, ss
;
1203 logln(disp
.format(d
, ss
.remove()) + " f> " + pattern
+
1204 " => \"" + s
+ "\"");
1205 ParsePosition
pos(0);
1206 d
= fmt
.parse(s
, pos
);
1207 logln(UnicodeString("\"") + s
+ "\" p> " + pattern
+
1208 " => " + disp
.format(d
, ss
.remove()));
1209 logln(UnicodeString("Parse pos = ") + pos
.getIndex() +
1210 ", error pos = " + pos
.getErrorIndex());
1211 if (pos
.getErrorIndex() != -1) {
1212 errln(UnicodeString("FAIL: Error index should be -1"));
1215 // The underlying bug is in GregorianCalendar. If the following lines
1216 // succeed, the bug is fixed. If the bug isn't fixed, they will throw
1218 GregorianCalendar
cal(status
);
1219 if (U_FAILURE(status
)) {
1220 errln("FAIL: Unable to create Calendar");
1224 cal
.setLenient(FALSE
);
1225 cal
.set(2000, UCAL_FEBRUARY
, 29); // This should work!
1226 logln(UnicodeString("Attempt to set Calendar to Feb 29 2000: ") +
1227 disp
.format(cal
.getTime(status
), ss
.remove()));
1228 if (U_FAILURE(status
)) {
1229 errln("FAIL: Unable to set Calendar to Feb 29 2000");
1233 void DateFormatRegressionTest::Test714(void)
1236 UDate
d(978103543000.);
1237 DateFormat
*fmt
= DateFormat::createDateTimeInstance(DateFormat::NONE
,
1241 dataerrln("Error calling DateFormat::createDateTimeInstance");
1246 UnicodeString tests
=
1247 (UnicodeString
) "7:25:43 AM" ;
1248 UErrorCode status
= U_ZERO_ERROR
;
1250 if(U_FAILURE(status
))
1252 errln((UnicodeString
) "Fail, errmsg " + u_errorName(status
));
1258 errln((UnicodeString
) "Fail: " + s
+ " != " + tests
);
1262 logln("OK: " + s
+ " == " + tests
);
1268 class Test1684Data
{
1278 UnicodeString normalized
;
1280 Test1684Data(int32_t xyear
, int32_t xmonth
, int32_t xdate
,
1281 int32_t xwomyear
, int32_t xwommon
, int32_t xwom
, int32_t xdow
,
1282 const char *xdata
, const char *xnormalized
) :
1291 normalized((xnormalized
==NULL
)?xdata
:xnormalized
,"")
1295 void DateFormatRegressionTest::Test1684(void)
1297 // July 2001 August 2001 January 2002
1298 // Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1299 // 1 2 3 4 5 6 7 1 2 3 4 1 2 3 4 5
1300 // 8 9 10 11 12 13 14 5 6 7 8 9 10 11 6 7 8 9 10 11 12
1301 // 15 16 17 18 19 20 21 12 13 14 15 16 17 18 13 14 15 16 17 18 19
1302 // 22 23 24 25 26 27 28 19 20 21 22 23 24 25 20 21 22 23 24 25 26
1303 // 29 30 31 26 27 28 29 30 31 27 28 29 30 31
1304 Test1684Data
*tests
[] = {
1305 new Test1684Data(2001, 8, 6, 2001,8,2,UCAL_MONDAY
, "2001 08 02 Mon", NULL
),
1306 new Test1684Data(2001, 8, 7, 2001,8,2,UCAL_TUESDAY
, "2001 08 02 Tue", NULL
),
1307 new Test1684Data(2001, 8, 5,/*12,*/ 2001,8,2,UCAL_SUNDAY
, "2001 08 02 Sun", NULL
),
1308 new Test1684Data(2001, 8,6, /*7, 30,*/ 2001,7,6,UCAL_MONDAY
, "2001 07 06 Mon", "2001 08 02 Mon"),
1309 new Test1684Data(2001, 8,7, /*7, 31,*/ 2001,7,6,UCAL_TUESDAY
, "2001 07 06 Tue", "2001 08 02 Tue"),
1310 new Test1684Data(2001, 8, 5, 2001,7,6,UCAL_SUNDAY
, "2001 07 06 Sun", "2001 08 02 Sun"),
1311 new Test1684Data(2001, 7, 30, 2001,8,1,UCAL_MONDAY
, "2001 08 01 Mon", "2001 07 05 Mon"),
1312 new Test1684Data(2001, 7, 31, 2001,8,1,UCAL_TUESDAY
, "2001 08 01 Tue", "2001 07 05 Tue"),
1313 new Test1684Data(2001, 7,29, /*8, 5,*/ 2001,8,1,UCAL_SUNDAY
, "2001 08 01 Sun", "2001 07 05 Sun"),
1314 new Test1684Data(2001, 12, 31, 2001,12,6,UCAL_MONDAY
, "2001 12 06 Mon", NULL
),
1315 new Test1684Data(2002, 1, 1, 2002,1,1,UCAL_TUESDAY
, "2002 01 01 Tue", NULL
),
1316 new Test1684Data(2002, 1, 2, 2002,1,1,UCAL_WEDNESDAY
, "2002 01 01 Wed", NULL
),
1317 new Test1684Data(2002, 1, 3, 2002,1,1,UCAL_THURSDAY
, "2002 01 01 Thu", NULL
),
1318 new Test1684Data(2002, 1, 4, 2002,1,1,UCAL_FRIDAY
, "2002 01 01 Fri", NULL
),
1319 new Test1684Data(2002, 1, 5, 2002,1,1,UCAL_SATURDAY
, "2002 01 01 Sat", NULL
),
1320 new Test1684Data(2001,12,30, /*2002, 1, 6,*/ 2002,1,1,UCAL_SUNDAY
, "2002 01 01 Sun", "2001 12 06 Sun")
1323 #define kTest1684Count ((int32_t)(sizeof(tests)/sizeof(tests[0])))
1325 int32_t pass
= 0, error
= 0, warning
= 0;
1328 UErrorCode status
= U_ZERO_ERROR
;
1329 UnicodeString
pattern("yyyy MM WW EEE","");
1330 Calendar
*cal
= new GregorianCalendar(status
);
1331 SimpleDateFormat
*sdf
= new SimpleDateFormat(pattern
,status
);
1332 if (U_FAILURE(status
)) {
1333 dataerrln("Error constructing SimpleDateFormat");
1334 for(i
=0;i
<kTest1684Count
;i
++) {
1341 cal
->setFirstDayOfWeek(UCAL_SUNDAY
);
1342 cal
->setMinimalDaysInFirstWeek(1);
1344 sdf
->adoptCalendar(cal
);
1346 cal
= sdf
->getCalendar()->clone(); // sdf may have deleted calendar
1348 if(!cal
|| !sdf
|| U_FAILURE(status
)) {
1349 errln(UnicodeString("Error setting up test: ") + u_errorName(status
));
1352 for (i
= 0; i
< kTest1684Count
; ++i
) {
1353 Test1684Data
&test
= *(tests
[i
]);
1354 logln(UnicodeString("#") + i
+ UnicodeString("\n-----\nTesting round trip of ") + test
.year
+
1355 " " + (test
.month
+ 1) +
1357 " (written as) " + test
.data
);
1360 cal
->set(test
.year
, test
.month
, test
.date
);
1361 UDate ms
= cal
->getTime(status
);
1364 cal
->set(UCAL_YEAR
, test
.womyear
);
1365 cal
->set(UCAL_MONTH
, test
.wommon
);
1366 cal
->set(UCAL_WEEK_OF_MONTH
, test
.wom
);
1367 cal
->set(UCAL_DAY_OF_WEEK
, test
.dow
);
1368 UDate ms2
= cal
->getTime(status
);
1371 errln((UnicodeString
)"\nError: GregorianUCAL_DOM gave " + ms
+
1372 "\n GregorianUCAL_WOM gave " + ms2
);
1378 ms2
= sdf
->parse(test
.data
, status
);
1379 if(U_FAILURE(status
)) {
1380 errln("parse exception: " + UnicodeString(u_errorName(status
)));
1384 errln((UnicodeString
)"\nError: GregorianCalendar gave " + ms
+
1385 "\n SimpleDateFormat.parse gave " + ms2
);
1391 UnicodeString result
;
1392 sdf
->format(ms
, result
);
1393 if (result
!= test
.normalized
) {
1394 errln("\nWarning: format of '" + test
.data
+ "' gave" +
1395 "\n '" + result
+ "'" +
1396 "\n expected '" + test
.normalized
+ "'");
1403 ms3
= sdf
->parse(result
, status
);
1404 if(U_FAILURE(status
)) {
1405 errln("parse exception 2: " + (UnicodeString
)u_errorName(status
));
1410 errln((UnicodeString
)"\nError: Re-parse of '" + result
+ "' gave time of " +
1419 = UnicodeString("Passed: ") + pass
+ ", Warnings: " + warning
+ ", Errors: " + error
;
1426 for(i
=0;i
<kTest1684Count
;i
++) {
1433 void DateFormatRegressionTest::Test5554(void)
1435 UErrorCode status
= U_ZERO_ERROR
;
1436 UnicodeString
pattern("Z","");
1437 UnicodeString
newfoundland("Canada/Newfoundland", "");
1438 TimeZone
*zone
= TimeZone::createTimeZone(newfoundland
);
1439 Calendar
*cal
= new GregorianCalendar(zone
, status
);
1440 SimpleDateFormat
*sdf
= new SimpleDateFormat(pattern
,status
);
1441 if (U_FAILURE(status
)) {
1442 dataerrln("Error constructing SimpleDateFormat");
1447 cal
->set(2007, 1, 14);
1448 UDate date
= cal
->getTime(status
);
1449 if (U_FAILURE(status
)) {
1450 errln("Error getting time to format");
1453 sdf
->adoptCalendar(cal
);
1454 UnicodeString result
;
1455 UnicodeString
correct("-0330", "");
1456 sdf
->format(date
, result
);
1457 if (result
!= correct
) {
1458 errln("\nError: Newfoundland Z of Jan 14, 2007 gave '" + result
+ "', expected '" + correct
+ "'");
1463 #endif /* #if !UCONFIG_NO_FORMATTING */