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