]>
Commit | Line | Data |
---|---|---|
1 | /******************************************************************** | |
2 | * COPYRIGHT: | |
3 | * Copyright (c) 1997-2012, International Business Machines Corporation and | |
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 | ||
26 | void | |
27 | DateFormatRegressionTest::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) | |
57 | CASE(26,Test5554) | |
58 | CASE(27,Test9237) | |
59 | CASE(28,TestParsing) | |
60 | default: name = ""; break; | |
61 | } | |
62 | } | |
63 | ||
64 | /** | |
65 | * @bug 4029195 | |
66 | */ | |
67 | void DateFormatRegressionTest::Test4029195(void) | |
68 | { | |
69 | UErrorCode status = U_ZERO_ERROR; | |
70 | ||
71 | UDate today = Calendar::getNow(); | |
72 | logln((UnicodeString) "today: " + today); | |
73 | ||
74 | SimpleDateFormat *sdf = (SimpleDateFormat*) DateFormat::createDateInstance(); | |
75 | if (failure(status, "SimpleDateFormat::createDateInstance")) { | |
76 | return; | |
77 | } | |
78 | UnicodeString pat; | |
79 | if(sdf == NULL){ | |
80 | dataerrln("Error calling DateFormat::createDateTimeInstance"); | |
81 | return; | |
82 | } | |
83 | ||
84 | pat = sdf->toPattern(pat); | |
85 | logln("pattern: " + pat); | |
86 | UnicodeString fmtd; | |
87 | FieldPosition pos(FieldPosition::DONT_CARE); | |
88 | fmtd = sdf->format(today, fmtd, pos); | |
89 | logln("today: " + fmtd); | |
90 | ||
91 | sdf->applyPattern("G yyyy DDD"); | |
92 | UnicodeString todayS; | |
93 | todayS = sdf->format(today, todayS, pos); | |
94 | logln("today: " + todayS); | |
95 | //try { | |
96 | today = sdf->parse(todayS, status); | |
97 | failure(status, "sdf->parse"); | |
98 | logln((UnicodeString)"today date: " + today); | |
99 | /*} catch(Exception e) { | |
100 | logln("Error reparsing date: " + e.getMessage()); | |
101 | }*/ | |
102 | ||
103 | //try { | |
104 | UnicodeString rt; | |
105 | rt = sdf->format(sdf->parse(todayS, status), rt, pos); | |
106 | failure(status, "sdf->parse"); | |
107 | logln("round trip: " + rt); | |
108 | if(rt != todayS) | |
109 | errln("Fail: Want " + todayS + " Got " + rt); | |
110 | /*} | |
111 | catch (ParseException e) { | |
112 | errln("Fail: " + e); | |
113 | e.printStackTrace(); | |
114 | }*/ | |
115 | ||
116 | delete sdf; | |
117 | } | |
118 | ||
119 | /** | |
120 | * @bug 4052408 | |
121 | */ | |
122 | void DateFormatRegressionTest::Test4052408(void) | |
123 | { | |
124 | ||
125 | DateFormat *fmt = DateFormat::createDateTimeInstance(DateFormat::SHORT, | |
126 | DateFormat::SHORT, Locale::getUS()); | |
127 | if (fmt == NULL) { | |
128 | dataerrln("Error calling DateFormat::createDateTimeInstance"); | |
129 | return; | |
130 | } | |
131 | ||
132 | UDate dt = date(97, UCAL_MAY, 3, 8, 55); | |
133 | UnicodeString str; | |
134 | str = fmt->format(dt, str); | |
135 | logln(str); | |
136 | ||
137 | if(str != "5/3/97, 8:55 AM") | |
138 | errln("Fail: Test broken; Want 5/3/97, 8:55 AM Got " + str); | |
139 | ||
140 | UnicodeString expected[] = { | |
141 | (UnicodeString) "", //"ERA_FIELD", | |
142 | (UnicodeString) "97", //"YEAR_FIELD", | |
143 | (UnicodeString) "5", //"MONTH_FIELD", | |
144 | (UnicodeString) "3", //"DATE_FIELD", | |
145 | (UnicodeString) "", //"HOUR_OF_DAY1_FIELD", | |
146 | (UnicodeString) "", //"HOUR_OF_DAY0_FIELD", | |
147 | (UnicodeString) "55", //"MINUTE_FIELD", | |
148 | (UnicodeString) "", //"SECOND_FIELD", | |
149 | (UnicodeString) "", //"MILLISECOND_FIELD", | |
150 | (UnicodeString) "", //"DAY_OF_WEEK_FIELD", | |
151 | (UnicodeString) "", //"DAY_OF_YEAR_FIELD", | |
152 | (UnicodeString) "", //"DAY_OF_WEEK_IN_MONTH_FIELD", | |
153 | (UnicodeString) "", //"WEEK_OF_YEAR_FIELD", | |
154 | (UnicodeString) "", //"WEEK_OF_MONTH_FIELD", | |
155 | (UnicodeString) "AM", //"AM_PM_FIELD", | |
156 | (UnicodeString) "8", //"HOUR1_FIELD", | |
157 | (UnicodeString) "", //"HOUR0_FIELD", | |
158 | (UnicodeString) "" //"TIMEZONE_FIELD" | |
159 | }; | |
160 | ||
161 | //Hashtable expected;// = new Hashtable(); | |
162 | //expected.put(new LongKey(DateFormat.MONTH_FIELD), "5"); | |
163 | //expected.put(new LongKey(DateFormat.DATE_FIELD), "3"); | |
164 | //expected.put(new LongKey(DateFormat.YEAR_FIELD), "97"); | |
165 | //expected.put(new LongKey(DateFormat.HOUR1_FIELD), "8"); | |
166 | //expected.put(new LongKey(DateFormat.MINUTE_FIELD), "55"); | |
167 | //expected.put(new LongKey(DateFormat.AM_PM_FIELD), "AM"); | |
168 | ||
169 | //StringBuffer buf = new StringBuffer(); | |
170 | UnicodeString fieldNames[] = { | |
171 | (UnicodeString) "ERA_FIELD", | |
172 | (UnicodeString) "YEAR_FIELD", | |
173 | (UnicodeString) "MONTH_FIELD", | |
174 | (UnicodeString) "DATE_FIELD", | |
175 | (UnicodeString) "HOUR_OF_DAY1_FIELD", | |
176 | (UnicodeString) "HOUR_OF_DAY0_FIELD", | |
177 | (UnicodeString) "MINUTE_FIELD", | |
178 | (UnicodeString) "SECOND_FIELD", | |
179 | (UnicodeString) "MILLISECOND_FIELD", | |
180 | (UnicodeString) "DAY_OF_WEEK_FIELD", | |
181 | (UnicodeString) "DAY_OF_YEAR_FIELD", | |
182 | (UnicodeString) "DAY_OF_WEEK_IN_MONTH_FIELD", | |
183 | (UnicodeString) "WEEK_OF_YEAR_FIELD", | |
184 | (UnicodeString) "WEEK_OF_MONTH_FIELD", | |
185 | (UnicodeString) "AM_PM_FIELD", | |
186 | (UnicodeString) "HOUR1_FIELD", | |
187 | (UnicodeString) "HOUR0_FIELD", | |
188 | (UnicodeString) "TIMEZONE_FIELD" | |
189 | }; | |
190 | ||
191 | UBool pass = TRUE; | |
192 | for(int i = 0; i <= 17; ++i) { | |
193 | FieldPosition pos(i); | |
194 | UnicodeString buf; | |
195 | fmt->format(dt, buf, pos); | |
196 | //char[] dst = new char[pos.getEndIndex() - pos.getBeginIndex()]; | |
197 | UnicodeString dst; | |
198 | buf.extractBetween(pos.getBeginIndex(), pos.getEndIndex(), dst); | |
199 | UnicodeString str(dst); | |
200 | logln((UnicodeString)"" + i + (UnicodeString)": " + fieldNames[i] + | |
201 | (UnicodeString)", \"" + str + (UnicodeString)"\", " + | |
202 | pos.getBeginIndex() + (UnicodeString)", " + | |
203 | pos.getEndIndex()); | |
204 | UnicodeString exp = expected[i]; | |
205 | if((exp.length() == 0 && str.length() == 0) || str == exp) | |
206 | logln(" ok"); | |
207 | else { | |
208 | errln(UnicodeString(" expected ") + exp); | |
209 | pass = FALSE; | |
210 | } | |
211 | ||
212 | } | |
213 | if( ! pass) | |
214 | errln("Fail: FieldPosition not set right by DateFormat"); | |
215 | ||
216 | delete fmt; | |
217 | } | |
218 | ||
219 | /** | |
220 | * @bug 4056591 | |
221 | * Verify the function of the [s|g]et2DigitYearStart() API. | |
222 | */ | |
223 | void DateFormatRegressionTest::Test4056591(void) | |
224 | { | |
225 | UErrorCode status = U_ZERO_ERROR; | |
226 | ||
227 | //try { | |
228 | SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("yyMMdd"), Locale::getUS(), status); | |
229 | if (failure(status, "new SimpleDateFormat", TRUE)) { | |
230 | delete fmt; | |
231 | return; | |
232 | } | |
233 | UDate start = date(1809-1900, UCAL_DECEMBER, 25); | |
234 | fmt->set2DigitYearStart(start, status); | |
235 | failure(status, "fmt->setTwoDigitStartDate"); | |
236 | if( (fmt->get2DigitYearStart(status) != start) || failure(status, "get2DigitStartDate")) | |
237 | errln("get2DigitYearStart broken"); | |
238 | UDate dates [] = { | |
239 | date(1809-1900, UCAL_DECEMBER, 25), | |
240 | date(1909-1900, UCAL_DECEMBER, 24), | |
241 | date(1809-1900, UCAL_DECEMBER, 26), | |
242 | date(1861-1900, UCAL_DECEMBER, 25), | |
243 | }; | |
244 | ||
245 | UnicodeString strings [] = { | |
246 | (UnicodeString) "091225", | |
247 | (UnicodeString) "091224", | |
248 | (UnicodeString) "091226", | |
249 | (UnicodeString) "611225" | |
250 | }; | |
251 | ||
252 | /*Object[] DATA = { | |
253 | "091225", new Date(1809-1900, Calendar.DECEMBER, 25), | |
254 | "091224", new Date(1909-1900, Calendar.DECEMBER, 24), | |
255 | "091226", new Date(1809-1900, Calendar.DECEMBER, 26), | |
256 | "611225", new Date(1861-1900, Calendar.DECEMBER, 25), | |
257 | };*/ | |
258 | ||
259 | for(int i = 0; i < 4; i++) { | |
260 | UnicodeString s = strings[i]; | |
261 | UDate exp = dates[i]; | |
262 | UDate got = fmt->parse(s, status); | |
263 | failure(status, "fmt->parse"); | |
264 | logln(s + " -> " + got + "; exp " + exp); | |
265 | if(got != exp) | |
266 | errln("set2DigitYearStart broken"); | |
267 | } | |
268 | /*} | |
269 | catch (ParseException e) { | |
270 | errln("Fail: " + e); | |
271 | e.printStackTrace(); | |
272 | }*/ | |
273 | ||
274 | delete fmt; | |
275 | } | |
276 | ||
277 | /** | |
278 | * @bug 4059917 | |
279 | */ | |
280 | void DateFormatRegressionTest::Test4059917(void) | |
281 | { | |
282 | UErrorCode status = U_ZERO_ERROR; | |
283 | ||
284 | SimpleDateFormat *fmt; | |
285 | UnicodeString myDate; | |
286 | ||
287 | fmt = new SimpleDateFormat( UnicodeString("yyyy/MM/dd"), status ); | |
288 | if (failure(status, "new SimpleDateFormat", TRUE)) return; | |
289 | myDate = "1997/01/01"; | |
290 | aux917( fmt, myDate ); | |
291 | ||
292 | delete fmt; | |
293 | fmt = NULL; | |
294 | ||
295 | fmt = new SimpleDateFormat( UnicodeString("yyyyMMdd"), status ); | |
296 | if(failure(status, "new SimpleDateFormat")) return; | |
297 | myDate = "19970101"; | |
298 | aux917( fmt, myDate ); | |
299 | ||
300 | delete fmt; | |
301 | } | |
302 | ||
303 | void DateFormatRegressionTest::aux917( SimpleDateFormat *fmt, UnicodeString& str ) { | |
304 | //try { | |
305 | UnicodeString pat; | |
306 | pat = fmt->toPattern(pat); | |
307 | logln( "==================" ); | |
308 | logln( "testIt: pattern=" + pat + | |
309 | " string=" + str ); | |
310 | ||
311 | ||
312 | Formattable o; | |
313 | //Object o; | |
314 | ParsePosition pos(0); | |
315 | fmt->parseObject( str, o, pos ); | |
316 | //logln( UnicodeString("Parsed object: ") + o ); | |
317 | ||
318 | UErrorCode status = U_ZERO_ERROR; | |
319 | UnicodeString formatted; | |
320 | FieldPosition poss(FieldPosition::DONT_CARE); | |
321 | formatted = fmt->format( o, formatted, poss, status ); | |
322 | failure(status, "fmt->format"); | |
323 | logln( "Formatted string: " + formatted ); | |
324 | if( formatted != str) | |
325 | errln("Fail: Want " + str + " Got " + formatted); | |
326 | /*} | |
327 | catch (ParseException e) { | |
328 | errln("Fail: " + e); | |
329 | e.printStackTrace(); | |
330 | }*/ | |
331 | } | |
332 | ||
333 | /** | |
334 | * @bug 4060212 | |
335 | */ | |
336 | void DateFormatRegressionTest::Test4060212(void) | |
337 | { | |
338 | UnicodeString dateString = "1995-040.05:01:29"; | |
339 | ||
340 | logln( "dateString= " + dateString ); | |
341 | logln("Using yyyy-DDD.hh:mm:ss"); | |
342 | UErrorCode status = U_ZERO_ERROR; | |
343 | SimpleDateFormat *formatter = new SimpleDateFormat(UnicodeString("yyyy-DDD.hh:mm:ss"), status); | |
344 | if (failure(status, "new SimpleDateFormat", TRUE)) return; | |
345 | ParsePosition pos(0); | |
346 | UDate myDate = formatter->parse( dateString, pos ); | |
347 | UnicodeString myString; | |
348 | DateFormat *fmt = DateFormat::createDateTimeInstance( DateFormat::FULL, | |
349 | DateFormat::LONG); | |
350 | if (fmt == NULL) { | |
351 | dataerrln("Error calling DateFormat::createDateTimeInstance"); | |
352 | delete formatter; | |
353 | return; | |
354 | } | |
355 | ||
356 | myString = fmt->format( myDate, myString); | |
357 | logln( myString ); | |
358 | ||
359 | Calendar *cal = new GregorianCalendar(status); | |
360 | failure(status, "new GregorianCalendar"); | |
361 | cal->setTime(myDate, status); | |
362 | failure(status, "cal->setTime"); | |
363 | if ((cal->get(UCAL_DAY_OF_YEAR, status) != 40) || failure(status, "cal->get")) | |
364 | errln((UnicodeString) "Fail: Got " + cal->get(UCAL_DAY_OF_YEAR, status) + | |
365 | " Want 40"); | |
366 | ||
367 | // this is an odd usage of "ddd" and it doesn't | |
368 | // work now that date values are range checked per #3579. | |
369 | logln("Using yyyy-ddd.hh:mm:ss"); | |
370 | delete formatter; | |
371 | formatter = NULL; | |
372 | formatter = new SimpleDateFormat(UnicodeString("yyyy-ddd.hh:mm:ss"), status); | |
373 | if(failure(status, "new SimpleDateFormat")) return; | |
374 | pos.setIndex(0); | |
375 | myDate = formatter->parse( dateString, pos ); | |
376 | myString = fmt->format( myDate, myString ); | |
377 | logln( myString ); | |
378 | cal->setTime(myDate, status); | |
379 | failure(status, "cal->setTime"); | |
380 | if ((cal->get(UCAL_DAY_OF_YEAR, status) != 40) || failure(status, "cal->get")) | |
381 | errln((UnicodeString) "Fail: Got " + cal->get(UCAL_DAY_OF_YEAR, status) + | |
382 | " Want 40"); | |
383 | ||
384 | delete formatter; | |
385 | delete fmt; | |
386 | delete cal; | |
387 | } | |
388 | ||
389 | /** | |
390 | * @bug 4061287 | |
391 | */ | |
392 | void DateFormatRegressionTest::Test4061287(void) | |
393 | { | |
394 | UErrorCode status = U_ZERO_ERROR; | |
395 | ||
396 | SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("dd/MM/yyyy"), status); | |
397 | if (U_FAILURE(status)) { | |
398 | dataerrln("Fail new SimpleDateFormat: %s", u_errorName(status)); | |
399 | delete df; | |
400 | return; | |
401 | } | |
402 | failure(status, "new SimpleDateFormat"); | |
403 | //try { | |
404 | logln(UnicodeString("") + df->parse("35/01/1971", status)); | |
405 | failure(status, "df->parse(\"35/01/1971\")"); | |
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 { | |
415 | logln(UnicodeString("") + df->parse("35/01/1971", status)); | |
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 | */ | |
428 | void 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); | |
446 | if (shortdate == NULL){ | |
447 | dataerrln("Error calling DateFormat::createDateInstance"); | |
448 | return; | |
449 | } | |
450 | ||
451 | fulldate = DateFormat::createDateTimeInstance(DateFormat::LONG, DateFormat::LONG); | |
452 | if (fulldate == NULL){ | |
453 | dataerrln("Error calling DateFormat::createDateTimeInstance"); | |
454 | return; | |
455 | } | |
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( | |
472 | NULL, *curLocale, status); | |
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 | //} | |
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 | */ | |
511 | void DateFormatRegressionTest::Test4071441(void) | |
512 | { | |
513 | DateFormat *fmtA = DateFormat::createInstance(); | |
514 | DateFormat *fmtB = DateFormat::createInstance(); | |
515 | ||
516 | if (fmtA == NULL || fmtB == NULL){ | |
517 | dataerrln("Error calling DateFormat::createInstance"); | |
518 | delete fmtA; | |
519 | delete fmtB; | |
520 | return; | |
521 | } | |
522 | ||
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 | ||
561 | When given a string mm/dd/yyyy [sic] it only parses up to the first | |
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 | */ | |
570 | void DateFormatRegressionTest::Test4073003(void) | |
571 | { | |
572 | //try { | |
573 | UErrorCode ec = U_ZERO_ERROR; | |
574 | SimpleDateFormat fmt("MM/dd/yy", Locale::getUK(), ec); | |
575 | if (U_FAILURE(ec)) { | |
576 | dataerrln("FAIL: SimpleDateFormat constructor - %s", u_errorName(ec)); | |
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 | */ | |
606 | void 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)) { | |
615 | dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status)); | |
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 | ||
641 | void 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 | */ | |
710 | void DateFormatRegressionTest::Test4101483(void) | |
711 | { | |
712 | UErrorCode status = U_ZERO_ERROR; | |
713 | SimpleDateFormat *sdf = new SimpleDateFormat(UnicodeString("z"), Locale::getUS(), status); | |
714 | if (failure(status, "new SimpleDateFormat", TRUE)) return; | |
715 | FieldPosition fp(UDAT_TIMEZONE_FIELD); | |
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(UnicodeString("beginIndex = ") + fp.getBeginIndex()); | |
724 | logln(UnicodeString("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 | */ | |
739 | void 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); | |
747 | if (failure(status, "new SimpleDateFormat", TRUE)) return; | |
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 | */ | |
767 | void 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)) { | |
777 | dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status)); | |
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 | */ | |
797 | void DateFormatRegressionTest::Test4104136(void) | |
798 | { | |
799 | UErrorCode status = U_ZERO_ERROR; | |
800 | SimpleDateFormat *sdf = new SimpleDateFormat(status); | |
801 | if(U_FAILURE(status)) { | |
802 | dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status)); | |
803 | delete sdf; | |
804 | return; | |
805 | } | |
806 | if(failure(status, "new SimpleDateFormat")) return; | |
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(UnicodeString("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 | */ | |
861 | void DateFormatRegressionTest::Test4104522(void) | |
862 | { | |
863 | UErrorCode status = U_ZERO_ERROR; | |
864 | ||
865 | SimpleDateFormat *sdf = new SimpleDateFormat(status); | |
866 | if(U_FAILURE(status)) { | |
867 | dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status)); | |
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 | */ | |
896 | void 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)) { | |
910 | dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status)); | |
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 ?? | |
989 | void 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 | */ | |
1008 | void 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); | |
1013 | if (failure(status, "new SimpleDateFormat", TRUE)) return; | |
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 | */ | |
1029 | void 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); | |
1035 | if (failure(status, "new SimpleDateFormat", TRUE)) return; | |
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 | */ | |
1054 | void 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); | |
1059 | if (failure(status, "new SimpleDateFormat", TRUE)) return; | |
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 | */ | |
1079 | void | |
1080 | DateFormatRegressionTest::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); | |
1086 | if(U_FAILURE(status)) { | |
1087 | dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status)); | |
1088 | return; | |
1089 | } | |
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 | */ | |
1108 | void 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)) { | |
1113 | dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status)); | |
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 | */ | |
1189 | void | |
1190 | DateFormatRegressionTest::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)) { | |
1198 | dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status)); | |
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 | ||
1236 | void DateFormatRegressionTest::Test714(void) | |
1237 | { | |
1238 | //try { | |
1239 | UDate d(978103543000.); | |
1240 | DateFormat *fmt = DateFormat::createDateTimeInstance(DateFormat::NONE, | |
1241 | DateFormat::MEDIUM, | |
1242 | Locale::getUS()); | |
1243 | if (fmt == NULL) { | |
1244 | dataerrln("Error calling DateFormat::createDateTimeInstance"); | |
1245 | return; | |
1246 | } | |
1247 | ||
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 | ||
1271 | class Test1684Data { | |
1272 | public: | |
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 | ||
1298 | void 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); | |
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 | } | |
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 | } | |
1428 | ||
1429 | for(i=0;i<kTest1684Count;i++) { | |
1430 | delete tests[i]; | |
1431 | } | |
1432 | delete cal; | |
1433 | delete sdf; | |
1434 | } | |
1435 | ||
1436 | void 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 | ||
1466 | void DateFormatRegressionTest::Test9237(void) | |
1467 | { | |
1468 | UErrorCode status = U_ZERO_ERROR; | |
1469 | UnicodeString pattern("VVVV"); | |
1470 | ||
1471 | SimpleDateFormat fmt(pattern, status); // default locale | |
1472 | SimpleDateFormat fmtDE(pattern, Locale("de_DE"), status); | |
1473 | if (U_FAILURE(status)) { | |
1474 | dataerrln("Error constructing SimpleDateFormat"); | |
1475 | return; | |
1476 | } | |
1477 | ||
1478 | // copy constructor | |
1479 | SimpleDateFormat fmtCopyDE(fmtDE); | |
1480 | UnicodeString resDE, resCopyDE; | |
1481 | ||
1482 | fmtDE.format(0.0, resDE); | |
1483 | fmtCopyDE.format(0.0, resCopyDE); | |
1484 | ||
1485 | if (resDE != resCopyDE) { | |
1486 | errln(UnicodeString("Error: different result by the copied instance - org:") + resDE + " copy:" + resCopyDE); | |
1487 | } | |
1488 | ||
1489 | // test for assignment operator | |
1490 | fmt = fmtDE; | |
1491 | ||
1492 | UnicodeString resAssigned; | |
1493 | fmt.format(0.0, resAssigned); | |
1494 | ||
1495 | if (resDE != resAssigned) { | |
1496 | errln(UnicodeString("Error: different results by the assigned instance - org:") + resDE + " assigned:" + resAssigned); | |
1497 | } | |
1498 | } | |
1499 | ||
1500 | void DateFormatRegressionTest::TestParsing(void) { | |
1501 | UErrorCode status = U_ZERO_ERROR; | |
1502 | UnicodeString pattern("EEE-WW-MMMM-yyyy"); | |
1503 | UnicodeString text("mon-02-march-2011"); | |
1504 | int32_t expectedDay = 7; | |
1505 | ||
1506 | SimpleDateFormat format(pattern, status); | |
1507 | if (U_FAILURE(status)) { | |
1508 | dataerrln("Unable to create SimpleDateFormat - %s", u_errorName(status)); | |
1509 | return; | |
1510 | } | |
1511 | ||
1512 | Calendar *cal = new GregorianCalendar(status); | |
1513 | if (cal == NULL || U_FAILURE(status)) { | |
1514 | errln("Unable to create calendar - %s", u_errorName(status)); | |
1515 | return; | |
1516 | } | |
1517 | ||
1518 | ParsePosition pos(0); | |
1519 | format.parse(text, *cal, pos); | |
1520 | ||
1521 | if (cal->get(UCAL_DAY_OF_MONTH, status) != expectedDay) { | |
1522 | errln("Parsing failed: day of month should be '7' with pattern: \"" + pattern + "\" for text: \"" + text + "\""); | |
1523 | } | |
1524 | ||
1525 | delete cal; | |
1526 | } | |
1527 | ||
1528 | #endif /* #if !UCONFIG_NO_FORMATTING */ | |
1529 | ||
1530 | //eof |