1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4 * COPYRIGHT:
5 * Copyright (c) 1997-2014, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
8 /********************************************************************************
9 *
10 * File CDTRGTST.C
11 *
12 * Madhu Katragadda Ported for C API
13 * Modification History:
14 * Date Name Description
15 * 07/15/99 helena Ported to HPUX 10/11 CC.
16 *********************************************************************************
17 */
18 /* REGRESSION TEST FOR DATE FORMAT */
19
20 #include "unicode/utypes.h"
21
22 #if !UCONFIG_NO_FORMATTING
23
24 #include <stdbool.h>
25
26 #include "unicode/uloc.h"
27 #include "unicode/udat.h"
28 #include "unicode/ucal.h"
29 #include "unicode/unum.h"
30 #include "unicode/ustring.h"
31 #include "cintltst.h"
32 #include "cdtrgtst.h"
33 #include "cmemory.h"
34
35 void addDateForRgrTest(TestNode** root);
36
addDateForRgrTest(TestNode ** root)37 void addDateForRgrTest(TestNode** root)
38 {
39 addTest(root, &Test4029195, "tsformat/cdtrgtst/Test4029195");
40 addTest(root, &Test4056591, "tsformat/cdtrgtst/Test4056591");
41 addTest(root, &Test4059917, "tsformat/cdtrgtst/Test4059917");
42 addTest(root, &Test4060212, "tsformat/cdtrgtst/Test4060212");
43 addTest(root, &Test4061287, "tsformat/cdtrgtst/Test4061287");
44 addTest(root, &Test4073003, "tsformat/cdtrgtst/Test4073003");
45 addTest(root, &Test4162071, "tsformat/cdtrgtst/Test4162071");
46 addTest(root, &Test714, "tsformat/cdtrgtst/Test714");
47 addTest(root, &Test_GEec, "tsformat/cdtrgtst/Test_GEec"); /* tests for format chars GEec, jitterbugs 5726 6072 6585 */
48 }
49
50 /**
51 * @bug 4029195
52 */
Test4029195()53 void Test4029195()
54 {
55 int32_t resultlength, resultlengthneeded;
56 UChar *fmdt, *todayS, *rt;
57 UChar *pat=NULL;
58 UChar *temp;
59 UDate today, d1;
60 UDateFormat *df;
61 int32_t parsepos;
62 UErrorCode status = U_ZERO_ERROR;
63
64 log_verbose("Testing date format and parse function in regression test\n");
65 today = ucal_getNow();
66
67 df = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,"en_US", NULL, 0, NULL, 0, &status);
68 if(U_FAILURE(status))
69 {
70 log_data_err("FAIL: error in creating the dateformat using default date and time style : %s (Are you missing data?)\n", myErrorName(status));
71 return;
72 }
73 resultlength=0;
74 resultlengthneeded=udat_toPattern(df, true, NULL, resultlength, &status);
75 if(status==U_BUFFER_OVERFLOW_ERROR)
76 {
77 status=U_ZERO_ERROR;
78 resultlength=resultlengthneeded + 1;
79 pat=(UChar*)malloc(sizeof(UChar) * resultlength);
80 udat_toPattern(df, true, pat, resultlength, &status);
81 }
82
83 log_verbose("pattern: %s\n", austrdup(pat));
84
85
86 fmdt = myFormatit(df, today);
87 if(fmdt) {
88 log_verbose("today: %s\n", austrdup(fmdt));
89 } else {
90 log_data_err("ERROR: couldn't format, exiting test");
91 return;
92 }
93
94 temp=(UChar*)malloc(sizeof(UChar) * 10);
95 u_uastrcpy(temp, "M yyyy dd");
96 udat_applyPattern(df, true, temp, u_strlen(temp));
97
98 todayS =myFormatit(df, today);
99 log_verbose("After the pattern is applied\n today: %s\n", austrdup(todayS) );
100 parsepos=0;
101 d1=udat_parse(df, todayS, u_strlen(todayS), &parsepos, &status);
102 if(U_FAILURE(status))
103 {
104 log_err("FAIL: Error in parsing using udat_parse(.....): %s\n", myErrorName(status));
105 }
106
107 rt =myFormatit(df, d1);
108 log_verbose("today: %s\n", austrdup(rt) );
109
110 log_verbose("round trip: %s\n", austrdup(rt) );
111
112 if(u_strcmp(rt, todayS)!=0) {
113 log_err("Fail: Want %s Got %s\n", austrdup(todayS), austrdup(rt) );
114 }
115 else
116 log_verbose("Pass: parse and format working fine\n");
117 udat_close(df);
118 free(temp);
119 if(pat != NULL) {
120 free(pat);
121 }
122 }
123
124
125 /**
126 * @bug 4056591
127 * Verify the function of the [s|g]et2DigitYearStart() API.
128 */
Test4056591()129 void Test4056591()
130 {
131 int i;
132 UCalendar *cal;
133 UDateFormat *def;
134 UDate start,exp,got;
135 UChar s[10];
136 UChar *gotdate, *expdate;
137 UChar pat[10];
138 UDate d[4];
139 UErrorCode status = U_ZERO_ERROR;
140 const char* strings[] = {
141 "091225",
142 "091224",
143 "611226",
144 "991227"
145 };
146
147 log_verbose("Testing s[get] 2 digit year start regressively\n");
148 cal=ucal_open(NULL, 0, "en_US", UCAL_GREGORIAN, &status);
149 if(U_FAILURE(status)){
150 log_data_err("error in ucal_open caldef : %s - (Are you missing data?)\n", myErrorName(status));
151 return;
152 }
153 ucal_setDateTime(cal, 1809, UCAL_DECEMBER, 25, 17, 40, 30, &status);
154 d[0]=ucal_getMillis(cal, &status);
155 if(U_FAILURE(status)){
156 log_err("Error: failure in get millis: %s\n", myErrorName(status));
157 }
158 ucal_setDateTime(cal, 1909, UCAL_DECEMBER, 24, 17, 40, 30, &status);
159 d[1]=ucal_getMillis(cal, &status);
160 ucal_setDateTime(cal, 1861, UCAL_DECEMBER, 26, 17, 40, 30, &status);
161 d[2]=ucal_getMillis(cal, &status);
162 ucal_setDateTime(cal, 1999, UCAL_DECEMBER, 27, 17, 40, 30, &status);
163 d[3]=ucal_getMillis(cal, &status);
164
165
166 u_uastrcpy(pat, "yyMMdd");
167 def = udat_open(UDAT_PATTERN,UDAT_PATTERN,NULL, NULL, 0, pat, u_strlen(pat), &status);
168 if(U_FAILURE(status))
169 {
170 log_data_err("FAIL: error in creating the dateformat using u_openPattern(): %s - (Are you missing data?)\n", myErrorName(status));
171 ucal_close(cal);
172 return;
173 }
174 start = 1800;
175 udat_set2DigitYearStart(def, start, &status);
176 if(U_FAILURE(status))
177 log_err("ERROR: in setTwoDigitStartDate: %s\n", myErrorName(status));
178 if( (udat_get2DigitYearStart(def, &status) != start))
179 log_err("ERROR: get2DigitYearStart broken\n");
180
181
182 for(i = 0; i < 4; ++i) {
183 u_uastrcpy(s, strings[i]);
184 exp = d[i];
185 got = udat_parse(def, s, u_strlen(s), 0, &status);
186 gotdate=myFormatit(def, got);
187 expdate=myFormatit(def, exp);
188
189 if (gotdate == NULL || expdate == NULL) {
190 log_err("myFormatit failed!\n");
191 }
192 else if(u_strcmp(gotdate, expdate) !=0){
193 log_err("set2DigitYearStart broken for %s \n got: %s, expected: %s\n", austrdup(s),
194 austrdup(gotdate), austrdup(expdate) );
195 }
196 }
197
198 udat_close(def);
199 ucal_close(cal);
200 }
201
202
203 /**
204 * SimpleDateFormat does not properly parse date strings without delimiters
205 * @bug 4059917
206 */
Test4059917()207 void Test4059917()
208 {
209 UDateFormat* def;
210 UChar *myDate;
211 UErrorCode status = U_ZERO_ERROR;
212 UChar pattern[11];
213 UChar tzID[4];
214
215 log_verbose("Testing apply pattern and to pattern regressively\n");
216 u_uastrcpy(tzID, "PST");
217 u_uastrcpy(pattern, "yyyy/MM/dd");
218 log_verbose("%s\n", austrdup(pattern) );
219 def = udat_open(UDAT_PATTERN,UDAT_PATTERN,NULL,tzID,-1,pattern, u_strlen(pattern),&status);
220 if(U_FAILURE(status))
221 {
222 log_data_err("FAIL: error in creating the dateformat using openPattern: %s - (Are you missing data?)\n", myErrorName(status));
223 return;
224 }
225 myDate=(UChar*)malloc(sizeof(UChar) * 11);
226 u_uastrcpy(myDate, "1970/01/12");
227
228 aux917( def, myDate );
229 udat_close(def);
230
231 u_uastrcpy(pattern, "yyyyMMdd");
232 def = udat_open(UDAT_PATTERN,UDAT_PATTERN,NULL,tzID,-1,pattern, u_strlen(pattern),&status);
233 if(U_FAILURE(status))
234 {
235 log_err("FAIL: error in creating the dateformat using openPattern: %s\n", myErrorName(status));
236 return;
237 }
238 u_uastrcpy(myDate, "19700112");
239 aux917( def, myDate );
240 udat_close(def);
241 free(myDate);
242 }
243
aux917(UDateFormat * fmt,UChar * str)244 void aux917( UDateFormat *fmt, UChar* str)
245 {
246 int32_t resultlength, resultlengthneeded;
247 UErrorCode status = U_ZERO_ERROR;
248 UChar* formatted=NULL;
249 UChar *pat=NULL;
250 UDate d1=1000000000.0;
251
252 resultlength=0;
253 resultlengthneeded=udat_toPattern(fmt, true, NULL, resultlength, &status);
254 if(status==U_BUFFER_OVERFLOW_ERROR)
255 {
256 status=U_ZERO_ERROR;
257 resultlength=resultlengthneeded + 1;
258 pat=(UChar*)malloc(sizeof(UChar) * (resultlength));
259 udat_toPattern(fmt, true, pat, resultlength, &status);
260 }
261 if(U_FAILURE(status)){
262 log_err("failure in retrieving the pattern: %s\n", myErrorName(status));
263 }
264 log_verbose("pattern: %s\n", austrdup(pat) );
265
266 status = U_ZERO_ERROR;
267 formatted = myFormatit(fmt, d1);
268 if( u_strcmp(formatted,str)!=0) {
269 log_err("Fail: Want %s Got: %s\n", austrdup(str), austrdup(formatted) );
270 }
271 free(pat);
272 }
273
274 /**
275 * @bug 4060212
276 */
Test4060212()277 void Test4060212()
278 {
279 int32_t pos;
280 UCalendar *cal;
281 UDateFormat *formatter, *fmt;
282 UErrorCode status = U_ZERO_ERROR;
283 UDate myDate;
284 UChar *myString;
285 UChar dateString[30], pattern[20], tzID[4];
286 u_uastrcpy(dateString, "1995-040.05:01:29 -8");
287 u_uastrcpy(pattern, "yyyy-DDD.hh:mm:ss z");
288
289 log_verbose( "dateString= %s Using yyyy-DDD.hh:mm:ss\n", austrdup(dateString) );
290 status = U_ZERO_ERROR;
291 u_uastrcpy(tzID, "PST");
292
293 formatter = udat_open(UDAT_PATTERN,UDAT_PATTERN,"en_US",tzID,-1,pattern, u_strlen(pattern), &status);
294 pos=0;
295 myDate = udat_parse(formatter, dateString, u_strlen(dateString), &pos, &status);
296
297
298 fmt = udat_open(UDAT_FULL,UDAT_LONG ,NULL, tzID, -1, NULL, 0, &status);
299 if(U_FAILURE(status))
300 {
301 log_data_err("FAIL: error in creating the dateformat using default date and time style: %s - (Are you missing data?)\n",
302 myErrorName(status) );
303 return;
304 }
305 myString = myFormatit(fmt, myDate);
306 (void)myString; /* Suppress set but not used warning. */
307 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_GREGORIAN, &status);
308 if(U_FAILURE(status)){
309 log_err("FAIL: error in ucal_open caldef : %s\n", myErrorName(status));
310 }
311 ucal_setMillis(cal, myDate, &status);
312 if ((ucal_get(cal, UCAL_DAY_OF_YEAR, &status) != 40)){
313 log_err("Fail: Got %d Expected 40\n", ucal_get(cal, UCAL_DAY_OF_YEAR, &status));
314 }
315
316 udat_close(formatter);
317 ucal_close(cal);
318 udat_close(fmt);
319
320 }
321
322 /**
323 * @bug 4061287
324 */
Test4061287()325 void Test4061287()
326 {
327 UBool ok;
328 int32_t pos;
329 UDateFormat *df;
330 UErrorCode status = U_ZERO_ERROR;
331 UDate myDate;
332 UChar pattern[21], dateString[11];
333
334 u_uastrcpy(dateString, "35/13/1971");
335 u_uastrcpy(pattern, "dd/mm/yyyy");
336 status = U_ZERO_ERROR;
337 log_verbose("Testing parsing by changing the attribute lenient\n");
338 df = udat_open(UDAT_PATTERN,UDAT_PATTERN,NULL,NULL,0,pattern, u_strlen(pattern),&status);
339 if(U_FAILURE(status)){
340 log_data_err("ERROR: failure in open pattern of test4061287: %s - (Are you missing data?)\n", myErrorName(status));
341 return;
342 }
343
344 pos=0;
345
346 udat_setLenient(df, false);
347 ok=udat_isLenient(df);
348 if(ok==true)
349 log_err("setLenient nor working\n");
350 ok = false;
351 myDate = udat_parse(df, dateString, u_strlen(dateString), &pos, &status);
352 (void)myDate; /* Suppress set but not used warning. */
353 if(U_FAILURE(status))
354 ok = true;
355 if(ok!=true)
356 log_err("Fail: Lenient not working: does lenient parsing in spite of setting Lenient as false ");
357
358 udat_close(df);
359
360 }
361
362
363
364 /* The java.text.DateFormat.parse(String) method expects for the
365 US locale a string formatted according to mm/dd/yy and parses it
366 correctly.
367
368 When given a string mm/dd/yyyy it only parses up to the first
369 two y's, typically resulting in a date in the year 1919.
370
371 Please extend the parsing method(s) to handle strings with
372 four-digit year values (probably also applicable to various
373 other locales. */
374 /**
375 * @bug 4073003
376 */
Test4073003()377 void Test4073003()
378 {
379 int32_t pos,i;
380 UDate d,dd;
381 UChar *datestr;
382 UChar temp[15];
383 UErrorCode status = U_ZERO_ERROR;
384 UDateFormat *fmt;
385 UChar *result, *result2;
386 const char* tests [] = {
387 "12/25/61",
388 "12/25/1961",
389 "4/3/1999",
390 "4/3/99"
391 };
392
393 fmt= udat_open(UDAT_SHORT,UDAT_SHORT ,NULL, NULL, 0, NULL, 0, &status);
394 if(U_FAILURE(status))
395 {
396 log_data_err("FAIL: error in creating the dateformat using short date and time style: %s (Are you missing data?)\n",
397 myErrorName(status));
398 return;
399 }
400 u_uastrcpy(temp, "m/D/yy");
401 udat_applyPattern(fmt, false, temp, u_strlen(temp));
402
403 for(i= 0; i < 4; i+=2) {
404 status=U_ZERO_ERROR;
405 datestr=(UChar*)malloc(sizeof(UChar) * (strlen(tests[i])+1));
406 u_uastrcpy(datestr, tests[i]);
407
408 pos=0;
409 d = udat_parse(fmt, datestr, u_strlen(datestr), &pos, &status);
410 if(U_FAILURE(status)){
411 log_err("ERROR : in test 4073003: %s\n", myErrorName(status));
412 }
413
414 free(datestr);
415 datestr=(UChar*)malloc(sizeof(UChar) * (strlen(tests[i+1])+1));
416 u_uastrcpy(datestr, tests[i+1]);
417
418 pos=0;
419 status=U_ZERO_ERROR;
420 dd = udat_parse(fmt, datestr, u_strlen(datestr), &pos, &status);
421 if(U_FAILURE(status)){
422 log_err("ERROR : in test 4073003: %s\n", myErrorName(status));
423 }
424 free(datestr);
425
426 result =myFormatit(fmt, d);
427 result2 =myFormatit(fmt, dd);
428 if(!result || !result2) {
429 log_data_err("Fail: could not format - exiting test\n");
430 return;
431 }
432 if (u_strcmp(result, result2)!=0){
433 log_err("Fail: %s != %s\n", austrdup(result), austrdup(result2) );
434 }
435 else{
436 log_verbose("Ok: %s == %s\n", austrdup(result), austrdup(result2) );
437 }
438
439 }
440 udat_close(fmt);
441 }
442
443 /**
444 * @bug 4162071
445 **/
Test4162071()446 void Test4162071()
447 {
448 int32_t pos;
449 UDate x;
450 UErrorCode status = U_ZERO_ERROR;
451 UDateFormat *df;
452 UChar datestr[30];
453 UChar format[50];
454 u_uastrcpy(datestr, "Thu, 30-Jul-1999 11:51:14 GMT");
455 u_uastrcpy(format, "EEE', 'dd-MMM-yyyy HH:mm:ss z"); /* RFC 822/1123 */
456 status = U_ZERO_ERROR;
457 /* Can't hardcode the result to assume the default locale is "en_US". */
458 df = udat_open(UDAT_PATTERN,UDAT_PATTERN,"en_US",NULL,0,format, u_strlen(format),&status);
459 if(U_FAILURE(status)){
460 log_data_err("ERROR: couldn't create date format: %s\n", myErrorName(status));
461 return;
462 }
463 pos=0;
464 x = udat_parse(df, datestr, u_strlen(datestr), &pos, &status);
465 (void)x; /* Suppress set but not used warning. */
466 if(U_FAILURE(status)){
467 log_data_err("ERROR : parse format %s fails : %s\n", austrdup(format), myErrorName(status));
468 }
469 else{
470 log_verbose("Parse format \"%s \" ok.\n", austrdup(format) );
471 }
472 /*log_verbose("date= %s\n", austrdup(myFormatit(df, x)) );*/
473 udat_close(df);
474 }
475
Test714(void)476 void Test714(void)
477 {
478 UDate d=978103543000.0;
479 UErrorCode status = U_ZERO_ERROR;
480 UDateFormat *fmt;
481 UChar *result;
482 const UChar* expect = u"7:25:43\u202FAM";
483
484 ctest_setTimeZone(NULL, &status);
485
486 fmt= udat_open(UDAT_MEDIUM,UDAT_NONE ,"en_US_CA", NULL, -1, NULL, 0, &status);
487 if(U_FAILURE(status))
488 {
489 log_data_err("FAIL: error in creating the dateformat using medium time style and NO date style: %s (Are you missing data?)\n",
490 myErrorName(status));
491 return;
492 }
493 result =myFormatit(fmt, d);
494 if(!result) {
495 log_data_err("Fail: could not format - exiting test\n");
496 return;
497 }
498 if (u_strcmp(result, expect)!=0){
499 log_err("Fail: %s != %s\n", austrdup(result), austrdup(expect));
500 }
501 else{
502 log_verbose("Ok: %s == %s\n", austrdup(result), austrdup(expect));
503 }
504
505 udat_close(fmt);
506
507 ctest_resetTimeZone();
508 }
509
510 enum { DATE_TEXT_MAX_CHARS = 64 };
511 static const UChar zonePST[] = { 0x50,0x53,0x54,0 }; /* "PST" */
512 static const UDate july022008 = 1215000001979.0; /* 02 July 2008 5:00:01.979 AM PDT (near ICU 4.0 release date :-) */
513 static const double dayMillisec = 8.64e+07;
514
515 static const UChar dMyGGGPattern[] = { 0x64,0x64,0x20,0x4D,0x4D,0x4D,0x20,0x79,0x79,0x79,0x79,0x20,0x47,0x47,0x47,0 }; /* "dd MMM yyyy GGG" */
516 static const UChar dMyGGGGGPattern[] = { 0x64,0x64,0x20,0x4D,0x4D,0x4D,0x20,0x79,0x79,0x79,0x79,0x20,0x47,0x47,0x47,0x47,0x47,0 }; /* "dd MMM yyyy GGGGG" */
517 static const UChar dMyGGGText[] = { 0x30,0x32,0x20,0x4A,0x75,0x6C,0x20,0x32,0x30,0x30,0x38,0x20,0x41,0x44,0 }; /* "02 Jul 2008 AD" */
518 static const UChar dMyGGGGGText[] = { 0x30,0x32,0x20,0x4A,0x75,0x6C,0x20,0x32,0x30,0x30,0x38,0x20,0x41,0 }; /* "02 Jul 2008 A" */
519 static const UChar edMyPattern[] = { 0x65,0x20,0x64,0x64,0x20,0x4D,0x4D,0x4D,0x20,0x79,0x79,0x79,0x79,0 }; /* "e dd MMM yyyy" */
520 static const UChar eedMyPattern[] = { 0x65,0x65,0x20,0x64,0x64,0x20,0x4D,0x4D,0x4D,0x20,0x79,0x79,0x79,0x79,0 }; /* "ee dd MMM yyyy" */
521 static const UChar cdMyPattern[] = { 0x63,0x20,0x64,0x64,0x20,0x4D,0x4D,0x4D,0x20,0x79,0x79,0x79,0x79,0 }; /* "c dd MMM yyyy" */
522 static const UChar ccdMyPattern[] = { 0x63,0x63,0x20,0x64,0x64,0x20,0x4D,0x4D,0x4D,0x20,0x79,0x79,0x79,0x79,0 }; /* "cc dd MMM yyyy" */
523 static const UChar edMyText[] = { 0x34,0x20,0x30,0x32,0x20,0x4A,0x75,0x6C,0x20,0x32,0x30,0x30,0x38,0 }; /* "4 02 Jul 2008" */
524 static const UChar eedMyText[] = { 0x30,0x34,0x20,0x30,0x32,0x20,0x4A,0x75,0x6C,0x20,0x32,0x30,0x30,0x38,0 }; /* "04 02 Jul 2008" */
525 static const UChar eeedMyPattern[] = { 0x65,0x65,0x65,0x20,0x64,0x64,0x20,0x4D,0x4D,0x4D,0x20,0x79,0x79,0x79,0x79,0 }; /* "eee dd MMM yyyy" */
526 static const UChar EEEdMyPattern[] = { 0x45,0x45,0x45,0x20,0x64,0x64,0x20,0x4D,0x4D,0x4D,0x20,0x79,0x79,0x79,0x79,0 }; /* "EEE dd MMM yyyy" */
527 static const UChar EEdMyPattern[] = { 0x45,0x45,0x20,0x64,0x64,0x20,0x4D,0x4D,0x4D,0x20,0x79,0x79,0x79,0x79,0 }; /* "EE dd MMM yyyy" */
528 static const UChar eeedMyText[] = { 0x57,0x65,0x64,0x20,0x30,0x32,0x20,0x4A,0x75,0x6C,0x20,0x32,0x30,0x30,0x38,0 }; /* "Wed 02 Jul 2008" */
529 static const UChar eeeedMyPattern[] = { 0x65,0x65,0x65,0x65,0x20,0x64,0x64,0x20,0x4D,0x4D,0x4D,0x20,0x79,0x79,0x79,0x79,0 }; /* "eeee dd MMM yyyy" */
530 static const UChar eeeedMyText[] = { 0x57,0x65,0x64,0x6E,0x65,0x73,0x64,0x61,0x79,0x20,0x30,0x32,0x20,0x4A,0x75,0x6C,0x20,0x32,0x30,0x30,0x38,0 }; /* "Wednesday 02 Jul 2008" */
531 static const UChar eeeeedMyPattern[] = { 0x65,0x65,0x65,0x65,0x65,0x20,0x64,0x64,0x20,0x4D,0x4D,0x4D,0x20,0x79,0x79,0x79,0x79,0 }; /* "eeeee dd MMM yyyy" */
532 static const UChar eeeeedMyText[] = { 0x57,0x20,0x30,0x32,0x20,0x4A,0x75,0x6C,0x20,0x32,0x30,0x30,0x38,0 }; /* "W 02 Jul 2008" */
533 static const UChar ewYPattern[] = { 0x65,0x20,0x77,0x77,0x20,0x59,0x59,0x59,0x59,0 }; /* "e ww YYYY" */
534 static const UChar cwYPattern[] = { 0x63,0x20,0x77,0x77,0x20,0x59,0x59,0x59,0x59,0 }; /* "c ww YYYY" */
535 static const UChar ewYText[] = { 0x34,0x20,0x32,0x37,0x20,0x32,0x30,0x30,0x38,0 }; /* "4 27 2008" */
536 static const UChar HHmmssPattern[] = { 0x48,0x48,0x3A,0x6D,0x6D,0x3A,0x73,0x73,0 }; /* "HH:mm:ss" */
537 static const UChar HHmmssText[] = { 0x30,0x35,0x3A,0x30,0x30,0x3A,0x30,0x31,0 }; /* "05:00:01" */
538 static const UChar ssSPattern[] = { 0x73,0x73,0x2E,0x53,0 }; /* "ss.S" */
539 static const UChar ssSText[] = { 0x30,0x31,0x2E,0x39,0 }; /* "01.9" */
540 static const UChar ssSSPattern[] = { 0x73,0x73,0x2E,0x53,0x53,0 }; /* "ss.SS" */
541 static const UChar ssSSText[] = { 0x30,0x31,0x2E,0x39,0x37,0 }; /* "01.97" */
542
543 typedef struct {
544 const UChar * pattern;
545 const UChar * text;
546 const char * label;
547 } DatePatternAndText;
548 static const DatePatternAndText datePatternsAndText[] = {
549 { dMyGGGPattern, dMyGGGText, "dd MMM yyyy GGG" },
550 { dMyGGGGGPattern, dMyGGGGGText, "dd MMM yyyy GGGGG" },
551 { edMyPattern, edMyText, "e dd MMM yyyy" },
552 { eedMyPattern, eedMyText, "ee dd MMM yyyy" },
553 { cdMyPattern, edMyText, "c dd MMM yyyy" },
554 { ccdMyPattern, edMyText, "cc dd MMM yyyy" },
555 { eeedMyPattern, eeedMyText, "eee dd MMM yyyy" },
556 { EEEdMyPattern, eeedMyText, "EEE dd MMM yyyy" },
557 { EEdMyPattern, eeedMyText, "EE dd MMM yyyy" },
558 { eeeedMyPattern, eeeedMyText, "eeee dd MMM yyyy" },
559 { eeeeedMyPattern, eeeeedMyText, "eeeee dd MMM yyyy" },
560 { ewYPattern, ewYText, "e ww YYYY" },
561 { cwYPattern, ewYText, "c ww YYYY" },
562 { HHmmssPattern, HHmmssText, "* HH:mm:ss" }, /* '*' at start means don't check value from parse (won't be july022008) */
563 { ssSPattern, ssSText, "* ss.S" },
564 { ssSSPattern, ssSSText, "* ss.SS" },
565 { NULL, NULL, NULL }
566 };
Test_GEec(void)567 void Test_GEec(void)
568 {
569 UErrorCode status = U_ZERO_ERROR;
570 UDateFormat * dtfmt = udat_open(UDAT_LONG, UDAT_LONG, "en", zonePST, -1, NULL, 0, &status);
571 if ( U_SUCCESS(status) ) {
572 const DatePatternAndText *patTextPtr;
573 for (patTextPtr = datePatternsAndText; patTextPtr->pattern != NULL; ++patTextPtr) {
574 UChar dmyGnText[DATE_TEXT_MAX_CHARS];
575 char byteText[3*DATE_TEXT_MAX_CHARS];
576 int32_t dmyGnTextLen;
577 UDate dateResult;
578
579 udat_applyPattern(dtfmt, false, patTextPtr->pattern, -1);
580 dmyGnTextLen = udat_format(dtfmt, july022008, dmyGnText, DATE_TEXT_MAX_CHARS, NULL, &status);
581 (void)dmyGnTextLen; /* Suppress set but not used warning. */
582 if ( U_FAILURE(status) ) {
583 log_err("FAIL: udat_format with %s: %s\n", patTextPtr->label, myErrorName(status) );
584 status = U_ZERO_ERROR;
585 } else if ( u_strcmp(dmyGnText, patTextPtr->text) != 0 ) {
586 log_err("FAIL: udat_format with %s: wrong UChar[] result %s\n", patTextPtr->label, u_austrcpy(byteText,dmyGnText) );
587 }
588
589 dateResult = udat_parse(dtfmt, patTextPtr->text, -1, NULL, &status); /* no time, dateResult != july022008 by some hours */
590 if ( U_FAILURE(status) ) {
591 log_err("FAIL: udat_parse with %s: %s\n", patTextPtr->label, myErrorName(status) );
592 status = U_ZERO_ERROR;
593 } else if ( patTextPtr->label[0] != '*' && july022008 - dateResult > dayMillisec ) {
594 log_err("FAIL: udat_parse with %s: wrong UDate result\n", patTextPtr->label );
595 }
596 }
597 udat_close(dtfmt);
598 } else {
599 log_data_err("FAIL: udat_open fails: %s (Are you missing data?)\n", myErrorName(status));
600 }
601 }
602
603 /*INTERNAL FUNCTION USED */
604
myFormatit(UDateFormat * datdef,UDate d1)605 UChar* myFormatit(UDateFormat* datdef, UDate d1)
606 {
607 UChar *result1=NULL;
608 int32_t resultlength, resultlengthneeded;
609 UErrorCode status = U_ZERO_ERROR;
610
611 resultlength=0;
612 resultlengthneeded=udat_format(datdef, d1, NULL, resultlength, NULL, &status);
613 if(status==U_BUFFER_OVERFLOW_ERROR)
614 {
615 status=U_ZERO_ERROR;
616 resultlength=resultlengthneeded+1;
617 /*result1=(UChar*)malloc(sizeof(UChar) * resultlength);*/ /*this leaks*/
618 result1=(UChar*)ctst_malloc(sizeof(UChar) * resultlength); /*this won't*/
619 udat_format(datdef, d1, result1, resultlength, NULL, &status);
620 }
621 if(U_FAILURE(status))
622 {
623 log_err("FAIL: Error in formatting using udat_format(.....): %s\n", myErrorName(status));
624 return 0;
625 }
626
627
628 return result1;
629
630 }
631
632 #endif /* #if !UCONFIG_NO_FORMATTING */
633
634 /*eof*/
635