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