1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 1997-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
9 #include "unicode/utypes.h"
11 #if !UCONFIG_NO_FORMATTING
14 #include "unicode/timezone.h"
15 #include "unicode/gregocal.h"
16 #include "unicode/smpdtfmt.h"
17 #include "unicode/datefmt.h"
18 #include "unicode/simpletz.h"
19 #include "unicode/resbund.h"
22 // *****************************************************************************
23 // class DateFormatRegressionTest
24 // *****************************************************************************
26 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
29 DateFormatRegressionTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
31 // if (exec) logln((UnicodeString)"TestSuite DateFormatRegressionTest");
62 CASE(29,Test12902_yWithGregoCalInThaiLoc
)
68 default: name
= ""; break;
75 void DateFormatRegressionTest::Test4029195(void)
77 UErrorCode status
= U_ZERO_ERROR
;
79 UDate today
= Calendar::getNow();
80 logln((UnicodeString
) "today: " + today
);
82 SimpleDateFormat
*sdf
= (SimpleDateFormat
*) DateFormat::createDateInstance();
83 if (failure(status
, "SimpleDateFormat::createDateInstance")) {
88 dataerrln("Error calling DateFormat::createDateTimeInstance");
92 pat
= sdf
->toPattern(pat
);
93 logln("pattern: " + pat
);
96 FieldPosition
pos(FieldPosition::DONT_CARE
);
97 fmtd
= sdf
->format(today
, fmtd
, pos
);
98 logln("today: " + fmtd
);
100 sdf
->applyPattern("G yyyy DDD");
101 UnicodeString todayS
;
102 todayS
= sdf
->format(today
, todayS
, pos
);
103 logln("today: " + todayS
);
105 today
= sdf
->parse(todayS
, status
);
106 failure(status
, "sdf->parse");
107 logln((UnicodeString
)"today date: " + today
);
108 /*} catch(Exception e) {
109 logln("Error reparsing date: " + e.getMessage());
114 rt
= sdf
->format(sdf
->parse(todayS
, status
), rt
, pos
);
115 failure(status
, "sdf->parse");
116 logln("round trip: " + rt
);
118 errln("Fail: Want " + todayS
+ " Got " + rt
);
120 catch (ParseException e) {
131 void DateFormatRegressionTest::Test4052408(void)
134 DateFormat
*fmt
= DateFormat::createDateTimeInstance(DateFormat::SHORT
,
135 DateFormat::SHORT
, Locale::getUS());
137 dataerrln("Error calling DateFormat::createDateTimeInstance");
141 UDate dt
= date(97, UCAL_MAY
, 3, 8, 55);
143 str
= fmt
->format(dt
, str
);
146 if(str
!= "5/3/97, 8:55 AM")
147 errln("Fail: Test broken; Want 5/3/97, 8:55 AM Got " + str
);
149 UnicodeString expected
[] = {
150 (UnicodeString
) "", //"ERA_FIELD",
151 (UnicodeString
) "97", //"YEAR_FIELD",
152 (UnicodeString
) "5", //"MONTH_FIELD",
153 (UnicodeString
) "3", //"DATE_FIELD",
154 (UnicodeString
) "", //"HOUR_OF_DAY1_FIELD",
155 (UnicodeString
) "", //"HOUR_OF_DAY0_FIELD",
156 (UnicodeString
) "55", //"MINUTE_FIELD",
157 (UnicodeString
) "", //"SECOND_FIELD",
158 (UnicodeString
) "", //"MILLISECOND_FIELD",
159 (UnicodeString
) "", //"DAY_OF_WEEK_FIELD",
160 (UnicodeString
) "", //"DAY_OF_YEAR_FIELD",
161 (UnicodeString
) "", //"DAY_OF_WEEK_IN_MONTH_FIELD",
162 (UnicodeString
) "", //"WEEK_OF_YEAR_FIELD",
163 (UnicodeString
) "", //"WEEK_OF_MONTH_FIELD",
164 (UnicodeString
) "AM", //"AM_PM_FIELD",
165 (UnicodeString
) "8", //"HOUR1_FIELD",
166 (UnicodeString
) "", //"HOUR0_FIELD",
167 (UnicodeString
) "" //"TIMEZONE_FIELD"
170 //Hashtable expected;// = new Hashtable();
171 //expected.put(new LongKey(DateFormat.MONTH_FIELD), "5");
172 //expected.put(new LongKey(DateFormat.DATE_FIELD), "3");
173 //expected.put(new LongKey(DateFormat.YEAR_FIELD), "97");
174 //expected.put(new LongKey(DateFormat.HOUR1_FIELD), "8");
175 //expected.put(new LongKey(DateFormat.MINUTE_FIELD), "55");
176 //expected.put(new LongKey(DateFormat.AM_PM_FIELD), "AM");
178 //StringBuffer buf = new StringBuffer();
179 UnicodeString fieldNames
[] = {
180 (UnicodeString
) "ERA_FIELD",
181 (UnicodeString
) "YEAR_FIELD",
182 (UnicodeString
) "MONTH_FIELD",
183 (UnicodeString
) "DATE_FIELD",
184 (UnicodeString
) "HOUR_OF_DAY1_FIELD",
185 (UnicodeString
) "HOUR_OF_DAY0_FIELD",
186 (UnicodeString
) "MINUTE_FIELD",
187 (UnicodeString
) "SECOND_FIELD",
188 (UnicodeString
) "MILLISECOND_FIELD",
189 (UnicodeString
) "DAY_OF_WEEK_FIELD",
190 (UnicodeString
) "DAY_OF_YEAR_FIELD",
191 (UnicodeString
) "DAY_OF_WEEK_IN_MONTH_FIELD",
192 (UnicodeString
) "WEEK_OF_YEAR_FIELD",
193 (UnicodeString
) "WEEK_OF_MONTH_FIELD",
194 (UnicodeString
) "AM_PM_FIELD",
195 (UnicodeString
) "HOUR1_FIELD",
196 (UnicodeString
) "HOUR0_FIELD",
197 (UnicodeString
) "TIMEZONE_FIELD"
201 for(int i
= 0; i
<= 17; ++i
) {
202 FieldPosition
pos(i
);
204 fmt
->format(dt
, buf
, pos
);
205 //char[] dst = new char[pos.getEndIndex() - pos.getBeginIndex()];
207 buf
.extractBetween(pos
.getBeginIndex(), pos
.getEndIndex(), dst
);
208 UnicodeString
str(dst
);
209 logln((UnicodeString
)"" + i
+ (UnicodeString
)": " + fieldNames
[i
] +
210 (UnicodeString
)", \"" + str
+ (UnicodeString
)"\", " +
211 pos
.getBeginIndex() + (UnicodeString
)", " +
213 UnicodeString exp
= expected
[i
];
214 if((exp
.length() == 0 && str
.length() == 0) || str
== exp
)
217 errln(UnicodeString(" expected ") + exp
);
223 errln("Fail: FieldPosition not set right by DateFormat");
230 * Verify the function of the [s|g]et2DigitYearStart() API.
232 void DateFormatRegressionTest::Test4056591(void)
234 UErrorCode status
= U_ZERO_ERROR
;
237 SimpleDateFormat
*fmt
= new SimpleDateFormat(UnicodeString("yyMMdd"), Locale::getUS(), status
);
238 if (failure(status
, "new SimpleDateFormat", TRUE
)) {
242 UDate start
= date(1809-1900, UCAL_DECEMBER
, 25);
243 fmt
->set2DigitYearStart(start
, status
);
244 failure(status
, "fmt->setTwoDigitStartDate");
245 if( (fmt
->get2DigitYearStart(status
) != start
) || failure(status
, "get2DigitStartDate"))
246 errln("get2DigitYearStart broken");
248 date(1809-1900, UCAL_DECEMBER
, 25),
249 date(1909-1900, UCAL_DECEMBER
, 24),
250 date(1809-1900, UCAL_DECEMBER
, 26),
251 date(1861-1900, UCAL_DECEMBER
, 25),
254 UnicodeString strings
[] = {
255 (UnicodeString
) "091225",
256 (UnicodeString
) "091224",
257 (UnicodeString
) "091226",
258 (UnicodeString
) "611225"
262 "091225", new Date(1809-1900, Calendar.DECEMBER, 25),
263 "091224", new Date(1909-1900, Calendar.DECEMBER, 24),
264 "091226", new Date(1809-1900, Calendar.DECEMBER, 26),
265 "611225", new Date(1861-1900, Calendar.DECEMBER, 25),
268 for(int i
= 0; i
< 4; i
++) {
269 UnicodeString s
= strings
[i
];
270 UDate exp
= dates
[i
];
271 UDate got
= fmt
->parse(s
, status
);
272 failure(status
, "fmt->parse");
273 logln(s
+ " -> " + got
+ "; exp " + exp
);
275 errln("set2DigitYearStart broken");
278 catch (ParseException e) {
289 void DateFormatRegressionTest::Test4059917(void)
291 UErrorCode status
= U_ZERO_ERROR
;
293 SimpleDateFormat
*fmt
;
294 UnicodeString myDate
;
296 fmt
= new SimpleDateFormat( UnicodeString("yyyy/MM/dd"), status
);
297 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
298 myDate
= "1997/01/01";
299 aux917( fmt
, myDate
);
304 fmt
= new SimpleDateFormat( UnicodeString("yyyyMMdd"), status
);
305 if(failure(status
, "new SimpleDateFormat")) return;
307 aux917( fmt
, myDate
);
312 void DateFormatRegressionTest::aux917( SimpleDateFormat
*fmt
, UnicodeString
& str
) {
315 pat
= fmt
->toPattern(pat
);
316 logln( "==================" );
317 logln( "testIt: pattern=" + pat
+
323 ParsePosition
pos(0);
324 fmt
->parseObject( str
, o
, pos
);
325 //logln( UnicodeString("Parsed object: ") + o );
327 UErrorCode status
= U_ZERO_ERROR
;
328 UnicodeString formatted
;
329 FieldPosition
poss(FieldPosition::DONT_CARE
);
330 formatted
= fmt
->format( o
, formatted
, poss
, status
);
331 failure(status
, "fmt->format");
332 logln( "Formatted string: " + formatted
);
333 if( formatted
!= str
)
334 errln("Fail: Want " + str
+ " Got " + formatted
);
336 catch (ParseException e) {
345 void DateFormatRegressionTest::Test4060212(void)
347 UnicodeString dateString
= "1995-040.05:01:29";
349 logln( "dateString= " + dateString
);
350 logln("Using yyyy-DDD.hh:mm:ss");
351 UErrorCode status
= U_ZERO_ERROR
;
352 SimpleDateFormat
*formatter
= new SimpleDateFormat(UnicodeString("yyyy-DDD.hh:mm:ss"), status
);
353 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
354 ParsePosition
pos(0);
355 UDate myDate
= formatter
->parse( dateString
, pos
);
356 UnicodeString myString
;
357 DateFormat
*fmt
= DateFormat::createDateTimeInstance( DateFormat::FULL
,
360 dataerrln("Error calling DateFormat::createDateTimeInstance");
365 myString
= fmt
->format( myDate
, myString
);
368 Calendar
*cal
= new GregorianCalendar(status
);
369 failure(status
, "new GregorianCalendar");
370 cal
->setTime(myDate
, status
);
371 failure(status
, "cal->setTime");
372 if ((cal
->get(UCAL_DAY_OF_YEAR
, status
) != 40) || failure(status
, "cal->get"))
373 errln((UnicodeString
) "Fail: Got " + cal
->get(UCAL_DAY_OF_YEAR
, status
) +
376 // this is an odd usage of "ddd" and it doesn't
377 // work now that date values are range checked per #3579.
378 logln("Using yyyy-ddd.hh:mm:ss");
381 formatter
= new SimpleDateFormat(UnicodeString("yyyy-ddd.hh:mm:ss"), status
);
382 if(failure(status
, "new SimpleDateFormat")) return;
384 myDate
= formatter
->parse( dateString
, pos
);
385 myString
= fmt
->format( myDate
, myString
);
387 cal
->setTime(myDate
, status
);
388 failure(status
, "cal->setTime");
389 if ((cal
->get(UCAL_DAY_OF_YEAR
, status
) != 40) || failure(status
, "cal->get"))
390 errln((UnicodeString
) "Fail: Got " + cal
->get(UCAL_DAY_OF_YEAR
, status
) +
401 void DateFormatRegressionTest::Test4061287(void)
403 UErrorCode status
= U_ZERO_ERROR
;
405 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("dd/MM/yyyy"), status
);
406 if (U_FAILURE(status
)) {
407 dataerrln("Fail new SimpleDateFormat: %s", u_errorName(status
));
411 failure(status
, "new SimpleDateFormat");
413 logln(UnicodeString("") + df
->parse("35/01/1971", status
));
414 failure(status
, "df->parse(\"35/01/1971\")");
415 //logln(df.parse("35/01/1971").toString());
417 /*catch (ParseException e) {
421 df
->setLenient(FALSE
);
424 logln(UnicodeString("") + df
->parse("35/01/1971", status
));
425 if(U_FAILURE(status
))
427 //logln(df.parse("35/01/1971").toString());
428 //} catch (ParseException e) {ok=TRUE;}
430 errln("Fail: Lenient not working");
437 void DateFormatRegressionTest::Test4065240(void)
440 DateFormat
*shortdate
, *fulldate
;
441 UnicodeString strShortDate
, strFullDate
;
442 Locale saveLocale
= Locale::getDefault();
443 TimeZone
*saveZone
= TimeZone::createDefault();
445 UErrorCode status
= U_ZERO_ERROR
;
447 Locale
*curLocale
= new Locale("de","DE");
448 Locale::setDefault(*curLocale
, status
);
449 failure(status
, "Locale::setDefault");
450 // {sfb} adoptDefault instead of setDefault
451 //TimeZone::setDefault(TimeZone::createTimeZone("EST"));
452 TimeZone::adoptDefault(TimeZone::createTimeZone("EST"));
453 curDate
= date(98, 0, 1);
454 shortdate
= DateFormat::createDateInstance(DateFormat::SHORT
);
455 if (shortdate
== NULL
){
456 dataerrln("Error calling DateFormat::createDateInstance");
460 fulldate
= DateFormat::createDateTimeInstance(DateFormat::LONG
, DateFormat::LONG
);
461 if (fulldate
== NULL
){
462 dataerrln("Error calling DateFormat::createDateTimeInstance");
465 strShortDate
= "The current date (short form) is ";
467 temp
= shortdate
->format(curDate
, temp
);
468 strShortDate
+= temp
;
469 strFullDate
= "The current date (long form) is ";
471 fulldate
->format(curDate
, temp2
);
472 strFullDate
+= temp2
;
477 // {sfb} What to do with resource bundle stuff?????
479 // Check to see if the resource is present; if not, we can't test
480 ResourceBundle
*bundle
= new ResourceBundle(
481 NULL
, *curLocale
, status
);
482 failure(status
, "new ResourceBundle");
483 //(UnicodeString) "java.text.resources.DateFormatZoneData", curLocale);
485 // {sfb} API change to ResourceBundle -- add getLocale()
486 /*if (bundle->getLocale().getLanguage(temp) == UnicodeString("de")) {
487 // UPDATE THIS AS ZONE NAME RESOURCE FOR <EST> in de_DE is updated
488 if (!strFullDate.endsWith(UnicodeString("GMT-05:00")))
489 errln("Fail: Want GMT-05:00");
492 logln("*** TEST COULD NOT BE COMPLETED BECAUSE DateFormatZoneData ***");
493 logln("*** FOR LOCALE de OR de_DE IS MISSING ***");
497 Locale::setDefault(saveLocale
, status
);
498 failure(status
, "Locale::setDefault");
499 TimeZone::setDefault(*saveZone
);
509 DateFormat.equals is too narrowly defined. As a result, MessageFormat
510 does not work correctly. DateFormat.equals needs to be written so
511 that the Calendar sub-object is not compared using Calendar.equals,
512 but rather compared for equivalency. This may necessitate adding a
513 (package private) method to Calendar to test for equivalency.
515 Currently this bug breaks MessageFormat.toPattern
520 void DateFormatRegressionTest::Test4071441(void)
522 DateFormat
*fmtA
= DateFormat::createInstance();
523 DateFormat
*fmtB
= DateFormat::createInstance();
525 if (fmtA
== NULL
|| fmtB
== NULL
){
526 dataerrln("Error calling DateFormat::createInstance");
532 // {sfb} Is it OK to cast away const here?
533 Calendar
*calA
= (Calendar
*) fmtA
->getCalendar();
534 Calendar
*calB
= (Calendar
*) fmtB
->getCalendar();
536 errln("Couldn't get proper calendars, exiting");
541 UDate epoch
= date(0, 0, 0);
542 UDate xmas
= date(61, UCAL_DECEMBER
, 25);
544 UErrorCode status
= U_ZERO_ERROR
;
545 calA
->setTime(epoch
, status
);
546 failure(status
, "calA->setTime");
547 calB
->setTime(epoch
, status
);
548 failure(status
, "calB->setTime");
550 errln("Fail: Can't complete test; Calendar instances unequal");
552 errln("Fail: DateFormat unequal when Calendars equal");
553 calB
->setTime(xmas
, status
);
554 failure(status
, "calB->setTime");
556 errln("Fail: Can't complete test; Calendar instances equal");
558 errln("Fail: DateFormat unequal when Calendars equivalent");
560 logln("DateFormat.equals ok");
566 /* The java.text.DateFormat.parse(String) method expects for the
567 US locale a string formatted according to mm/dd/yy and parses it
570 When given a string mm/dd/yyyy [sic] it only parses up to the first
571 two y's, typically resulting in a date in the year 1919.
573 Please extend the parsing method(s) to handle strings with
574 four-digit year values (probably also applicable to various
579 void DateFormatRegressionTest::Test4073003(void)
582 UErrorCode ec
= U_ZERO_ERROR
;
583 SimpleDateFormat
fmt("MM/dd/yy", Locale::getUK(), ec
);
585 dataerrln("FAIL: SimpleDateFormat constructor - %s", u_errorName(ec
));
588 UnicodeString tests
[] = {
589 (UnicodeString
) "12/25/61",
590 (UnicodeString
) "12/25/1961",
591 (UnicodeString
) "4/3/2010",
592 (UnicodeString
) "4/3/10"
594 UErrorCode status
= U_ZERO_ERROR
;
595 for(int i
= 0; i
< 4; i
+=2) {
596 UDate d
= fmt
.parse(tests
[i
], status
);
597 failure(status
, "fmt.parse");
598 UDate dd
= fmt
.parse(tests
[i
+1], status
);
599 failure(status
, "fmt.parse");
601 s
= fmt
.format(d
, s
);
603 ss
= fmt
.format(dd
, ss
);
605 errln((UnicodeString
) "Fail: " + d
+ " != " + dd
);
607 errln((UnicodeString
)"Fail: " + s
+ " != " + ss
);
608 logln("Ok: " + s
+ " " + d
);
615 void DateFormatRegressionTest::Test4089106(void)
617 TimeZone
*def
= TimeZone::createDefault();
619 TimeZone
*z
= new SimpleTimeZone((int)(1.25 * 3600000), "FAKEZONE");
620 TimeZone::setDefault(*z
);
621 UErrorCode status
= U_ZERO_ERROR
;
622 SimpleDateFormat
*f
= new SimpleDateFormat(status
);
623 if(U_FAILURE(status
)) {
624 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
630 failure(status
, "new SimpleDateFormat");
631 if (f
->getTimeZone()!= *z
)
632 errln("Fail: SimpleTimeZone should use TimeZone.getDefault()");
636 TimeZone::setDefault(*def
);
648 // {sfb} not applicable in C++??
650 void DateFormatRegressionTest::Test4100302(void)
652 /* Locale locales [] = {
654 Locale::CANADA_FRENCH,
669 Locale::SIMPLIFIED_CHINESE,
671 Locale::TRADITIONAL_CHINESE,
677 for(int i = 0; i < 21; i++) {
679 Format *format = DateFormat::createDateTimeInstance(DateFormat::FULL,
680 DateFormat::FULL, locales[i]);
683 ByteArrayOutputStream baos = new ByteArrayOutputStream();
684 ObjectOutputStream oos = new ObjectOutputStream(baos);
686 oos.writeObject(format);
690 bytes = baos.toByteArray();
692 ObjectInputStream ois =
693 new ObjectInputStream(new ByteArrayInputStream(bytes));
695 if (!format.equals(ois.readObject())) {
697 logln("DateFormat instance for locale " +
698 locales[i] + " is incorrectly serialized/deserialized.");
700 logln("DateFormat instance for locale " +
701 locales[i] + " is OKAY.");
704 if (!pass) errln("Fail: DateFormat serialization/equality bug");
706 catch (IOException e) {
710 catch (ClassNotFoundException e) {
719 void DateFormatRegressionTest::Test4101483(void)
721 UErrorCode status
= U_ZERO_ERROR
;
722 SimpleDateFormat
*sdf
= new SimpleDateFormat(UnicodeString("z"), Locale::getUS(), status
);
723 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
724 FieldPosition
fp(UDAT_TIMEZONE_FIELD
);
725 //Date d = date(9234567890L);
726 UDate d
= 9234567890.0;
727 //StringBuffer buf = new StringBuffer("");
729 sdf
->format(d
, buf
, fp
);
730 //logln(sdf.format(d, buf, fp).toString());
731 logln(dateToString(d
) + " => " + buf
);
732 logln(UnicodeString("beginIndex = ") + fp
.getBeginIndex());
733 logln(UnicodeString("endIndex = ") + fp
.getEndIndex());
734 if (fp
.getBeginIndex() == fp
.getEndIndex())
735 errln("Fail: Empty field");
743 * This bug really only works in Locale.US, since that's what the locale
744 * used for Date.toString() is. Bug 4138203 reports that it fails on Korean
745 * NT; it would actually have failed on any non-US locale. Now it should
746 * work on all locales.
748 void DateFormatRegressionTest::Test4103340(void)
750 UErrorCode status
= U_ZERO_ERROR
;
752 // choose a date that is the FIRST of some month
753 // and some arbitrary time
754 UDate d
= date(97, 3, 1, 1, 1, 1);
755 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("MMMM"), Locale::getUS(), status
);
756 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
759 s
= dateToString(d
, s
);
761 FieldPosition
pos(FieldPosition::DONT_CARE
);
762 s2
= df
->format(d
, s2
, pos
);
765 UnicodeString substr
;
766 s2
.extract(0,2, substr
);
767 if (s
.indexOf(substr
) == -1)
768 errln("Months should match");
776 void DateFormatRegressionTest::Test4103341(void)
778 TimeZone
*saveZone
=TimeZone::createDefault();
781 // {sfb} changed from setDefault to adoptDefault
782 TimeZone::adoptDefault(TimeZone::createTimeZone("CST"));
783 UErrorCode status
= U_ZERO_ERROR
;
784 SimpleDateFormat
*simple
= new SimpleDateFormat(UnicodeString("MM/dd/yyyy HH:mm"), status
);
785 if(U_FAILURE(status
)) {
786 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
790 failure(status
, "new SimpleDateFormat");
791 TimeZone
*temp
= TimeZone::createDefault();
792 if(simple
->getTimeZone() != *temp
)
793 errln("Fail: SimpleDateFormat not using default zone");
796 TimeZone::adoptDefault(saveZone
);
806 void DateFormatRegressionTest::Test4104136(void)
808 UErrorCode status
= U_ZERO_ERROR
;
809 SimpleDateFormat
*sdf
= new SimpleDateFormat(status
);
810 if(U_FAILURE(status
)) {
811 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
815 if(failure(status
, "new SimpleDateFormat")) return;
816 UnicodeString pattern
= "'time' hh:mm";
817 sdf
->applyPattern(pattern
);
818 logln("pattern: \"" + pattern
+ "\"");
820 UnicodeString strings
[] = {
821 (UnicodeString
)"time 10:30",
822 (UnicodeString
) "time 10:x",
823 (UnicodeString
) "time 10x"
826 ParsePosition ppos
[] = {
833 date(70, UCAL_JANUARY
, 1, 10, 30),
839 "time 10:30", new ParsePosition(10), new Date(70, Calendar.JANUARY, 1, 10, 30),
840 "time 10:x", new ParsePosition(0), null,
841 "time 10x", new ParsePosition(0), null,
844 for(int i
= 0; i
< 3; i
++) {
845 UnicodeString text
= strings
[i
];
846 ParsePosition finish
= ppos
[i
];
847 UDate exp
= dates
[i
];
849 ParsePosition
pos(0);
850 UDate d
= sdf
->parse(text
, pos
);
851 logln(" text: \"" + text
+ "\"");
852 logln(" index: %d", pos
.getIndex());
853 logln((UnicodeString
) " result: " + d
);
854 if(pos
.getIndex() != finish
.getIndex())
855 errln(UnicodeString("Fail: Expected pos ") + finish
.getIndex());
856 if (! ((d
== 0 && exp
== -1) || (d
== exp
)))
857 errln((UnicodeString
) "Fail: Expected result " + exp
);
866 * According to the bug report, this test should throw a
867 * StringIndexOutOfBoundsException during the second parse. However,
870 void DateFormatRegressionTest::Test4104522(void)
872 UErrorCode status
= U_ZERO_ERROR
;
874 SimpleDateFormat
*sdf
= new SimpleDateFormat(status
);
875 if(U_FAILURE(status
)) {
876 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
880 failure(status
, "new SimpleDateFormat");
881 UnicodeString pattern
= "'time' hh:mm";
882 sdf
->applyPattern(pattern
);
883 logln("pattern: \"" + pattern
+ "\"");
887 UnicodeString text
= "time ";
888 UDate dt
= sdf
->parse(text
, pp
);
889 logln(" text: \"" + text
+ "\"" +
895 dt
= sdf
->parse(text
, pp
);
896 logln(" text: \"" + text
+ "\"" +
905 void DateFormatRegressionTest::Test4106807(void)
908 DateFormat
*df
= DateFormat::createDateTimeInstance();
910 UErrorCode status
= U_ZERO_ERROR
;
911 SimpleDateFormat
*sdfs
[] = {
912 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss"), status
),
913 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'Z'"), status
),
914 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss''"), status
),
915 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'a''a'"), status
),
916 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss %"), status
)
918 if(U_FAILURE(status
)) {
919 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
928 failure(status
, "new SimpleDateFormat");
930 UnicodeString strings
[] = {
931 (UnicodeString
) "19980211140000",
932 (UnicodeString
) "19980211140000",
933 (UnicodeString
) "19980211140000",
934 (UnicodeString
) "19980211140000a",
935 (UnicodeString
) "19980211140000 "
939 new SimpleDateFormat("yyyyMMddHHmmss"), "19980211140000",
940 new SimpleDateFormat("yyyyMMddHHmmss'Z'"), "19980211140000",
941 new SimpleDateFormat("yyyyMMddHHmmss''"), "19980211140000",
942 new SimpleDateFormat("yyyyMMddHHmmss'a''a'"), "19980211140000a",
943 new SimpleDateFormat("yyyyMMddHHmmss %"), "19980211140000 ",
945 GregorianCalendar
*gc
= new GregorianCalendar(status
);
946 failure(status
, "new GregorianCalendar");
947 TimeZone
*timeZone
= TimeZone::createDefault();
949 TimeZone
*gmt
= timeZone
->clone();
951 gmt
->setRawOffset(0);
953 for(int32_t i
= 0; i
< 5; i
++) {
954 SimpleDateFormat
*format
= sdfs
[i
];
955 UnicodeString dateString
= strings
[i
];
957 format
->setTimeZone(*gmt
);
958 dt
= format
->parse(dateString
, status
);
959 // {sfb} some of these parses will fail purposely
960 if(U_FAILURE(status
))
962 status
= U_ZERO_ERROR
;
964 FieldPosition
pos(FieldPosition::DONT_CARE
);
965 fmtd
= df
->format(dt
, fmtd
, pos
);
967 //logln(df->format(dt));
968 gc
->setTime(dt
, status
);
969 failure(status
, "gc->getTime");
970 logln(UnicodeString("") + gc
->get(UCAL_ZONE_OFFSET
, status
));
971 failure(status
, "gc->get");
973 s
= format
->format(dt
, s
, pos
);
976 catch (ParseException e) {
977 logln("No way Jose");
983 for(int32_t j
= 0; j
< 5; j
++)
990 Synopsis: Chinese time zone CTT is not recogonized correctly.
991 Description: Platform Chinese Windows 95 - ** Time zone set to CST **
997 // {sfb} what to do with this one ??
998 void DateFormatRegressionTest::Test4108407(void)
1000 /*long l = System.currentTimeMillis();
1001 logln("user.timezone = " + System.getProperty("user.timezone", "?"));
1002 logln("Time Zone :" +
1003 DateFormat.getDateInstance().getTimeZone().getID());
1004 logln("Default format :" +
1005 DateFormat.getDateInstance().format(new Date(l)));
1006 logln("Full format :" +
1007 DateFormat.getDateInstance(DateFormat.FULL).format(new
1009 logln("*** Set host TZ to CST ***");
1010 logln("*** THE RESULTS OF THIS TEST MUST BE VERIFIED MANUALLY ***");*/
1015 * SimpleDateFormat won't parse "GMT"
1017 void DateFormatRegressionTest::Test4134203(void)
1019 UErrorCode status
= U_ZERO_ERROR
;
1020 UnicodeString dateFormat
= "MM/dd/yy HH:mm:ss zzz";
1021 SimpleDateFormat
*fmt
= new SimpleDateFormat(dateFormat
, status
);
1022 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
1023 ParsePosition
p0(0);
1024 UDate d
= fmt
->parse("01/22/92 04:52:00 GMT", p0
);
1025 logln(dateToString(d
));
1026 if(p0
== ParsePosition(0))
1027 errln("Fail: failed to parse 'GMT'");
1028 // In the failure case an exception is thrown by parse();
1029 // if no exception is thrown, the test passes.
1036 * SimpleDateFormat incorrect handling of 2 single quotes in format()
1038 void DateFormatRegressionTest::Test4151631(void)
1040 UnicodeString pattern
= "'TO_DATE('''dd'-'MM'-'yyyy HH:mm:ss''' , ''DD-MM-YYYY HH:MI:SS'')'";
1041 logln("pattern=" + pattern
);
1042 UErrorCode status
= U_ZERO_ERROR
;
1043 SimpleDateFormat
*format
= new SimpleDateFormat(pattern
, Locale::getUS(), status
);
1044 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
1045 UnicodeString result
;
1046 FieldPosition
pos(FieldPosition::DONT_CARE
);
1047 result
= format
->format(date(1998-1900, UCAL_JUNE
, 30, 13, 30, 0), result
, pos
);
1048 if (result
!= "TO_DATE('30-06-1998 13:30:00' , 'DD-MM-YYYY HH:MI:SS')") {
1049 errln("Fail: result=" + result
);
1052 logln("Pass: result=" + result
);
1060 * 'z' at end of date format throws index exception in SimpleDateFormat
1061 * CANNOT REPRODUCE THIS BUG ON 1.2FCS
1063 void DateFormatRegressionTest::Test4151706(void)
1065 UnicodeString
dateString("Thursday, 31-Dec-98 23:00:00 GMT");
1066 UErrorCode status
= U_ZERO_ERROR
;
1067 SimpleDateFormat
fmt(UnicodeString("EEEE, dd-MMM-yy HH:mm:ss z"), Locale::getUS(), status
);
1068 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
1070 UDate d
= fmt
.parse(dateString
, status
);
1071 failure(status
, "fmt->parse");
1072 // {sfb} what about next two lines?
1073 //if (d.getTime() != Date.UTC(1998-1900, Calendar.DECEMBER, 31, 23, 0, 0))
1074 // errln("Incorrect value: " + d);
1075 /*} catch (Exception e) {
1076 errln("Fail: " + e);
1079 FieldPosition
pos(FieldPosition::DONT_CARE
);
1080 logln(dateString
+ " -> " + fmt
.format(d
, temp
, pos
));
1085 * Cannot reproduce this bug under 1.2 FCS -- it may be a convoluted duplicate
1086 * of some other bug that has been fixed.
1089 DateFormatRegressionTest::Test4162071(void)
1091 UnicodeString
dateString("Thu, 30-Jul-1999 11:51:14 GMT");
1092 UnicodeString
format("EEE', 'dd-MMM-yyyy HH:mm:ss z"); // RFC 822/1123
1093 UErrorCode status
= U_ZERO_ERROR
;
1094 SimpleDateFormat
df(format
, Locale::getUS(), status
);
1095 if(U_FAILURE(status
)) {
1096 dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status
));
1101 UDate x
= df
.parse(dateString
, status
);
1102 if(U_SUCCESS(status
))
1103 logln("Parse format \"" + format
+ "\" ok");
1105 errln("Parse format \"" + format
+ "\" failed.");
1107 FieldPosition
pos(FieldPosition::DONT_CARE
);
1108 logln(dateString
+ " -> " + df
.format(x
, temp
, pos
));
1109 //} catch (Exception e) {
1110 // errln("Parse format \"" + format + "\" failed.");
1115 * DateFormat shouldn't parse year "-1" as a two-digit year (e.g., "-1" -> 1999).
1117 void DateFormatRegressionTest::Test4182066(void) {
1118 UErrorCode status
= U_ZERO_ERROR
;
1119 SimpleDateFormat
fmt("MM/dd/yy", Locale::getUS(), status
);
1120 SimpleDateFormat
dispFmt("MMM dd yyyy GG", Locale::getUS(), status
);
1121 if (U_FAILURE(status
)) {
1122 dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status
));
1126 /* We expect 2-digit year formats to put 2-digit years in the right
1127 * window. Out of range years, that is, anything less than "00" or
1128 * greater than "99", are treated as literal years. So "1/2/3456"
1129 * becomes 3456 AD. Likewise, "1/2/-3" becomes -3 AD == 2 BC.
1131 const char* STRINGS
[] = {
1141 int32_t STRINGS_COUNT
= UPRV_LENGTHOF(STRINGS
);
1142 UDate FAIL_DATE
= (UDate
) 0;
1144 date(2000-1900, UCAL_FEBRUARY
, 29),
1145 date(2001-1900, UCAL_JANUARY
, 23),
1146 date( -1-1900, UCAL_APRIL
, 5),
1147 date( -9-1900, UCAL_JANUARY
, 23),
1148 date(1314-1900, UCAL_NOVEMBER
, 12),
1149 date( 1-1900, UCAL_OCTOBER
, 31),
1150 FAIL_DATE
, // "+1" isn't recognized by US NumberFormat
1151 date( 1-1900, UCAL_SEPTEMBER
,12),
1156 for (int32_t i
=0; i
<STRINGS_COUNT
; ++i
) {
1157 UnicodeString
str(STRINGS
[i
]);
1158 UDate expected
= DATES
[i
];
1159 status
= U_ZERO_ERROR
;
1160 UDate actual
= fmt
.parse(str
, status
);
1161 if (U_FAILURE(status
)) {
1164 UnicodeString actStr
;
1165 if (actual
== FAIL_DATE
) {
1166 actStr
.append("null");
1169 ((DateFormat
*)&dispFmt
)->format(actual
, actStr
);
1172 if (expected
== actual
) {
1173 out
.append(str
+ " => " + actStr
+ "\n");
1175 UnicodeString expStr
;
1176 if (expected
== FAIL_DATE
) {
1177 expStr
.append("null");
1180 ((DateFormat
*)&dispFmt
)->format(expected
, expStr
);
1182 out
.append("FAIL: " + str
+ " => " + actStr
1183 + ", expected " + expStr
+ "\n");
1195 * j32 {JDK Bug 4210209 4209272}
1196 * DateFormat cannot parse Feb 29 2000 when setLenient(false)
1199 DateFormatRegressionTest::Test4210209(void) {
1200 UErrorCode status
= U_ZERO_ERROR
;
1201 UnicodeString
pattern("MMM d, yyyy");
1202 SimpleDateFormat
sfmt(pattern
, Locale::getUS(), status
);
1203 SimpleDateFormat
sdisp("MMM dd yyyy GG", Locale::getUS(), status
);
1204 DateFormat
& fmt
= *(DateFormat
*)&sfmt
; // Yuck: See j25
1205 DateFormat
& disp
= *(DateFormat
*)&sdisp
; // Yuck: See j25
1206 if (U_FAILURE(status
)) {
1207 dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status
));
1210 Calendar
* calx
= (Calendar
*)fmt
.getCalendar(); // cast away const!
1211 calx
->setLenient(FALSE
);
1212 UDate d
= date(2000-1900, UCAL_FEBRUARY
, 29);
1213 UnicodeString s
, ss
;
1215 logln(disp
.format(d
, ss
.remove()) + " f> " + pattern
+
1216 " => \"" + s
+ "\"");
1217 ParsePosition
pos(0);
1218 d
= fmt
.parse(s
, pos
);
1219 logln(UnicodeString("\"") + s
+ "\" p> " + pattern
+
1220 " => " + disp
.format(d
, ss
.remove()));
1221 logln(UnicodeString("Parse pos = ") + pos
.getIndex() +
1222 ", error pos = " + pos
.getErrorIndex());
1223 if (pos
.getErrorIndex() != -1) {
1224 errln(UnicodeString("FAIL: Error index should be -1"));
1227 // The underlying bug is in GregorianCalendar. If the following lines
1228 // succeed, the bug is fixed. If the bug isn't fixed, they will throw
1230 GregorianCalendar
cal(status
);
1231 if (U_FAILURE(status
)) {
1232 errln("FAIL: Unable to create Calendar");
1236 cal
.setLenient(FALSE
);
1237 cal
.set(2000, UCAL_FEBRUARY
, 29); // This should work!
1238 logln(UnicodeString("Attempt to set Calendar to Feb 29 2000: ") +
1239 disp
.format(cal
.getTime(status
), ss
.remove()));
1240 if (U_FAILURE(status
)) {
1241 errln("FAIL: Unable to set Calendar to Feb 29 2000");
1245 void DateFormatRegressionTest::Test714(void)
1248 UDate
d(978103543000.);
1249 DateFormat
*fmt
= DateFormat::createDateTimeInstance(DateFormat::NONE
,
1253 dataerrln("Error calling DateFormat::createDateTimeInstance");
1258 UnicodeString tests
=
1259 (UnicodeString
) "7:25:43 AM" ;
1260 UErrorCode status
= U_ZERO_ERROR
;
1262 if(U_FAILURE(status
))
1264 errln((UnicodeString
) "Fail, errmsg " + u_errorName(status
));
1270 errln((UnicodeString
) "Fail: " + s
+ " != " + tests
);
1274 logln("OK: " + s
+ " == " + tests
);
1280 class Test1684Data
{
1290 UnicodeString normalized
;
1292 Test1684Data(int32_t xyear
, int32_t xmonth
, int32_t xdate
,
1293 int32_t xwomyear
, int32_t xwommon
, int32_t xwom
, int32_t xdow
,
1294 const char *xdata
, const char *xnormalized
) :
1303 normalized((xnormalized
==NULL
)?xdata
:xnormalized
,"")
1307 void DateFormatRegressionTest::Test1684(void)
1309 // July 2001 August 2001 January 2002
1310 // Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1311 // 1 2 3 4 5 6 7 1 2 3 4 1 2 3 4 5
1312 // 8 9 10 11 12 13 14 5 6 7 8 9 10 11 6 7 8 9 10 11 12
1313 // 15 16 17 18 19 20 21 12 13 14 15 16 17 18 13 14 15 16 17 18 19
1314 // 22 23 24 25 26 27 28 19 20 21 22 23 24 25 20 21 22 23 24 25 26
1315 // 29 30 31 26 27 28 29 30 31 27 28 29 30 31
1316 Test1684Data
*tests
[] = {
1317 new Test1684Data(2001, 8, 6, 2001,8,2,UCAL_MONDAY
, "2001 08 02 Mon", NULL
),
1318 new Test1684Data(2001, 8, 7, 2001,8,2,UCAL_TUESDAY
, "2001 08 02 Tue", NULL
),
1319 new Test1684Data(2001, 8, 5,/*12,*/ 2001,8,2,UCAL_SUNDAY
, "2001 08 02 Sun", NULL
),
1320 new Test1684Data(2001, 8,6, /*7, 30,*/ 2001,7,6,UCAL_MONDAY
, "2001 07 06 Mon", "2001 08 02 Mon"),
1321 new Test1684Data(2001, 8,7, /*7, 31,*/ 2001,7,6,UCAL_TUESDAY
, "2001 07 06 Tue", "2001 08 02 Tue"),
1322 new Test1684Data(2001, 8, 5, 2001,7,6,UCAL_SUNDAY
, "2001 07 06 Sun", "2001 08 02 Sun"),
1323 new Test1684Data(2001, 7, 30, 2001,8,1,UCAL_MONDAY
, "2001 08 01 Mon", "2001 07 05 Mon"),
1324 new Test1684Data(2001, 7, 31, 2001,8,1,UCAL_TUESDAY
, "2001 08 01 Tue", "2001 07 05 Tue"),
1325 new Test1684Data(2001, 7,29, /*8, 5,*/ 2001,8,1,UCAL_SUNDAY
, "2001 08 01 Sun", "2001 07 05 Sun"),
1326 new Test1684Data(2001, 12, 31, 2001,12,6,UCAL_MONDAY
, "2001 12 06 Mon", NULL
),
1327 new Test1684Data(2002, 1, 1, 2002,1,1,UCAL_TUESDAY
, "2002 01 01 Tue", NULL
),
1328 new Test1684Data(2002, 1, 2, 2002,1,1,UCAL_WEDNESDAY
, "2002 01 01 Wed", NULL
),
1329 new Test1684Data(2002, 1, 3, 2002,1,1,UCAL_THURSDAY
, "2002 01 01 Thu", NULL
),
1330 new Test1684Data(2002, 1, 4, 2002,1,1,UCAL_FRIDAY
, "2002 01 01 Fri", NULL
),
1331 new Test1684Data(2002, 1, 5, 2002,1,1,UCAL_SATURDAY
, "2002 01 01 Sat", NULL
),
1332 new Test1684Data(2001,12,30, /*2002, 1, 6,*/ 2002,1,1,UCAL_SUNDAY
, "2002 01 01 Sun", "2001 12 06 Sun")
1335 #define kTest1684Count UPRV_LENGTHOF(tests)
1337 int32_t pass
= 0, error
= 0, warning
= 0;
1340 UErrorCode status
= U_ZERO_ERROR
;
1341 UnicodeString
pattern("yyyy MM WW EEE","");
1342 Calendar
*cal
= new GregorianCalendar(status
);
1343 SimpleDateFormat
*sdf
= new SimpleDateFormat(pattern
,status
);
1344 if (U_FAILURE(status
)) {
1345 dataerrln("Error constructing SimpleDateFormat");
1346 for(i
=0;i
<kTest1684Count
;i
++) {
1353 cal
->setFirstDayOfWeek(UCAL_SUNDAY
);
1354 cal
->setMinimalDaysInFirstWeek(1);
1356 sdf
->adoptCalendar(cal
);
1358 cal
= sdf
->getCalendar()->clone(); // sdf may have deleted calendar
1360 if(!cal
|| !sdf
|| U_FAILURE(status
)) {
1361 errln(UnicodeString("Error setting up test: ") + u_errorName(status
));
1364 for (i
= 0; i
< kTest1684Count
; ++i
) {
1365 Test1684Data
&test
= *(tests
[i
]);
1366 logln(UnicodeString("#") + i
+ UnicodeString("\n-----\nTesting round trip of ") + test
.year
+
1367 " " + (test
.month
+ 1) +
1369 " (written as) " + test
.data
);
1372 cal
->set(test
.year
, test
.month
, test
.date
);
1373 UDate ms
= cal
->getTime(status
);
1376 cal
->set(UCAL_YEAR
, test
.womyear
);
1377 cal
->set(UCAL_MONTH
, test
.wommon
);
1378 cal
->set(UCAL_WEEK_OF_MONTH
, test
.wom
);
1379 cal
->set(UCAL_DAY_OF_WEEK
, test
.dow
);
1380 UDate ms2
= cal
->getTime(status
);
1383 errln((UnicodeString
)"\nError: GregorianUCAL_DOM gave " + ms
+
1384 "\n GregorianUCAL_WOM gave " + ms2
);
1390 ms2
= sdf
->parse(test
.data
, status
);
1391 if(U_FAILURE(status
)) {
1392 errln("parse exception: " + UnicodeString(u_errorName(status
)));
1396 errln((UnicodeString
)"\nError: GregorianCalendar gave " + ms
+
1397 "\n SimpleDateFormat.parse gave " + ms2
);
1403 UnicodeString result
;
1404 sdf
->format(ms
, result
);
1405 if (result
!= test
.normalized
) {
1406 errln("\nWarning: format of '" + test
.data
+ "' gave" +
1407 "\n '" + result
+ "'" +
1408 "\n expected '" + test
.normalized
+ "'");
1415 ms3
= sdf
->parse(result
, status
);
1416 if(U_FAILURE(status
)) {
1417 errln("parse exception 2: " + (UnicodeString
)u_errorName(status
));
1422 errln((UnicodeString
)"\nError: Re-parse of '" + result
+ "' gave time of " +
1431 = UnicodeString("Passed: ") + pass
+ ", Warnings: " + warning
+ ", Errors: " + error
;
1438 for(i
=0;i
<kTest1684Count
;i
++) {
1445 void DateFormatRegressionTest::Test5554(void)
1447 UErrorCode status
= U_ZERO_ERROR
;
1448 UnicodeString
pattern("Z","");
1449 UnicodeString
newfoundland("Canada/Newfoundland", "");
1450 TimeZone
*zone
= TimeZone::createTimeZone(newfoundland
);
1451 Calendar
*cal
= new GregorianCalendar(zone
, status
);
1452 SimpleDateFormat
*sdf
= new SimpleDateFormat(pattern
,status
);
1453 if (U_FAILURE(status
)) {
1454 dataerrln("Error constructing SimpleDateFormat");
1459 cal
->set(2007, 1, 14);
1460 UDate date
= cal
->getTime(status
);
1461 if (U_FAILURE(status
)) {
1462 errln("Error getting time to format");
1465 sdf
->adoptCalendar(cal
);
1466 UnicodeString result
;
1467 UnicodeString
correct("-0330", "");
1468 sdf
->format(date
, result
);
1469 if (result
!= correct
) {
1470 errln("\nError: Newfoundland Z of Jan 14, 2007 gave '" + result
+ "', expected '" + correct
+ "'");
1475 void DateFormatRegressionTest::Test9237(void)
1477 UErrorCode status
= U_ZERO_ERROR
;
1478 UnicodeString
pattern("VVVV");
1480 SimpleDateFormat
fmt(pattern
, status
); // default locale
1481 SimpleDateFormat
fmtDE(pattern
, Locale("de_DE"), status
);
1482 if (U_FAILURE(status
)) {
1483 dataerrln("Error constructing SimpleDateFormat");
1488 SimpleDateFormat
fmtCopyDE(fmtDE
);
1489 UnicodeString resDE
, resCopyDE
;
1491 fmtDE
.format(0.0, resDE
);
1492 fmtCopyDE
.format(0.0, resCopyDE
);
1494 if (resDE
!= resCopyDE
) {
1495 errln(UnicodeString("Error: different result by the copied instance - org:") + resDE
+ " copy:" + resCopyDE
);
1498 // test for assignment operator
1501 UnicodeString resAssigned
;
1502 fmt
.format(0.0, resAssigned
);
1504 if (resDE
!= resAssigned
) {
1505 errln(UnicodeString("Error: different results by the assigned instance - org:") + resDE
+ " assigned:" + resAssigned
);
1509 void DateFormatRegressionTest::TestParsing(void) {
1510 UErrorCode status
= U_ZERO_ERROR
;
1511 UnicodeString
pattern("EEE-WW-MMMM-yyyy");
1512 UnicodeString
text("mon-02-march-2011");
1513 int32_t expectedDay
= 7;
1515 SimpleDateFormat
format(pattern
, status
);
1516 if (U_FAILURE(status
)) {
1517 dataerrln("Unable to create SimpleDateFormat - %s", u_errorName(status
));
1521 Calendar
*cal
= new GregorianCalendar(status
);
1522 if (cal
== NULL
|| U_FAILURE(status
)) {
1523 errln("Unable to create calendar - %s", u_errorName(status
));
1527 ParsePosition
pos(0);
1528 format
.parse(text
, *cal
, pos
);
1530 if (cal
->get(UCAL_DAY_OF_MONTH
, status
) != expectedDay
) {
1531 errln("Parsing failed: day of month should be '7' with pattern: \"" + pattern
+ "\" for text: \"" + text
+ "\"");
1537 void DateFormatRegressionTest::Test12902_yWithGregoCalInThaiLoc(void) {
1538 UErrorCode status
= U_ZERO_ERROR
;
1539 UDate testDate
= 43200000.0; // 1970-Jan-01 12:00 GMT
1540 const char* skeleton
= "y";
1541 // Note that in locale "th", the availableFormats for skeleton "y" differ by calendar:
1542 // for buddhist (default calendar): y{"G y"}
1543 // for gregorian: y{"y"}
1544 const char* expectFormat
= "1970"; // format for skeleton y in Thai locale with Gregorian calendar
1545 UnicodeString getFmtStr
;
1547 const TimeZone
* gmtZone
= TimeZone::getGMT();
1548 LocalPointer
<GregorianCalendar
> pureGregoCal(new GregorianCalendar(*gmtZone
, Locale::getEnglish(), status
));
1549 if (U_FAILURE(status
)) {
1550 dataerrln("Fail in new GregorianCalendar(GMT, en): %s", u_errorName(status
));
1553 LocalPointer
<DateFormat
> df1(DateFormat::createInstanceForSkeleton(pureGregoCal
.orphan(), skeleton
, "th", status
));
1554 if (U_FAILURE(status
)) {
1555 dataerrln("Fail in DateFormat::createInstanceForSkeleton for th with Grego cal: %s", u_errorName(status
));
1558 status
= U_ZERO_ERROR
;
1560 getFmtStr
= df1
->format(testDate
, getFmtStr
);
1561 if (U_FAILURE(status
)) {
1562 errln("Fail in DateFormat::format for th with Grego cal: %s", u_errorName(status
));
1563 } else if (getFmtStr
!= expectFormat
) {
1565 getFmtStr
.extract(0, getFmtStr
.length(), getFormat
, 32);
1566 errln("Error in DateFormat::format for th with Grego cal, expect: %s, get: %s", expectFormat
, getFormat
);
1569 LocalPointer
<DateFormat
> df2(DateFormat::createInstanceForSkeleton(skeleton
, "th-u-ca-gregory", status
));
1570 if (U_FAILURE(status
)) {
1571 dataerrln("Fail in DateFormat::createInstanceForSkeleton for th-u-ca-gregory: %s", u_errorName(status
));
1574 status
= U_ZERO_ERROR
;
1576 getFmtStr
= df2
->format(testDate
, getFmtStr
);
1577 if (U_FAILURE(status
)) {
1578 errln("Fail in DateFormat::format for th-u-ca-gregory: %s", u_errorName(status
));
1579 } else if (getFmtStr
!= expectFormat
) {
1581 getFmtStr
.extract(0, getFmtStr
.length(), getFormat
, 32);
1582 errln("Error in DateFormat::format for th with Grego cal, expect: %s, get: %s", expectFormat
, getFormat
);
1586 void DateFormatRegressionTest::TestT10334(void) {
1587 UErrorCode status
= U_ZERO_ERROR
;
1588 UnicodeString
pattern("'--: 'EEE-WW-MMMM-yyyy");
1589 UnicodeString
text("--mon-02-march-2011");
1590 SimpleDateFormat
format(pattern
, status
);
1592 logln("pattern["+pattern
+"] text["+text
+"]");
1594 if (U_FAILURE(status
)) {
1595 dataerrln("Fail creating SimpleDateFormat object - %s", u_errorName(status
));
1599 format
.setBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH
, FALSE
, status
);
1600 format
.parse(text
, status
);
1601 if (!U_FAILURE(status
)) {
1602 errln("parse partial match did NOT fail in strict mode - %s", u_errorName(status
));
1605 status
= U_ZERO_ERROR
;
1606 format
.setBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH
, TRUE
, status
);
1607 format
.parse(text
, status
);
1608 if (U_FAILURE(status
)) {
1609 errln("parse partial match failure in lenient mode - %s", u_errorName(status
));
1612 status
= U_ZERO_ERROR
;
1613 pattern
= UnicodeString("YYYY MM dd");
1614 text
= UnicodeString("2013 12 10");
1615 format
.applyPattern(pattern
);
1616 UDate referenceDate
= format
.parse(text
, status
);
1618 FieldPosition
fp(FieldPosition::DONT_CARE
);
1619 UnicodeString
formattedString("");
1620 pattern
= UnicodeString("YYYY LL dd ee cc qq QQ");
1621 format
.applyPattern(pattern
);
1622 format
.format(referenceDate
, formattedString
, fp
, status
);
1623 logln("ref date: " + formattedString
);
1626 char patternArray
[] = "YYYY LLL dd eee ccc qqq QQQ";
1627 pattern
= UnicodeString(patternArray
);
1628 text
= UnicodeString("2013 12 10 03 3 04 04");
1629 status
= U_ZERO_ERROR
;
1630 format
.setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC
, TRUE
, status
);
1631 format
.applyPattern(pattern
);
1632 ParsePosition
pp(0);
1633 format
.parse(text
, pp
);
1634 if (pp
.getErrorIndex() != -1) {
1635 errln("numeric parse error");
1638 status
= U_ZERO_ERROR
;
1639 format
.setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC
, FALSE
, status
);
1640 format
.parse(text
, status
);
1641 if (!U_FAILURE(status
)) {
1642 errln("numeric parse did NOT fail in strict mode", u_errorName(status
));
1649 const char * locale
;
1651 UnicodeString parseString
;
1652 UnicodeString pattern
;
1653 UnicodeString expectedResult
; // null indicates expected error
1654 } TestDateFormatLeniencyItem
;
1657 void DateFormatRegressionTest::TestT10619(void) {
1658 const UDate july022008
= 1215000001979.0;
1659 const TestDateFormatLeniencyItem items
[] = {
1660 //locale leniency parse String pattern expected result
1661 { "en", true, UnicodeString("2008-07 02"), UnicodeString("yyyy-LLLL dd"), UnicodeString("2008-July 02") },
1662 { "en", false, UnicodeString("2008-07 03"), UnicodeString("yyyy-LLLL dd"), UnicodeString("") },
1663 { "en", true, UnicodeString("2008-Jan. 04"), UnicodeString("yyyy-LLL dd"), UnicodeString("2008-Jan 04") },
1664 { "en", false, UnicodeString("2008-Jan. 05"), UnicodeString("yyyy-LLL dd"), UnicodeString("") },
1665 { "en", true, UnicodeString("2008-Jan--06"), UnicodeString("yyyy-MMM -- dd"), UnicodeString("2008-Jan 06") },
1666 { "en", false, UnicodeString("2008-Jan--07"), UnicodeString("yyyy-MMM -- dd"), UnicodeString("") },
1667 { "en", true, UnicodeString("6 Jan 08 2008"), UnicodeString("eee MMM dd yyyy"), UnicodeString("Sat Jan 08 2008") },
1668 { "en", false, UnicodeString("6 Jan 09 2008"), UnicodeString("eee MMM dd yyyy"), UnicodeString("") },
1670 { NULL
, true, UnicodeString(""), UnicodeString(""), UnicodeString("") }
1672 UErrorCode status
= U_ZERO_ERROR
;
1673 Calendar
* cal
= Calendar::createInstance(status
);
1674 if (U_FAILURE(status
)) {
1675 dataerrln(UnicodeString("FAIL: Unable to create Calendar for default timezone and locale."));
1677 cal
->setTime(july022008
, status
);
1678 const TestDateFormatLeniencyItem
* itemPtr
;
1679 for (itemPtr
= items
; itemPtr
->locale
!= NULL
; itemPtr
++ ) {
1681 Locale locale
= Locale::createFromName(itemPtr
->locale
);
1682 status
= U_ZERO_ERROR
;
1683 ParsePosition
pos(0);
1684 SimpleDateFormat
* sdmft
= new SimpleDateFormat(itemPtr
->pattern
, locale
, status
);
1685 if (U_FAILURE(status
)) {
1686 dataerrln("Unable to create SimpleDateFormat - %s", u_errorName(status
));
1689 logln("parsing " + itemPtr
->parseString
);
1690 sdmft
->setLenient(itemPtr
->leniency
);
1691 sdmft
->setBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE
, itemPtr
->leniency
, status
);
1692 sdmft
->setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC
, itemPtr
->leniency
, status
);
1693 sdmft
->setBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH
, itemPtr
->leniency
, status
);
1694 sdmft
->parse(itemPtr
->parseString
, pos
);
1697 if(pos
.getErrorIndex() > -1) {
1698 if(itemPtr
->expectedResult
.length() != 0) {
1699 errln("error: unexpected error - " + itemPtr
->parseString
+ " - error index " + pos
.getErrorIndex() +
1700 " - leniency " + itemPtr
->leniency
);
1715 UnicodeString pattern
;
1716 int initialParsePos
;
1719 void DateFormatRegressionTest::TestT10855(void) {
1720 // NOTE: these should NOT parse
1721 const T10855Data items
[] = {
1722 //parse String pattern initial parse pos
1723 // { UnicodeString("September 30, 1998"), UnicodeString("MM-dd-yyyy"), 0}, // for Apple, this can succeed
1724 { UnicodeString("123-73-1950"), UnicodeString("MM-dd-yyyy"), -1},
1725 { UnicodeString("12-23-1950"), UnicodeString("MM-dd-yyyy"), -1},
1727 { UnicodeString(""), UnicodeString(""), 0}
1729 UErrorCode status
= U_ZERO_ERROR
;
1732 while(items
[x
].pattern
.length() > 0)
1734 status
= U_ZERO_ERROR
;
1735 logln("Date to parse: \""+items
[x
].text
+"\"");
1736 logln("Starting Index: %d", items
[x
].initialParsePos
);
1738 SimpleDateFormat
dateFmt(items
[x
].pattern
, status
);
1739 if(U_FAILURE(status
)) {
1740 errcheckln(status
, "Failed dateFmt: %s", u_errorName(status
));
1744 status
= U_ZERO_ERROR
;
1746 dateFmt
.setLenient(false);
1747 dateFmt
.setTimeZone(*TimeZone::getGMT());
1749 ParsePosition
position(items
[x
].initialParsePos
);
1750 logln("set position is now: %d", position
.getIndex());
1751 UDate d
= dateFmt
.parse(items
[x
].text
, position
);
1752 if (position
.getErrorIndex() != -1 || position
.getIndex() == items
[x
].initialParsePos
) {
1753 logln("Parse Failed. ErrorIndex is %d - Index is %d", position
.getErrorIndex(), position
.getIndex());
1755 errln("Parse Succeeded...should have failed. Index is %d - ErrorIndex is %d", position
.getIndex(), position
.getErrorIndex());
1757 logln("Parsed date returns %d\n", d
);
1763 void DateFormatRegressionTest::TestT10906(void) {
1765 UErrorCode status
= U_ZERO_ERROR
;
1766 UnicodeString pattern
= "MM-dd-yyyy";
1767 UnicodeString text
= "06-10-2014";
1768 SimpleDateFormat
format(pattern
, status
);
1769 int32_t errorIdx
= 0;
1770 ParsePosition
pp(-1);
1771 format
.parse(text
, pp
);
1772 errorIdx
= pp
.getErrorIndex();
1773 if (errorIdx
== -1) {
1774 errln("failed to report invalid (negative) starting parse position");
1778 void DateFormatRegressionTest::TestT13380(void) {
1779 UErrorCode errorCode
= U_ZERO_ERROR
;
1780 LocalPointer
<DateFormat
> enFmt(DateFormat::createDateInstance(DateFormat::kShort
, Locale("en")), errorCode
);
1781 if (U_FAILURE(errorCode
)) {
1782 errln("failure creating 'en' DateFormat");
1785 errorCode
= U_ZERO_ERROR
;
1786 LocalPointer
<DateFormat
> tgFmt(DateFormat::createDateInstance(DateFormat::kShort
, Locale("tg")), errorCode
);
1787 if (U_FAILURE(errorCode
)) {
1788 errln("failure creating 'tg' DateFormat");
1792 #endif /* #if !UCONFIG_NO_FORMATTING */