]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/cdtdptst.c
ICU-6.2.14.tar.gz
[apple/icu.git] / icuSources / test / cintltst / cdtdptst.c
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2003, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /********************************************************************************
7 *
8 * File CDTDPTST.C
9 *
10 * Modification History:
11 * Name Description
12 * Madhu Katragadda Creation
13 *********************************************************************************
14 */
15 /* INDEPTH TEST FOR DATE FORMAT */
16
17 #include "unicode/utypes.h"
18
19 #if !UCONFIG_NO_FORMATTING
20
21 #include "unicode/uloc.h"
22 #include "unicode/udat.h"
23 #include "unicode/ucal.h"
24 #include "unicode/unum.h"
25 #include "unicode/ustring.h"
26 #include "cintltst.h"
27 #include "cdtdptst.h"
28 #include "cformtst.h"
29
30 #include "cmemory.h"
31
32 void addDtFrDepTest(TestNode** root);
33
34 void addDtFrDepTest(TestNode** root)
35 {
36 addTest(root, &TestTwoDigitYearDSTParse, "tsformat/cdtdptst/TestTwoDigitYearDSTParse");
37 addTest(root, &TestPartialParse994, "tsformat/cdtdptst/TestPartialParse994");
38 addTest(root, &TestRunTogetherPattern985, "tsformat/cdtdptst/TestRunTogetherPattern985");
39 addTest(root, &TestCzechMonths459, "tsformat/cdtdptst/TestCzechMonths459");
40 addTest(root, &TestQuotePattern161, "tsformat/cdtdptst/TestQuotePattern161");
41
42 }
43
44 /**
45 * Test the parsing of 2-digit years.
46 */
47 void TestTwoDigitYearDSTParse()
48 {
49 UDateFormat *fullFmt, *fmt;
50 UErrorCode status = U_ZERO_ERROR;
51 UChar *pattern;
52 UDate d;
53 UChar *s;
54 int32_t pos;
55
56 ctest_setTimeZone(NULL, &status);
57
58 pattern=(UChar*)malloc(sizeof(UChar) * (strlen("EEE MMM dd HH:mm:ss.SSS zzz yyyy G")+1 ));
59 u_uastrcpy(pattern, "EEE MMM dd HH:mm:ss.SSS zzz yyyy G");
60 fullFmt= udat_open(UDAT_IGNORE, UDAT_IGNORE,"en_US",NULL,0,pattern, u_strlen(pattern),&status);
61 if(U_FAILURE(status)) {
62 log_err("FAIL: Error in creating a date format using udat_openPattern \n %s\n",
63 myErrorName(status) );
64 }
65 else {log_verbose("PASS: creating dateformat using udat_openPattern() succesful\n");}
66
67 u_uastrcpy(pattern, "dd-MMM-yy h:mm:ss 'o''clock' a z");
68 fmt= udat_open(UDAT_IGNORE,UDAT_IGNORE,"en_US", NULL, 0,pattern, u_strlen(pattern), &status);
69
70
71 s=(UChar*)malloc(sizeof(UChar) * (strlen("03-Apr-04 2:20:47 o'clock AM PST")+1) );
72 u_uastrcpy(s, "03-Apr-04 2:20:47 o'clock AM PST");
73 pos=0;
74 d = udat_parse(fmt, s, u_strlen(s), &pos, &status);
75 if (U_FAILURE(status)) {
76 log_err("FAIL: Could not parse \"%s\"\n", austrdup(s));
77 } else {
78 UCalendar *cal = ucal_open(NULL, 0, uloc_getDefault(), UCAL_TRADITIONAL, &status);
79 if (U_FAILURE(status)) {
80 log_err("FAIL: Could not open calendar");
81 } else {
82 int32_t h;
83 ucal_setMillis(cal, d, &status);
84 h = ucal_get(cal, UCAL_HOUR_OF_DAY, &status);
85 if (U_FAILURE(status)) {
86 log_err("FAIL: Some calendar operations failed");
87 } else if (h != 2) {
88 log_err("FAIL: Parse of \"%s\" returned HOUR_OF_DAY %d\n",
89 austrdup(s), h);
90 }
91 ucal_close(cal);
92 }
93 }
94
95 udat_close(fullFmt);
96 udat_close(fmt);
97 free(pattern);
98 free(s);
99
100 ctest_resetTimeZone();
101 }
102
103
104 /**
105 * Verify that strings which contain incomplete specifications are parsed
106 * correctly. In some instances, this means not being parsed at all, and
107 * returning an appropriate error.
108 */
109 void TestPartialParse994()
110 {
111 int32_t pos;
112 UDateFormat *f;
113 UErrorCode status = U_ZERO_ERROR;
114 UChar *s;
115 UChar *fmtChars;
116 UDate d, null;
117 null=0;
118
119 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
120 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
121 /* f = udat_open(UDAT_DEFAULT, UDAT_SHORT, NULL, NULL, 0, &status); */
122 f = udat_open(UDAT_DEFAULT, UDAT_SHORT, "en_US", NULL, 0, NULL, 0,&status);
123 if(U_FAILURE(status)){
124 log_err("FAIL: ErrorCode received during test: %s\n", myErrorName(status));
125 return;
126 }
127 s=(UChar*)malloc(sizeof(UChar) * (strlen("01/01/1997 10:11:42 AM")+1) );
128 u_uastrcpy(s, "01/01/1997 10:11:42 AM");
129 pos=0;
130 d = udat_parse(f, s, u_strlen(s), &pos, &status);
131 if(U_FAILURE(status)) {
132 log_data_err("FAIL: could not parse - exitting");
133 return;
134 }
135 fmtChars = myDateFormat(f, d);
136 if(fmtChars) {
137 log_verbose("%s\n", fmtChars);
138 } else {
139 log_data_err("FAIL: could not format \n");
140 return;
141 }
142 tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01 10:11:42", d);
143 tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01 10:", null);
144 tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01 10", null);
145 tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01 ", null);
146 tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01", null);
147 udat_close(f);
148 free(s);
149 }
150
151
152
153 void tryPat994(UDateFormat* format, const char* pattern, const char* s, UDate expected)
154 {
155 UChar *f;
156 UChar *str, *pat;
157 UDate date;
158 UDate null=0;
159 int32_t pos;
160 UErrorCode status = U_ZERO_ERROR;
161 str=(UChar*)malloc(sizeof(UChar) * (strlen(s) + 1) );
162 u_uastrcpy(str, s);
163 pat=(UChar*)malloc(sizeof(UChar) * (strlen(pattern) + 1) );
164 u_uastrcpy(pat, pattern);
165 log_verbose("Pattern : %s ; String : %s\n", austrdup(pat), austrdup(str));
166 udat_applyPattern(format, FALSE, pat, u_strlen(pat));
167 pos=0;
168 date = udat_parse(format, str, u_strlen(str), &pos, &status);
169 if(U_FAILURE(status) || date == null) {
170 log_verbose("ParseException: : %s\n", myErrorName(status) );
171 if (expected != null)
172 log_err("FAIL: Expected: %s\n", austrdup(myDateFormat(format, expected)) );
173 }
174 else {
175 f=myDateFormat(format, date);
176 log_verbose(" parse( %s ) -> %s\n", austrdup(str), austrdup(f));
177 if (expected == null || date != expected)
178 log_err("FAIL: Expected null for \"%s\"\n", s);
179 if (u_strcmp(f, str) !=0)
180 log_err("FAIL: Expected : %s\n", austrdup(str) );
181 }
182
183 free(str);
184 free(pat);
185 }
186
187
188 /**
189 * Verify the behavior of patterns in which digits for different fields run together
190 * without intervening separators.
191 */
192 void TestRunTogetherPattern985()
193 {
194 int32_t pos;
195 UChar *pattern=NULL, *now=NULL, *then=NULL;
196 UDateFormat *format;
197 UDate date1, date2;
198 UErrorCode status = U_ZERO_ERROR;
199 pattern=(UChar*)malloc(sizeof(UChar) * (strlen("yyyyMMddHHmmssSSS")+1) );
200 u_uastrcpy(pattern, "yyyyMMddHHmmssSSS");
201 format = udat_open(UDAT_IGNORE, UDAT_IGNORE, NULL, NULL, 0,pattern, u_strlen(pattern), &status);
202 if(U_FAILURE(status)){
203 log_err("FAIL: Error in date format construction with pattern: %s\n", myErrorName(status));
204 return;
205 }
206 date1 = ucal_getNow();
207 now=myDateFormat(format, date1);
208 log_verbose("%s\n", austrdup(now) );
209 pos = 0;
210 date2 = udat_parse(format, now, u_strlen(now), &pos, &status);
211 if (date2 == 0) log_verbose("Parse stopped at : %d\n", pos);
212 else then=myDateFormat(format, date2);
213 log_verbose("%s\n", austrdup(then) );
214 if (!(date2 == date1)) log_err("FAIL\n");
215
216 udat_close(format);
217 free(pattern);
218
219 }
220
221 /**
222 * Verify the handling of Czech June and July, which have the unique attribute that
223 * one is a proper prefix substring of the other.
224 */
225 void TestCzechMonths459()
226 {
227 int32_t lneed, pos;
228 UChar *pattern=NULL, *tzID=NULL;
229 UChar *juneStr, *julyStr;
230 UDateFormat *fmt;
231 UCalendar *cal;
232 UDate june, july, d;
233 UErrorCode status = U_ZERO_ERROR;
234 UChar *date;
235
236 fmt = udat_open(UDAT_FULL, UDAT_FULL, "cs", NULL, 0, NULL, 0, &status);
237 if(U_FAILURE(status)){
238 log_err("Error in constructing the date format\n");
239 return;
240 }
241 lneed=0;
242 lneed=udat_toPattern(fmt, TRUE, NULL, lneed, &status);
243 if(status==U_BUFFER_OVERFLOW_ERROR){
244 status=U_ZERO_ERROR;
245 pattern=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
246 udat_toPattern(fmt, TRUE, pattern, lneed+1, &status);
247 }
248 if(U_FAILURE(status)){ log_err("Error in extracting the pattern\n"); }
249 tzID=(UChar*)malloc(sizeof(UChar) * 4);
250 u_uastrcpy(tzID, "GMT");
251 cal=ucal_open(tzID, u_strlen(tzID), "cs", UCAL_GREGORIAN, &status);
252 if(U_FAILURE(status)){ log_err("error in ucal_open caldef : %s\n", myErrorName(status)); }
253
254 ucal_setDate(cal, 1997, UCAL_JUNE, 15, &status);
255 june=ucal_getMillis(cal, &status);
256 ucal_setDate(cal, 1997, UCAL_JULY, 15, &status);
257 july=ucal_getMillis(cal, &status);
258
259
260 juneStr = myDateFormat(fmt, june);
261 julyStr = myDateFormat(fmt, july);
262 pos=0;
263 if(juneStr == NULL) {
264 log_data_err("Can't load juneStr. Quitting.\n");
265 return;
266 }
267 d = udat_parse(fmt, juneStr, u_strlen(juneStr), &pos, &status);
268 date = myDateFormat(fmt, d);
269 if(u_strcmp(myDateFormat(fmt, june), myDateFormat(fmt, d) ) !=0)
270 log_err("Error in handling the czech month june\n");
271 else
272 log_verbose("Pass: Date = %s (czech month June)\n", aescstrdup(date, -1));
273 pos=0;
274 d = udat_parse(fmt, julyStr, u_strlen(julyStr), &pos, &status);
275 date = myDateFormat(fmt, d);
276 if(u_strcmp(myDateFormat(fmt, july), myDateFormat(fmt, d) ) !=0)
277 log_err("Error in handling the czech month july\n");
278 else
279 log_verbose("Pass: Date = %s (czech month July)\n", aescstrdup(date, -1));
280
281 udat_close(fmt);
282 ucal_close(cal);
283 free(pattern);
284 free(tzID);
285 }
286
287 /**
288 * Test the handling of single quotes in patterns.
289 */
290 void TestQuotePattern161()
291 {
292 UDateFormat *format;
293 UCalendar *cal;
294 UDate currentTime_1;
295 UChar *pattern, *tzID, *exp;
296 UChar *dateString;
297 UErrorCode status = U_ZERO_ERROR;
298 const char* expStr = "04/13/1999 at 10:42:28 AM ";
299
300 ctest_setTimeZone(NULL, &status);
301
302 pattern=(UChar*)malloc(sizeof(UChar) * (strlen("MM/dd/yyyy 'at' hh:mm:ss a zzz")+1) );
303 u_uastrcpy(pattern, "MM/dd/yyyy 'at' hh:mm:ss a zzz");
304
305 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
306 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
307 /* format= udat_openPattern(pattern, u_strlen(pattern), NULL, &status); */
308 format= udat_open(UDAT_IGNORE, UDAT_IGNORE,"en_US", NULL, 0,pattern, u_strlen(pattern), &status);
309 if(U_FAILURE(status)){
310 log_err("error in udat_open: %s\n", myErrorName(status));
311 return;
312 }
313 tzID=(UChar*)malloc(sizeof(UChar) * 4);
314 u_uastrcpy(tzID, "PST");
315 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
316 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
317 /* cal=ucal_open(tzID, u_strlen(tzID), NULL, UCAL_TRADITIONAL, &status); */
318 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
319 if(U_FAILURE(status)){ log_err("error in ucal_open cal : %s\n", myErrorName(status)); }
320
321 ucal_setDateTime(cal, 1999, UCAL_APRIL, 13, 10, 42, 28, &status);
322 currentTime_1 = ucal_getMillis(cal, &status);
323
324 dateString = myDateFormat(format, currentTime_1);
325 exp=(UChar*)malloc(sizeof(UChar) * (strlen(expStr) + 1) );
326 u_uastrcpy(exp, expStr);
327
328 log_verbose("%s\n", austrdup(dateString) );
329 if(u_strncmp(dateString, exp, (int32_t)strlen(expStr)) !=0)
330 log_err("Error in formatting a pattern with single quotes\n");
331
332 udat_close(format);
333 ucal_close(cal);
334 free(exp);
335 free(tzID);
336 free(pattern);
337
338 ctest_resetTimeZone();
339 }
340
341 #endif /* #if !UCONFIG_NO_FORMATTING */