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