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 UnicodeString myDate
;
295 SimpleDateFormat
fmt(UnicodeString(u
"yyyy/MM/dd"), status
);
296 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
297 myDate
= "1997/01/01";
298 aux917( &fmt
, myDate
);
300 SimpleDateFormat
fmt2( UnicodeString(u
"yyyyMMdd"), status
);
301 if(failure(status
, "new SimpleDateFormat")) return;
303 aux917(&fmt2
, myDate
);
306 void DateFormatRegressionTest::aux917( SimpleDateFormat
*fmt
, UnicodeString
& str
) {
309 pat
= fmt
->toPattern(pat
);
310 logln( "==================" );
311 logln( "testIt: pattern=" + pat
+
317 ParsePosition
pos(0);
318 fmt
->parseObject( str
, o
, pos
);
319 //logln( UnicodeString("Parsed object: ") + o );
321 UErrorCode status
= U_ZERO_ERROR
;
322 UnicodeString formatted
;
323 FieldPosition
poss(FieldPosition::DONT_CARE
);
324 formatted
= fmt
->format( o
, formatted
, poss
, status
);
325 failure(status
, "fmt->format");
326 logln( "Formatted string: " + formatted
);
327 if( formatted
!= str
)
328 errln("Fail: Want " + str
+ " Got " + formatted
);
330 catch (ParseException e) {
339 void DateFormatRegressionTest::Test4060212(void)
341 UnicodeString dateString
= "1995-040.05:01:29";
343 logln( "dateString= " + dateString
);
344 logln("Using yyyy-DDD.hh:mm:ss");
345 UErrorCode status
= U_ZERO_ERROR
;
346 SimpleDateFormat
formatter(UnicodeString("yyyy-DDD.hh:mm:ss"), status
);
347 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
348 ParsePosition
pos(0);
349 UDate myDate
= formatter
.parse( dateString
, pos
);
350 UnicodeString myString
;
351 LocalPointer
<DateFormat
> fmt(DateFormat::createDateTimeInstance( DateFormat::FULL
,
353 if (!fmt
.isValid()) {
354 dataerrln("Error calling DateFormat::createDateTimeInstance");
358 myString
= fmt
->format( myDate
, myString
);
361 LocalPointer
<Calendar
> cal(new GregorianCalendar(status
));
362 failure(status
, "new GregorianCalendar");
363 cal
->setTime(myDate
, status
);
364 failure(status
, "cal->setTime");
365 if ((cal
->get(UCAL_DAY_OF_YEAR
, status
) != 40) || failure(status
, "cal->get"))
366 errln((UnicodeString
) "Fail: Got " + cal
->get(UCAL_DAY_OF_YEAR
, status
) +
369 // this is an odd usage of "ddd" and it doesn't
370 // work now that date values are range checked per #3579.
371 logln("Using yyyy-ddd.hh:mm:ss");
372 SimpleDateFormat
formatter2(UnicodeString(u
"yyyy-ddd.hh:mm:ss"), status
);
373 if(failure(status
, "new SimpleDateFormat")) return;
375 myDate
= formatter2
.parse( dateString
, pos
);
376 myString
= fmt
->format( myDate
, myString
);
378 cal
->setTime(myDate
, status
);
379 failure(status
, "cal->setTime");
380 if ((cal
->get(UCAL_DAY_OF_YEAR
, status
) != 40) || failure(status
, "cal->get"))
381 errln((UnicodeString
) "Fail: Got " + cal
->get(UCAL_DAY_OF_YEAR
, status
) +
388 void DateFormatRegressionTest::Test4061287(void)
390 UErrorCode status
= U_ZERO_ERROR
;
392 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("dd/MM/yyyy"), status
);
393 if (U_FAILURE(status
)) {
394 dataerrln("Fail new SimpleDateFormat: %s", u_errorName(status
));
398 failure(status
, "new SimpleDateFormat");
400 logln(UnicodeString("") + df
->parse("35/01/1971", status
));
401 failure(status
, "df->parse(\"35/01/1971\")");
402 //logln(df.parse("35/01/1971").toString());
404 /*catch (ParseException e) {
408 df
->setLenient(FALSE
);
411 logln(UnicodeString("") + df
->parse("35/01/1971", status
));
412 if(U_FAILURE(status
))
414 //logln(df.parse("35/01/1971").toString());
415 //} catch (ParseException e) {ok=TRUE;}
417 errln("Fail: Lenient not working");
424 void DateFormatRegressionTest::Test4065240(void)
427 DateFormat
*shortdate
, *fulldate
;
428 UnicodeString strShortDate
, strFullDate
;
429 Locale saveLocale
= Locale::getDefault();
430 TimeZone
*saveZone
= TimeZone::createDefault();
432 UErrorCode status
= U_ZERO_ERROR
;
434 Locale
*curLocale
= new Locale("de","DE");
435 Locale::setDefault(*curLocale
, status
);
436 failure(status
, "Locale::setDefault");
437 // {sfb} adoptDefault instead of setDefault
438 //TimeZone::setDefault(TimeZone::createTimeZone("EST"));
439 TimeZone::adoptDefault(TimeZone::createTimeZone("EST"));
440 curDate
= date(98, 0, 1);
441 shortdate
= DateFormat::createDateInstance(DateFormat::SHORT
);
442 if (shortdate
== NULL
){
443 dataerrln("Error calling DateFormat::createDateInstance");
447 fulldate
= DateFormat::createDateTimeInstance(DateFormat::LONG
, DateFormat::LONG
);
448 if (fulldate
== NULL
){
449 dataerrln("Error calling DateFormat::createDateTimeInstance");
452 strShortDate
= "The current date (short form) is ";
454 temp
= shortdate
->format(curDate
, temp
);
455 strShortDate
+= temp
;
456 strFullDate
= "The current date (long form) is ";
458 fulldate
->format(curDate
, temp2
);
459 strFullDate
+= temp2
;
464 // {sfb} What to do with resource bundle stuff?????
466 // Check to see if the resource is present; if not, we can't test
467 ResourceBundle
*bundle
= new ResourceBundle(
468 NULL
, *curLocale
, status
);
469 failure(status
, "new ResourceBundle");
470 //(UnicodeString) "java.text.resources.DateFormatZoneData", curLocale);
472 // {sfb} API change to ResourceBundle -- add getLocale()
473 /*if (bundle->getLocale().getLanguage(temp) == UnicodeString("de")) {
474 // UPDATE THIS AS ZONE NAME RESOURCE FOR <EST> in de_DE is updated
475 if (!strFullDate.endsWith(UnicodeString("GMT-05:00")))
476 errln("Fail: Want GMT-05:00");
479 logln("*** TEST COULD NOT BE COMPLETED BECAUSE DateFormatZoneData ***");
480 logln("*** FOR LOCALE de OR de_DE IS MISSING ***");
484 Locale::setDefault(saveLocale
, status
);
485 failure(status
, "Locale::setDefault");
486 TimeZone::setDefault(*saveZone
);
496 DateFormat.equals is too narrowly defined. As a result, MessageFormat
497 does not work correctly. DateFormat.equals needs to be written so
498 that the Calendar sub-object is not compared using Calendar.equals,
499 but rather compared for equivalency. This may necessitate adding a
500 (package private) method to Calendar to test for equivalency.
502 Currently this bug breaks MessageFormat.toPattern
507 void DateFormatRegressionTest::Test4071441(void)
509 DateFormat
*fmtA
= DateFormat::createInstance();
510 DateFormat
*fmtB
= DateFormat::createInstance();
512 if (fmtA
== NULL
|| fmtB
== NULL
){
513 dataerrln("Error calling DateFormat::createInstance");
519 // {sfb} Is it OK to cast away const here?
520 Calendar
*calA
= (Calendar
*) fmtA
->getCalendar();
521 Calendar
*calB
= (Calendar
*) fmtB
->getCalendar();
523 errln("Couldn't get proper calendars, exiting");
528 UDate epoch
= date(0, 0, 0);
529 UDate xmas
= date(61, UCAL_DECEMBER
, 25);
531 UErrorCode status
= U_ZERO_ERROR
;
532 calA
->setTime(epoch
, status
);
533 failure(status
, "calA->setTime");
534 calB
->setTime(epoch
, status
);
535 failure(status
, "calB->setTime");
537 errln("Fail: Can't complete test; Calendar instances unequal");
539 errln("Fail: DateFormat unequal when Calendars equal");
540 calB
->setTime(xmas
, status
);
541 failure(status
, "calB->setTime");
543 errln("Fail: Can't complete test; Calendar instances equal");
545 errln("Fail: DateFormat unequal when Calendars equivalent");
547 logln("DateFormat.equals ok");
553 /* The java.text.DateFormat.parse(String) method expects for the
554 US locale a string formatted according to mm/dd/yy and parses it
557 When given a string mm/dd/yyyy [sic] it only parses up to the first
558 two y's, typically resulting in a date in the year 1919.
560 Please extend the parsing method(s) to handle strings with
561 four-digit year values (probably also applicable to various
566 void DateFormatRegressionTest::Test4073003(void)
569 UErrorCode ec
= U_ZERO_ERROR
;
570 SimpleDateFormat
fmt("MM/dd/yy", Locale::getUK(), ec
);
572 dataerrln("FAIL: SimpleDateFormat constructor - %s", u_errorName(ec
));
575 UnicodeString tests
[] = {
576 (UnicodeString
) "12/25/61",
577 (UnicodeString
) "12/25/1961",
578 (UnicodeString
) "4/3/2010",
579 (UnicodeString
) "4/3/10"
581 UErrorCode status
= U_ZERO_ERROR
;
582 for(int i
= 0; i
< 4; i
+=2) {
583 UDate d
= fmt
.parse(tests
[i
], status
);
584 failure(status
, "fmt.parse");
585 UDate dd
= fmt
.parse(tests
[i
+1], status
);
586 failure(status
, "fmt.parse");
588 s
= fmt
.format(d
, s
);
590 ss
= fmt
.format(dd
, ss
);
592 errln((UnicodeString
) "Fail: " + d
+ " != " + dd
);
594 errln((UnicodeString
)"Fail: " + s
+ " != " + ss
);
595 logln("Ok: " + s
+ " " + d
);
602 void DateFormatRegressionTest::Test4089106(void)
604 TimeZone
*def
= TimeZone::createDefault();
606 TimeZone
*z
= new SimpleTimeZone((int)(1.25 * 3600000), "FAKEZONE");
607 TimeZone::setDefault(*z
);
608 UErrorCode status
= U_ZERO_ERROR
;
609 SimpleDateFormat
*f
= new SimpleDateFormat(status
);
610 if(U_FAILURE(status
)) {
611 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
617 failure(status
, "new SimpleDateFormat");
618 if (f
->getTimeZone()!= *z
)
619 errln("Fail: SimpleTimeZone should use TimeZone.getDefault()");
623 TimeZone::setDefault(*def
);
635 // {sfb} not applicable in C++??
637 void DateFormatRegressionTest::Test4100302(void)
639 /* Locale locales [] = {
641 Locale::CANADA_FRENCH,
656 Locale::SIMPLIFIED_CHINESE,
658 Locale::TRADITIONAL_CHINESE,
664 for(int i = 0; i < 21; i++) {
666 Format *format = DateFormat::createDateTimeInstance(DateFormat::FULL,
667 DateFormat::FULL, locales[i]);
670 ByteArrayOutputStream baos = new ByteArrayOutputStream();
671 ObjectOutputStream oos = new ObjectOutputStream(baos);
673 oos.writeObject(format);
677 bytes = baos.toByteArray();
679 ObjectInputStream ois =
680 new ObjectInputStream(new ByteArrayInputStream(bytes));
682 if (!format.equals(ois.readObject())) {
684 logln("DateFormat instance for locale " +
685 locales[i] + " is incorrectly serialized/deserialized.");
687 logln("DateFormat instance for locale " +
688 locales[i] + " is OKAY.");
691 if (!pass) errln("Fail: DateFormat serialization/equality bug");
693 catch (IOException e) {
697 catch (ClassNotFoundException e) {
706 void DateFormatRegressionTest::Test4101483(void)
708 UErrorCode status
= U_ZERO_ERROR
;
709 SimpleDateFormat
sdf(UnicodeString("z"), Locale::getUS(), status
);
710 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
711 FieldPosition
fp(UDAT_TIMEZONE_FIELD
);
712 //Date d = date(9234567890L);
713 UDate d
= 9234567890.0;
714 //StringBuffer buf = new StringBuffer("");
716 sdf
.format(d
, buf
, fp
);
717 //logln(sdf.format(d, buf, fp).toString());
718 logln(dateToString(d
) + " => " + buf
);
719 logln(UnicodeString("beginIndex = ") + fp
.getBeginIndex());
720 logln(UnicodeString("endIndex = ") + fp
.getEndIndex());
721 if (fp
.getBeginIndex() == fp
.getEndIndex())
722 errln("Fail: Empty field");
728 * This bug really only works in Locale.US, since that's what the locale
729 * used for Date.toString() is. Bug 4138203 reports that it fails on Korean
730 * NT; it would actually have failed on any non-US locale. Now it should
731 * work on all locales.
733 void DateFormatRegressionTest::Test4103340(void)
735 UErrorCode status
= U_ZERO_ERROR
;
737 // choose a date that is the FIRST of some month
738 // and some arbitrary time
739 UDate d
= date(97, 3, 1, 1, 1, 1);
740 SimpleDateFormat
df(UnicodeString(u
"MMMM"), Locale::getUS(), status
);
741 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
744 s
= dateToString(d
, s
);
746 FieldPosition
pos(FieldPosition::DONT_CARE
);
747 s2
= df
.format(d
, s2
, pos
);
750 UnicodeString substr
;
751 s2
.extract(0,2, substr
);
752 if (s
.indexOf(substr
) == -1)
753 errln("Months should match");
759 void DateFormatRegressionTest::Test4103341(void)
761 LocalPointer
<TimeZone
> saveZone(TimeZone::createDefault());
762 if (!saveZone
.isValid()) {
763 dataerrln("TimeZone::createDefault() failed.");
766 // {sfb} changed from setDefault to adoptDefault
767 TimeZone::adoptDefault(TimeZone::createTimeZone("CST"));
768 UErrorCode status
= U_ZERO_ERROR
;
769 SimpleDateFormat
simple(UnicodeString("MM/dd/yyyy HH:mm"), status
);
770 if(U_FAILURE(status
)) {
771 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
773 LocalPointer
<TimeZone
> temp(TimeZone::createDefault());
774 if(simple
.getTimeZone() != *temp
)
775 errln("Fail: SimpleDateFormat not using default zone");
777 TimeZone::adoptDefault(saveZone
.orphan());
783 void DateFormatRegressionTest::Test4104136(void)
785 UErrorCode status
= U_ZERO_ERROR
;
786 SimpleDateFormat
*sdf
= new SimpleDateFormat(status
);
787 if(U_FAILURE(status
)) {
788 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
792 if(failure(status
, "new SimpleDateFormat")) return;
793 UnicodeString pattern
= "'time' hh:mm";
794 sdf
->applyPattern(pattern
);
795 logln("pattern: \"" + pattern
+ "\"");
797 UnicodeString strings
[] = {
798 (UnicodeString
)"time 10:30",
799 (UnicodeString
) "time 10:x",
800 (UnicodeString
) "time 10x"
803 ParsePosition ppos
[] = {
810 date(70, UCAL_JANUARY
, 1, 10, 30),
816 "time 10:30", new ParsePosition(10), new Date(70, Calendar.JANUARY, 1, 10, 30),
817 "time 10:x", new ParsePosition(0), null,
818 "time 10x", new ParsePosition(0), null,
821 for(int i
= 0; i
< 3; i
++) {
822 UnicodeString text
= strings
[i
];
823 ParsePosition finish
= ppos
[i
];
824 UDate exp
= dates
[i
];
826 ParsePosition
pos(0);
827 UDate d
= sdf
->parse(text
, pos
);
828 logln(" text: \"" + text
+ "\"");
829 logln(" index: %d", pos
.getIndex());
830 logln((UnicodeString
) " result: " + d
);
831 if(pos
.getIndex() != finish
.getIndex())
832 errln(UnicodeString("Fail: Expected pos ") + finish
.getIndex());
833 if (! ((d
== 0 && exp
== -1) || (d
== exp
)))
834 errln((UnicodeString
) "Fail: Expected result " + exp
);
843 * According to the bug report, this test should throw a
844 * StringIndexOutOfBoundsException during the second parse. However,
847 void DateFormatRegressionTest::Test4104522(void)
849 UErrorCode status
= U_ZERO_ERROR
;
851 SimpleDateFormat
*sdf
= new SimpleDateFormat(status
);
852 if(U_FAILURE(status
)) {
853 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
857 failure(status
, "new SimpleDateFormat");
858 UnicodeString pattern
= "'time' hh:mm";
859 sdf
->applyPattern(pattern
);
860 logln("pattern: \"" + pattern
+ "\"");
864 UnicodeString text
= "time ";
865 UDate dt
= sdf
->parse(text
, pp
);
866 logln(" text: \"" + text
+ "\"" +
872 dt
= sdf
->parse(text
, pp
);
873 logln(" text: \"" + text
+ "\"" +
882 void DateFormatRegressionTest::Test4106807(void)
885 DateFormat
*df
= DateFormat::createDateTimeInstance();
887 UErrorCode status
= U_ZERO_ERROR
;
888 SimpleDateFormat
*sdfs
[] = {
889 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss"), status
),
890 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'Z'"), status
),
891 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss''"), status
),
892 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'a''a'"), status
),
893 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss %"), status
)
895 if(U_FAILURE(status
)) {
896 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status
));
905 failure(status
, "new SimpleDateFormat");
907 UnicodeString strings
[] = {
908 (UnicodeString
) "19980211140000",
909 (UnicodeString
) "19980211140000",
910 (UnicodeString
) "19980211140000",
911 (UnicodeString
) "19980211140000a",
912 (UnicodeString
) "19980211140000 "
916 new SimpleDateFormat("yyyyMMddHHmmss"), "19980211140000",
917 new SimpleDateFormat("yyyyMMddHHmmss'Z'"), "19980211140000",
918 new SimpleDateFormat("yyyyMMddHHmmss''"), "19980211140000",
919 new SimpleDateFormat("yyyyMMddHHmmss'a''a'"), "19980211140000a",
920 new SimpleDateFormat("yyyyMMddHHmmss %"), "19980211140000 ",
922 GregorianCalendar
*gc
= new GregorianCalendar(status
);
923 failure(status
, "new GregorianCalendar");
924 TimeZone
*timeZone
= TimeZone::createDefault();
926 TimeZone
*gmt
= timeZone
->clone();
928 gmt
->setRawOffset(0);
930 for(int32_t i
= 0; i
< 5; i
++) {
931 SimpleDateFormat
*format
= sdfs
[i
];
932 UnicodeString dateString
= strings
[i
];
934 format
->setTimeZone(*gmt
);
935 dt
= format
->parse(dateString
, status
);
936 // {sfb} some of these parses will fail purposely
937 if(U_FAILURE(status
))
939 status
= U_ZERO_ERROR
;
941 FieldPosition
pos(FieldPosition::DONT_CARE
);
942 fmtd
= df
->format(dt
, fmtd
, pos
);
944 //logln(df->format(dt));
945 gc
->setTime(dt
, status
);
946 failure(status
, "gc->getTime");
947 logln(UnicodeString("") + gc
->get(UCAL_ZONE_OFFSET
, status
));
948 failure(status
, "gc->get");
950 s
= format
->format(dt
, s
, pos
);
953 catch (ParseException e) {
954 logln("No way Jose");
960 for(int32_t j
= 0; j
< 5; j
++)
967 Synopsis: Chinese time zone CTT is not recogonized correctly.
968 Description: Platform Chinese Windows 95 - ** Time zone set to CST **
974 // {sfb} what to do with this one ??
975 void DateFormatRegressionTest::Test4108407(void)
977 /*long l = System.currentTimeMillis();
978 logln("user.timezone = " + System.getProperty("user.timezone", "?"));
979 logln("Time Zone :" +
980 DateFormat.getDateInstance().getTimeZone().getID());
981 logln("Default format :" +
982 DateFormat.getDateInstance().format(new Date(l)));
983 logln("Full format :" +
984 DateFormat.getDateInstance(DateFormat.FULL).format(new
986 logln("*** Set host TZ to CST ***");
987 logln("*** THE RESULTS OF THIS TEST MUST BE VERIFIED MANUALLY ***");*/
992 * SimpleDateFormat won't parse "GMT"
994 void DateFormatRegressionTest::Test4134203(void)
996 UErrorCode status
= U_ZERO_ERROR
;
997 UnicodeString dateFormat
= "MM/dd/yy HH:mm:ss zzz";
998 SimpleDateFormat
fmt (dateFormat
, status
);
999 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
1000 ParsePosition
p0(0);
1001 UDate d
= fmt
.parse("01/22/92 04:52:00 GMT", p0
);
1002 logln(dateToString(d
));
1003 if(p0
== ParsePosition(0))
1004 errln("Fail: failed to parse 'GMT'");
1005 // In the failure case an exception is thrown by parse();
1006 // if no exception is thrown, the test passes.
1011 * SimpleDateFormat incorrect handling of 2 single quotes in format()
1013 void DateFormatRegressionTest::Test4151631(void)
1015 UnicodeString pattern
= "'TO_DATE('''dd'-'MM'-'yyyy HH:mm:ss''' , ''DD-MM-YYYY HH:MI:SS'')'";
1016 logln("pattern=" + pattern
);
1017 UErrorCode status
= U_ZERO_ERROR
;
1018 SimpleDateFormat
format(pattern
, Locale::getUS(), status
);
1019 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
1020 UnicodeString result
;
1021 FieldPosition
pos(FieldPosition::DONT_CARE
);
1022 result
= format
.format(date(1998-1900, UCAL_JUNE
, 30, 13, 30, 0), result
, pos
);
1023 if (result
!= "TO_DATE('30-06-1998 13:30:00' , 'DD-MM-YYYY HH:MI:SS')") {
1024 errln("Fail: result=" + result
);
1027 logln("Pass: result=" + result
);
1033 * 'z' at end of date format throws index exception in SimpleDateFormat
1034 * CANNOT REPRODUCE THIS BUG ON 1.2FCS
1036 void DateFormatRegressionTest::Test4151706(void)
1038 UnicodeString
dateString("Thursday, 31-Dec-98 23:00:00 GMT");
1039 UErrorCode status
= U_ZERO_ERROR
;
1040 SimpleDateFormat
fmt(UnicodeString("EEEE, dd-MMM-yy HH:mm:ss z"), Locale::getUS(), status
);
1041 if (failure(status
, "new SimpleDateFormat", TRUE
)) return;
1043 UDate d
= fmt
.parse(dateString
, status
);
1044 failure(status
, "fmt->parse");
1045 // {sfb} what about next two lines?
1046 //if (d.getTime() != Date.UTC(1998-1900, Calendar.DECEMBER, 31, 23, 0, 0))
1047 // errln("Incorrect value: " + d);
1048 /*} catch (Exception e) {
1049 errln("Fail: " + e);
1052 FieldPosition
pos(FieldPosition::DONT_CARE
);
1053 logln(dateString
+ " -> " + fmt
.format(d
, temp
, pos
));
1058 * Cannot reproduce this bug under 1.2 FCS -- it may be a convoluted duplicate
1059 * of some other bug that has been fixed.
1062 DateFormatRegressionTest::Test4162071(void)
1064 UnicodeString
dateString("Thu, 30-Jul-1999 11:51:14 GMT");
1065 UnicodeString
format("EEE', 'dd-MMM-yyyy HH:mm:ss z"); // RFC 822/1123
1066 UErrorCode status
= U_ZERO_ERROR
;
1067 SimpleDateFormat
df(format
, Locale::getUS(), status
);
1068 if(U_FAILURE(status
)) {
1069 dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status
));
1074 UDate x
= df
.parse(dateString
, status
);
1075 if(U_SUCCESS(status
))
1076 logln("Parse format \"" + format
+ "\" ok");
1078 errln("Parse format \"" + format
+ "\" failed.");
1080 FieldPosition
pos(FieldPosition::DONT_CARE
);
1081 logln(dateString
+ " -> " + df
.format(x
, temp
, pos
));
1082 //} catch (Exception e) {
1083 // errln("Parse format \"" + format + "\" failed.");
1088 * DateFormat shouldn't parse year "-1" as a two-digit year (e.g., "-1" -> 1999).
1090 void DateFormatRegressionTest::Test4182066(void) {
1091 UErrorCode status
= U_ZERO_ERROR
;
1092 SimpleDateFormat
fmt("MM/dd/yy", Locale::getUS(), status
);
1093 SimpleDateFormat
dispFmt("MMM dd yyyy GG", Locale::getUS(), status
);
1094 if (U_FAILURE(status
)) {
1095 dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status
));
1099 /* We expect 2-digit year formats to put 2-digit years in the right
1100 * window. Out of range years, that is, anything less than "00" or
1101 * greater than "99", are treated as literal years. So "1/2/3456"
1102 * becomes 3456 AD. Likewise, "1/2/-3" becomes -3 AD == 2 BC.
1104 const char* STRINGS
[] = {
1114 int32_t STRINGS_COUNT
= UPRV_LENGTHOF(STRINGS
);
1115 UDate FAIL_DATE
= (UDate
) 0;
1117 date(2000-1900, UCAL_FEBRUARY
, 29),
1118 date(2001-1900, UCAL_JANUARY
, 23),
1119 date( -1-1900, UCAL_APRIL
, 5),
1120 date( -9-1900, UCAL_JANUARY
, 23),
1121 date(1314-1900, UCAL_NOVEMBER
, 12),
1122 date( 1-1900, UCAL_OCTOBER
, 31),
1123 FAIL_DATE
, // "+1" isn't recognized by US NumberFormat
1124 date( 1-1900, UCAL_SEPTEMBER
,12),
1129 for (int32_t i
=0; i
<STRINGS_COUNT
; ++i
) {
1130 UnicodeString
str(STRINGS
[i
]);
1131 UDate expected
= DATES
[i
];
1132 status
= U_ZERO_ERROR
;
1133 UDate actual
= fmt
.parse(str
, status
);
1134 if (U_FAILURE(status
)) {
1137 UnicodeString actStr
;
1138 if (actual
== FAIL_DATE
) {
1139 actStr
.append("null");
1142 ((DateFormat
*)&dispFmt
)->format(actual
, actStr
);
1145 if (expected
== actual
) {
1146 out
.append(str
+ " => " + actStr
+ "\n");
1148 UnicodeString expStr
;
1149 if (expected
== FAIL_DATE
) {
1150 expStr
.append("null");
1153 ((DateFormat
*)&dispFmt
)->format(expected
, expStr
);
1155 out
.append("FAIL: " + str
+ " => " + actStr
1156 + ", expected " + expStr
+ "\n");
1168 * j32 {JDK Bug 4210209 4209272}
1169 * DateFormat cannot parse Feb 29 2000 when setLenient(false)
1172 DateFormatRegressionTest::Test4210209(void) {
1173 UErrorCode status
= U_ZERO_ERROR
;
1174 UnicodeString
pattern("MMM d, yyyy");
1175 SimpleDateFormat
sfmt(pattern
, Locale::getUS(), status
);
1176 SimpleDateFormat
sdisp("MMM dd yyyy GG", Locale::getUS(), status
);
1177 DateFormat
& fmt
= *(DateFormat
*)&sfmt
; // Yuck: See j25
1178 DateFormat
& disp
= *(DateFormat
*)&sdisp
; // Yuck: See j25
1179 if (U_FAILURE(status
)) {
1180 dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status
));
1183 Calendar
* calx
= (Calendar
*)fmt
.getCalendar(); // cast away const!
1184 calx
->setLenient(FALSE
);
1185 UDate d
= date(2000-1900, UCAL_FEBRUARY
, 29);
1186 UnicodeString s
, ss
;
1188 logln(disp
.format(d
, ss
.remove()) + " f> " + pattern
+
1189 " => \"" + s
+ "\"");
1190 ParsePosition
pos(0);
1191 d
= fmt
.parse(s
, pos
);
1192 logln(UnicodeString("\"") + s
+ "\" p> " + pattern
+
1193 " => " + disp
.format(d
, ss
.remove()));
1194 logln(UnicodeString("Parse pos = ") + pos
.getIndex() +
1195 ", error pos = " + pos
.getErrorIndex());
1196 if (pos
.getErrorIndex() != -1) {
1197 errln(UnicodeString("FAIL: Error index should be -1"));
1200 // The underlying bug is in GregorianCalendar. If the following lines
1201 // succeed, the bug is fixed. If the bug isn't fixed, they will throw
1203 GregorianCalendar
cal(status
);
1204 if (U_FAILURE(status
)) {
1205 errln("FAIL: Unable to create Calendar");
1209 cal
.setLenient(FALSE
);
1210 cal
.set(2000, UCAL_FEBRUARY
, 29); // This should work!
1211 logln(UnicodeString("Attempt to set Calendar to Feb 29 2000: ") +
1212 disp
.format(cal
.getTime(status
), ss
.remove()));
1213 if (U_FAILURE(status
)) {
1214 errln("FAIL: Unable to set Calendar to Feb 29 2000");
1218 void DateFormatRegressionTest::Test714(void)
1221 UDate
d(978103543000.);
1222 DateFormat
*fmt
= DateFormat::createDateTimeInstance(DateFormat::NONE
,
1226 dataerrln("Error calling DateFormat::createDateTimeInstance");
1231 UnicodeString tests
=
1232 (UnicodeString
) "7:25:43 AM" ;
1233 UErrorCode status
= U_ZERO_ERROR
;
1235 if(U_FAILURE(status
))
1237 errln((UnicodeString
) "Fail, errmsg " + u_errorName(status
));
1243 errln((UnicodeString
) "Fail: " + s
+ " != " + tests
);
1247 logln("OK: " + s
+ " == " + tests
);
1253 class Test1684Data
{
1263 UnicodeString normalized
;
1265 Test1684Data(int32_t xyear
, int32_t xmonth
, int32_t xdate
,
1266 int32_t xwomyear
, int32_t xwommon
, int32_t xwom
, int32_t xdow
,
1267 const char *xdata
, const char *xnormalized
) :
1276 normalized((xnormalized
==NULL
)?xdata
:xnormalized
,"")
1280 void DateFormatRegressionTest::Test1684(void)
1282 // July 2001 August 2001 January 2002
1283 // Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1284 // 1 2 3 4 5 6 7 1 2 3 4 1 2 3 4 5
1285 // 8 9 10 11 12 13 14 5 6 7 8 9 10 11 6 7 8 9 10 11 12
1286 // 15 16 17 18 19 20 21 12 13 14 15 16 17 18 13 14 15 16 17 18 19
1287 // 22 23 24 25 26 27 28 19 20 21 22 23 24 25 20 21 22 23 24 25 26
1288 // 29 30 31 26 27 28 29 30 31 27 28 29 30 31
1289 Test1684Data
*tests
[] = {
1290 new Test1684Data(2001, 8, 6, 2001,8,2,UCAL_MONDAY
, "2001 08 02 Mon", NULL
),
1291 new Test1684Data(2001, 8, 7, 2001,8,2,UCAL_TUESDAY
, "2001 08 02 Tue", NULL
),
1292 new Test1684Data(2001, 8, 5,/*12,*/ 2001,8,2,UCAL_SUNDAY
, "2001 08 02 Sun", NULL
),
1293 new Test1684Data(2001, 8,6, /*7, 30,*/ 2001,7,6,UCAL_MONDAY
, "2001 07 06 Mon", "2001 08 02 Mon"),
1294 new Test1684Data(2001, 8,7, /*7, 31,*/ 2001,7,6,UCAL_TUESDAY
, "2001 07 06 Tue", "2001 08 02 Tue"),
1295 new Test1684Data(2001, 8, 5, 2001,7,6,UCAL_SUNDAY
, "2001 07 06 Sun", "2001 08 02 Sun"),
1296 new Test1684Data(2001, 7, 30, 2001,8,1,UCAL_MONDAY
, "2001 08 01 Mon", "2001 07 05 Mon"),
1297 new Test1684Data(2001, 7, 31, 2001,8,1,UCAL_TUESDAY
, "2001 08 01 Tue", "2001 07 05 Tue"),
1298 new Test1684Data(2001, 7,29, /*8, 5,*/ 2001,8,1,UCAL_SUNDAY
, "2001 08 01 Sun", "2001 07 05 Sun"),
1299 new Test1684Data(2001, 12, 31, 2001,12,6,UCAL_MONDAY
, "2001 12 06 Mon", NULL
),
1300 new Test1684Data(2002, 1, 1, 2002,1,1,UCAL_TUESDAY
, "2002 01 01 Tue", NULL
),
1301 new Test1684Data(2002, 1, 2, 2002,1,1,UCAL_WEDNESDAY
, "2002 01 01 Wed", NULL
),
1302 new Test1684Data(2002, 1, 3, 2002,1,1,UCAL_THURSDAY
, "2002 01 01 Thu", NULL
),
1303 new Test1684Data(2002, 1, 4, 2002,1,1,UCAL_FRIDAY
, "2002 01 01 Fri", NULL
),
1304 new Test1684Data(2002, 1, 5, 2002,1,1,UCAL_SATURDAY
, "2002 01 01 Sat", NULL
),
1305 new Test1684Data(2001,12,30, /*2002, 1, 6,*/ 2002,1,1,UCAL_SUNDAY
, "2002 01 01 Sun", "2001 12 06 Sun")
1308 #define kTest1684Count UPRV_LENGTHOF(tests)
1310 int32_t pass
= 0, error
= 0, warning
= 0;
1313 UErrorCode status
= U_ZERO_ERROR
;
1314 UnicodeString
pattern("yyyy MM WW EEE","");
1315 Calendar
*cal
= new GregorianCalendar(status
);
1316 SimpleDateFormat
*sdf
= new SimpleDateFormat(pattern
,status
);
1317 if (U_FAILURE(status
)) {
1318 dataerrln("Error constructing SimpleDateFormat");
1319 for(i
=0;i
<kTest1684Count
;i
++) {
1326 cal
->setFirstDayOfWeek(UCAL_SUNDAY
);
1327 cal
->setMinimalDaysInFirstWeek(1);
1329 sdf
->adoptCalendar(cal
);
1331 cal
= sdf
->getCalendar()->clone(); // sdf may have deleted calendar
1333 if(!cal
|| !sdf
|| U_FAILURE(status
)) {
1334 errln(UnicodeString("Error setting up test: ") + u_errorName(status
));
1337 for (i
= 0; i
< kTest1684Count
; ++i
) {
1338 Test1684Data
&test
= *(tests
[i
]);
1339 logln(UnicodeString("#") + i
+ UnicodeString("\n-----\nTesting round trip of ") + test
.year
+
1340 " " + (test
.month
+ 1) +
1342 " (written as) " + test
.data
);
1345 cal
->set(test
.year
, test
.month
, test
.date
);
1346 UDate ms
= cal
->getTime(status
);
1349 cal
->set(UCAL_YEAR
, test
.womyear
);
1350 cal
->set(UCAL_MONTH
, test
.wommon
);
1351 cal
->set(UCAL_WEEK_OF_MONTH
, test
.wom
);
1352 cal
->set(UCAL_DAY_OF_WEEK
, test
.dow
);
1353 UDate ms2
= cal
->getTime(status
);
1356 errln((UnicodeString
)"\nError: GregorianUCAL_DOM gave " + ms
+
1357 "\n GregorianUCAL_WOM gave " + ms2
);
1363 ms2
= sdf
->parse(test
.data
, status
);
1364 if(U_FAILURE(status
)) {
1365 errln("parse exception: " + UnicodeString(u_errorName(status
)));
1369 errln((UnicodeString
)"\nError: GregorianCalendar gave " + ms
+
1370 "\n SimpleDateFormat.parse gave " + ms2
);
1376 UnicodeString result
;
1377 sdf
->format(ms
, result
);
1378 if (result
!= test
.normalized
) {
1379 errln("\nWarning: format of '" + test
.data
+ "' gave" +
1380 "\n '" + result
+ "'" +
1381 "\n expected '" + test
.normalized
+ "'");
1388 ms3
= sdf
->parse(result
, status
);
1389 if(U_FAILURE(status
)) {
1390 errln("parse exception 2: " + (UnicodeString
)u_errorName(status
));
1395 errln((UnicodeString
)"\nError: Re-parse of '" + result
+ "' gave time of " +
1404 = UnicodeString("Passed: ") + pass
+ ", Warnings: " + warning
+ ", Errors: " + error
;
1411 for(i
=0;i
<kTest1684Count
;i
++) {
1418 void DateFormatRegressionTest::Test5554(void)
1420 UErrorCode status
= U_ZERO_ERROR
;
1421 UnicodeString
pattern("Z","");
1422 UnicodeString
newfoundland("Canada/Newfoundland", "");
1423 TimeZone
*zone
= TimeZone::createTimeZone(newfoundland
);
1424 Calendar
*cal
= new GregorianCalendar(zone
, status
);
1425 SimpleDateFormat
*sdf
= new SimpleDateFormat(pattern
,status
);
1426 if (U_FAILURE(status
)) {
1427 dataerrln("Error constructing SimpleDateFormat");
1432 cal
->set(2007, 1, 14);
1433 UDate date
= cal
->getTime(status
);
1434 if (U_FAILURE(status
)) {
1435 errln("Error getting time to format");
1438 sdf
->adoptCalendar(cal
);
1439 UnicodeString result
;
1440 UnicodeString
correct("-0330", "");
1441 sdf
->format(date
, result
);
1442 if (result
!= correct
) {
1443 errln("\nError: Newfoundland Z of Jan 14, 2007 gave '" + result
+ "', expected '" + correct
+ "'");
1448 void DateFormatRegressionTest::Test9237(void)
1450 UErrorCode status
= U_ZERO_ERROR
;
1451 UnicodeString
pattern("VVVV");
1453 SimpleDateFormat
fmt(pattern
, status
); // default locale
1454 SimpleDateFormat
fmtDE(pattern
, Locale("de_DE"), status
);
1455 if (U_FAILURE(status
)) {
1456 dataerrln("Error constructing SimpleDateFormat");
1461 SimpleDateFormat
fmtCopyDE(fmtDE
);
1462 UnicodeString resDE
, resCopyDE
;
1464 fmtDE
.format(0.0, resDE
);
1465 fmtCopyDE
.format(0.0, resCopyDE
);
1467 if (resDE
!= resCopyDE
) {
1468 errln(UnicodeString("Error: different result by the copied instance - org:") + resDE
+ " copy:" + resCopyDE
);
1471 // test for assignment operator
1474 UnicodeString resAssigned
;
1475 fmt
.format(0.0, resAssigned
);
1477 if (resDE
!= resAssigned
) {
1478 errln(UnicodeString("Error: different results by the assigned instance - org:") + resDE
+ " assigned:" + resAssigned
);
1482 void DateFormatRegressionTest::TestParsing(void) {
1483 UErrorCode status
= U_ZERO_ERROR
;
1484 UnicodeString
pattern("EEE-WW-MMMM-yyyy");
1485 UnicodeString
text("mon-02-march-2011");
1486 int32_t expectedDay
= 7;
1488 SimpleDateFormat
format(pattern
, status
);
1489 if (U_FAILURE(status
)) {
1490 dataerrln("Unable to create SimpleDateFormat - %s", u_errorName(status
));
1494 Calendar
*cal
= new GregorianCalendar(status
);
1495 if (cal
== NULL
|| U_FAILURE(status
)) {
1496 errln("Unable to create calendar - %s", u_errorName(status
));
1500 ParsePosition
pos(0);
1501 format
.parse(text
, *cal
, pos
);
1503 if (cal
->get(UCAL_DAY_OF_MONTH
, status
) != expectedDay
) {
1504 errln("Parsing failed: day of month should be '7' with pattern: \"" + pattern
+ "\" for text: \"" + text
+ "\"");
1510 void DateFormatRegressionTest::Test12902_yWithGregoCalInThaiLoc(void) {
1511 UErrorCode status
= U_ZERO_ERROR
;
1512 UDate testDate
= 43200000.0; // 1970-Jan-01 12:00 GMT
1513 const char* skeleton
= "y";
1514 // Note that in locale "th", the availableFormats for skeleton "y" differ by calendar:
1515 // for buddhist (default calendar): y{"G y"}
1516 // for gregorian: y{"y"}
1517 const char* expectFormat
= "1970"; // format for skeleton y in Thai locale with Gregorian calendar
1518 UnicodeString getFmtStr
;
1520 const TimeZone
* gmtZone
= TimeZone::getGMT();
1521 LocalPointer
<GregorianCalendar
> pureGregoCal(new GregorianCalendar(*gmtZone
, Locale::getEnglish(), status
));
1522 if (U_FAILURE(status
)) {
1523 dataerrln("Fail in new GregorianCalendar(GMT, en): %s", u_errorName(status
));
1526 LocalPointer
<DateFormat
> df1(DateFormat::createInstanceForSkeleton(pureGregoCal
.orphan(), skeleton
, "th", status
));
1527 if (U_FAILURE(status
)) {
1528 dataerrln("Fail in DateFormat::createInstanceForSkeleton for th with Grego cal: %s", u_errorName(status
));
1531 status
= U_ZERO_ERROR
;
1533 getFmtStr
= df1
->format(testDate
, getFmtStr
);
1534 if (U_FAILURE(status
)) {
1535 errln("Fail in DateFormat::format for th with Grego cal: %s", u_errorName(status
));
1536 } else if (getFmtStr
!= expectFormat
) {
1538 getFmtStr
.extract(0, getFmtStr
.length(), getFormat
, 32);
1539 errln("Error in DateFormat::format for th with Grego cal, expect: %s, get: %s", expectFormat
, getFormat
);
1542 LocalPointer
<DateFormat
> df2(DateFormat::createInstanceForSkeleton(skeleton
, "th-u-ca-gregory", status
));
1543 if (U_FAILURE(status
)) {
1544 dataerrln("Fail in DateFormat::createInstanceForSkeleton for th-u-ca-gregory: %s", u_errorName(status
));
1547 status
= U_ZERO_ERROR
;
1549 getFmtStr
= df2
->format(testDate
, getFmtStr
);
1550 if (U_FAILURE(status
)) {
1551 errln("Fail in DateFormat::format for th-u-ca-gregory: %s", u_errorName(status
));
1552 } else if (getFmtStr
!= expectFormat
) {
1554 getFmtStr
.extract(0, getFmtStr
.length(), getFormat
, 32);
1555 errln("Error in DateFormat::format for th with Grego cal, expect: %s, get: %s", expectFormat
, getFormat
);
1559 void DateFormatRegressionTest::TestT10334(void) {
1560 UErrorCode status
= U_ZERO_ERROR
;
1561 UnicodeString
pattern("'--: 'EEE-WW-MMMM-yyyy");
1562 UnicodeString
text("--mon-02-march-2011");
1563 SimpleDateFormat
format(pattern
, status
);
1565 logln("pattern["+pattern
+"] text["+text
+"]");
1567 if (U_FAILURE(status
)) {
1568 dataerrln("Fail creating SimpleDateFormat object - %s", u_errorName(status
));
1572 format
.setBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH
, FALSE
, status
);
1573 format
.parse(text
, status
);
1574 if (!U_FAILURE(status
)) {
1575 errln("parse partial match did NOT fail in strict mode - %s", u_errorName(status
));
1578 status
= U_ZERO_ERROR
;
1579 format
.setBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH
, TRUE
, status
);
1580 format
.parse(text
, status
);
1581 if (U_FAILURE(status
)) {
1582 errln("parse partial match failure in lenient mode - %s", u_errorName(status
));
1585 status
= U_ZERO_ERROR
;
1586 pattern
= UnicodeString("YYYY MM dd");
1587 text
= UnicodeString("2013 12 10");
1588 format
.applyPattern(pattern
);
1589 UDate referenceDate
= format
.parse(text
, status
);
1591 FieldPosition
fp(FieldPosition::DONT_CARE
);
1592 UnicodeString
formattedString("");
1593 pattern
= UnicodeString("YYYY LL dd ee cc qq QQ");
1594 format
.applyPattern(pattern
);
1595 format
.format(referenceDate
, formattedString
, fp
, status
);
1596 logln("ref date: " + formattedString
);
1599 char patternArray
[] = "YYYY LLL dd eee ccc qqq QQQ";
1600 pattern
= UnicodeString(patternArray
);
1601 text
= UnicodeString("2013 12 10 03 3 04 04");
1602 status
= U_ZERO_ERROR
;
1603 format
.setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC
, TRUE
, status
);
1604 format
.applyPattern(pattern
);
1605 ParsePosition
pp(0);
1606 format
.parse(text
, pp
);
1607 if (pp
.getErrorIndex() != -1) {
1608 errln("numeric parse error");
1611 status
= U_ZERO_ERROR
;
1612 format
.setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC
, FALSE
, status
);
1613 format
.parse(text
, status
);
1614 if (!U_FAILURE(status
)) {
1615 errln("numeric parse did NOT fail in strict mode", u_errorName(status
));
1622 const char * locale
;
1624 UnicodeString parseString
;
1625 UnicodeString pattern
;
1626 UnicodeString expectedResult
; // null indicates expected error
1627 } TestDateFormatLeniencyItem
;
1630 void DateFormatRegressionTest::TestT10619(void) {
1631 const UDate july022008
= 1215000001979.0;
1632 const TestDateFormatLeniencyItem items
[] = {
1633 //locale leniency parse String pattern expected result
1634 { "en", true, UnicodeString("2008-07 02"), UnicodeString("yyyy-LLLL dd"), UnicodeString("2008-July 02") },
1635 { "en", false, UnicodeString("2008-07 03"), UnicodeString("yyyy-LLLL dd"), UnicodeString("") },
1636 { "en", true, UnicodeString("2008-Jan. 04"), UnicodeString("yyyy-LLL dd"), UnicodeString("2008-Jan 04") },
1637 { "en", false, UnicodeString("2008-Jan. 05"), UnicodeString("yyyy-LLL dd"), UnicodeString("") },
1638 { "en", true, UnicodeString("2008-Jan--06"), UnicodeString("yyyy-MMM -- dd"), UnicodeString("2008-Jan 06") },
1639 { "en", false, UnicodeString("2008-Jan--07"), UnicodeString("yyyy-MMM -- dd"), UnicodeString("") },
1640 { "en", true, UnicodeString("6 Jan 08 2008"), UnicodeString("eee MMM dd yyyy"), UnicodeString("Sat Jan 08 2008") },
1641 { "en", false, UnicodeString("6 Jan 09 2008"), UnicodeString("eee MMM dd yyyy"), UnicodeString("") },
1643 { NULL
, true, UnicodeString(""), UnicodeString(""), UnicodeString("") }
1645 UErrorCode status
= U_ZERO_ERROR
;
1646 Calendar
* cal
= Calendar::createInstance(status
);
1647 if (U_FAILURE(status
)) {
1648 dataerrln(UnicodeString("FAIL: Unable to create Calendar for default timezone and locale."));
1650 cal
->setTime(july022008
, status
);
1651 const TestDateFormatLeniencyItem
* itemPtr
;
1652 for (itemPtr
= items
; itemPtr
->locale
!= NULL
; itemPtr
++ ) {
1654 Locale locale
= Locale::createFromName(itemPtr
->locale
);
1655 status
= U_ZERO_ERROR
;
1656 ParsePosition
pos(0);
1657 SimpleDateFormat
* sdmft
= new SimpleDateFormat(itemPtr
->pattern
, locale
, status
);
1658 if (U_FAILURE(status
)) {
1659 dataerrln("Unable to create SimpleDateFormat - %s", u_errorName(status
));
1662 logln("parsing " + itemPtr
->parseString
);
1663 sdmft
->setLenient(itemPtr
->leniency
);
1664 sdmft
->setBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE
, itemPtr
->leniency
, status
);
1665 sdmft
->setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC
, itemPtr
->leniency
, status
);
1666 sdmft
->setBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH
, itemPtr
->leniency
, status
);
1667 sdmft
->parse(itemPtr
->parseString
, pos
);
1670 if(pos
.getErrorIndex() > -1) {
1671 if(itemPtr
->expectedResult
.length() != 0) {
1672 errln("error: unexpected error - " + itemPtr
->parseString
+ " - error index " + pos
.getErrorIndex() +
1673 " - leniency " + itemPtr
->leniency
);
1688 UnicodeString pattern
;
1689 int initialParsePos
;
1692 void DateFormatRegressionTest::TestT10855(void) {
1693 // NOTE: these should NOT parse
1694 const T10855Data items
[] = {
1695 //parse String pattern initial parse pos
1696 // { UnicodeString("September 30, 1998"), UnicodeString("MM-dd-yyyy"), 0}, // for Apple, this can succeed
1697 { UnicodeString("123-73-1950"), UnicodeString("MM-dd-yyyy"), -1},
1698 { UnicodeString("12-23-1950"), UnicodeString("MM-dd-yyyy"), -1},
1700 { UnicodeString(""), UnicodeString(""), 0}
1702 UErrorCode status
= U_ZERO_ERROR
;
1705 while(items
[x
].pattern
.length() > 0)
1707 status
= U_ZERO_ERROR
;
1708 logln("Date to parse: \""+items
[x
].text
+"\"");
1709 logln("Starting Index: %d", items
[x
].initialParsePos
);
1711 SimpleDateFormat
dateFmt(items
[x
].pattern
, status
);
1712 if(U_FAILURE(status
)) {
1713 errcheckln(status
, "Failed dateFmt: %s", u_errorName(status
));
1717 status
= U_ZERO_ERROR
;
1719 dateFmt
.setLenient(false);
1720 dateFmt
.setTimeZone(*TimeZone::getGMT());
1722 ParsePosition
position(items
[x
].initialParsePos
);
1723 logln("set position is now: %d", position
.getIndex());
1724 UDate d
= dateFmt
.parse(items
[x
].text
, position
);
1725 if (position
.getErrorIndex() != -1 || position
.getIndex() == items
[x
].initialParsePos
) {
1726 logln("Parse Failed. ErrorIndex is %d - Index is %d", position
.getErrorIndex(), position
.getIndex());
1728 errln("Parse Succeeded...should have failed. Index is %d - ErrorIndex is %d", position
.getIndex(), position
.getErrorIndex());
1730 logln("Parsed date returns %d\n", d
);
1736 void DateFormatRegressionTest::TestT10906(void) {
1738 UErrorCode status
= U_ZERO_ERROR
;
1739 UnicodeString pattern
= "MM-dd-yyyy";
1740 UnicodeString text
= "06-10-2014";
1741 SimpleDateFormat
format(pattern
, status
);
1742 int32_t errorIdx
= 0;
1743 ParsePosition
pp(-1);
1744 format
.parse(text
, pp
);
1745 errorIdx
= pp
.getErrorIndex();
1746 if (errorIdx
== -1) {
1747 errln("failed to report invalid (negative) starting parse position");
1751 void DateFormatRegressionTest::TestT13380(void) {
1752 UErrorCode errorCode
= U_ZERO_ERROR
;
1753 LocalPointer
<DateFormat
> enFmt(DateFormat::createDateInstance(DateFormat::kShort
, Locale("en")), errorCode
);
1754 if (U_FAILURE(errorCode
)) {
1755 errln("failure creating 'en' DateFormat");
1758 errorCode
= U_ZERO_ERROR
;
1759 LocalPointer
<DateFormat
> tgFmt(DateFormat::createDateInstance(DateFormat::kShort
, Locale("tg")), errorCode
);
1760 if (U_FAILURE(errorCode
)) {
1761 errln("failure creating 'tg' DateFormat");
1765 #endif /* #if !UCONFIG_NO_FORMATTING */