]>
Commit | Line | Data |
---|---|---|
1 | /*********************************************************************** | |
2 | * COPYRIGHT: | |
3 | * Copyright (c) 1997-2015, International Business Machines Corporation | |
4 | * and others. All Rights Reserved. | |
5 | ***********************************************************************/ | |
6 | ||
7 | #include "unicode/utypes.h" | |
8 | ||
9 | #if !UCONFIG_NO_FORMATTING | |
10 | ||
11 | #include "unicode/datefmt.h" | |
12 | #include "unicode/smpdtfmt.h" | |
13 | #include "unicode/gregocal.h" | |
14 | #include "dtfmtrtts.h" | |
15 | #include "caltest.h" | |
16 | #include "cstring.h" | |
17 | ||
18 | #include <stdio.h> | |
19 | #include <string.h> | |
20 | ||
21 | // ***************************************************************************** | |
22 | // class DateFormatRoundTripTest | |
23 | // ***************************************************************************** | |
24 | ||
25 | // Useful for turning up subtle bugs: Change the following to TRUE, recompile, | |
26 | // and run while at lunch. | |
27 | // Warning -- makes test run infinite loop!!! | |
28 | #ifndef INFINITE | |
29 | #define INFINITE 0 | |
30 | #endif | |
31 | ||
32 | // Define this to test just a single locale | |
33 | //#define TEST_ONE_LOC "cs_CZ" | |
34 | ||
35 | // If SPARSENESS is > 0, we don't run each exhaustive possibility. | |
36 | // There are 24 total possible tests per each locale. A SPARSENESS | |
37 | // of 12 means we run half of them. A SPARSENESS of 23 means we run | |
38 | // 1 of them. SPARSENESS _must_ be in the range 0..23. | |
39 | int32_t DateFormatRoundTripTest::SPARSENESS = 0; | |
40 | int32_t DateFormatRoundTripTest::TRIALS = 4; | |
41 | int32_t DateFormatRoundTripTest::DEPTH = 5; | |
42 | ||
43 | DateFormatRoundTripTest::DateFormatRoundTripTest() : dateFormat(0) { | |
44 | } | |
45 | ||
46 | DateFormatRoundTripTest::~DateFormatRoundTripTest() { | |
47 | delete dateFormat; | |
48 | } | |
49 | ||
50 | #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break; | |
51 | ||
52 | void | |
53 | DateFormatRoundTripTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* par ) | |
54 | { | |
55 | optionv = (par && *par=='v'); | |
56 | switch (index) { | |
57 | CASE(0,TestDateFormatRoundTrip) | |
58 | CASE(1, TestCentury) | |
59 | default: name = ""; break; | |
60 | } | |
61 | } | |
62 | ||
63 | UBool | |
64 | DateFormatRoundTripTest::failure(UErrorCode status, const char* msg) | |
65 | { | |
66 | if(U_FAILURE(status)) { | |
67 | errln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status)); | |
68 | return TRUE; | |
69 | } | |
70 | ||
71 | return FALSE; | |
72 | } | |
73 | ||
74 | UBool | |
75 | DateFormatRoundTripTest::failure(UErrorCode status, const char* msg, const UnicodeString& str) | |
76 | { | |
77 | if(U_FAILURE(status)) { | |
78 | UnicodeString escaped; | |
79 | escape(str,escaped); | |
80 | errln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status) + ", str=" + escaped); | |
81 | return TRUE; | |
82 | } | |
83 | ||
84 | return FALSE; | |
85 | } | |
86 | ||
87 | void DateFormatRoundTripTest::TestCentury() | |
88 | { | |
89 | UErrorCode status = U_ZERO_ERROR; | |
90 | Locale locale("es_PA"); | |
91 | UnicodeString pattern = "MM/dd/yy hh:mm:ss a z"; | |
92 | SimpleDateFormat fmt(pattern, locale, status); | |
93 | if (U_FAILURE(status)) { | |
94 | dataerrln("Fail: construct SimpleDateFormat: %s", u_errorName(status)); | |
95 | return; | |
96 | } | |
97 | UDate date[] = {-55018555891590.05, 0, 0}; | |
98 | UnicodeString result[2]; | |
99 | ||
100 | fmt.format(date[0], result[0]); | |
101 | date[1] = fmt.parse(result[0], status); | |
102 | fmt.format(date[1], result[1]); | |
103 | date[2] = fmt.parse(result[1], status); | |
104 | ||
105 | /* This test case worked OK by accident before. date[1] != date[0], | |
106 | * because we use -80/+20 year window for 2-digit year parsing. | |
107 | * (date[0] is in year 1926, date[1] is in year 2026.) result[1] set | |
108 | * by the first format call returns "07/13/26 07:48:28 p.m. PST", | |
109 | * which is correct, because DST was not used in year 1926 in zone | |
110 | * America/Los_Angeles. When this is parsed, date[1] becomes a time | |
111 | * in 2026, which is "07/13/26 08:48:28 p.m. PDT". There was a zone | |
112 | * offset calculation bug that observed DST in 1926, which was resolved. | |
113 | * Before the bug was resolved, result[0] == result[1] was true, | |
114 | * but after the bug fix, the expected result is actually | |
115 | * result[0] != result[1]. -Yoshito | |
116 | */ | |
117 | /* TODO: We need to review this code and clarify what we really | |
118 | * want to test here. | |
119 | */ | |
120 | //if (date[1] != date[2] || result[0] != result[1]) { | |
121 | if (date[1] != date[2]) { | |
122 | errln("Round trip failure: \"%S\" (%f), \"%S\" (%f)", result[0].getBuffer(), date[1], result[1].getBuffer(), date[2]); | |
123 | } | |
124 | } | |
125 | ||
126 | // == | |
127 | ||
128 | void DateFormatRoundTripTest::TestDateFormatRoundTrip() | |
129 | { | |
130 | UErrorCode status = U_ZERO_ERROR; | |
131 | ||
132 | getFieldCal = Calendar::createInstance(status); | |
133 | if (U_FAILURE(status)) { | |
134 | dataerrln("Fail: Calendar::createInstance: %s", u_errorName(status)); | |
135 | return; | |
136 | } | |
137 | ||
138 | ||
139 | int32_t locCount = 0; | |
140 | const Locale *avail = DateFormat::getAvailableLocales(locCount); | |
141 | logln("DateFormat available locales: %d", locCount); | |
142 | if(quick) { | |
143 | SPARSENESS = 18; | |
144 | logln("Quick mode: only testing SPARSENESS = 18"); | |
145 | } | |
146 | TimeZone *tz = TimeZone::createDefault(); | |
147 | UnicodeString temp; | |
148 | logln("Default TimeZone: " + tz->getID(temp)); | |
149 | delete tz; | |
150 | ||
151 | #ifdef TEST_ONE_LOC // define this to just test ONE locale. | |
152 | Locale loc(TEST_ONE_LOC); | |
153 | test(loc); | |
154 | #if INFINITE | |
155 | for(;;) { | |
156 | test(loc); | |
157 | } | |
158 | #endif | |
159 | ||
160 | #else | |
161 | # if INFINITE | |
162 | // Special infinite loop test mode for finding hard to reproduce errors | |
163 | Locale loc = Locale::getDefault(); | |
164 | logln("ENTERING INFINITE TEST LOOP FOR Locale: " + loc.getDisplayName(temp)); | |
165 | for(;;) | |
166 | test(loc); | |
167 | # else | |
168 | test(Locale::getDefault()); | |
169 | ||
170 | #if 1 | |
171 | // installed locales | |
172 | for (int i=0; i < locCount; ++i) { | |
173 | test(avail[i]); | |
174 | } | |
175 | #endif | |
176 | ||
177 | #if 1 | |
178 | // special locales | |
179 | int32_t jCount = CalendarTest::testLocaleCount(); | |
180 | for (int32_t j=0; j < jCount; ++j) { | |
181 | test(Locale(CalendarTest::testLocaleID(j))); | |
182 | } | |
183 | #endif | |
184 | ||
185 | # endif | |
186 | #endif | |
187 | ||
188 | delete getFieldCal; | |
189 | } | |
190 | ||
191 | static const char *styleName(DateFormat::EStyle s) | |
192 | { | |
193 | switch(s) | |
194 | { | |
195 | case DateFormat::SHORT: return "SHORT"; | |
196 | case DateFormat::MEDIUM: return "MEDIUM"; | |
197 | case DateFormat::LONG: return "LONG"; | |
198 | case DateFormat::FULL: return "FULL"; | |
199 | // case DateFormat::DEFAULT: return "DEFAULT"; | |
200 | case DateFormat::DATE_OFFSET: return "DATE_OFFSET"; | |
201 | case DateFormat::NONE: return "NONE"; | |
202 | case DateFormat::DATE_TIME: return "DATE_TIME"; | |
203 | default: return "Unknown"; | |
204 | } | |
205 | } | |
206 | ||
207 | void DateFormatRoundTripTest::test(const Locale& loc) | |
208 | { | |
209 | UnicodeString temp; | |
210 | #if !INFINITE | |
211 | logln("Locale: " + loc.getDisplayName(temp)); | |
212 | #endif | |
213 | ||
214 | // Total possibilities = 24 | |
215 | // 4 date | |
216 | // 4 time | |
217 | // 16 date-time | |
218 | UBool TEST_TABLE [24];//= new boolean[24]; | |
219 | int32_t i = 0; | |
220 | for(i = 0; i < 24; ++i) | |
221 | TEST_TABLE[i] = TRUE; | |
222 | ||
223 | // If we have some sparseness, implement it here. Sparseness decreases | |
224 | // test time by eliminating some tests, up to 23. | |
225 | for(i = 0; i < SPARSENESS; ) { | |
226 | int random = (int)(randFraction() * 24); | |
227 | if (random >= 0 && random < 24 && TEST_TABLE[i]) { | |
228 | TEST_TABLE[i] = FALSE; | |
229 | ++i; | |
230 | } | |
231 | } | |
232 | ||
233 | int32_t itable = 0; | |
234 | int32_t style = 0; | |
235 | for(style = DateFormat::FULL; style <= DateFormat::SHORT; ++style) { | |
236 | if(TEST_TABLE[itable++]) { | |
237 | logln("Testing style " + UnicodeString(styleName((DateFormat::EStyle)style))); | |
238 | DateFormat *df = DateFormat::createDateInstance((DateFormat::EStyle)style, loc); | |
239 | if(df == NULL) { | |
240 | errln(UnicodeString("Could not DF::createDateInstance ") + UnicodeString(styleName((DateFormat::EStyle)style)) + " Locale: " + loc.getDisplayName(temp)); | |
241 | } else { | |
242 | test(df, loc); | |
243 | delete df; | |
244 | } | |
245 | } | |
246 | } | |
247 | ||
248 | for(style = DateFormat::FULL; style <= DateFormat::SHORT; ++style) { | |
249 | if (TEST_TABLE[itable++]) { | |
250 | logln("Testing style " + UnicodeString(styleName((DateFormat::EStyle)style))); | |
251 | DateFormat *df = DateFormat::createTimeInstance((DateFormat::EStyle)style, loc); | |
252 | if(df == NULL) { | |
253 | errln(UnicodeString("Could not DF::createTimeInstance ") + UnicodeString(styleName((DateFormat::EStyle)style)) + " Locale: " + loc.getDisplayName(temp)); | |
254 | } else { | |
255 | test(df, loc, TRUE); | |
256 | delete df; | |
257 | } | |
258 | } | |
259 | } | |
260 | ||
261 | for(int32_t dstyle = DateFormat::FULL; dstyle <= DateFormat::SHORT; ++dstyle) { | |
262 | for(int32_t tstyle = DateFormat::FULL; tstyle <= DateFormat::SHORT; ++tstyle) { | |
263 | if(TEST_TABLE[itable++]) { | |
264 | logln("Testing dstyle" + UnicodeString(styleName((DateFormat::EStyle)dstyle)) + ", tstyle" + UnicodeString(styleName((DateFormat::EStyle)tstyle)) ); | |
265 | DateFormat *df = DateFormat::createDateTimeInstance((DateFormat::EStyle)dstyle, (DateFormat::EStyle)tstyle, loc); | |
266 | if(df == NULL) { | |
267 | dataerrln(UnicodeString("Could not DF::createDateTimeInstance ") + UnicodeString(styleName((DateFormat::EStyle)dstyle)) + ", tstyle" + UnicodeString(styleName((DateFormat::EStyle)tstyle)) + "Locale: " + loc.getDisplayName(temp)); | |
268 | } else { | |
269 | test(df, loc); | |
270 | delete df; | |
271 | } | |
272 | } | |
273 | } | |
274 | } | |
275 | } | |
276 | ||
277 | void DateFormatRoundTripTest::test(DateFormat *fmt, const Locale &origLocale, UBool timeOnly) | |
278 | { | |
279 | UnicodeString pat; | |
280 | if(fmt->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) { | |
281 | errln("DateFormat wasn't a SimpleDateFormat"); | |
282 | return; | |
283 | } | |
284 | ||
285 | UBool isGregorian = FALSE; | |
286 | UErrorCode minStatus = U_ZERO_ERROR; | |
287 | if(fmt->getCalendar() == NULL) { | |
288 | errln((UnicodeString)"DateFormatRoundTripTest::test, DateFormat getCalendar() returns null for " + origLocale.getName()); | |
289 | return; | |
290 | } | |
291 | UDate minDate = CalendarTest::minDateOfCalendar(*fmt->getCalendar(), isGregorian, minStatus); | |
292 | if(U_FAILURE(minStatus)) { | |
293 | errln((UnicodeString)"Failure getting min date for " + origLocale.getName()); | |
294 | return; | |
295 | } | |
296 | //logln(UnicodeString("Min date is ") + fullFormat(minDate) + " for " + origLocale.getName()); | |
297 | ||
298 | pat = ((SimpleDateFormat*)fmt)->toPattern(pat); | |
299 | ||
300 | // NOTE TO MAINTAINER | |
301 | // This indexOf check into the pattern needs to be refined to ignore | |
302 | // quoted characters. Currently, this isn't a problem with the locale | |
303 | // patterns we have, but it may be a problem later. | |
304 | ||
305 | UBool hasEra = (pat.indexOf(UnicodeString("G")) != -1); | |
306 | UBool hasZoneDisplayName = (pat.indexOf(UnicodeString("z")) != -1) || (pat.indexOf(UnicodeString("v")) != -1) | |
307 | || (pat.indexOf(UnicodeString("V")) != -1); | |
308 | ||
309 | // Because patterns contain incomplete data representing the Date, | |
310 | // we must be careful of how we do the roundtrip. We start with | |
311 | // a randomly generated Date because they're easier to generate. | |
312 | // From this we get a string. The string is our real starting point, | |
313 | // because this string should parse the same way all the time. Note | |
314 | // that it will not necessarily parse back to the original date because | |
315 | // of incompleteness in patterns. For example, a time-only pattern won't | |
316 | // parse back to the same date. | |
317 | ||
318 | //try { | |
319 | for(int i = 0; i < TRIALS; ++i) { | |
320 | UDate *d = new UDate [DEPTH]; | |
321 | UnicodeString *s = new UnicodeString[DEPTH]; | |
322 | ||
323 | if(isGregorian == TRUE) { | |
324 | d[0] = generateDate(); | |
325 | } else { | |
326 | d[0] = generateDate(minDate); | |
327 | } | |
328 | ||
329 | UErrorCode status = U_ZERO_ERROR; | |
330 | ||
331 | // We go through this loop until we achieve a match or until | |
332 | // the maximum loop count is reached. We record the points at | |
333 | // which the date and the string starts to match. Once matching | |
334 | // starts, it should continue. | |
335 | int loop; | |
336 | int dmatch = 0; // d[dmatch].getTime() == d[dmatch-1].getTime() | |
337 | int smatch = 0; // s[smatch].equals(s[smatch-1]) | |
338 | for(loop = 0; loop < DEPTH; ++loop) { | |
339 | if (loop > 0) { | |
340 | d[loop] = fmt->parse(s[loop-1], status); | |
341 | failure(status, "fmt->parse", s[loop-1]+" in locale: " + origLocale.getName() + " with pattern: " + pat); | |
342 | status = U_ZERO_ERROR; /* any error would have been reported */ | |
343 | } | |
344 | ||
345 | s[loop] = fmt->format(d[loop], s[loop]); | |
346 | ||
347 | // For displaying which date is being tested | |
348 | //logln(s[loop] + " = " + fullFormat(d[loop])); | |
349 | ||
350 | if(s[loop].length() == 0) { | |
351 | errln("FAIL: fmt->format gave 0-length string in " + pat + " with number " + d[loop] + " in locale " + origLocale.getName()); | |
352 | } | |
353 | ||
354 | if(loop > 0) { | |
355 | if(smatch == 0) { | |
356 | UBool match = s[loop] == s[loop-1]; | |
357 | if(smatch == 0) { | |
358 | if(match) | |
359 | smatch = loop; | |
360 | } | |
361 | else if( ! match) | |
362 | errln("FAIL: String mismatch after match"); | |
363 | } | |
364 | ||
365 | if(dmatch == 0) { | |
366 | // {sfb} watch out here, this might not work | |
367 | UBool match = d[loop]/*.getTime()*/ == d[loop-1]/*.getTime()*/; | |
368 | if(dmatch == 0) { | |
369 | if(match) | |
370 | dmatch = loop; | |
371 | } | |
372 | else if( ! match) | |
373 | errln("FAIL: Date mismatch after match"); | |
374 | } | |
375 | ||
376 | if(smatch != 0 && dmatch != 0) | |
377 | break; | |
378 | } | |
379 | } | |
380 | // At this point loop == DEPTH if we've failed, otherwise loop is the | |
381 | // max(smatch, dmatch), that is, the index at which we have string and | |
382 | // date matching. | |
383 | ||
384 | // Date usually matches in 2. Exceptions handled below. | |
385 | int maxDmatch = 2; | |
386 | int maxSmatch = 1; | |
387 | if (dmatch > maxDmatch) { | |
388 | // Time-only pattern with zone information and a starting date in PST. | |
389 | if(timeOnly && hasZoneDisplayName) { | |
390 | int32_t startRaw, startDst; | |
391 | fmt->getTimeZone().getOffset(d[0], FALSE, startRaw, startDst, status); | |
392 | failure(status, "TimeZone::getOffset"); | |
393 | // if the start offset is greater than the offset on Jan 1, 1970 | |
394 | // in PST, then need one more round trip. There are two cases | |
395 | // fall into this category. The start date is 1) DST or | |
396 | // 2) LMT (GMT-07:52:58). | |
397 | if (startRaw + startDst > -28800000) { | |
398 | maxDmatch = 3; | |
399 | maxSmatch = 2; | |
400 | } | |
401 | } | |
402 | } | |
403 | ||
404 | // String usually matches in 1. Exceptions are checked for here. | |
405 | if(smatch > maxSmatch) { // Don't compute unless necessary | |
406 | UBool in0; | |
407 | // Starts in BC, with no era in pattern | |
408 | if( ! hasEra && getField(d[0], UCAL_ERA) == GregorianCalendar::BC) | |
409 | maxSmatch = 2; | |
410 | // Starts in DST, no year in pattern | |
411 | else if((in0=fmt->getTimeZone().inDaylightTime(d[0], status)) && ! failure(status, "gettingDaylightTime") && | |
412 | pat.indexOf(UnicodeString("yyyy")) == -1) | |
413 | maxSmatch = 2; | |
414 | // If we start not in DST, but transition into DST | |
415 | else if (!in0 && | |
416 | fmt->getTimeZone().inDaylightTime(d[1], status) && !failure(status, "gettingDaylightTime")) | |
417 | maxSmatch = 2; | |
418 | // Two digit year with no time zone change, | |
419 | // unless timezone isn't used or we aren't close to the DST changover | |
420 | else if (pat.indexOf(UnicodeString("y")) != -1 | |
421 | && pat.indexOf(UnicodeString("yyyy")) == -1 | |
422 | && getField(d[0], UCAL_YEAR) | |
423 | != getField(d[dmatch], UCAL_YEAR) | |
424 | && !failure(status, "error status [smatch>maxSmatch]") | |
425 | && ((hasZoneDisplayName | |
426 | && (fmt->getTimeZone().inDaylightTime(d[0], status) | |
427 | == fmt->getTimeZone().inDaylightTime(d[dmatch], status) | |
428 | || getField(d[0], UCAL_MONTH) == UCAL_APRIL | |
429 | || getField(d[0], UCAL_MONTH) == UCAL_OCTOBER)) | |
430 | || !hasZoneDisplayName) | |
431 | ) | |
432 | { | |
433 | maxSmatch = 2; | |
434 | } | |
435 | // If zone display name is used, fallback format might be used before 1970 | |
436 | else if (hasZoneDisplayName && d[0] < 0) { | |
437 | maxSmatch = 2; | |
438 | } | |
439 | else if (timeOnly && !isGregorian && hasZoneDisplayName && maxSmatch == 1) { | |
440 | int32_t startRaw, startDst; | |
441 | fmt->getTimeZone().getOffset(d[1], FALSE, startRaw, startDst, status); | |
442 | failure(status, "TimeZone::getOffset"); | |
443 | // If the calendar type is not Gregorian and the pattern is time only, | |
444 | // the calendar implementation may use a date before 1970 as day 0. | |
445 | // In this case, time zone offset of the default year might be | |
446 | // different from the one at 1970-01-01 in PST and string match requires | |
447 | // one more iteration. | |
448 | if (startRaw + startDst != -28800000) { | |
449 | maxSmatch = 2; | |
450 | } | |
451 | } | |
452 | } | |
453 | ||
454 | /* | |
455 | * Special case for Japanese and Buddhist (could have large negative years) | |
456 | * Also, Hebrew calendar need help handling leap month. | |
457 | */ | |
458 | if(dmatch > maxDmatch || smatch > maxSmatch) { | |
459 | const char *type = fmt->getCalendar()->getType(); | |
460 | if(!strcmp(type,"japanese") || (!strcmp(type,"buddhist"))) { | |
461 | maxSmatch = 4; | |
462 | maxDmatch = 4; | |
463 | } else if(!strcmp(type,"hebrew")) { | |
464 | maxSmatch = 3; | |
465 | maxDmatch = 3; | |
466 | } | |
467 | } | |
468 | ||
469 | // Use @v to see verbose results on successful cases | |
470 | UBool fail = (dmatch > maxDmatch || smatch > maxSmatch); | |
471 | if (optionv || fail) { | |
472 | if (fail) { | |
473 | errln(UnicodeString("\nFAIL: Pattern: ") + pat + | |
474 | " in Locale: " + origLocale.getName()); | |
475 | } else { | |
476 | errln(UnicodeString("\nOk: Pattern: ") + pat + | |
477 | " in Locale: " + origLocale.getName()); | |
478 | } | |
479 | ||
480 | logln("Date iters until match=%d (max allowed=%d), string iters until match=%d (max allowed=%d)", | |
481 | dmatch,maxDmatch, smatch, maxSmatch); | |
482 | ||
483 | for(int j = 0; j <= loop && j < DEPTH; ++j) { | |
484 | UnicodeString temp; | |
485 | FieldPosition pos(FieldPosition::DONT_CARE); | |
486 | errln((j>0?" P> ":" ") + fullFormat(d[j]) + " F> " + | |
487 | escape(s[j], temp) + UnicodeString(" d=") + d[j] + | |
488 | (j > 0 && d[j]/*.getTime()*/==d[j-1]/*.getTime()*/?" d==":"") + | |
489 | (j > 0 && s[j] == s[j-1]?" s==":"")); | |
490 | } | |
491 | } | |
492 | delete[] d; | |
493 | delete[] s; | |
494 | } | |
495 | /*} | |
496 | catch (ParseException e) { | |
497 | errln("Exception: " + e.getMessage()); | |
498 | logln(e.toString()); | |
499 | }*/ | |
500 | } | |
501 | ||
502 | const UnicodeString& DateFormatRoundTripTest::fullFormat(UDate d) { | |
503 | UErrorCode ec = U_ZERO_ERROR; | |
504 | if (dateFormat == 0) { | |
505 | dateFormat = new SimpleDateFormat((UnicodeString)"EEE MMM dd HH:mm:ss.SSS zzz yyyy G", ec); | |
506 | if (U_FAILURE(ec) || dateFormat == 0) { | |
507 | fgStr = "[FAIL: SimpleDateFormat constructor]"; | |
508 | delete dateFormat; | |
509 | dateFormat = 0; | |
510 | return fgStr; | |
511 | } | |
512 | } | |
513 | fgStr.truncate(0); | |
514 | dateFormat->format(d, fgStr); | |
515 | return fgStr; | |
516 | } | |
517 | ||
518 | /** | |
519 | * Return a field of the given date | |
520 | */ | |
521 | int32_t DateFormatRoundTripTest::getField(UDate d, int32_t f) { | |
522 | // Should be synchronized, but we're single threaded so it's ok | |
523 | UErrorCode status = U_ZERO_ERROR; | |
524 | getFieldCal->setTime(d, status); | |
525 | failure(status, "getfieldCal->setTime"); | |
526 | int32_t ret = getFieldCal->get((UCalendarDateFields)f, status); | |
527 | failure(status, "getfieldCal->get"); | |
528 | return ret; | |
529 | } | |
530 | ||
531 | UnicodeString& DateFormatRoundTripTest::escape(const UnicodeString& src, UnicodeString& dst ) | |
532 | { | |
533 | dst.remove(); | |
534 | for (int32_t i = 0; i < src.length(); ++i) { | |
535 | UChar c = src[i]; | |
536 | if(c < 0x0080) | |
537 | dst += c; | |
538 | else { | |
539 | dst += UnicodeString("["); | |
540 | char buf [8]; | |
541 | sprintf(buf, "%#x", c); | |
542 | dst += UnicodeString(buf); | |
543 | dst += UnicodeString("]"); | |
544 | } | |
545 | } | |
546 | ||
547 | return dst; | |
548 | } | |
549 | ||
550 | #define U_MILLIS_PER_YEAR (365.25 * 24 * 60 * 60 * 1000) | |
551 | ||
552 | UDate DateFormatRoundTripTest::generateDate(UDate minDate) | |
553 | { | |
554 | // Bring range in conformance to generateDate() below. | |
555 | if(minDate < (U_MILLIS_PER_YEAR * -(4000-1970))) { | |
556 | minDate = (U_MILLIS_PER_YEAR * -(4000-1970)); | |
557 | } | |
558 | for(int i=0;i<8;i++) { | |
559 | double a = randFraction(); | |
560 | ||
561 | // Range from (min) to (8000-1970) AD | |
562 | double dateRange = (0.0 - minDate) + (U_MILLIS_PER_YEAR + (8000-1970)); | |
563 | ||
564 | a *= dateRange; | |
565 | ||
566 | // Now offset from minDate | |
567 | a += minDate; | |
568 | ||
569 | // Last sanity check | |
570 | if(a>=minDate) { | |
571 | return a; | |
572 | } | |
573 | } | |
574 | return minDate; | |
575 | } | |
576 | ||
577 | UDate DateFormatRoundTripTest::generateDate() | |
578 | { | |
579 | double a = randFraction(); | |
580 | ||
581 | // Now 'a' ranges from 0..1; scale it to range from 0 to 8000 years | |
582 | a *= 8000; | |
583 | ||
584 | // Range from (4000-1970) BC to (8000-1970) AD | |
585 | a -= 4000; | |
586 | ||
587 | // Now scale up to ms | |
588 | a *= 365.25 * 24 * 60 * 60 * 1000; | |
589 | ||
590 | //return new Date((long)a); | |
591 | return a; | |
592 | } | |
593 | ||
594 | #endif /* #if !UCONFIG_NO_FORMATTING */ | |
595 | ||
596 | //eof |