1 /********************************************************************
3 * Copyright (c) 1997-2016, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
12 #include "unicode/timezone.h"
13 #include "unicode/gregocal.h"
14 #include "unicode/smpdtfmt.h"
15 #include "unicode/datefmt.h"
16 #include "unicode/simpletz.h"
17 #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");
64 default: name
= ""; break;
71 void DateFormatRegressionTest::Test4029195(void)
73 UErrorCode status
= U_ZERO_ERROR
;
75 UDate today
= Calendar::getNow();
76 logln((UnicodeString
) "today: " + today
);
78 SimpleDateFormat
*sdf
= (SimpleDateFormat
*) DateFormat::createDateInstance();
79 if (failure(status
, "SimpleDateFormat::createDateInstance")) {
84 dataerrln("Error calling DateFormat::createDateTimeInstance");
88 pat
= sdf
->toPattern(pat
);
89 logln("pattern: " + pat
);
92 FieldPosition
pos(FieldPosition::DONT_CARE
);
93 fmtd
= sdf
->format(today
, fmtd
, pos
);
94 logln("today: " + fmtd
);
96 sdf
->applyPattern("G yyyy DDD");
98 todayS
= sdf
->format(today
, todayS
, pos
);
99 logln("today: " + todayS
);
101 today
= sdf
->parse(todayS
, status
);
102 failure(status
, "sdf->parse");
103 logln((UnicodeString
)"today date: " + today
);
104 /*} catch(Exception e) {
105 logln("Error reparsing date: " + e.getMessage());
110 rt
= sdf
->format(sdf
->parse(todayS
, status
), rt
, pos
);
111 failure(status
, "sdf->parse");
112 logln("round trip: " + rt
);
114 errln("Fail: Want " + todayS
+ " Got " + rt
);
116 catch (ParseException e) {
127 void DateFormatRegressionTest::Test4052408(void)
130 DateFormat
*fmt
= DateFormat::createDateTimeInstance(DateFormat::SHORT
,
131 DateFormat::SHORT
, Locale::getUS());
133 dataerrln("Error calling DateFormat::createDateTimeInstance");
137 UDate dt
= date(97, UCAL_MAY
, 3, 8, 55);
139 str
= fmt
->format(dt
, str
);
142 if(str
!= "5/3/97, 8:55 AM")
143 errln("Fail: Test broken; Want 5/3/97, 8:55 AM Got " + str
);
145 UnicodeString expected
[] = {
146 (UnicodeString
) "", //"ERA_FIELD",
147 (UnicodeString
) "97", //"YEAR_FIELD",
148 (UnicodeString
) "5", //"MONTH_FIELD",
149 (UnicodeString
) "3", //"DATE_FIELD",
150 (UnicodeString
) "", //"HOUR_OF_DAY1_FIELD",
151 (UnicodeString
) "", //"HOUR_OF_DAY0_FIELD",
152 (UnicodeString
) "55", //"MINUTE_FIELD",
153 (UnicodeString
) "", //"SECOND_FIELD",
154 (UnicodeString
) "", //"MILLISECOND_FIELD",
155 (UnicodeString
) "", //"DAY_OF_WEEK_FIELD",
156 (UnicodeString
) "", //"DAY_OF_YEAR_FIELD",
157 (UnicodeString
) "", //"DAY_OF_WEEK_IN_MONTH_FIELD",
158 (UnicodeString
) "", //"WEEK_OF_YEAR_FIELD",
159 (UnicodeString
) "", //"WEEK_OF_MONTH_FIELD",
160 (UnicodeString
) "AM", //"AM_PM_FIELD",
161 (UnicodeString
) "8", //"HOUR1_FIELD",
162 (UnicodeString
) "", //"HOUR0_FIELD",
163 (UnicodeString
) "" //"TIMEZONE_FIELD"
166 //Hashtable expected;// = new Hashtable();
167 //expected.put(new LongKey(DateFormat.MONTH_FIELD), "5");
168 //expected.put(new LongKey(DateFormat.DATE_FIELD), "3");
169 //expected.put(new LongKey(DateFormat.YEAR_FIELD), "97");
170 //expected.put(new LongKey(DateFormat.HOUR1_FIELD), "8");
171 //expected.put(new LongKey(DateFormat.MINUTE_FIELD), "55");
172 //expected.put(new LongKey(DateFormat.AM_PM_FIELD), "AM");
174 //StringBuffer buf = new StringBuffer();
175 UnicodeString fieldNames
[] = {
176 (UnicodeString
) "ERA_FIELD",
177 (UnicodeString
) "YEAR_FIELD",
178 (UnicodeString
) "MONTH_FIELD",
179 (UnicodeString
) "DATE_FIELD",
180 (UnicodeString
) "HOUR_OF_DAY1_FIELD",
181 (UnicodeString
) "HOUR_OF_DAY0_FIELD",
182 (UnicodeString
) "MINUTE_FIELD",
183 (UnicodeString
) "SECOND_FIELD",
184 (UnicodeString
) "MILLISECOND_FIELD",
185 (UnicodeString
) "DAY_OF_WEEK_FIELD",
186 (UnicodeString
) "DAY_OF_YEAR_FIELD",
187 (UnicodeString
) "DAY_OF_WEEK_IN_MONTH_FIELD",
188 (UnicodeString
) "WEEK_OF_YEAR_FIELD",
189 (UnicodeString
) "WEEK_OF_MONTH_FIELD",
190 (UnicodeString
) "AM_PM_FIELD",
191 (UnicodeString
) "HOUR1_FIELD",
192 (UnicodeString
) "HOUR0_FIELD",
193 (UnicodeString
) "TIMEZONE_FIELD"
197 for(int i
= 0; i
<= 17; ++i
) {
198 FieldPosition
pos(i
);
200 fmt
->format(dt
, buf
, pos
);
201 //char[] dst = new char[pos.getEndIndex() - pos.getBeginIndex()];
203 buf
.extractBetween(pos
.getBeginIndex(), pos
.getEndIndex(), dst
);
204 UnicodeString
str(dst
);
205 logln((UnicodeString
)"" + i
+ (UnicodeString
)": " + fieldNames
[i
] +
206 (UnicodeString
)", \"" + str
+ (UnicodeString
)"\", " +
207 pos
.getBeginIndex() + (UnicodeString
)", " +
209 UnicodeString exp
= expected
[i
];
210 if((exp
.length() == 0 && str
.length() == 0) || str
== exp
)
213 errln(UnicodeString(" expected ") + exp
);
219 errln("Fail: FieldPosition not set right by DateFormat");
226 * Verify the function of the [s|g]et2DigitYearStart() API.
228 void DateFormatRegressionTest::Test4056591(void)
230 UErrorCode status
= U_ZERO_ERROR
;
233 SimpleDateFormat
*fmt
= new SimpleDateFormat(UnicodeString("yyMMdd"), Locale::getUS(), status
);
234 if (failure(status
, "new SimpleDateFormat", TRUE
)) {
238 UDate start
= date(1809-1900, UCAL_DECEMBER
, 25);
239 fmt
->set2DigitYearStart(start
, status
);
240 failure(status
, "fmt->setTwoDigitStartDate");
241 if( (fmt
->get2DigitYearStart(status
) != start
) || failure(status
, "get2DigitStartDate"))
242 errln("get2DigitYearStart broken");
244 date(1809-1900, UCAL_DECEMBER
, 25),
245 date(1909-1900, UCAL_DECEMBER
, 24),
246 date(1809-1900, UCAL_DECEMBER
, 26),
247 date(1861-1900, UCAL_DECEMBER
, 25),
250 UnicodeString strings
[] = {
251 (UnicodeString
) "091225",
252 (UnicodeString
) "091224",
253 (UnicodeString
) "091226",
254 (UnicodeString
) "611225"
258 "091225", new Date(1809-1900, Calendar.DECEMBER, 25),
259 "091224", new Date(1909-1900, Calendar.DECEMBER, 24),
260 "091226", new Date(1809-1900, Calendar.DECEMBER, 26),
261 "611225", new Date(1861-1900, Calendar.DECEMBER, 25),
264 for(int i
= 0; i
< 4; i
++) {
265 UnicodeString s
= strings
[i
];
266 UDate exp
= dates
[i
];
267 UDate got
= fmt
->parse(s
, status
);
268 failure(status
, "fmt->parse");
269 logln(s
+ " -> " + got
+ "; exp " + exp
);
271 errln("set2DigitYearStart broken");
274 catch (ParseException e) {
285 void DateFormatRegressionTest::Test4059917(void)
287 UErrorCode status
= U_ZERO_ERROR
;
289 SimpleDateFormat
*fmt
;
290 UnicodeString myDate
;
292 fmt
= new SimpleDateFormat( UnicodeString("yyyy/MM/dd"), status
);
293 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
294 myDate
= "1997/01/01";
295 aux917( fmt
, myDate
);
300 fmt
= new SimpleDateFormat( UnicodeString("yyyyMMdd"), status
);
301 if(failure(status
, "new SimpleDateFormat")) return;
303 aux917( fmt
, myDate
);
308 void DateFormatRegressionTest::aux917( SimpleDateFormat
*fmt
, UnicodeString
& str
) {
311 pat
= fmt
->toPattern(pat
);
312 logln( "==================" );
313 logln( "testIt: pattern=" + pat
+
319 ParsePosition
pos(0);
320 fmt
->parseObject( str
, o
, pos
);
321 //logln( UnicodeString("Parsed object: ") + o );
323 UErrorCode status
= U_ZERO_ERROR
;
324 UnicodeString formatted
;
325 FieldPosition
poss(FieldPosition::DONT_CARE
);
326 formatted
= fmt
->format( o
, formatted
, poss
, status
);
327 failure(status
, "fmt->format");
328 logln( "Formatted string: " + formatted
);
329 if( formatted
!= str
)
330 errln("Fail: Want " + str
+ " Got " + formatted
);
332 catch (ParseException e) {
341 void DateFormatRegressionTest::Test4060212(void)
343 UnicodeString dateString
= "1995-040.05:01:29";
345 logln( "dateString= " + dateString
);
346 logln("Using yyyy-DDD.hh:mm:ss");
347 UErrorCode status
= U_ZERO_ERROR
;
348 SimpleDateFormat
*formatter
= new SimpleDateFormat(UnicodeString("yyyy-DDD.hh:mm:ss"), status
);
349 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
350 ParsePosition
pos(0);
351 UDate myDate
= formatter
->parse( dateString
, pos
);
352 UnicodeString myString
;
353 DateFormat
*fmt
= DateFormat::createDateTimeInstance( DateFormat::FULL
,
356 dataerrln("Error calling DateFormat::createDateTimeInstance");
361 myString
= fmt
->format( myDate
, myString
);
364 Calendar
*cal
= new GregorianCalendar(status
);
365 failure(status
, "new GregorianCalendar");
366 cal
->setTime(myDate
, status
);
367 failure(status
, "cal->setTime");
368 if ((cal
->get(UCAL_DAY_OF_YEAR
, status
) != 40) || failure(status
, "cal->get"))
369 errln((UnicodeString
) "Fail: Got " + cal
->get(UCAL_DAY_OF_YEAR
, status
) +
372 // this is an odd usage of "ddd" and it doesn't
373 // work now that date values are range checked per #3579.
374 logln("Using yyyy-ddd.hh:mm:ss");
377 formatter
= new SimpleDateFormat(UnicodeString("yyyy-ddd.hh:mm:ss"), status
);
378 if(failure(status
, "new SimpleDateFormat")) return;
380 myDate
= formatter
->parse( dateString
, pos
);
381 myString
= fmt
->format( myDate
, myString
);
383 cal
->setTime(myDate
, status
);
384 failure(status
, "cal->setTime");
385 if ((cal
->get(UCAL_DAY_OF_YEAR
, status
) != 40) || failure(status
, "cal->get"))
386 errln((UnicodeString
) "Fail: Got " + cal
->get(UCAL_DAY_OF_YEAR
, status
) +
397 void DateFormatRegressionTest::Test4061287(void)
399 UErrorCode status
= U_ZERO_ERROR
;
401 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("dd/MM/yyyy"), status
);
402 if (U_FAILURE(status
)) {
403 dataerrln("Fail new SimpleDateFormat: %s", u_errorName(status
));
407 failure(status
, "new SimpleDateFormat");
409 logln(UnicodeString("") + df
->parse("35/01/1971", status
));
410 failure(status
, "df->parse(\"35/01/1971\")");
411 //logln(df.parse("35/01/1971").toString());
413 /*catch (ParseException e) {
417 df
->setLenient(FALSE
);
420 logln(UnicodeString("") + df
->parse("35/01/1971", status
));
421 if(U_FAILURE(status
))
423 //logln(df.parse("35/01/1971").toString());
424 //} catch (ParseException e) {ok=TRUE;}
426 errln("Fail: Lenient not working");
433 void DateFormatRegressionTest::Test4065240(void)
436 DateFormat
*shortdate
, *fulldate
;
437 UnicodeString strShortDate
, strFullDate
;
438 Locale saveLocale
= Locale::getDefault();
439 TimeZone
*saveZone
= TimeZone::createDefault();
441 UErrorCode status
= U_ZERO_ERROR
;
443 Locale
*curLocale
= new Locale("de","DE");
444 Locale::setDefault(*curLocale
, status
);
445 failure(status
, "Locale::setDefault");
446 // {sfb} adoptDefault instead of setDefault
447 //TimeZone::setDefault(TimeZone::createTimeZone("EST"));
448 TimeZone::adoptDefault(TimeZone::createTimeZone("EST"));
449 curDate
= date(98, 0, 1);
450 shortdate
= DateFormat::createDateInstance(DateFormat::SHORT
);
451 if (shortdate
== NULL
){
452 dataerrln("Error calling DateFormat::createDateInstance");
456 fulldate
= DateFormat::createDateTimeInstance(DateFormat::LONG
, DateFormat::LONG
);
457 if (fulldate
== NULL
){
458 dataerrln("Error calling DateFormat::createDateTimeInstance");
461 strShortDate
= "The current date (short form) is ";
463 temp
= shortdate
->format(curDate
, temp
);
464 strShortDate
+= temp
;
465 strFullDate
= "The current date (long form) is ";
467 fulldate
->format(curDate
, temp2
);
468 strFullDate
+= temp2
;
473 // {sfb} What to do with resource bundle stuff?????
475 // Check to see if the resource is present; if not, we can't test
476 ResourceBundle
*bundle
= new ResourceBundle(
477 NULL
, *curLocale
, status
);
478 failure(status
, "new ResourceBundle");
479 //(UnicodeString) "java.text.resources.DateFormatZoneData", curLocale);
481 // {sfb} API change to ResourceBundle -- add getLocale()
482 /*if (bundle->getLocale().getLanguage(temp) == UnicodeString("de")) {
483 // UPDATE THIS AS ZONE NAME RESOURCE FOR <EST> in de_DE is updated
484 if (!strFullDate.endsWith(UnicodeString("GMT-05:00")))
485 errln("Fail: Want GMT-05:00");
488 logln("*** TEST COULD NOT BE COMPLETED BECAUSE DateFormatZoneData ***");
489 logln("*** FOR LOCALE de OR de_DE IS MISSING ***");
493 Locale::setDefault(saveLocale
, status
);
494 failure(status
, "Locale::setDefault");
495 TimeZone::setDefault(*saveZone
);
505 DateFormat.equals is too narrowly defined. As a result, MessageFormat
506 does not work correctly. DateFormat.equals needs to be written so
507 that the Calendar sub-object is not compared using Calendar.equals,
508 but rather compared for equivalency. This may necessitate adding a
509 (package private) method to Calendar to test for equivalency.
511 Currently this bug breaks MessageFormat.toPattern
516 void DateFormatRegressionTest::Test4071441(void)
518 DateFormat
*fmtA
= DateFormat::createInstance();
519 DateFormat
*fmtB
= DateFormat::createInstance();
521 if (fmtA
== NULL
|| fmtB
== NULL
){
522 dataerrln("Error calling DateFormat::createInstance");
528 // {sfb} Is it OK to cast away const here?
529 Calendar
*calA
= (Calendar
*) fmtA
->getCalendar();
530 Calendar
*calB
= (Calendar
*) fmtB
->getCalendar();
532 errln("Couldn't get proper calendars, exiting");
537 UDate epoch
= date(0, 0, 0);
538 UDate xmas
= date(61, UCAL_DECEMBER
, 25);
540 UErrorCode status
= U_ZERO_ERROR
;
541 calA
->setTime(epoch
, status
);
542 failure(status
, "calA->setTime");
543 calB
->setTime(epoch
, status
);
544 failure(status
, "calB->setTime");
546 errln("Fail: Can't complete test; Calendar instances unequal");
548 errln("Fail: DateFormat unequal when Calendars equal");
549 calB
->setTime(xmas
, status
);
550 failure(status
, "calB->setTime");
552 errln("Fail: Can't complete test; Calendar instances equal");
554 errln("Fail: DateFormat unequal when Calendars equivalent");
556 logln("DateFormat.equals ok");
562 /* The java.text.DateFormat.parse(String) method expects for the
563 US locale a string formatted according to mm/dd/yy and parses it
566 When given a string mm/dd/yyyy [sic] it only parses up to the first
567 two y's, typically resulting in a date in the year 1919.
569 Please extend the parsing method(s) to handle strings with
570 four-digit year values (probably also applicable to various
575 void DateFormatRegressionTest::Test4073003(void)
578 UErrorCode ec
= U_ZERO_ERROR
;
579 SimpleDateFormat
fmt("MM/dd/yy", Locale::getUK(), ec
);
581 dataerrln("FAIL: SimpleDateFormat constructor - %s", u_errorName(ec
));
584 UnicodeString tests
[] = {
585 (UnicodeString
) "12/25/61",
586 (UnicodeString
) "12/25/1961",
587 (UnicodeString
) "4/3/2010",
588 (UnicodeString
) "4/3/10"
590 UErrorCode status
= U_ZERO_ERROR
;
591 for(int i
= 0; i
< 4; i
+=2) {
592 UDate d
= fmt
.parse(tests
[i
], status
);
593 failure(status
, "fmt.parse");
594 UDate dd
= fmt
.parse(tests
[i
+1], status
);
595 failure(status
, "fmt.parse");
597 s
= fmt
.format(d
, s
);
599 ss
= fmt
.format(dd
, ss
);
601 errln((UnicodeString
) "Fail: " + d
+ " != " + dd
);
603 errln((UnicodeString
)"Fail: " + s
+ " != " + ss
);
604 logln("Ok: " + s
+ " " + d
);
611 void DateFormatRegressionTest::Test4089106(void)
613 TimeZone
*def
= TimeZone::createDefault();
615 TimeZone
*z
= new SimpleTimeZone((int)(1.25 * 3600000), "FAKEZONE");
616 TimeZone::setDefault(*z
);
617 UErrorCode status
= U_ZERO_ERROR
;
618 SimpleDateFormat
*f
= new SimpleDateFormat(status
);
619 if(U_FAILURE(status
)) {
620 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
626 failure(status
, "new SimpleDateFormat");
627 if (f
->getTimeZone()!= *z
)
628 errln("Fail: SimpleTimeZone should use TimeZone.getDefault()");
632 TimeZone::setDefault(*def
);
644 // {sfb} not applicable in C++??
646 void DateFormatRegressionTest::Test4100302(void)
648 /* Locale locales [] = {
650 Locale::CANADA_FRENCH,
665 Locale::SIMPLIFIED_CHINESE,
667 Locale::TRADITIONAL_CHINESE,
673 for(int i = 0; i < 21; i++) {
675 Format *format = DateFormat::createDateTimeInstance(DateFormat::FULL,
676 DateFormat::FULL, locales[i]);
679 ByteArrayOutputStream baos = new ByteArrayOutputStream();
680 ObjectOutputStream oos = new ObjectOutputStream(baos);
682 oos.writeObject(format);
686 bytes = baos.toByteArray();
688 ObjectInputStream ois =
689 new ObjectInputStream(new ByteArrayInputStream(bytes));
691 if (!format.equals(ois.readObject())) {
693 logln("DateFormat instance for locale " +
694 locales[i] + " is incorrectly serialized/deserialized.");
696 logln("DateFormat instance for locale " +
697 locales[i] + " is OKAY.");
700 if (!pass) errln("Fail: DateFormat serialization/equality bug");
702 catch (IOException e) {
706 catch (ClassNotFoundException e) {
715 void DateFormatRegressionTest::Test4101483(void)
717 UErrorCode status
= U_ZERO_ERROR
;
718 SimpleDateFormat
*sdf
= new SimpleDateFormat(UnicodeString("z"), Locale::getUS(), status
);
719 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
720 FieldPosition
fp(UDAT_TIMEZONE_FIELD
);
721 //Date d = date(9234567890L);
722 UDate d
= 9234567890.0;
723 //StringBuffer buf = new StringBuffer("");
725 sdf
->format(d
, buf
, fp
);
726 //logln(sdf.format(d, buf, fp).toString());
727 logln(dateToString(d
) + " => " + buf
);
728 logln(UnicodeString("beginIndex = ") + fp
.getBeginIndex());
729 logln(UnicodeString("endIndex = ") + fp
.getEndIndex());
730 if (fp
.getBeginIndex() == fp
.getEndIndex())
731 errln("Fail: Empty field");
739 * This bug really only works in Locale.US, since that's what the locale
740 * used for Date.toString() is. Bug 4138203 reports that it fails on Korean
741 * NT; it would actually have failed on any non-US locale. Now it should
742 * work on all locales.
744 void DateFormatRegressionTest::Test4103340(void)
746 UErrorCode status
= U_ZERO_ERROR
;
748 // choose a date that is the FIRST of some month
749 // and some arbitrary time
750 UDate d
= date(97, 3, 1, 1, 1, 1);
751 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("MMMM"), Locale::getUS(), status
);
752 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
755 s
= dateToString(d
, s
);
757 FieldPosition
pos(FieldPosition::DONT_CARE
);
758 s2
= df
->format(d
, s2
, pos
);
761 UnicodeString substr
;
762 s2
.extract(0,2, substr
);
763 if (s
.indexOf(substr
) == -1)
764 errln("Months should match");
772 void DateFormatRegressionTest::Test4103341(void)
774 TimeZone
*saveZone
=TimeZone::createDefault();
777 // {sfb} changed from setDefault to adoptDefault
778 TimeZone::adoptDefault(TimeZone::createTimeZone("CST"));
779 UErrorCode status
= U_ZERO_ERROR
;
780 SimpleDateFormat
*simple
= new SimpleDateFormat(UnicodeString("MM/dd/yyyy HH:mm"), status
);
781 if(U_FAILURE(status
)) {
782 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
786 failure(status
, "new SimpleDateFormat");
787 TimeZone
*temp
= TimeZone::createDefault();
788 if(simple
->getTimeZone() != *temp
)
789 errln("Fail: SimpleDateFormat not using default zone");
792 TimeZone::adoptDefault(saveZone
);
802 void DateFormatRegressionTest::Test4104136(void)
804 UErrorCode status
= U_ZERO_ERROR
;
805 SimpleDateFormat
*sdf
= new SimpleDateFormat(status
);
806 if(U_FAILURE(status
)) {
807 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
811 if(failure(status
, "new SimpleDateFormat")) return;
812 UnicodeString pattern
= "'time' hh:mm";
813 sdf
->applyPattern(pattern
);
814 logln("pattern: \"" + pattern
+ "\"");
816 UnicodeString strings
[] = {
817 (UnicodeString
)"time 10:30",
818 (UnicodeString
) "time 10:x",
819 (UnicodeString
) "time 10x"
822 ParsePosition ppos
[] = {
829 date(70, UCAL_JANUARY
, 1, 10, 30),
835 "time 10:30", new ParsePosition(10), new Date(70, Calendar.JANUARY, 1, 10, 30),
836 "time 10:x", new ParsePosition(0), null,
837 "time 10x", new ParsePosition(0), null,
840 for(int i
= 0; i
< 3; i
++) {
841 UnicodeString text
= strings
[i
];
842 ParsePosition finish
= ppos
[i
];
843 UDate exp
= dates
[i
];
845 ParsePosition
pos(0);
846 UDate d
= sdf
->parse(text
, pos
);
847 logln(" text: \"" + text
+ "\"");
848 logln(" index: %d", pos
.getIndex());
849 logln((UnicodeString
) " result: " + d
);
850 if(pos
.getIndex() != finish
.getIndex())
851 errln(UnicodeString("Fail: Expected pos ") + finish
.getIndex());
852 if (! ((d
== 0 && exp
== -1) || (d
== exp
)))
853 errln((UnicodeString
) "Fail: Expected result " + exp
);
862 * According to the bug report, this test should throw a
863 * StringIndexOutOfBoundsException during the second parse. However,
866 void DateFormatRegressionTest::Test4104522(void)
868 UErrorCode status
= U_ZERO_ERROR
;
870 SimpleDateFormat
*sdf
= new SimpleDateFormat(status
);
871 if(U_FAILURE(status
)) {
872 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
876 failure(status
, "new SimpleDateFormat");
877 UnicodeString pattern
= "'time' hh:mm";
878 sdf
->applyPattern(pattern
);
879 logln("pattern: \"" + pattern
+ "\"");
883 UnicodeString text
= "time ";
884 UDate dt
= sdf
->parse(text
, pp
);
885 logln(" text: \"" + text
+ "\"" +
891 dt
= sdf
->parse(text
, pp
);
892 logln(" text: \"" + text
+ "\"" +
901 void DateFormatRegressionTest::Test4106807(void)
904 DateFormat
*df
= DateFormat::createDateTimeInstance();
906 UErrorCode status
= U_ZERO_ERROR
;
907 SimpleDateFormat
*sdfs
[] = {
908 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss"), status
),
909 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'Z'"), status
),
910 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss''"), status
),
911 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'a''a'"), status
),
912 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss %"), status
)
914 if(U_FAILURE(status
)) {
915 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
924 failure(status
, "new SimpleDateFormat");
926 UnicodeString strings
[] = {
927 (UnicodeString
) "19980211140000",
928 (UnicodeString
) "19980211140000",
929 (UnicodeString
) "19980211140000",
930 (UnicodeString
) "19980211140000a",
931 (UnicodeString
) "19980211140000 "
935 new SimpleDateFormat("yyyyMMddHHmmss"), "19980211140000",
936 new SimpleDateFormat("yyyyMMddHHmmss'Z'"), "19980211140000",
937 new SimpleDateFormat("yyyyMMddHHmmss''"), "19980211140000",
938 new SimpleDateFormat("yyyyMMddHHmmss'a''a'"), "19980211140000a",
939 new SimpleDateFormat("yyyyMMddHHmmss %"), "19980211140000 ",
941 GregorianCalendar
*gc
= new GregorianCalendar(status
);
942 failure(status
, "new GregorianCalendar");
943 TimeZone
*timeZone
= TimeZone::createDefault();
945 TimeZone
*gmt
= timeZone
->clone();
947 gmt
->setRawOffset(0);
949 for(int32_t i
= 0; i
< 5; i
++) {
950 SimpleDateFormat
*format
= sdfs
[i
];
951 UnicodeString dateString
= strings
[i
];
953 format
->setTimeZone(*gmt
);
954 dt
= format
->parse(dateString
, status
);
955 // {sfb} some of these parses will fail purposely
956 if(U_FAILURE(status
))
958 status
= U_ZERO_ERROR
;
960 FieldPosition
pos(FieldPosition::DONT_CARE
);
961 fmtd
= df
->format(dt
, fmtd
, pos
);
963 //logln(df->format(dt));
964 gc
->setTime(dt
, status
);
965 failure(status
, "gc->getTime");
966 logln(UnicodeString("") + gc
->get(UCAL_ZONE_OFFSET
, status
));
967 failure(status
, "gc->get");
969 s
= format
->format(dt
, s
, pos
);
972 catch (ParseException e) {
973 logln("No way Jose");
979 for(int32_t j
= 0; j
< 5; j
++)
986 Synopsis: Chinese time zone CTT is not recogonized correctly.
987 Description: Platform Chinese Windows 95 - ** Time zone set to CST **
993 // {sfb} what to do with this one ??
994 void DateFormatRegressionTest::Test4108407(void)
996 /*long l = System.currentTimeMillis();
997 logln("user.timezone = " + System.getProperty("user.timezone", "?"));
998 logln("Time Zone :" +
999 DateFormat.getDateInstance().getTimeZone().getID());
1000 logln("Default format :" +
1001 DateFormat.getDateInstance().format(new Date(l)));
1002 logln("Full format :" +
1003 DateFormat.getDateInstance(DateFormat.FULL).format(new
1005 logln("*** Set host TZ to CST ***");
1006 logln("*** THE RESULTS OF THIS TEST MUST BE VERIFIED MANUALLY ***");*/
1011 * SimpleDateFormat won't parse "GMT"
1013 void DateFormatRegressionTest::Test4134203(void)
1015 UErrorCode status
= U_ZERO_ERROR
;
1016 UnicodeString dateFormat
= "MM/dd/yy HH:mm:ss zzz";
1017 SimpleDateFormat
*fmt
= new SimpleDateFormat(dateFormat
, status
);
1018 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
1019 ParsePosition
p0(0);
1020 UDate d
= fmt
->parse("01/22/92 04:52:00 GMT", p0
);
1021 logln(dateToString(d
));
1022 if(p0
== ParsePosition(0))
1023 errln("Fail: failed to parse 'GMT'");
1024 // In the failure case an exception is thrown by parse();
1025 // if no exception is thrown, the test passes.
1032 * SimpleDateFormat incorrect handling of 2 single quotes in format()
1034 void DateFormatRegressionTest::Test4151631(void)
1036 UnicodeString pattern
= "'TO_DATE('''dd'-'MM'-'yyyy HH:mm:ss''' , ''DD-MM-YYYY HH:MI:SS'')'";
1037 logln("pattern=" + pattern
);
1038 UErrorCode status
= U_ZERO_ERROR
;
1039 SimpleDateFormat
*format
= new SimpleDateFormat(pattern
, Locale::getUS(), status
);
1040 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
1041 UnicodeString result
;
1042 FieldPosition
pos(FieldPosition::DONT_CARE
);
1043 result
= format
->format(date(1998-1900, UCAL_JUNE
, 30, 13, 30, 0), result
, pos
);
1044 if (result
!= "TO_DATE('30-06-1998 13:30:00' , 'DD-MM-YYYY HH:MI:SS')") {
1045 errln("Fail: result=" + result
);
1048 logln("Pass: result=" + result
);
1056 * 'z' at end of date format throws index exception in SimpleDateFormat
1057 * CANNOT REPRODUCE THIS BUG ON 1.2FCS
1059 void DateFormatRegressionTest::Test4151706(void)
1061 UnicodeString
dateString("Thursday, 31-Dec-98 23:00:00 GMT");
1062 UErrorCode status
= U_ZERO_ERROR
;
1063 SimpleDateFormat
fmt(UnicodeString("EEEE, dd-MMM-yy HH:mm:ss z"), Locale::getUS(), status
);
1064 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
1066 UDate d
= fmt
.parse(dateString
, status
);
1067 failure(status
, "fmt->parse");
1068 // {sfb} what about next two lines?
1069 //if (d.getTime() != Date.UTC(1998-1900, Calendar.DECEMBER, 31, 23, 0, 0))
1070 // errln("Incorrect value: " + d);
1071 /*} catch (Exception e) {
1072 errln("Fail: " + e);
1075 FieldPosition
pos(0);
1076 logln(dateString
+ " -> " + fmt
.format(d
, temp
, pos
));
1081 * Cannot reproduce this bug under 1.2 FCS -- it may be a convoluted duplicate
1082 * of some other bug that has been fixed.
1085 DateFormatRegressionTest::Test4162071(void)
1087 UnicodeString
dateString("Thu, 30-Jul-1999 11:51:14 GMT");
1088 UnicodeString
format("EEE', 'dd-MMM-yyyy HH:mm:ss z"); // RFC 822/1123
1089 UErrorCode status
= U_ZERO_ERROR
;
1090 SimpleDateFormat
df(format
, Locale::getUS(), status
);
1091 if(U_FAILURE(status
)) {
1092 dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status
));
1097 UDate x
= df
.parse(dateString
, status
);
1098 if(U_SUCCESS(status
))
1099 logln("Parse format \"" + format
+ "\" ok");
1101 errln("Parse format \"" + format
+ "\" failed.");
1103 FieldPosition
pos(0);
1104 logln(dateString
+ " -> " + df
.format(x
, temp
, pos
));
1105 //} catch (Exception e) {
1106 // errln("Parse format \"" + format + "\" failed.");
1111 * DateFormat shouldn't parse year "-1" as a two-digit year (e.g., "-1" -> 1999).
1113 void DateFormatRegressionTest::Test4182066(void) {
1114 UErrorCode status
= U_ZERO_ERROR
;
1115 SimpleDateFormat
fmt("MM/dd/yy", Locale::getUS(), status
);
1116 SimpleDateFormat
dispFmt("MMM dd yyyy GG", Locale::getUS(), status
);
1117 if (U_FAILURE(status
)) {
1118 dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status
));
1122 /* We expect 2-digit year formats to put 2-digit years in the right
1123 * window. Out of range years, that is, anything less than "00" or
1124 * greater than "99", are treated as literal years. So "1/2/3456"
1125 * becomes 3456 AD. Likewise, "1/2/-3" becomes -3 AD == 2 BC.
1127 const char* STRINGS
[] = {
1137 int32_t STRINGS_COUNT
= UPRV_LENGTHOF(STRINGS
);
1138 UDate FAIL_DATE
= (UDate
) 0;
1140 date(2000-1900, UCAL_FEBRUARY
, 29),
1141 date(2001-1900, UCAL_JANUARY
, 23),
1142 date( -1-1900, UCAL_APRIL
, 5),
1143 date( -9-1900, UCAL_JANUARY
, 23),
1144 date(1314-1900, UCAL_NOVEMBER
, 12),
1145 date( 1-1900, UCAL_OCTOBER
, 31),
1146 FAIL_DATE
, // "+1" isn't recognized by US NumberFormat
1147 date( 1-1900, UCAL_SEPTEMBER
,12),
1152 for (int32_t i
=0; i
<STRINGS_COUNT
; ++i
) {
1153 UnicodeString
str(STRINGS
[i
]);
1154 UDate expected
= DATES
[i
];
1155 status
= U_ZERO_ERROR
;
1156 UDate actual
= fmt
.parse(str
, status
);
1157 if (U_FAILURE(status
)) {
1160 UnicodeString actStr
;
1161 if (actual
== FAIL_DATE
) {
1162 actStr
.append("null");
1165 ((DateFormat
*)&dispFmt
)->format(actual
, actStr
);
1168 if (expected
== actual
) {
1169 out
.append(str
+ " => " + actStr
+ "\n");
1171 UnicodeString expStr
;
1172 if (expected
== FAIL_DATE
) {
1173 expStr
.append("null");
1176 ((DateFormat
*)&dispFmt
)->format(expected
, expStr
);
1178 out
.append("FAIL: " + str
+ " => " + actStr
1179 + ", expected " + expStr
+ "\n");
1191 * j32 {JDK Bug 4210209 4209272}
1192 * DateFormat cannot parse Feb 29 2000 when setLenient(false)
1195 DateFormatRegressionTest::Test4210209(void) {
1196 UErrorCode status
= U_ZERO_ERROR
;
1197 UnicodeString
pattern("MMM d, yyyy");
1198 SimpleDateFormat
sfmt(pattern
, Locale::getUS(), status
);
1199 SimpleDateFormat
sdisp("MMM dd yyyy GG", Locale::getUS(), status
);
1200 DateFormat
& fmt
= *(DateFormat
*)&sfmt
; // Yuck: See j25
1201 DateFormat
& disp
= *(DateFormat
*)&sdisp
; // Yuck: See j25
1202 if (U_FAILURE(status
)) {
1203 dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status
));
1206 Calendar
* calx
= (Calendar
*)fmt
.getCalendar(); // cast away const!
1207 calx
->setLenient(FALSE
);
1208 UDate d
= date(2000-1900, UCAL_FEBRUARY
, 29);
1209 UnicodeString s
, ss
;
1211 logln(disp
.format(d
, ss
.remove()) + " f> " + pattern
+
1212 " => \"" + s
+ "\"");
1213 ParsePosition
pos(0);
1214 d
= fmt
.parse(s
, pos
);
1215 logln(UnicodeString("\"") + s
+ "\" p> " + pattern
+
1216 " => " + disp
.format(d
, ss
.remove()));
1217 logln(UnicodeString("Parse pos = ") + pos
.getIndex() +
1218 ", error pos = " + pos
.getErrorIndex());
1219 if (pos
.getErrorIndex() != -1) {
1220 errln(UnicodeString("FAIL: Error index should be -1"));
1223 // The underlying bug is in GregorianCalendar. If the following lines
1224 // succeed, the bug is fixed. If the bug isn't fixed, they will throw
1226 GregorianCalendar
cal(status
);
1227 if (U_FAILURE(status
)) {
1228 errln("FAIL: Unable to create Calendar");
1232 cal
.setLenient(FALSE
);
1233 cal
.set(2000, UCAL_FEBRUARY
, 29); // This should work!
1234 logln(UnicodeString("Attempt to set Calendar to Feb 29 2000: ") +
1235 disp
.format(cal
.getTime(status
), ss
.remove()));
1236 if (U_FAILURE(status
)) {
1237 errln("FAIL: Unable to set Calendar to Feb 29 2000");
1241 void DateFormatRegressionTest::Test714(void)
1244 UDate
d(978103543000.);
1245 DateFormat
*fmt
= DateFormat::createDateTimeInstance(DateFormat::NONE
,
1249 dataerrln("Error calling DateFormat::createDateTimeInstance");
1254 UnicodeString tests
=
1255 (UnicodeString
) "7:25:43 AM" ;
1256 UErrorCode status
= U_ZERO_ERROR
;
1258 if(U_FAILURE(status
))
1260 errln((UnicodeString
) "Fail, errmsg " + u_errorName(status
));
1266 errln((UnicodeString
) "Fail: " + s
+ " != " + tests
);
1270 logln("OK: " + s
+ " == " + tests
);
1276 class Test1684Data
{
1286 UnicodeString normalized
;
1288 Test1684Data(int32_t xyear
, int32_t xmonth
, int32_t xdate
,
1289 int32_t xwomyear
, int32_t xwommon
, int32_t xwom
, int32_t xdow
,
1290 const char *xdata
, const char *xnormalized
) :
1299 normalized((xnormalized
==NULL
)?xdata
:xnormalized
,"")
1303 void DateFormatRegressionTest::Test1684(void)
1305 // July 2001 August 2001 January 2002
1306 // Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1307 // 1 2 3 4 5 6 7 1 2 3 4 1 2 3 4 5
1308 // 8 9 10 11 12 13 14 5 6 7 8 9 10 11 6 7 8 9 10 11 12
1309 // 15 16 17 18 19 20 21 12 13 14 15 16 17 18 13 14 15 16 17 18 19
1310 // 22 23 24 25 26 27 28 19 20 21 22 23 24 25 20 21 22 23 24 25 26
1311 // 29 30 31 26 27 28 29 30 31 27 28 29 30 31
1312 Test1684Data
*tests
[] = {
1313 new Test1684Data(2001, 8, 6, 2001,8,2,UCAL_MONDAY
, "2001 08 02 Mon", NULL
),
1314 new Test1684Data(2001, 8, 7, 2001,8,2,UCAL_TUESDAY
, "2001 08 02 Tue", NULL
),
1315 new Test1684Data(2001, 8, 5,/*12,*/ 2001,8,2,UCAL_SUNDAY
, "2001 08 02 Sun", NULL
),
1316 new Test1684Data(2001, 8,6, /*7, 30,*/ 2001,7,6,UCAL_MONDAY
, "2001 07 06 Mon", "2001 08 02 Mon"),
1317 new Test1684Data(2001, 8,7, /*7, 31,*/ 2001,7,6,UCAL_TUESDAY
, "2001 07 06 Tue", "2001 08 02 Tue"),
1318 new Test1684Data(2001, 8, 5, 2001,7,6,UCAL_SUNDAY
, "2001 07 06 Sun", "2001 08 02 Sun"),
1319 new Test1684Data(2001, 7, 30, 2001,8,1,UCAL_MONDAY
, "2001 08 01 Mon", "2001 07 05 Mon"),
1320 new Test1684Data(2001, 7, 31, 2001,8,1,UCAL_TUESDAY
, "2001 08 01 Tue", "2001 07 05 Tue"),
1321 new Test1684Data(2001, 7,29, /*8, 5,*/ 2001,8,1,UCAL_SUNDAY
, "2001 08 01 Sun", "2001 07 05 Sun"),
1322 new Test1684Data(2001, 12, 31, 2001,12,6,UCAL_MONDAY
, "2001 12 06 Mon", NULL
),
1323 new Test1684Data(2002, 1, 1, 2002,1,1,UCAL_TUESDAY
, "2002 01 01 Tue", NULL
),
1324 new Test1684Data(2002, 1, 2, 2002,1,1,UCAL_WEDNESDAY
, "2002 01 01 Wed", NULL
),
1325 new Test1684Data(2002, 1, 3, 2002,1,1,UCAL_THURSDAY
, "2002 01 01 Thu", NULL
),
1326 new Test1684Data(2002, 1, 4, 2002,1,1,UCAL_FRIDAY
, "2002 01 01 Fri", NULL
),
1327 new Test1684Data(2002, 1, 5, 2002,1,1,UCAL_SATURDAY
, "2002 01 01 Sat", NULL
),
1328 new Test1684Data(2001,12,30, /*2002, 1, 6,*/ 2002,1,1,UCAL_SUNDAY
, "2002 01 01 Sun", "2001 12 06 Sun")
1331 #define kTest1684Count UPRV_LENGTHOF(tests)
1333 int32_t pass
= 0, error
= 0, warning
= 0;
1336 UErrorCode status
= U_ZERO_ERROR
;
1337 UnicodeString
pattern("yyyy MM WW EEE","");
1338 Calendar
*cal
= new GregorianCalendar(status
);
1339 SimpleDateFormat
*sdf
= new SimpleDateFormat(pattern
,status
);
1340 if (U_FAILURE(status
)) {
1341 dataerrln("Error constructing SimpleDateFormat");
1342 for(i
=0;i
<kTest1684Count
;i
++) {
1349 cal
->setFirstDayOfWeek(UCAL_SUNDAY
);
1350 cal
->setMinimalDaysInFirstWeek(1);
1352 sdf
->adoptCalendar(cal
);
1354 cal
= sdf
->getCalendar()->clone(); // sdf may have deleted calendar
1356 if(!cal
|| !sdf
|| U_FAILURE(status
)) {
1357 errln(UnicodeString("Error setting up test: ") + u_errorName(status
));
1360 for (i
= 0; i
< kTest1684Count
; ++i
) {
1361 Test1684Data
&test
= *(tests
[i
]);
1362 logln(UnicodeString("#") + i
+ UnicodeString("\n-----\nTesting round trip of ") + test
.year
+
1363 " " + (test
.month
+ 1) +
1365 " (written as) " + test
.data
);
1368 cal
->set(test
.year
, test
.month
, test
.date
);
1369 UDate ms
= cal
->getTime(status
);
1372 cal
->set(UCAL_YEAR
, test
.womyear
);
1373 cal
->set(UCAL_MONTH
, test
.wommon
);
1374 cal
->set(UCAL_WEEK_OF_MONTH
, test
.wom
);
1375 cal
->set(UCAL_DAY_OF_WEEK
, test
.dow
);
1376 UDate ms2
= cal
->getTime(status
);
1379 errln((UnicodeString
)"\nError: GregorianUCAL_DOM gave " + ms
+
1380 "\n GregorianUCAL_WOM gave " + ms2
);
1386 ms2
= sdf
->parse(test
.data
, status
);
1387 if(U_FAILURE(status
)) {
1388 errln("parse exception: " + UnicodeString(u_errorName(status
)));
1392 errln((UnicodeString
)"\nError: GregorianCalendar gave " + ms
+
1393 "\n SimpleDateFormat.parse gave " + ms2
);
1399 UnicodeString result
;
1400 sdf
->format(ms
, result
);
1401 if (result
!= test
.normalized
) {
1402 errln("\nWarning: format of '" + test
.data
+ "' gave" +
1403 "\n '" + result
+ "'" +
1404 "\n expected '" + test
.normalized
+ "'");
1411 ms3
= sdf
->parse(result
, status
);
1412 if(U_FAILURE(status
)) {
1413 errln("parse exception 2: " + (UnicodeString
)u_errorName(status
));
1418 errln((UnicodeString
)"\nError: Re-parse of '" + result
+ "' gave time of " +
1427 = UnicodeString("Passed: ") + pass
+ ", Warnings: " + warning
+ ", Errors: " + error
;
1434 for(i
=0;i
<kTest1684Count
;i
++) {
1441 void DateFormatRegressionTest::Test5554(void)
1443 UErrorCode status
= U_ZERO_ERROR
;
1444 UnicodeString
pattern("Z","");
1445 UnicodeString
newfoundland("Canada/Newfoundland", "");
1446 TimeZone
*zone
= TimeZone::createTimeZone(newfoundland
);
1447 Calendar
*cal
= new GregorianCalendar(zone
, status
);
1448 SimpleDateFormat
*sdf
= new SimpleDateFormat(pattern
,status
);
1449 if (U_FAILURE(status
)) {
1450 dataerrln("Error constructing SimpleDateFormat");
1455 cal
->set(2007, 1, 14);
1456 UDate date
= cal
->getTime(status
);
1457 if (U_FAILURE(status
)) {
1458 errln("Error getting time to format");
1461 sdf
->adoptCalendar(cal
);
1462 UnicodeString result
;
1463 UnicodeString
correct("-0330", "");
1464 sdf
->format(date
, result
);
1465 if (result
!= correct
) {
1466 errln("\nError: Newfoundland Z of Jan 14, 2007 gave '" + result
+ "', expected '" + correct
+ "'");
1471 void DateFormatRegressionTest::Test9237(void)
1473 UErrorCode status
= U_ZERO_ERROR
;
1474 UnicodeString
pattern("VVVV");
1476 SimpleDateFormat
fmt(pattern
, status
); // default locale
1477 SimpleDateFormat
fmtDE(pattern
, Locale("de_DE"), status
);
1478 if (U_FAILURE(status
)) {
1479 dataerrln("Error constructing SimpleDateFormat");
1484 SimpleDateFormat
fmtCopyDE(fmtDE
);
1485 UnicodeString resDE
, resCopyDE
;
1487 fmtDE
.format(0.0, resDE
);
1488 fmtCopyDE
.format(0.0, resCopyDE
);
1490 if (resDE
!= resCopyDE
) {
1491 errln(UnicodeString("Error: different result by the copied instance - org:") + resDE
+ " copy:" + resCopyDE
);
1494 // test for assignment operator
1497 UnicodeString resAssigned
;
1498 fmt
.format(0.0, resAssigned
);
1500 if (resDE
!= resAssigned
) {
1501 errln(UnicodeString("Error: different results by the assigned instance - org:") + resDE
+ " assigned:" + resAssigned
);
1505 void DateFormatRegressionTest::TestParsing(void) {
1506 UErrorCode status
= U_ZERO_ERROR
;
1507 UnicodeString
pattern("EEE-WW-MMMM-yyyy");
1508 UnicodeString
text("mon-02-march-2011");
1509 int32_t expectedDay
= 7;
1511 SimpleDateFormat
format(pattern
, status
);
1512 if (U_FAILURE(status
)) {
1513 dataerrln("Unable to create SimpleDateFormat - %s", u_errorName(status
));
1517 Calendar
*cal
= new GregorianCalendar(status
);
1518 if (cal
== NULL
|| U_FAILURE(status
)) {
1519 errln("Unable to create calendar - %s", u_errorName(status
));
1523 ParsePosition
pos(0);
1524 format
.parse(text
, *cal
, pos
);
1526 if (cal
->get(UCAL_DAY_OF_MONTH
, status
) != expectedDay
) {
1527 errln("Parsing failed: day of month should be '7' with pattern: \"" + pattern
+ "\" for text: \"" + text
+ "\"");
1533 void DateFormatRegressionTest::TestT10334(void) {
1534 UErrorCode status
= U_ZERO_ERROR
;
1535 UnicodeString
pattern("'--: 'EEE-WW-MMMM-yyyy");
1536 UnicodeString
text("--mon-02-march-2011");
1537 SimpleDateFormat
format(pattern
, status
);
1539 logln("pattern["+pattern
+"] text["+text
+"]");
1541 if (U_FAILURE(status
)) {
1542 dataerrln("Fail creating SimpleDateFormat object - %s", u_errorName(status
));
1546 format
.setBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH
, FALSE
, status
);
1547 format
.parse(text
, status
);
1548 if (!U_FAILURE(status
)) {
1549 errln("parse partial match did NOT fail in strict mode - %s", u_errorName(status
));
1552 status
= U_ZERO_ERROR
;
1553 format
.setBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH
, TRUE
, status
);
1554 format
.parse(text
, status
);
1555 if (U_FAILURE(status
)) {
1556 errln("parse partial match failure in lenient mode - %s", u_errorName(status
));
1559 status
= U_ZERO_ERROR
;
1560 pattern
= UnicodeString("YYYY MM dd");
1561 text
= UnicodeString("2013 12 10");
1562 format
.applyPattern(pattern
);
1563 UDate referenceDate
= format
.parse(text
, status
);
1565 FieldPosition
fp(0);
1566 UnicodeString
formattedString("");
1567 pattern
= UnicodeString("YYYY LL dd ee cc qq QQ");
1568 format
.applyPattern(pattern
);
1569 format
.format(referenceDate
, formattedString
, fp
, status
);
1570 logln("ref date: " + formattedString
);
1573 char patternArray
[] = "YYYY LLL dd eee ccc qqq QQQ";
1574 pattern
= UnicodeString(patternArray
);
1575 text
= UnicodeString("2013 12 10 03 3 04 04");
1576 status
= U_ZERO_ERROR
;
1577 format
.setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC
, TRUE
, status
);
1578 format
.applyPattern(pattern
);
1579 ParsePosition
pp(0);
1580 format
.parse(text
, pp
);
1581 if (pp
.getErrorIndex() != -1) {
1582 errln("numeric parse error");
1585 status
= U_ZERO_ERROR
;
1586 format
.setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC
, FALSE
, status
);
1587 format
.parse(text
, status
);
1588 if (!U_FAILURE(status
)) {
1589 errln("numeric parse did NOT fail in strict mode", u_errorName(status
));
1596 const char * locale
;
1598 UnicodeString parseString
;
1599 UnicodeString pattern
;
1600 UnicodeString expectedResult
; // null indicates expected error
1601 } TestDateFormatLeniencyItem
;
1604 void DateFormatRegressionTest::TestT10619(void) {
1605 const UDate july022008
= 1215000001979.0;
1606 const TestDateFormatLeniencyItem items
[] = {
1607 //locale leniency parse String pattern expected result
1608 { "en", true, UnicodeString("2008-07 02"), UnicodeString("yyyy-LLLL dd"), UnicodeString("2008-July 02") },
1609 { "en", false, UnicodeString("2008-07 03"), UnicodeString("yyyy-LLLL dd"), UnicodeString("") },
1610 { "en", true, UnicodeString("2008-Jan. 04"), UnicodeString("yyyy-LLL dd"), UnicodeString("2008-Jan 04") },
1611 { "en", false, UnicodeString("2008-Jan. 05"), UnicodeString("yyyy-LLL dd"), UnicodeString("") },
1612 { "en", true, UnicodeString("2008-Jan--06"), UnicodeString("yyyy-MMM -- dd"), UnicodeString("2008-Jan 06") },
1613 { "en", false, UnicodeString("2008-Jan--07"), UnicodeString("yyyy-MMM -- dd"), UnicodeString("") },
1614 { "en", true, UnicodeString("6 Jan 08 2008"), UnicodeString("eee MMM dd yyyy"), UnicodeString("Sat Jan 08 2008") },
1615 { "en", false, UnicodeString("6 Jan 09 2008"), UnicodeString("eee MMM dd yyyy"), UnicodeString("") },
1617 { NULL
, true, UnicodeString(""), UnicodeString(""), UnicodeString("") }
1619 UErrorCode status
= U_ZERO_ERROR
;
1620 Calendar
* cal
= Calendar::createInstance(status
);
1621 if (U_FAILURE(status
)) {
1622 dataerrln(UnicodeString("FAIL: Unable to create Calendar for default timezone and locale."));
1624 cal
->setTime(july022008
, status
);
1625 const TestDateFormatLeniencyItem
* itemPtr
;
1626 for (itemPtr
= items
; itemPtr
->locale
!= NULL
; itemPtr
++ ) {
1628 Locale locale
= Locale::createFromName(itemPtr
->locale
);
1629 status
= U_ZERO_ERROR
;
1630 ParsePosition
pos(0);
1631 SimpleDateFormat
* sdmft
= new SimpleDateFormat(itemPtr
->pattern
, locale
, status
);
1632 if (U_FAILURE(status
)) {
1633 dataerrln("Unable to create SimpleDateFormat - %s", u_errorName(status
));
1636 logln("parsing " + itemPtr
->parseString
);
1637 sdmft
->setLenient(itemPtr
->leniency
);
1638 sdmft
->setBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE
, itemPtr
->leniency
, status
);
1639 sdmft
->setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC
, itemPtr
->leniency
, status
);
1640 sdmft
->setBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH
, itemPtr
->leniency
, status
);
1641 sdmft
->parse(itemPtr
->parseString
, pos
);
1644 if(pos
.getErrorIndex() > -1) {
1645 if(itemPtr
->expectedResult
.length() != 0) {
1646 errln("error: unexpected error - " + itemPtr
->parseString
+ " - error index " + pos
.getErrorIndex() +
1647 " - leniency " + itemPtr
->leniency
);
1662 UnicodeString pattern
;
1663 int initialParsePos
;
1666 void DateFormatRegressionTest::TestT10855(void) {
1667 // NOTE: these should NOT parse
1668 const T10855Data items
[] = {
1669 //parse String pattern initial parse pos
1670 // { UnicodeString("September 30, 1998"), UnicodeString("MM-dd-yyyy"), 0}, // for Apple, this can succeed
1671 { UnicodeString("123-73-1950"), UnicodeString("MM-dd-yyyy"), -1},
1672 { UnicodeString("12-23-1950"), UnicodeString("MM-dd-yyyy"), -1},
1674 { UnicodeString(""), UnicodeString(""), 0}
1676 UErrorCode status
= U_ZERO_ERROR
;
1679 while(items
[x
].pattern
.length() > 0)
1681 status
= U_ZERO_ERROR
;
1682 logln("Date to parse: \""+items
[x
].text
+"\"");
1683 logln("Starting Index: %d", items
[x
].initialParsePos
);
1685 SimpleDateFormat
dateFmt(items
[x
].pattern
, status
);
1686 if(U_FAILURE(status
)) {
1687 errcheckln(status
, "Failed dateFmt: %s", u_errorName(status
));
1691 status
= U_ZERO_ERROR
;
1693 dateFmt
.setLenient(false);
1694 dateFmt
.setTimeZone(*TimeZone::getGMT());
1696 ParsePosition
position(items
[x
].initialParsePos
);
1697 logln("set position is now: %d", position
.getIndex());
1698 UDate d
= dateFmt
.parse(items
[x
].text
, position
);
1699 if (position
.getErrorIndex() != -1 || position
.getIndex() == items
[x
].initialParsePos
) {
1700 logln("Parse Failed. ErrorIndex is %d - Index is %d", position
.getErrorIndex(), position
.getIndex());
1702 errln("Parse Succeeded...should have failed. Index is %d - ErrorIndex is %d", position
.getIndex(), position
.getErrorIndex());
1704 logln("Parsed date returns %d\n", d
);
1710 void DateFormatRegressionTest::TestT10906(void) {
1712 UErrorCode status
= U_ZERO_ERROR
;
1713 UnicodeString pattern
= "MM-dd-yyyy";
1714 UnicodeString text
= "06-10-2014";
1715 SimpleDateFormat
format(pattern
, status
);
1716 int32_t errorIdx
= 0;
1717 ParsePosition
pp(-1);
1718 format
.parse(text
, pp
);
1719 errorIdx
= pp
.getErrorIndex();
1720 if (errorIdx
== -1) {
1721 errln("failed to report invalid (negative) starting parse position");
1725 #endif /* #if !UCONFIG_NO_FORMATTING */