]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/dtfmrgts.cpp
ICU-461.18.tar.gz
[apple/icu.git] / icuSources / test / intltest / dtfmrgts.cpp
CommitLineData
b75a7d8f
A
1/********************************************************************
2 * COPYRIGHT:
729e4ab9 3 * Copyright (c) 1997-2010, International Business Machines Corporation and
b75a7d8f
A
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7#include "unicode/utypes.h"
8
9#if !UCONFIG_NO_FORMATTING
10
11#include "dtfmrgts.h"
12
13#include "unicode/timezone.h"
14#include "unicode/gregocal.h"
15#include "unicode/smpdtfmt.h"
16#include "unicode/datefmt.h"
17#include "unicode/simpletz.h"
18#include "unicode/resbund.h"
19
20// *****************************************************************************
21// class DateFormatRegressionTest
22// *****************************************************************************
23
24#define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
25
26void
27DateFormatRegressionTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
28{
29 // if (exec) logln((UnicodeString)"TestSuite DateFormatRegressionTest");
30 switch (index) {
31 CASE(0,Test4029195)
32 CASE(1,Test4052408)
33 CASE(2,Test4056591)
34 CASE(3,Test4059917)
35 CASE(4,Test4060212)
36 CASE(5,Test4061287)
37 CASE(6,Test4065240)
38 CASE(7,Test4071441)
39 CASE(8,Test4073003)
40 CASE(9,Test4089106)
41 CASE(10,Test4100302)
42 CASE(11,Test4101483)
43 CASE(12,Test4103340)
44 CASE(13,Test4103341)
45 CASE(14,Test4104136)
46 CASE(15,Test4104522)
47 CASE(16,Test4106807)
48 CASE(17,Test4108407)
49 CASE(18,Test4134203)
50 CASE(19,Test4151631)
51 CASE(20,Test4151706)
52 CASE(21,Test4162071)
53 CASE(22,Test4182066)
54 CASE(23,Test4210209)
55 CASE(24,Test714)
56 CASE(25,Test1684)
46f4442e 57 CASE(26,Test5554)
b75a7d8f
A
58 default: name = ""; break;
59 }
60}
61
62/**
63 * @bug 4029195
64 */
65void DateFormatRegressionTest::Test4029195(void)
66{
67 UErrorCode status = U_ZERO_ERROR;
68
69 UDate today = Calendar::getNow();
70 logln((UnicodeString) "today: " + today);
71
72 SimpleDateFormat *sdf = (SimpleDateFormat*) DateFormat::createDateInstance();
73c04bcf
A
73 if (failure(status, "SimpleDateFormat::createDateInstance")) {
74 return;
75 }
b75a7d8f 76 UnicodeString pat;
73c04bcf
A
77 if(sdf == NULL){
78 dataerrln("Error calling DateFormat::createDateTimeInstance");
79 return;
80 }
81
b75a7d8f
A
82 pat = sdf->toPattern(pat);
83 logln("pattern: " + pat);
84 UnicodeString fmtd;
85 FieldPosition pos(FieldPosition::DONT_CARE);
86 fmtd = sdf->format(today, fmtd, pos);
87 logln("today: " + fmtd);
88
89 sdf->applyPattern("G yyyy DDD");
90 UnicodeString todayS;
91 todayS = sdf->format(today, todayS, pos);
92 logln("today: " + todayS);
93 //try {
94 today = sdf->parse(todayS, status);
95 failure(status, "sdf->parse");
96 logln((UnicodeString)"today date: " + today);
97 /*} catch(Exception e) {
98 logln("Error reparsing date: " + e.getMessage());
99 }*/
100
101 //try {
102 UnicodeString rt;
103 rt = sdf->format(sdf->parse(todayS, status), rt, pos);
104 failure(status, "sdf->parse");
105 logln("round trip: " + rt);
106 if(rt != todayS)
107 errln("Fail: Want " + todayS + " Got " + rt);
108 /*}
109 catch (ParseException e) {
110 errln("Fail: " + e);
111 e.printStackTrace();
112 }*/
113
114 delete sdf;
115}
116
117/**
118 * @bug 4052408
119 */
120void DateFormatRegressionTest::Test4052408(void)
121{
122
123 DateFormat *fmt = DateFormat::createDateTimeInstance(DateFormat::SHORT,
124 DateFormat::SHORT, Locale::getUS());
73c04bcf
A
125 if (fmt == NULL) {
126 dataerrln("Error calling DateFormat::createDateTimeInstance");
127 return;
128 }
129
b75a7d8f
A
130 UDate dt = date(97, UCAL_MAY, 3, 8, 55);
131 UnicodeString str;
132 str = fmt->format(dt, str);
133 logln(str);
134
135 if(str != "5/3/97 8:55 AM")
136 errln("Fail: Test broken; Want 5/3/97 8:55 AM Got " + str);
137
138 UnicodeString expected[] = {
139 (UnicodeString) "", //"ERA_FIELD",
140 (UnicodeString) "97", //"YEAR_FIELD",
141 (UnicodeString) "5", //"MONTH_FIELD",
142 (UnicodeString) "3", //"DATE_FIELD",
143 (UnicodeString) "", //"HOUR_OF_DAY1_FIELD",
144 (UnicodeString) "", //"HOUR_OF_DAY0_FIELD",
145 (UnicodeString) "55", //"MINUTE_FIELD",
146 (UnicodeString) "", //"SECOND_FIELD",
147 (UnicodeString) "", //"MILLISECOND_FIELD",
148 (UnicodeString) "", //"DAY_OF_WEEK_FIELD",
149 (UnicodeString) "", //"DAY_OF_YEAR_FIELD",
150 (UnicodeString) "", //"DAY_OF_WEEK_IN_MONTH_FIELD",
151 (UnicodeString) "", //"WEEK_OF_YEAR_FIELD",
152 (UnicodeString) "", //"WEEK_OF_MONTH_FIELD",
153 (UnicodeString) "AM", //"AM_PM_FIELD",
154 (UnicodeString) "8", //"HOUR1_FIELD",
155 (UnicodeString) "", //"HOUR0_FIELD",
156 (UnicodeString) "" //"TIMEZONE_FIELD"
157 };
158
159 //Hashtable expected;// = new Hashtable();
160 //expected.put(new LongKey(DateFormat.MONTH_FIELD), "5");
161 //expected.put(new LongKey(DateFormat.DATE_FIELD), "3");
162 //expected.put(new LongKey(DateFormat.YEAR_FIELD), "97");
163 //expected.put(new LongKey(DateFormat.HOUR1_FIELD), "8");
164 //expected.put(new LongKey(DateFormat.MINUTE_FIELD), "55");
165 //expected.put(new LongKey(DateFormat.AM_PM_FIELD), "AM");
166
167 //StringBuffer buf = new StringBuffer();
168 UnicodeString fieldNames[] = {
169 (UnicodeString) "ERA_FIELD",
170 (UnicodeString) "YEAR_FIELD",
171 (UnicodeString) "MONTH_FIELD",
172 (UnicodeString) "DATE_FIELD",
173 (UnicodeString) "HOUR_OF_DAY1_FIELD",
174 (UnicodeString) "HOUR_OF_DAY0_FIELD",
175 (UnicodeString) "MINUTE_FIELD",
176 (UnicodeString) "SECOND_FIELD",
177 (UnicodeString) "MILLISECOND_FIELD",
178 (UnicodeString) "DAY_OF_WEEK_FIELD",
179 (UnicodeString) "DAY_OF_YEAR_FIELD",
180 (UnicodeString) "DAY_OF_WEEK_IN_MONTH_FIELD",
181 (UnicodeString) "WEEK_OF_YEAR_FIELD",
182 (UnicodeString) "WEEK_OF_MONTH_FIELD",
183 (UnicodeString) "AM_PM_FIELD",
184 (UnicodeString) "HOUR1_FIELD",
185 (UnicodeString) "HOUR0_FIELD",
186 (UnicodeString) "TIMEZONE_FIELD"
187 };
188
189 UBool pass = TRUE;
190 for(int i = 0; i <= 17; ++i) {
191 FieldPosition pos(i);
192 UnicodeString buf;
193 fmt->format(dt, buf, pos);
194 //char[] dst = new char[pos.getEndIndex() - pos.getBeginIndex()];
195 UnicodeString dst;
196 buf.extractBetween(pos.getBeginIndex(), pos.getEndIndex(), dst);
197 UnicodeString str(dst);
198 logln((UnicodeString)"" + i + (UnicodeString)": " + fieldNames[i] +
199 (UnicodeString)", \"" + str + (UnicodeString)"\", " +
200 pos.getBeginIndex() + (UnicodeString)", " +
201 pos.getEndIndex());
202 UnicodeString exp = expected[i];
203 if((exp.length() == 0 && str.length() == 0) || str == exp)
204 logln(" ok");
205 else {
206 errln(UnicodeString(" expected ") + exp);
207 pass = FALSE;
208 }
209
210 }
211 if( ! pass)
212 errln("Fail: FieldPosition not set right by DateFormat");
213
214 delete fmt;
215}
216
217/**
218 * @bug 4056591
219 * Verify the function of the [s|g]et2DigitYearStart() API.
220 */
221void DateFormatRegressionTest::Test4056591(void)
222{
223 UErrorCode status = U_ZERO_ERROR;
224
225 //try {
226 SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("yyMMdd"), Locale::getUS(), status);
729e4ab9
A
227 if (failure(status, "new SimpleDateFormat", TRUE)) {
228 delete fmt;
229 return;
230 }
b75a7d8f
A
231 UDate start = date(1809-1900, UCAL_DECEMBER, 25);
232 fmt->set2DigitYearStart(start, status);
233 failure(status, "fmt->setTwoDigitStartDate");
234 if( (fmt->get2DigitYearStart(status) != start) || failure(status, "get2DigitStartDate"))
235 errln("get2DigitYearStart broken");
236 UDate dates [] = {
237 date(1809-1900, UCAL_DECEMBER, 25),
238 date(1909-1900, UCAL_DECEMBER, 24),
239 date(1809-1900, UCAL_DECEMBER, 26),
240 date(1861-1900, UCAL_DECEMBER, 25),
241 };
242
243 UnicodeString strings [] = {
244 (UnicodeString) "091225",
245 (UnicodeString) "091224",
246 (UnicodeString) "091226",
247 (UnicodeString) "611225"
248 };
249
250 /*Object[] DATA = {
251 "091225", new Date(1809-1900, Calendar.DECEMBER, 25),
252 "091224", new Date(1909-1900, Calendar.DECEMBER, 24),
253 "091226", new Date(1809-1900, Calendar.DECEMBER, 26),
254 "611225", new Date(1861-1900, Calendar.DECEMBER, 25),
255 };*/
256
257 for(int i = 0; i < 4; i++) {
258 UnicodeString s = strings[i];
259 UDate exp = dates[i];
260 UDate got = fmt->parse(s, status);
261 failure(status, "fmt->parse");
262 logln(s + " -> " + got + "; exp " + exp);
263 if(got != exp)
264 errln("set2DigitYearStart broken");
265 }
266 /*}
267 catch (ParseException e) {
268 errln("Fail: " + e);
269 e.printStackTrace();
270 }*/
271
272 delete fmt;
273}
274
275/**
276 * @bug 4059917
277 */
278void DateFormatRegressionTest::Test4059917(void)
279{
280 UErrorCode status = U_ZERO_ERROR;
281
282 SimpleDateFormat *fmt;
283 UnicodeString myDate;
284
285 fmt = new SimpleDateFormat( UnicodeString("yyyy/MM/dd"), status );
729e4ab9 286 if (failure(status, "new SimpleDateFormat", TRUE)) return;
b75a7d8f
A
287 myDate = "1997/01/01";
288 aux917( fmt, myDate );
289
290 delete fmt;
291 fmt = NULL;
292
293 fmt = new SimpleDateFormat( UnicodeString("yyyyMMdd"), status );
73c04bcf 294 if(failure(status, "new SimpleDateFormat")) return;
b75a7d8f
A
295 myDate = "19970101";
296 aux917( fmt, myDate );
297
298 delete fmt;
299}
300
301void DateFormatRegressionTest::aux917( SimpleDateFormat *fmt, UnicodeString& str ) {
302 //try {
303 UnicodeString pat;
304 pat = fmt->toPattern(pat);
305 logln( "==================" );
306 logln( "testIt: pattern=" + pat +
307 " string=" + str );
308
309
310 Formattable o;
311 //Object o;
312 ParsePosition pos(0);
313 fmt->parseObject( str, o, pos );
314 //logln( UnicodeString("Parsed object: ") + o );
315
316 UErrorCode status = U_ZERO_ERROR;
317 UnicodeString formatted;
318 FieldPosition poss(FieldPosition::DONT_CARE);
319 formatted = fmt->format( o, formatted, poss, status );
320 failure(status, "fmt->format");
321 logln( "Formatted string: " + formatted );
322 if( formatted != str)
323 errln("Fail: Want " + str + " Got " + formatted);
324 /*}
325 catch (ParseException e) {
326 errln("Fail: " + e);
327 e.printStackTrace();
328 }*/
329}
330
331/**
332 * @bug 4060212
333 */
334void DateFormatRegressionTest::Test4060212(void)
335{
336 UnicodeString dateString = "1995-040.05:01:29";
337
338 logln( "dateString= " + dateString );
339 logln("Using yyyy-DDD.hh:mm:ss");
340 UErrorCode status = U_ZERO_ERROR;
341 SimpleDateFormat *formatter = new SimpleDateFormat(UnicodeString("yyyy-DDD.hh:mm:ss"), status);
729e4ab9 342 if (failure(status, "new SimpleDateFormat", TRUE)) return;
b75a7d8f
A
343 ParsePosition pos(0);
344 UDate myDate = formatter->parse( dateString, pos );
345 UnicodeString myString;
346 DateFormat *fmt = DateFormat::createDateTimeInstance( DateFormat::FULL,
347 DateFormat::LONG);
73c04bcf
A
348 if (fmt == NULL) {
349 dataerrln("Error calling DateFormat::createDateTimeInstance");
350 delete formatter;
351 return;
352 }
353
b75a7d8f
A
354 myString = fmt->format( myDate, myString);
355 logln( myString );
356
357 Calendar *cal = new GregorianCalendar(status);
358 failure(status, "new GregorianCalendar");
359 cal->setTime(myDate, status);
360 failure(status, "cal->setTime");
361 if ((cal->get(UCAL_DAY_OF_YEAR, status) != 40) || failure(status, "cal->get"))
362 errln((UnicodeString) "Fail: Got " + cal->get(UCAL_DAY_OF_YEAR, status) +
363 " Want 40");
364
46f4442e
A
365#if 0
366 // this is an odd usage of "ddd" and it doesn't
367 // work now that date values are range checked.
b75a7d8f
A
368 logln("Using yyyy-ddd.hh:mm:ss");
369 delete formatter;
370 formatter = NULL;
371 formatter = new SimpleDateFormat(UnicodeString("yyyy-ddd.hh:mm:ss"), status);
73c04bcf 372 if(failure(status, "new SimpleDateFormat")) return;
b75a7d8f
A
373 pos.setIndex(0);
374 myDate = formatter->parse( dateString, pos );
375 myString = fmt->format( myDate, myString );
376 logln( myString );
377 cal->setTime(myDate, status);
378 failure(status, "cal->setTime");
379 if ((cal->get(UCAL_DAY_OF_YEAR, status) != 40) || failure(status, "cal->get"))
380 errln((UnicodeString) "Fail: Got " + cal->get(UCAL_DAY_OF_YEAR, status) +
381 " Want 40");
46f4442e 382#endif
b75a7d8f
A
383
384 delete formatter;
385 delete fmt;
386 delete cal;
387}
388
389/**
390 * @bug 4061287
391 */
392void DateFormatRegressionTest::Test4061287(void)
393{
394 UErrorCode status = U_ZERO_ERROR;
395
396 SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("dd/MM/yyyy"), status);
729e4ab9
A
397 if (U_FAILURE(status)) {
398 dataerrln("Fail new SimpleDateFormat: %s", u_errorName(status));
399 delete df;
400 return;
b75a7d8f
A
401 }
402 failure(status, "new SimpleDateFormat");
403 //try {
46f4442e 404 logln(UnicodeString("") + df->parse("30/02/1971", status));
729e4ab9 405 failure(status, "df->parse(\"30/02/1971\")");
b75a7d8f
A
406 //logln(df.parse("35/01/1971").toString());
407 //}
408 /*catch (ParseException e) {
409 errln("Fail: " + e);
410 e.printStackTrace();
411 }*/
412 df->setLenient(FALSE);
413 UBool ok = FALSE;
414 //try {
46f4442e 415 logln(UnicodeString("") + df->parse("30/02/1971", status));
b75a7d8f
A
416 if(U_FAILURE(status))
417 ok = TRUE;
418 //logln(df.parse("35/01/1971").toString());
419 //} catch (ParseException e) {ok=TRUE;}
420 if(!ok)
421 errln("Fail: Lenient not working");
422 delete df;
423}
424
425/**
426 * @bug 4065240
427 */
428void DateFormatRegressionTest::Test4065240(void)
429{
430 UDate curDate;
431 DateFormat *shortdate, *fulldate;
432 UnicodeString strShortDate, strFullDate;
433 Locale saveLocale = Locale::getDefault();
434 TimeZone *saveZone = TimeZone::createDefault();
435
436 UErrorCode status = U_ZERO_ERROR;
437 //try {
438 Locale *curLocale = new Locale("de","DE");
439 Locale::setDefault(*curLocale, status);
440 failure(status, "Locale::setDefault");
441 // {sfb} adoptDefault instead of setDefault
442 //TimeZone::setDefault(TimeZone::createTimeZone("EST"));
443 TimeZone::adoptDefault(TimeZone::createTimeZone("EST"));
444 curDate = date(98, 0, 1);
445 shortdate = DateFormat::createDateInstance(DateFormat::SHORT);
73c04bcf
A
446 if (shortdate == NULL){
447 dataerrln("Error calling DateFormat::createDateInstance");
448 return;
449 }
450
b75a7d8f 451 fulldate = DateFormat::createDateTimeInstance(DateFormat::LONG, DateFormat::LONG);
73c04bcf
A
452 if (fulldate == NULL){
453 dataerrln("Error calling DateFormat::createDateTimeInstance");
454 return;
455 }
b75a7d8f
A
456 strShortDate = "The current date (short form) is ";
457 UnicodeString temp;
458 temp = shortdate->format(curDate, temp);
459 strShortDate += temp;
460 strFullDate = "The current date (long form) is ";
461 UnicodeString temp2;
462 fulldate->format(curDate, temp2);
463 strFullDate += temp2;
464
465 logln(strShortDate);
466 logln(strFullDate);
467
468 // {sfb} What to do with resource bundle stuff?????
469
470 // Check to see if the resource is present; if not, we can't test
471 ResourceBundle *bundle = new ResourceBundle(
374ca955 472 NULL, *curLocale, status);
b75a7d8f
A
473 failure(status, "new ResourceBundle");
474 //(UnicodeString) "java.text.resources.DateFormatZoneData", curLocale);
475
476 // {sfb} API change to ResourceBundle -- add getLocale()
477 /*if (bundle->getLocale().getLanguage(temp) == UnicodeString("de")) {
478 // UPDATE THIS AS ZONE NAME RESOURCE FOR <EST> in de_DE is updated
479 if (!strFullDate.endsWith(UnicodeString("GMT-05:00")))
480 errln("Fail: Want GMT-05:00");
481 }
482 else {
483 logln("*** TEST COULD NOT BE COMPLETED BECAUSE DateFormatZoneData ***");
484 logln("*** FOR LOCALE de OR de_DE IS MISSING ***");
485 }*/
486 //}
487 //finally {
488 Locale::setDefault(saveLocale, status);
489 failure(status, "Locale::setDefault");
490 TimeZone::setDefault(*saveZone);
491 //}
b75a7d8f
A
492 delete shortdate;
493 delete fulldate;
494 delete saveZone;
495 delete curLocale;
496 delete bundle;
497}
498
499/*
500 DateFormat.equals is too narrowly defined. As a result, MessageFormat
501 does not work correctly. DateFormat.equals needs to be written so
502 that the Calendar sub-object is not compared using Calendar.equals,
503 but rather compared for equivalency. This may necessitate adding a
504 (package private) method to Calendar to test for equivalency.
505
506 Currently this bug breaks MessageFormat.toPattern
507 */
508/**
509 * @bug 4071441
510 */
511void DateFormatRegressionTest::Test4071441(void)
512{
513 DateFormat *fmtA = DateFormat::createInstance();
514 DateFormat *fmtB = DateFormat::createInstance();
73c04bcf
A
515
516 if (fmtA == NULL || fmtB == NULL){
517 dataerrln("Error calling DateFormat::createInstance");
518 delete fmtA;
519 delete fmtB;
520 return;
521 }
522
b75a7d8f
A
523 // {sfb} Is it OK to cast away const here?
524 Calendar *calA = (Calendar*) fmtA->getCalendar();
525 Calendar *calB = (Calendar*) fmtB->getCalendar();
526 if(!calA || !calB) {
527 errln("Couldn't get proper calendars, exiting");
528 delete fmtA;
529 delete fmtB;
530 return;
531 }
532 UDate epoch = date(0, 0, 0);
533 UDate xmas = date(61, UCAL_DECEMBER, 25);
534
535 UErrorCode status = U_ZERO_ERROR;
536 calA->setTime(epoch, status);
537 failure(status, "calA->setTime");
538 calB->setTime(epoch, status);
539 failure(status, "calB->setTime");
540 if (*calA != *calB)
541 errln("Fail: Can't complete test; Calendar instances unequal");
542 if (*fmtA != *fmtB)
543 errln("Fail: DateFormat unequal when Calendars equal");
544 calB->setTime(xmas, status);
545 failure(status, "calB->setTime");
546 if (*calA == *calB)
547 errln("Fail: Can't complete test; Calendar instances equal");
548 if (*fmtA != *fmtB)
549 errln("Fail: DateFormat unequal when Calendars equivalent");
550
551 logln("DateFormat.equals ok");
552
553 delete fmtA;
554 delete fmtB;
555}
556
557/* The java.text.DateFormat.parse(String) method expects for the
558 US locale a string formatted according to mm/dd/yy and parses it
559 correctly.
560
46f4442e 561 When given a string mm/dd/yyyy [sic] it only parses up to the first
b75a7d8f
A
562 two y's, typically resulting in a date in the year 1919.
563
564 Please extend the parsing method(s) to handle strings with
565 four-digit year values (probably also applicable to various
566 other locales. */
567/**
568 * @bug 4073003
569 */
570void DateFormatRegressionTest::Test4073003(void)
571{
572 //try {
573 UErrorCode ec = U_ZERO_ERROR;
46f4442e 574 SimpleDateFormat fmt("MM/dd/yy", Locale::getUK(), ec);
b75a7d8f 575 if (U_FAILURE(ec)) {
729e4ab9 576 dataerrln("FAIL: SimpleDateFormat constructor - %s", u_errorName(ec));
b75a7d8f
A
577 return;
578 }
579 UnicodeString tests [] = {
580 (UnicodeString) "12/25/61",
581 (UnicodeString) "12/25/1961",
582 (UnicodeString) "4/3/2010",
583 (UnicodeString) "4/3/10"
584 };
585 UErrorCode status = U_ZERO_ERROR;
586 for(int i= 0; i < 4; i+=2) {
587 UDate d = fmt.parse(tests[i], status);
588 failure(status, "fmt.parse");
589 UDate dd = fmt.parse(tests[i+1], status);
590 failure(status, "fmt.parse");
591 UnicodeString s;
592 s = fmt.format(d, s);
593 UnicodeString ss;
594 ss = fmt.format(dd, ss);
595 if (d != dd)
596 errln((UnicodeString) "Fail: " + d + " != " + dd);
597 if (s != ss)
598 errln((UnicodeString)"Fail: " + s + " != " + ss);
599 logln("Ok: " + s + " " + d);
600 }
601}
602
603/**
604 * @bug 4089106
605 */
606void DateFormatRegressionTest::Test4089106(void)
607{
608 TimeZone *def = TimeZone::createDefault();
609 //try {
610 TimeZone *z = new SimpleTimeZone((int)(1.25 * 3600000), "FAKEZONE");
611 TimeZone::setDefault(*z);
612 UErrorCode status = U_ZERO_ERROR;
613 SimpleDateFormat *f = new SimpleDateFormat(status);
614 if(U_FAILURE(status)) {
729e4ab9 615 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
b75a7d8f
A
616 delete f;
617 delete def;
618 delete z;
619 return;
620 }
621 failure(status, "new SimpleDateFormat");
622 if (f->getTimeZone()!= *z)
623 errln("Fail: SimpleTimeZone should use TimeZone.getDefault()");
624
625 //}
626 //finally {
627 TimeZone::setDefault(*def);
628 //}
629
630 delete z;
631 delete f;
632 delete def;
633}
634
635/**
636 * @bug 4100302
637 */
638
639// {sfb} not applicable in C++??
640
641void DateFormatRegressionTest::Test4100302(void)
642{
643/* Locale locales [] = {
644 Locale::CANADA,
645 Locale::CANADA_FRENCH,
646 Locale::CHINA,
647 Locale::CHINESE,
648 Locale::ENGLISH,
649 Locale::FRANCE,
650 Locale::FRENCH,
651 Locale::GERMAN,
652 Locale::GERMANY,
653 Locale::ITALIAN,
654 Locale::ITALY,
655 Locale::JAPAN,
656 Locale::JAPANESE,
657 Locale::KOREA,
658 Locale::KOREAN,
659 Locale::PRC,
660 Locale::SIMPLIFIED_CHINESE,
661 Locale::TAIWAN,
662 Locale::TRADITIONAL_CHINESE,
663 Locale::UK,
664 Locale::US
665 };
666 //try {
667 UBool pass = TRUE;
668 for(int i = 0; i < 21; i++) {
669
670 Format *format = DateFormat::createDateTimeInstance(DateFormat::FULL,
671 DateFormat::FULL, locales[i]);
672 byte[] bytes;
673
674 ByteArrayOutputStream baos = new ByteArrayOutputStream();
675 ObjectOutputStream oos = new ObjectOutputStream(baos);
676
677 oos.writeObject(format);
678 oos.flush();
679
680 baos.close();
681 bytes = baos.toByteArray();
682
683 ObjectInputStream ois =
684 new ObjectInputStream(new ByteArrayInputStream(bytes));
685
686 if (!format.equals(ois.readObject())) {
687 pass = FALSE;
688 logln("DateFormat instance for locale " +
689 locales[i] + " is incorrectly serialized/deserialized.");
690 } else {
691 logln("DateFormat instance for locale " +
692 locales[i] + " is OKAY.");
693 }
694 }
695 if (!pass) errln("Fail: DateFormat serialization/equality bug");
696 }
697 catch (IOException e) {
698 errln("Fail: " + e);
699 e.printStackTrace();
700 }
701 catch (ClassNotFoundException e) {
702 errln("Fail: " + e);
703 e.printStackTrace();
704 }
705*/}
706
707/**
708 * @bug 4101483
709 */
710void DateFormatRegressionTest::Test4101483(void)
711{
712 UErrorCode status = U_ZERO_ERROR;
713 SimpleDateFormat *sdf = new SimpleDateFormat(UnicodeString("z"), Locale::getUS(), status);
729e4ab9 714 if (failure(status, "new SimpleDateFormat", TRUE)) return;
374ca955 715 FieldPosition fp(UDAT_TIMEZONE_FIELD);
b75a7d8f
A
716 //Date d = date(9234567890L);
717 UDate d = 9234567890.0;
718 //StringBuffer buf = new StringBuffer("");
719 UnicodeString buf;
720 sdf->format(d, buf, fp);
721 //logln(sdf.format(d, buf, fp).toString());
722 logln(dateToString(d) + " => " + buf);
723 logln("beginIndex = " + fp.getBeginIndex());
724 logln("endIndex = " + fp.getEndIndex());
725 if (fp.getBeginIndex() == fp.getEndIndex())
726 errln("Fail: Empty field");
727
728 delete sdf;
729}
730
731/**
732 * @bug 4103340
733 * @bug 4138203
734 * This bug really only works in Locale.US, since that's what the locale
735 * used for Date.toString() is. Bug 4138203 reports that it fails on Korean
736 * NT; it would actually have failed on any non-US locale. Now it should
737 * work on all locales.
738 */
739void DateFormatRegressionTest::Test4103340(void)
740{
741 UErrorCode status = U_ZERO_ERROR;
742
743 // choose a date that is the FIRST of some month
744 // and some arbitrary time
745 UDate d = date(97, 3, 1, 1, 1, 1);
746 SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("MMMM"), Locale::getUS(), status);
729e4ab9 747 if (failure(status, "new SimpleDateFormat", TRUE)) return;
b75a7d8f
A
748
749 UnicodeString s;
750 s = dateToString(d, s);
751 UnicodeString s2;
752 FieldPosition pos(FieldPosition::DONT_CARE);
753 s2 = df->format(d, s2, pos);
754 logln("Date=" + s);
755 logln("DF=" + s2);
756 UnicodeString substr;
757 s2.extract(0,2, substr);
758 if (s.indexOf(substr) == -1)
759 errln("Months should match");
760
761 delete df;
762}
763
764/**
765 * @bug 4103341
766 */
767void DateFormatRegressionTest::Test4103341(void)
768{
769 TimeZone *saveZone =TimeZone::createDefault();
770 //try {
771
772 // {sfb} changed from setDefault to adoptDefault
773 TimeZone::adoptDefault(TimeZone::createTimeZone("CST"));
774 UErrorCode status = U_ZERO_ERROR;
775 SimpleDateFormat *simple = new SimpleDateFormat(UnicodeString("MM/dd/yyyy HH:mm"), status);
776 if(U_FAILURE(status)) {
729e4ab9 777 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
b75a7d8f
A
778 delete simple;
779 return;
780 }
781 failure(status, "new SimpleDateFormat");
782 TimeZone *temp = TimeZone::createDefault();
783 if(simple->getTimeZone() != *temp)
784 errln("Fail: SimpleDateFormat not using default zone");
785 //}
786 //finally {
787 TimeZone::adoptDefault(saveZone);
788 //}
789
790 delete temp;
791 delete simple;
792}
793
794/**
795 * @bug 4104136
796 */
797void DateFormatRegressionTest::Test4104136(void)
798{
799 UErrorCode status = U_ZERO_ERROR;
800 SimpleDateFormat *sdf = new SimpleDateFormat(status);
801 if(U_FAILURE(status)) {
729e4ab9 802 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
b75a7d8f
A
803 delete sdf;
804 return;
805 }
73c04bcf 806 if(failure(status, "new SimpleDateFormat")) return;
b75a7d8f
A
807 UnicodeString pattern = "'time' hh:mm";
808 sdf->applyPattern(pattern);
809 logln("pattern: \"" + pattern + "\"");
810
811 UnicodeString strings [] = {
812 (UnicodeString)"time 10:30",
813 (UnicodeString) "time 10:x",
814 (UnicodeString) "time 10x"
815 };
816
817 ParsePosition ppos [] = {
818 ParsePosition(10),
819 ParsePosition(0),
820 ParsePosition(0)
821 };
822
823 UDate dates [] = {
824 date(70, UCAL_JANUARY, 1, 10, 30),
825 -1,
826 -1
827 };
828
829 /*Object[] DATA = {
830 "time 10:30", new ParsePosition(10), new Date(70, Calendar.JANUARY, 1, 10, 30),
831 "time 10:x", new ParsePosition(0), null,
832 "time 10x", new ParsePosition(0), null,
833 };*/
834
835 for(int i = 0; i < 3; i++) {
836 UnicodeString text = strings[i];
837 ParsePosition finish = ppos[i];
838 UDate exp = dates[i];
839
840 ParsePosition pos(0);
841 UDate d = sdf->parse(text, pos);
842 logln(" text: \"" + text + "\"");
843 logln(" index: %d", pos.getIndex());
844 logln((UnicodeString) " result: " + d);
845 if(pos.getIndex() != finish.getIndex())
846 errln("Fail: Expected pos " + finish.getIndex());
847 if (! ((d == 0 && exp == -1) || (d == exp)))
848 errln((UnicodeString) "Fail: Expected result " + exp);
849 }
850
851 delete sdf;
852}
853
854/**
855 * @bug 4104522
856 * CANNOT REPRODUCE
857 * According to the bug report, this test should throw a
858 * StringIndexOutOfBoundsException during the second parse. However,
859 * this is not seen.
860 */
861void DateFormatRegressionTest::Test4104522(void)
862{
863 UErrorCode status = U_ZERO_ERROR;
864
865 SimpleDateFormat *sdf = new SimpleDateFormat(status);
866 if(U_FAILURE(status)) {
729e4ab9 867 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
b75a7d8f
A
868 delete sdf;
869 return;
870 }
871 failure(status, "new SimpleDateFormat");
872 UnicodeString pattern = "'time' hh:mm";
873 sdf->applyPattern(pattern);
874 logln("pattern: \"" + pattern + "\"");
875
876 // works correctly
877 ParsePosition pp(0);
878 UnicodeString text = "time ";
879 UDate dt = sdf->parse(text, pp);
880 logln(" text: \"" + text + "\"" +
881 " date: " + dt);
882
883 // works wrong
884 pp.setIndex(0);
885 text = "time";
886 dt = sdf->parse(text, pp);
887 logln(" text: \"" + text + "\"" +
888 " date: " + dt);
889
890 delete sdf;
891}
892
893/**
894 * @bug 4106807
895 */
896void DateFormatRegressionTest::Test4106807(void)
897{
898 UDate dt;
899 DateFormat *df = DateFormat::createDateTimeInstance();
900
901 UErrorCode status = U_ZERO_ERROR;
902 SimpleDateFormat *sdfs [] = {
903 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss"), status),
904 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'Z'"), status),
905 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss''"), status),
906 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss'a''a'"), status),
907 new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss %"), status)
908 };
909 if(U_FAILURE(status)) {
729e4ab9 910 dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
b75a7d8f
A
911 delete sdfs[0];
912 delete sdfs[1];
913 delete sdfs[2];
914 delete sdfs[3];
915 delete sdfs[4];
916 return;
917 }
918
919 failure(status, "new SimpleDateFormat");
920
921 UnicodeString strings [] = {
922 (UnicodeString) "19980211140000",
923 (UnicodeString) "19980211140000",
924 (UnicodeString) "19980211140000",
925 (UnicodeString) "19980211140000a",
926 (UnicodeString) "19980211140000 "
927 };
928
929 /*Object[] data = {
930 new SimpleDateFormat("yyyyMMddHHmmss"), "19980211140000",
931 new SimpleDateFormat("yyyyMMddHHmmss'Z'"), "19980211140000",
932 new SimpleDateFormat("yyyyMMddHHmmss''"), "19980211140000",
933 new SimpleDateFormat("yyyyMMddHHmmss'a''a'"), "19980211140000a",
934 new SimpleDateFormat("yyyyMMddHHmmss %"), "19980211140000 ",
935 };*/
936 GregorianCalendar *gc = new GregorianCalendar(status);
937 failure(status, "new GregorianCalendar");
938 TimeZone *timeZone = TimeZone::createDefault();
939
940 TimeZone *gmt = timeZone->clone();
941
942 gmt->setRawOffset(0);
943
944 for(int32_t i = 0; i < 5; i++) {
945 SimpleDateFormat *format = sdfs[i];
946 UnicodeString dateString = strings[i];
947 //try {
948 format->setTimeZone(*gmt);
949 dt = format->parse(dateString, status);
950 // {sfb} some of these parses will fail purposely
951 if(U_FAILURE(status))
952 break;
953 status = U_ZERO_ERROR;
954 UnicodeString fmtd;
955 FieldPosition pos(FieldPosition::DONT_CARE);
956 fmtd = df->format(dt, fmtd, pos);
957 logln(fmtd);
958 //logln(df->format(dt));
959 gc->setTime(dt, status);
960 failure(status, "gc->getTime");
961 logln(UnicodeString("") + gc->get(UCAL_ZONE_OFFSET, status));
962 failure(status, "gc->get");
963 UnicodeString s;
964 s = format->format(dt, s, pos);
965 logln(s);
966 /*}
967 catch (ParseException e) {
968 logln("No way Jose");
969 }*/
970 }
971
972 delete timeZone;
973 delete df;
974 for(int32_t j = 0; j < 5; j++)
975 delete sdfs [j];
976 delete gc;
977 delete gmt;
978}
979
980/*
981 Synopsis: Chinese time zone CTT is not recogonized correctly.
982 Description: Platform Chinese Windows 95 - ** Time zone set to CST **
983 */
984/**
985 * @bug 4108407
986 */
987
988// {sfb} what to do with this one ??
989void DateFormatRegressionTest::Test4108407(void)
990{
991 /*long l = System.currentTimeMillis();
992 logln("user.timezone = " + System.getProperty("user.timezone", "?"));
993 logln("Time Zone :" +
994 DateFormat.getDateInstance().getTimeZone().getID());
995 logln("Default format :" +
996 DateFormat.getDateInstance().format(new Date(l)));
997 logln("Full format :" +
998 DateFormat.getDateInstance(DateFormat.FULL).format(new
999 Date(l)));
1000 logln("*** Set host TZ to CST ***");
1001 logln("*** THE RESULTS OF THIS TEST MUST BE VERIFIED MANUALLY ***");*/
1002}
1003
1004/**
1005 * @bug 4134203
1006 * SimpleDateFormat won't parse "GMT"
1007 */
1008void DateFormatRegressionTest::Test4134203(void)
1009{
1010 UErrorCode status = U_ZERO_ERROR;
1011 UnicodeString dateFormat = "MM/dd/yy HH:mm:ss zzz";
1012 SimpleDateFormat *fmt = new SimpleDateFormat(dateFormat, status);
729e4ab9 1013 if (failure(status, "new SimpleDateFormat", TRUE)) return;
b75a7d8f
A
1014 ParsePosition p0(0);
1015 UDate d = fmt->parse("01/22/92 04:52:00 GMT", p0);
1016 logln(dateToString(d));
1017 if(p0 == ParsePosition(0))
1018 errln("Fail: failed to parse 'GMT'");
1019 // In the failure case an exception is thrown by parse();
1020 // if no exception is thrown, the test passes.
1021
1022 delete fmt;
1023}
1024
1025/**
1026 * @bug 4151631
1027 * SimpleDateFormat incorrect handling of 2 single quotes in format()
1028 */
1029void DateFormatRegressionTest::Test4151631(void)
1030{
1031 UnicodeString pattern = "'TO_DATE('''dd'-'MM'-'yyyy HH:mm:ss''' , ''DD-MM-YYYY HH:MI:SS'')'";
1032 logln("pattern=" + pattern);
1033 UErrorCode status = U_ZERO_ERROR;
1034 SimpleDateFormat *format = new SimpleDateFormat(pattern, Locale::getUS(), status);
729e4ab9 1035 if (failure(status, "new SimpleDateFormat", TRUE)) return;
b75a7d8f
A
1036 UnicodeString result;
1037 FieldPosition pos(FieldPosition::DONT_CARE);
1038 result = format->format(date(1998-1900, UCAL_JUNE, 30, 13, 30, 0), result, pos);
1039 if (result != "TO_DATE('30-06-1998 13:30:00' , 'DD-MM-YYYY HH:MI:SS')") {
1040 errln("Fail: result=" + result);
1041 }
1042 else {
1043 logln("Pass: result=" + result);
1044 }
1045
1046 delete format;
1047}
1048
1049/**
1050 * @bug 4151706
1051 * 'z' at end of date format throws index exception in SimpleDateFormat
1052 * CANNOT REPRODUCE THIS BUG ON 1.2FCS
1053 */
1054void DateFormatRegressionTest::Test4151706(void)
1055{
1056 UnicodeString dateString("Thursday, 31-Dec-98 23:00:00 GMT");
1057 UErrorCode status = U_ZERO_ERROR;
1058 SimpleDateFormat fmt(UnicodeString("EEEE, dd-MMM-yy HH:mm:ss z"), Locale::getUS(), status);
729e4ab9 1059 if (failure(status, "new SimpleDateFormat", TRUE)) return;
b75a7d8f
A
1060 //try {
1061 UDate d = fmt.parse(dateString, status);
1062 failure(status, "fmt->parse");
1063 // {sfb} what about next two lines?
1064 //if (d.getTime() != Date.UTC(1998-1900, Calendar.DECEMBER, 31, 23, 0, 0))
1065 // errln("Incorrect value: " + d);
1066 /*} catch (Exception e) {
1067 errln("Fail: " + e);
1068 }*/
1069 UnicodeString temp;
1070 FieldPosition pos(0);
1071 logln(dateString + " -> " + fmt.format(d, temp, pos));
1072}
1073
1074/**
1075 * @bug 4162071
1076 * Cannot reproduce this bug under 1.2 FCS -- it may be a convoluted duplicate
1077 * of some other bug that has been fixed.
1078 */
1079void
1080DateFormatRegressionTest::Test4162071(void)
1081{
1082 UnicodeString dateString("Thu, 30-Jul-1999 11:51:14 GMT");
1083 UnicodeString format("EEE', 'dd-MMM-yyyy HH:mm:ss z"); // RFC 822/1123
1084 UErrorCode status = U_ZERO_ERROR;
1085 SimpleDateFormat df(format, Locale::getUS(), status);
73c04bcf 1086 if(U_FAILURE(status)) {
729e4ab9 1087 dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status));
73c04bcf
A
1088 return;
1089 }
b75a7d8f
A
1090
1091 //try {
1092 UDate x = df.parse(dateString, status);
1093 if(U_SUCCESS(status))
1094 logln("Parse format \"" + format + "\" ok");
1095 else
1096 errln("Parse format \"" + format + "\" failed.");
1097 UnicodeString temp;
1098 FieldPosition pos(0);
1099 logln(dateString + " -> " + df.format(x, temp, pos));
1100 //} catch (Exception e) {
1101 // errln("Parse format \"" + format + "\" failed.");
1102 //}
1103}
1104
1105/**
1106 * DateFormat shouldn't parse year "-1" as a two-digit year (e.g., "-1" -> 1999).
1107 */
1108void DateFormatRegressionTest::Test4182066(void) {
1109 UErrorCode status = U_ZERO_ERROR;
1110 SimpleDateFormat fmt("MM/dd/yy", Locale::getUS(), status);
1111 SimpleDateFormat dispFmt("MMM dd yyyy GG", Locale::getUS(), status);
1112 if (U_FAILURE(status)) {
729e4ab9 1113 dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status));
b75a7d8f
A
1114 return;
1115 }
1116
1117 /* We expect 2-digit year formats to put 2-digit years in the right
1118 * window. Out of range years, that is, anything less than "00" or
1119 * greater than "99", are treated as literal years. So "1/2/3456"
1120 * becomes 3456 AD. Likewise, "1/2/-3" becomes -3 AD == 2 BC.
1121 */
1122 const char* STRINGS[] = {
1123 "02/29/00",
1124 "01/23/01",
1125 "04/05/-1",
1126 "01/23/-9",
1127 "11/12/1314",
1128 "10/31/1",
1129 "09/12/+1",
1130 "09/12/001",
1131 };
1132 int32_t STRINGS_COUNT = (int32_t)(sizeof(STRINGS) / sizeof(STRINGS[0]));
1133 UDate FAIL_DATE = (UDate) 0;
1134 UDate DATES[] = {
1135 date(2000-1900, UCAL_FEBRUARY, 29),
1136 date(2001-1900, UCAL_JANUARY, 23),
1137 date( -1-1900, UCAL_APRIL, 5),
1138 date( -9-1900, UCAL_JANUARY, 23),
1139 date(1314-1900, UCAL_NOVEMBER, 12),
1140 date( 1-1900, UCAL_OCTOBER, 31),
1141 FAIL_DATE, // "+1" isn't recognized by US NumberFormat
1142 date( 1-1900, UCAL_SEPTEMBER,12),
1143 };
1144
1145 UnicodeString out;
1146 UBool pass = TRUE;
1147 for (int32_t i=0; i<STRINGS_COUNT; ++i) {
1148 UnicodeString str(STRINGS[i]);
1149 UDate expected = DATES[i];
1150 status = U_ZERO_ERROR;
1151 UDate actual = fmt.parse(str, status);
1152 if (U_FAILURE(status)) {
1153 actual = FAIL_DATE;
1154 }
1155 UnicodeString actStr;
1156 if (actual == FAIL_DATE) {
1157 actStr.append("null");
1158 } else {
1159 // Yuck: See j25
1160 ((DateFormat*)&dispFmt)->format(actual, actStr);
1161 }
1162
1163 if (expected == actual) {
1164 out.append(str + " => " + actStr + "\n");
1165 } else {
1166 UnicodeString expStr;
1167 if (expected == FAIL_DATE) {
1168 expStr.append("null");
1169 } else {
1170 // Yuck: See j25
1171 ((DateFormat*)&dispFmt)->format(expected, expStr);
1172 }
1173 out.append("FAIL: " + str + " => " + actStr
1174 + ", expected " + expStr + "\n");
1175 pass = FALSE;
1176 }
1177 }
1178 if (pass) {
1179 log(out);
1180 } else {
1181 err(out);
1182 }
1183}
1184
1185/**
1186 * j32 {JDK Bug 4210209 4209272}
1187 * DateFormat cannot parse Feb 29 2000 when setLenient(false)
1188 */
1189void
1190DateFormatRegressionTest::Test4210209(void) {
1191 UErrorCode status = U_ZERO_ERROR;
1192 UnicodeString pattern("MMM d, yyyy");
1193 SimpleDateFormat sfmt(pattern, Locale::getUS(), status);
1194 SimpleDateFormat sdisp("MMM dd yyyy GG", Locale::getUS(), status);
1195 DateFormat& fmt = *(DateFormat*)&sfmt; // Yuck: See j25
1196 DateFormat& disp = *(DateFormat*)&sdisp; // Yuck: See j25
1197 if (U_FAILURE(status)) {
729e4ab9 1198 dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status));
b75a7d8f
A
1199 return;
1200 }
1201 Calendar* calx = (Calendar*)fmt.getCalendar(); // cast away const!
1202 calx->setLenient(FALSE);
1203 UDate d = date(2000-1900, UCAL_FEBRUARY, 29);
1204 UnicodeString s, ss;
1205 fmt.format(d, s);
1206 logln(disp.format(d, ss.remove()) + " f> " + pattern +
1207 " => \"" + s + "\"");
1208 ParsePosition pos(0);
1209 d = fmt.parse(s, pos);
1210 logln(UnicodeString("\"") + s + "\" p> " + pattern +
1211 " => " + disp.format(d, ss.remove()));
1212 logln(UnicodeString("Parse pos = ") + pos.getIndex() +
1213 ", error pos = " + pos.getErrorIndex());
1214 if (pos.getErrorIndex() != -1) {
1215 errln(UnicodeString("FAIL: Error index should be -1"));
1216 }
1217
1218 // The underlying bug is in GregorianCalendar. If the following lines
1219 // succeed, the bug is fixed. If the bug isn't fixed, they will throw
1220 // an exception.
1221 GregorianCalendar cal(status);
1222 if (U_FAILURE(status)) {
1223 errln("FAIL: Unable to create Calendar");
1224 return;
1225 }
1226 cal.clear();
1227 cal.setLenient(FALSE);
1228 cal.set(2000, UCAL_FEBRUARY, 29); // This should work!
1229 logln(UnicodeString("Attempt to set Calendar to Feb 29 2000: ") +
1230 disp.format(cal.getTime(status), ss.remove()));
1231 if (U_FAILURE(status)) {
1232 errln("FAIL: Unable to set Calendar to Feb 29 2000");
1233 }
1234}
1235
1236void DateFormatRegressionTest::Test714(void)
1237{
1238 //try {
1239 UDate d(978103543000.);
1240 DateFormat *fmt = DateFormat::createDateTimeInstance(DateFormat::NONE,
1241 DateFormat::MEDIUM,
1242 Locale::getUS());
73c04bcf
A
1243 if (fmt == NULL) {
1244 dataerrln("Error calling DateFormat::createDateTimeInstance");
1245 return;
1246 }
1247
b75a7d8f
A
1248 UnicodeString s;
1249 UnicodeString tests =
1250 (UnicodeString) "7:25:43 AM" ;
1251 UErrorCode status = U_ZERO_ERROR;
1252 fmt->format (d,s);
1253 if(U_FAILURE(status))
1254 {
1255 errln((UnicodeString) "Fail, errmsg " + u_errorName(status));
1256 return;
1257 }
1258
1259 if(s != tests)
1260 {
1261 errln((UnicodeString) "Fail: " + s + " != " + tests);
1262 }
1263 else
1264 {
1265 logln("OK: " + s + " == " + tests);
1266 }
1267
1268 delete fmt;
1269}
1270
1271class Test1684Data {
1272public:
1273 int32_t year;
1274 int32_t month;
1275 int32_t date;
1276 int32_t womyear;
1277 int32_t wommon;
1278 int32_t wom;
1279 int32_t dow;
1280 UnicodeString data;
1281 UnicodeString normalized;
1282
1283 Test1684Data(int32_t xyear, int32_t xmonth, int32_t xdate,
1284 int32_t xwomyear, int32_t xwommon, int32_t xwom, int32_t xdow,
1285 const char *xdata, const char *xnormalized) :
1286 year(xyear),
1287 month(xmonth-1),
1288 date(xdate),
1289 womyear(xwomyear),
1290 wommon(xwommon-1),
1291 wom(xwom),
1292 dow(xdow),
1293 data(xdata,""),
1294 normalized((xnormalized==NULL)?xdata:xnormalized,"")
1295 { }
1296};
1297
1298void DateFormatRegressionTest::Test1684(void)
1299{
1300 // July 2001 August 2001 January 2002
1301 // Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1302 // 1 2 3 4 5 6 7 1 2 3 4 1 2 3 4 5
1303 // 8 9 10 11 12 13 14 5 6 7 8 9 10 11 6 7 8 9 10 11 12
1304 // 15 16 17 18 19 20 21 12 13 14 15 16 17 18 13 14 15 16 17 18 19
1305 // 22 23 24 25 26 27 28 19 20 21 22 23 24 25 20 21 22 23 24 25 26
1306 // 29 30 31 26 27 28 29 30 31 27 28 29 30 31
1307 Test1684Data *tests[] = {
1308 new Test1684Data(2001, 8, 6, 2001,8,2,UCAL_MONDAY, "2001 08 02 Mon", NULL),
1309 new Test1684Data(2001, 8, 7, 2001,8,2,UCAL_TUESDAY, "2001 08 02 Tue", NULL),
1310 new Test1684Data(2001, 8, 5,/*12,*/ 2001,8,2,UCAL_SUNDAY, "2001 08 02 Sun", NULL),
1311 new Test1684Data(2001, 8,6, /*7, 30,*/ 2001,7,6,UCAL_MONDAY, "2001 07 06 Mon", "2001 08 02 Mon"),
1312 new Test1684Data(2001, 8,7, /*7, 31,*/ 2001,7,6,UCAL_TUESDAY, "2001 07 06 Tue", "2001 08 02 Tue"),
1313 new Test1684Data(2001, 8, 5, 2001,7,6,UCAL_SUNDAY, "2001 07 06 Sun", "2001 08 02 Sun"),
1314 new Test1684Data(2001, 7, 30, 2001,8,1,UCAL_MONDAY, "2001 08 01 Mon", "2001 07 05 Mon"),
1315 new Test1684Data(2001, 7, 31, 2001,8,1,UCAL_TUESDAY, "2001 08 01 Tue", "2001 07 05 Tue"),
1316 new Test1684Data(2001, 7,29, /*8, 5,*/ 2001,8,1,UCAL_SUNDAY, "2001 08 01 Sun", "2001 07 05 Sun"),
1317 new Test1684Data(2001, 12, 31, 2001,12,6,UCAL_MONDAY, "2001 12 06 Mon", NULL),
1318 new Test1684Data(2002, 1, 1, 2002,1,1,UCAL_TUESDAY, "2002 01 01 Tue", NULL),
1319 new Test1684Data(2002, 1, 2, 2002,1,1,UCAL_WEDNESDAY, "2002 01 01 Wed", NULL),
1320 new Test1684Data(2002, 1, 3, 2002,1,1,UCAL_THURSDAY, "2002 01 01 Thu", NULL),
1321 new Test1684Data(2002, 1, 4, 2002,1,1,UCAL_FRIDAY, "2002 01 01 Fri", NULL),
1322 new Test1684Data(2002, 1, 5, 2002,1,1,UCAL_SATURDAY, "2002 01 01 Sat", NULL),
1323 new Test1684Data(2001,12,30, /*2002, 1, 6,*/ 2002,1,1,UCAL_SUNDAY, "2002 01 01 Sun", "2001 12 06 Sun")
1324 };
1325
1326#define kTest1684Count ((int32_t)(sizeof(tests)/sizeof(tests[0])))
1327
1328 int32_t pass = 0, error = 0, warning = 0;
1329 int32_t i;
1330
1331 UErrorCode status = U_ZERO_ERROR;
1332 UnicodeString pattern("yyyy MM WW EEE","");
1333 Calendar *cal = new GregorianCalendar(status);
1334 SimpleDateFormat *sdf = new SimpleDateFormat(pattern,status);
73c04bcf
A
1335 if (U_FAILURE(status)) {
1336 dataerrln("Error constructing SimpleDateFormat");
1337 for(i=0;i<kTest1684Count;i++) {
1338 delete tests[i];
1339 }
1340 delete cal;
1341 delete sdf;
1342 return;
1343 }
b75a7d8f
A
1344 cal->setFirstDayOfWeek(UCAL_SUNDAY);
1345 cal->setMinimalDaysInFirstWeek(1);
1346
1347 sdf->adoptCalendar(cal);
1348
1349 cal = sdf->getCalendar()->clone(); // sdf may have deleted calendar
1350
1351 if(!cal || !sdf || U_FAILURE(status)) {
1352 errln(UnicodeString("Error setting up test: ") + u_errorName(status));
1353 }
1354
1355 for (i = 0; i < kTest1684Count; ++i) {
1356 Test1684Data &test = *(tests[i]);
1357 logln(UnicodeString("#") + i + UnicodeString("\n-----\nTesting round trip of ") + test.year +
1358 " " + (test.month + 1) +
1359 " " + test.date +
1360 " (written as) " + test.data);
1361
1362 cal->clear();
1363 cal->set(test.year, test.month, test.date);
1364 UDate ms = cal->getTime(status);
1365
1366 cal->clear();
1367 cal->set(UCAL_YEAR, test.womyear);
1368 cal->set(UCAL_MONTH, test.wommon);
1369 cal->set(UCAL_WEEK_OF_MONTH, test.wom);
1370 cal->set(UCAL_DAY_OF_WEEK, test.dow);
1371 UDate ms2 = cal->getTime(status);
1372
1373 if (ms2 != ms) {
1374 errln((UnicodeString)"\nError: GregorianUCAL_DOM gave " + ms +
1375 "\n GregorianUCAL_WOM gave " + ms2);
1376 error++;
1377 } else {
1378 pass++;
1379 }
1380
1381 ms2 = sdf->parse(test.data, status);
1382 if(U_FAILURE(status)) {
1383 errln("parse exception: " + UnicodeString(u_errorName(status)));
1384 }
1385
1386 if (ms2!=ms) {
1387 errln((UnicodeString)"\nError: GregorianCalendar gave " + ms +
1388 "\n SimpleDateFormat.parse gave " + ms2);
1389 error++;
1390 } else {
1391 pass++;
1392 }
1393
1394 UnicodeString result;
1395 sdf->format(ms, result);
1396 if (result != test.normalized) {
1397 errln("\nWarning: format of '" + test.data + "' gave" +
1398 "\n '" + result + "'" +
1399 "\n expected '" + test.normalized + "'");
1400 warning++;
1401 } else {
1402 pass++;
1403 }
1404
1405 UDate ms3;
1406 ms3 = sdf->parse(result, status);
1407 if(U_FAILURE(status)) {
1408 errln("parse exception 2: " + (UnicodeString)u_errorName(status));
1409 }
1410
1411 if (ms3!=ms) {
1412 error++;
1413 errln((UnicodeString)"\nError: Re-parse of '" + result + "' gave time of " +
1414 "\n " + ms3 +
1415 "\n not " + ms);
1416 } else {
1417 pass++;
1418 }
1419 }
1420
1421 UnicodeString info
1422 = UnicodeString("Passed: ") + pass + ", Warnings: " + warning + ", Errors: " + error;
1423 if (error > 0) {
1424 errln(info);
1425 } else {
1426 logln(info);
1427 }
73c04bcf 1428
b75a7d8f
A
1429 for(i=0;i<kTest1684Count;i++) {
1430 delete tests[i];
1431 }
1432 delete cal;
1433 delete sdf;
1434}
1435
46f4442e
A
1436void DateFormatRegressionTest::Test5554(void)
1437{
1438 UErrorCode status = U_ZERO_ERROR;
1439 UnicodeString pattern("Z","");
1440 UnicodeString newfoundland("Canada/Newfoundland", "");
1441 TimeZone *zone = TimeZone::createTimeZone(newfoundland);
1442 Calendar *cal = new GregorianCalendar(zone, status);
1443 SimpleDateFormat *sdf = new SimpleDateFormat(pattern,status);
1444 if (U_FAILURE(status)) {
1445 dataerrln("Error constructing SimpleDateFormat");
1446 delete cal;
1447 delete sdf;
1448 return;
1449 }
1450 cal->set(2007, 1, 14);
1451 UDate date = cal->getTime(status);
1452 if (U_FAILURE(status)) {
1453 errln("Error getting time to format");
1454 return;
1455 };
1456 sdf->adoptCalendar(cal);
1457 UnicodeString result;
1458 UnicodeString correct("-0330", "");
1459 sdf->format(date, result);
1460 if (result != correct) {
1461 errln("\nError: Newfoundland Z of Jan 14, 2007 gave '" + result + "', expected '" + correct + "'");
1462 }
1463 delete sdf;
1464}
1465
b75a7d8f
A
1466#endif /* #if !UCONFIG_NO_FORMATTING */
1467
1468//eof