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