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