]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/cintltst/cdtrgtst.c
ICU-8.11.tar.gz
[apple/icu.git] / icuSources / test / cintltst / cdtrgtst.c
CommitLineData
b75a7d8f
A
1/********************************************************************
2 * COPYRIGHT:
73c04bcf 3 * Copyright (c) 1997-2006, International Business Machines Corporation and
b75a7d8f
A
4 * others. All Rights Reserved.
5 ********************************************************************/
6/********************************************************************************
7*
8* File CDTRGTST.C
9*
10* Madhu Katragadda Ported for C API
11* Modification History:
12* Date Name Description
13* 07/15/99 helena Ported to HPUX 10/11 CC.
14*********************************************************************************
15*/
16/* REGRESSION TEST FOR DATE FORMAT */
17
18#include "unicode/utypes.h"
19
20#if !UCONFIG_NO_FORMATTING
21
22#include "unicode/uloc.h"
23#include "unicode/udat.h"
24#include "unicode/ucal.h"
25#include "unicode/unum.h"
26#include "unicode/ustring.h"
27#include "cintltst.h"
28#include "cdtrgtst.h"
29#include "cmemory.h"
30
31void addDateForRgrTest(TestNode** root);
32
33void addDateForRgrTest(TestNode** root)
34{
35 addTest(root, &Test4029195, "tsformat/cdtrgtst/Test4029195");
36 addTest(root, &Test4056591, "tsformat/cdtrgtst/Test4056591");
37 addTest(root, &Test4059917, "tsformat/cdtrgtst/Test4059917");
38 addTest(root, &Test4060212, "tsformat/cdtrgtst/Test4060212");
39 addTest(root, &Test4061287, "tsformat/cdtrgtst/Test4061287");
40 addTest(root, &Test4073003, "tsformat/cdtrgtst/Test4073003");
41 addTest(root, &Test4162071, "tsformat/cdtrgtst/Test4162071");
42 addTest(root, &Test714, "tsformat/cdtrgtst/Test714");
43}
44
45/**
46 * @bug 4029195
47 */
48void Test4029195()
49{
50 int32_t resultlength, resultlengthneeded;
51 UChar *fmdt, *todayS, *rt;
52 UChar *pat=NULL;
53 UChar *temp;
54 UDate today, d1;
55 UDateFormat *df;
56 int32_t parsepos;
57 UErrorCode status = U_ZERO_ERROR;
58
59 log_verbose("Testing date format and parse function in regression test\n");
60 today = ucal_getNow();
61
62 df = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,"en_US", NULL, 0, NULL, 0, &status);
63 if(U_FAILURE(status))
64 {
65 log_err("FAIL: error in creating the dateformat using default date and time style : %s\n", myErrorName(status));
66 return;
67 }
68 resultlength=0;
69 resultlengthneeded=udat_toPattern(df, TRUE, NULL, resultlength, &status);
70 if(status==U_BUFFER_OVERFLOW_ERROR)
71 {
72 status=U_ZERO_ERROR;
73 resultlength=resultlengthneeded + 1;
74 pat=(UChar*)malloc(sizeof(UChar) * resultlength);
75 udat_toPattern(df, TRUE, pat, resultlength, &status);
76 }
77
78 log_verbose("pattern: %s\n", austrdup(pat));
79
80
81 fmdt = myFormatit(df, today);
82 if(fmdt) {
83 log_verbose("today: %s\n", austrdup(fmdt));
84 } else {
85 log_data_err("ERROR: couldn't format, exitting test");
86 return;
87 }
88
89 temp=(UChar*)malloc(sizeof(UChar) * 10);
90 u_uastrcpy(temp, "M yyyy dd");
91 udat_applyPattern(df, TRUE, temp, u_strlen(temp));
92
93 todayS =myFormatit(df, today);
94 log_verbose("After teh pattern is applied\n today: %s\n", austrdup(todayS) );
95 parsepos=0;
96 d1=udat_parse(df, todayS, u_strlen(todayS), &parsepos, &status);
97 if(U_FAILURE(status))
98 {
99 log_err("FAIL: Error in parsing using udat_parse(.....): %s\n", myErrorName(status));
100 }
101
102 rt =myFormatit(df, d1);
103 log_verbose("today: %s\n", austrdup(rt) );
104
105 log_verbose("round trip: %s\n", austrdup(rt) );
106
107 if(u_strcmp(rt, todayS)!=0) {
108 log_err("Fail: Want %s Got %s\n", austrdup(todayS), austrdup(rt) );
109 }
110 else
111 log_verbose("Pass: parse and format working fine\n");
112 udat_close(df);
113 free(temp);
114 if(pat != NULL) {
115 free(pat);
116 }
117}
118
119
120/**
121 * @bug 4056591
122 * Verify the function of the [s|g]et2DigitYearStart() API.
123 */
124void Test4056591()
125{
126 int i;
127 UCalendar *cal;
128 UDateFormat *def;
129 UDate start,exp,got;
130 UChar s[10];
131 UChar *gotdate, *expdate;
132 UChar pat[10];
b75a7d8f
A
133 UDate d[4];
134 UErrorCode status = U_ZERO_ERROR;
135 const char* strings[] = {
136 "091225",
137 "091224",
138 "611226",
139 "991227"
140 };
141
142 log_verbose("Testing s[get] 2 digit year start regressively\n");
374ca955 143 cal=ucal_open(NULL, 0, "en_US", UCAL_GREGORIAN, &status);
b75a7d8f
A
144 if(U_FAILURE(status)){
145 log_err("error in ucal_open caldef : %s\n", myErrorName(status));
146 }
147 ucal_setDateTime(cal, 1809, UCAL_DECEMBER, 25, 17, 40, 30, &status);
148 d[0]=ucal_getMillis(cal, &status);
149 if(U_FAILURE(status)){
150 log_err("Error: failure in get millis: %s\n", myErrorName(status));
151 }
152 ucal_setDateTime(cal, 1909, UCAL_DECEMBER, 24, 17, 40, 30, &status);
153 d[1]=ucal_getMillis(cal, &status);
154 ucal_setDateTime(cal, 1861, UCAL_DECEMBER, 26, 17, 40, 30, &status);
155 d[2]=ucal_getMillis(cal, &status);
156 ucal_setDateTime(cal, 1999, UCAL_DECEMBER, 27, 17, 40, 30, &status);
157 d[3]=ucal_getMillis(cal, &status);
158
159
160 u_uastrcpy(pat, "yyMMdd");
374ca955 161 def = udat_open(UDAT_IGNORE,UDAT_IGNORE,NULL, NULL, 0, pat, u_strlen(pat), &status);
b75a7d8f
A
162 if(U_FAILURE(status))
163 {
164 log_err("FAIL: error in creating the dateformat using u_openPattern(): %s\n", myErrorName(status));
165 return;
166 }
167 start = 1800;
168 udat_set2DigitYearStart(def, start, &status);
169 if(U_FAILURE(status))
170 log_err("ERROR: in setTwoDigitStartDate: %s\n", myErrorName(status));
171 if( (udat_get2DigitYearStart(def, &status) != start))
172 log_err("ERROR: get2DigitYearStart broken\n");
173
174
175 for(i = 0; i < 4; ++i) {
176 u_uastrcpy(s, strings[i]);
177 exp = d[i];
178 got = udat_parse(def, s, u_strlen(s), 0, &status);
179 gotdate=myFormatit(def, got);
180 expdate=myFormatit(def, exp);
181
73c04bcf
A
182 if (gotdate == NULL || expdate == NULL) {
183 log_err("myFormatit failed!\n");
184 }
185 else if(u_strcmp(gotdate, expdate) !=0){
b75a7d8f
A
186 log_err("set2DigitYearStart broken for %s \n got: %s, expected: %s\n", austrdup(s),
187 austrdup(gotdate), austrdup(expdate) );
188 }
189 }
190
191 udat_close(def);
192 ucal_close(cal);
b75a7d8f
A
193}
194
195
196/**
73c04bcf 197 * SimpleDateFormat does not properly parse date strings without delimiters
b75a7d8f
A
198 * @bug 4059917
199 */
200void Test4059917()
201{
202 UDateFormat* def;
203 UChar *myDate;
204 UErrorCode status = U_ZERO_ERROR;
205 UChar *pattern;
374ca955
A
206 UChar tzID[4];
207
b75a7d8f 208 log_verbose("Testing apply pattern and to pattern regressively\n");
374ca955 209 u_uastrcpy(tzID, "PST");
b75a7d8f
A
210 pattern=(UChar*)malloc(sizeof(UChar) * 11);
211 u_uastrcpy(pattern, "yyyy/MM/dd");
212 log_verbose("%s\n", austrdup(pattern) );
374ca955 213 def = udat_open(UDAT_IGNORE,UDAT_IGNORE,NULL,tzID,-1,pattern, u_strlen(pattern),&status);
b75a7d8f
A
214 if(U_FAILURE(status))
215 {
216 log_err("FAIL: error in creating the dateformat using openPattern: %s\n", myErrorName(status));
217 return;
218 }
219 myDate=(UChar*)malloc(sizeof(UChar) * 11);
220 u_uastrcpy(myDate, "1970/01/12");
221
222 aux917( def, myDate );
223 udat_close(def);
224
225 u_uastrcpy(pattern, "yyyyMMdd");
73c04bcf 226 def = udat_open(UDAT_IGNORE,UDAT_IGNORE,NULL,tzID,-1,pattern, u_strlen(pattern),&status);
b75a7d8f
A
227 if(U_FAILURE(status))
228 {
229 log_err("FAIL: error in creating the dateformat using openPattern: %s\n", myErrorName(status));
230 return;
231 }
232 u_uastrcpy(myDate, "19700112");
233 aux917( def, myDate );
234 udat_close(def);
235 free(pattern);
236 free(myDate);
237
238}
239
240void aux917( UDateFormat *fmt, UChar* str)
241{
374ca955
A
242 int32_t resultlength, resultlengthneeded;
243 UErrorCode status = U_ZERO_ERROR;
244 UChar* formatted=NULL;
245 UChar *pat=NULL;
246 UDate d1=1000000000.0;
b75a7d8f
A
247
248 resultlength=0;
249 resultlengthneeded=udat_toPattern(fmt, TRUE, NULL, resultlength, &status);
250 if(status==U_BUFFER_OVERFLOW_ERROR)
251 {
252 status=U_ZERO_ERROR;
253 resultlength=resultlengthneeded + 1;
254 pat=(UChar*)malloc(sizeof(UChar) * (resultlength));
255 udat_toPattern(fmt, TRUE, pat, resultlength, &status);
256 }
257 if(U_FAILURE(status)){
258 log_err("failure in retrieving the pattern: %s\n", myErrorName(status));
259 }
260 log_verbose("pattern: %s\n", austrdup(pat) );
261
262 status = U_ZERO_ERROR;
263 formatted = myFormatit(fmt, d1);
264 if( u_strcmp(formatted,str)!=0) {
265 log_err("Fail: Want %s Got: %s\n", austrdup(str), austrdup(formatted) );
266 }
267 free(pat);
268}
269
270/**
271 * @bug 4060212
272 */
273void Test4060212()
274{
275 int32_t pos;
276 UCalendar *cal;
277 UDateFormat *formatter, *fmt;
278 UErrorCode status = U_ZERO_ERROR;
279 UDate myDate;
280 UChar *myString;
374ca955
A
281 UChar dateString[30], pattern[20], tzID[4];
282 u_uastrcpy(dateString, "1995-040.05:01:29 -8");
283 u_uastrcpy(pattern, "yyyy-DDD.hh:mm:ss z");
b75a7d8f
A
284
285 log_verbose( "dateString= %s Using yyyy-DDD.hh:mm:ss\n", austrdup(dateString) );
286 status = U_ZERO_ERROR;
374ca955
A
287 u_uastrcpy(tzID, "PST");
288
289 formatter = udat_open(UDAT_IGNORE,UDAT_IGNORE,"en_US",tzID,-1,pattern, u_strlen(pattern), &status);
b75a7d8f
A
290 pos=0;
291 myDate = udat_parse(formatter, dateString, u_strlen(dateString), &pos, &status);
292
293
374ca955 294 fmt = udat_open(UDAT_FULL,UDAT_LONG ,NULL, tzID, -1, NULL, 0, &status);
b75a7d8f
A
295 if(U_FAILURE(status))
296 {
297 log_err("FAIL: error in creating the dateformat using default date and time style: %s\n",
298 myErrorName(status) );
299 return;
300 }
301 myString = myFormatit(fmt, myDate);
b75a7d8f
A
302 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_GREGORIAN, &status);
303 if(U_FAILURE(status)){
304 log_err("FAIL: error in ucal_open caldef : %s\n", myErrorName(status));
305 }
306 ucal_setMillis(cal, myDate, &status);
307 if ((ucal_get(cal, UCAL_DAY_OF_YEAR, &status) != 40)){
308 log_err("Fail: Got %d Expected 40\n", ucal_get(cal, UCAL_DAY_OF_YEAR, &status));
309 }
310
311 udat_close(formatter);
312 ucal_close(cal);
313 udat_close(fmt);
314
315}
316
317/**
318 * @bug 4061287
319 */
320void Test4061287()
321{
322 UBool ok;
323 int32_t pos;
324 UDateFormat *df;
325 UErrorCode status = U_ZERO_ERROR;
326 UDate myDate;
327 UChar pattern[21], dateString[11];
328
329 u_uastrcpy(dateString, "35/13/1971");
330 u_uastrcpy(pattern, "dd/mm/yyyy");
331 status = U_ZERO_ERROR;
332 log_verbose("Testing parsing by changing the attribute lenient\n");
333 df = udat_open(UDAT_IGNORE,UDAT_IGNORE,NULL,NULL,0,pattern, u_strlen(pattern),&status);
334 if(U_FAILURE(status)){
335 log_err("ERROR: failure in open pattern of test4061287: %s\n", myErrorName(status));
336 return;
337 }
338
339 pos=0;
340
341 udat_setLenient(df, FALSE);
342 ok=udat_isLenient(df);
343 if(ok==TRUE)
344 log_err("setLenient nor working\n");
345 ok = FALSE;
346 myDate = udat_parse(df, dateString, u_strlen(dateString), &pos, &status);
347 if(U_FAILURE(status))
348 ok = TRUE;
349 if(ok!=TRUE)
350 log_err("Fail: Lenient not working: does lenient parsing in spite of setting Leninent as FALSE ");
351
352 udat_close(df);
353
354}
355
356
357
358/* The java.text.DateFormat.parse(String) method expects for the
359 US locale a string formatted according to mm/dd/yy and parses it
360 correctly.
361
362 When given a string mm/dd/yyyy it only parses up to the first
363 two y's, typically resulting in a date in the year 1919.
364
365 Please extend the parsing method(s) to handle strings with
366 four-digit year values (probably also applicable to various
367 other locales. */
368/**
369 * @bug 4073003
370 */
371void Test4073003()
372{
373 int32_t pos,i;
374 UDate d,dd;
375 UChar *datestr;
376 UChar temp[15];
377 UErrorCode status = U_ZERO_ERROR;
378 UDateFormat *fmt;
379 UChar *result, *result2;
380 const char* tests [] = {
381 "12/25/61",
382 "12/25/1961",
383 "4/3/1999",
384 "4/3/99"
385 };
386
387 fmt= udat_open(UDAT_SHORT,UDAT_SHORT ,NULL, NULL, 0, NULL, 0, &status);
388 if(U_FAILURE(status))
389 {
390 log_err("FAIL: error in creating the dateformat using short date and time style: %s\n",
391 myErrorName(status));
392 return;
393 }
394 u_uastrcpy(temp, "m/D/yy");
73c04bcf
A
395 udat_applyPattern(fmt, FALSE, temp, u_strlen(temp));
396
397 for(i= 0; i < 4; i+=2) {
398 status=U_ZERO_ERROR;
399 datestr=(UChar*)malloc(sizeof(UChar) * (strlen(tests[i])+1));
400 u_uastrcpy(datestr, tests[i]);
401
402 pos=0;
403 d = udat_parse(fmt, datestr, u_strlen(datestr), &pos, &status);
404 if(U_FAILURE(status)){
405 log_err("ERROR : in test 4073003: %s\n", myErrorName(status));
406 }
407
408 free(datestr);
409 datestr=(UChar*)malloc(sizeof(UChar) * (strlen(tests[i+1])+1));
410 u_uastrcpy(datestr, tests[i+1]);
b75a7d8f 411
73c04bcf
A
412 pos=0;
413 status=U_ZERO_ERROR;
414 dd = udat_parse(fmt, datestr, u_strlen(datestr), &pos, &status);
415 if(U_FAILURE(status)){
416 log_err("ERROR : in test 4073003: %s\n", myErrorName(status));
417 }
418 free(datestr);
b75a7d8f 419
73c04bcf
A
420 result =myFormatit(fmt, d);
421 result2 =myFormatit(fmt, dd);
422 if(!result || !result2) {
423 log_data_err("Fail: could not format - exitting test\n");
424 return;
425 }
426 if (u_strcmp(result, result2)!=0){
427 log_err("Fail: %s != %s\n", austrdup(result), austrdup(result2) );
428 }
429 else{
b75a7d8f 430 log_verbose("Ok: %s == %s\n", austrdup(result), austrdup(result2) );
b75a7d8f 431 }
73c04bcf
A
432
433 }
434 udat_close(fmt);
b75a7d8f
A
435}
436
437/**
438 * @bug 4162071
439 **/
440void Test4162071()
441{
442 int32_t pos;
443 UDate x;
444 UErrorCode status = U_ZERO_ERROR;
445 UDateFormat *df;
446 UChar datestr[30];
447 UChar format[50];
448 u_uastrcpy(datestr, "Thu, 30-Jul-1999 11:51:14 GMT");
449 u_uastrcpy(format, "EEE', 'dd-MMM-yyyy HH:mm:ss z"); /* RFC 822/1123 */
450 status = U_ZERO_ERROR;
451 /* Can't hardcode the result to assume the default locale is "en_US". */
452 df = udat_open(UDAT_IGNORE,UDAT_IGNORE,"en_US",NULL,0,format, u_strlen(format),&status);
453 if(U_FAILURE(status)){
454 log_data_err("ERROR: couldn't create date format: %s\n", myErrorName(status));
455 return;
456 }
457 pos=0;
458 x = udat_parse(df, datestr, u_strlen(datestr), &pos, &status);
459 if(U_FAILURE(status)){
73c04bcf
A
460 log_data_err("ERROR : parse format %s fails : %s\n", austrdup(format), myErrorName(status));
461 }
b75a7d8f
A
462 else{
463 log_verbose("Parse format \"%s \" ok.\n", austrdup(format) );
464 }
73c04bcf 465 /*log_verbose("date= %s\n", austrdup(myFormatit(df, x)) );*/
b75a7d8f
A
466 udat_close(df);
467}
468
469void Test714(void)
470{
471 UDate d=978103543000.0;
472 UChar temp[20];
473 UErrorCode status = U_ZERO_ERROR;
474 UDateFormat *fmt;
475 UChar *result;
476 const char* expect = "7:25:43 AM";
477
374ca955
A
478 ctest_setTimeZone(NULL, &status);
479
b75a7d8f
A
480 fmt= udat_open(UDAT_MEDIUM,UDAT_NONE ,"en_US_CA", NULL, -1, NULL, 0, &status);
481 if(U_FAILURE(status))
482 {
483 log_err("FAIL: error in creating the dateformat using medium time style and NO date style: %s\n",
484 myErrorName(status));
485 return;
486 }
487 result =myFormatit(fmt, d);
488 if(!result) {
489 log_data_err("Fail: could not format - exitting test\n");
490 return;
491 }
492 u_uastrcpy(temp, expect);
493 if (u_strcmp(result, temp)!=0){
494 log_err("Fail: %s != %s\n", austrdup(result), expect);
495 }
496 else{
497 log_verbose("Ok: %s == %s\n", austrdup(result), expect );
498 }
499
500 udat_close(fmt);
374ca955
A
501
502 ctest_resetTimeZone();
b75a7d8f
A
503}
504
505/*INTERNAL FUNCTION USED */
506
507UChar* myFormatit(UDateFormat* datdef, UDate d1)
508{
509 UChar *result1=NULL;
510 int32_t resultlength, resultlengthneeded;
511 UErrorCode status = U_ZERO_ERROR;
512
513 resultlength=0;
514 resultlengthneeded=udat_format(datdef, d1, NULL, resultlength, NULL, &status);
515 if(status==U_BUFFER_OVERFLOW_ERROR)
516 {
517 status=U_ZERO_ERROR;
518 resultlength=resultlengthneeded+1;
519 /*result1=(UChar*)malloc(sizeof(UChar) * resultlength);*/ /*this leaks*/
520 result1=(UChar*)ctst_malloc(sizeof(UChar) * resultlength); /*this won't*/
521 udat_format(datdef, d1, result1, resultlength, NULL, &status);
522 }
523 if(U_FAILURE(status))
524 {
525 log_err("FAIL: Error in formatting using udat_format(.....): %s\n", myErrorName(status));
526 return 0;
527 }
528
529
530 return result1;
531
532}
533
534#endif /* #if !UCONFIG_NO_FORMATTING */
535
536/*eof*/