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