1 /********************************************************************
3 * Copyright (c) 1997-2014, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /********************************************************************************
10 * Modification History:
12 * Madhu Katragadda Creation
13 *********************************************************************************
15 /* INDEPTH TEST FOR DATE FORMAT */
17 #include "unicode/utypes.h"
19 #if !UCONFIG_NO_FORMATTING
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"
32 void addDtFrDepTest(TestNode
** root
);
34 void addDtFrDepTest(TestNode
** root
)
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 addTest(root
, &TestBooleanAttributes
, "tsformat/cdtdptst/TestBooleanAttributes");
47 * Test the parsing of 2-digit years.
49 void TestTwoDigitYearDSTParse()
51 UDateFormat
*fullFmt
, *fmt
;
52 UErrorCode status
= U_ZERO_ERROR
;
58 ctest_setTimeZone(NULL
, &status
);
60 pattern
=(UChar
*)malloc(sizeof(UChar
) * (strlen("EEE MMM dd HH:mm:ss.SSS zzz yyyy G")+1 ));
61 u_uastrcpy(pattern
, "EEE MMM dd HH:mm:ss.SSS zzz yyyy G");
62 fullFmt
= udat_open(UDAT_PATTERN
, UDAT_PATTERN
,"en_US",NULL
,0,pattern
, u_strlen(pattern
),&status
);
63 if(U_FAILURE(status
)) {
64 log_data_err("FAIL: Error in creating a date format using udat_openPattern %s - (Are you missing data?)\n",
65 myErrorName(status
) );
68 log_verbose("PASS: creating dateformat using udat_openPattern() succesful\n");
70 u_uastrcpy(pattern
, "dd-MMM-yy h:mm:ss 'o''clock' a z");
71 fmt
= udat_open(UDAT_PATTERN
,UDAT_PATTERN
,"en_US", NULL
, 0,pattern
, u_strlen(pattern
), &status
);
74 s
=(UChar
*)malloc(sizeof(UChar
) * (strlen("03-Apr-04 2:20:47 o'clock AM PST")+1) );
75 u_uastrcpy(s
, "03-Apr-04 2:20:47 o'clock AM PST");
77 d
= udat_parse(fmt
, s
, u_strlen(s
), &pos
, &status
);
78 if (U_FAILURE(status
)) {
79 log_err("FAIL: Could not parse \"%s\"\n", austrdup(s
));
81 UCalendar
*cal
= ucal_open(NULL
, 0, uloc_getDefault(), UCAL_TRADITIONAL
, &status
);
82 if (U_FAILURE(status
)) {
83 log_err_status(status
, "FAIL: Could not open calendar: %s\n", u_errorName(status
));
86 ucal_setMillis(cal
, d
, &status
);
87 h
= ucal_get(cal
, UCAL_HOUR_OF_DAY
, &status
);
88 if (U_FAILURE(status
)) {
89 log_err("FAIL: Some calendar operations failed");
91 log_err("FAIL: Parse of \"%s\" returned HOUR_OF_DAY %d\n",
104 ctest_resetTimeZone();
109 * Verify that strings which contain incomplete specifications are parsed
110 * correctly. In some instances, this means not being parsed at all, and
111 * returning an appropriate error.
113 void TestPartialParse994()
117 UErrorCode status
= U_ZERO_ERROR
;
123 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
124 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
125 /* f = udat_open(UDAT_DEFAULT, UDAT_SHORT, NULL, NULL, 0, &status); */
126 f
= udat_open(UDAT_DEFAULT
, UDAT_SHORT
, "en_US", NULL
, 0, NULL
, 0,&status
);
127 if(U_FAILURE(status
)){
128 log_data_err("FAIL: ErrorCode received during test: %s (Are you missing data?)\n", myErrorName(status
));
131 s
=(UChar
*)malloc(sizeof(UChar
) * (strlen("01/01/1997 10:11:42 AM")+1) );
132 u_uastrcpy(s
, "01/01/1997 10:11:42 AM");
134 d
= udat_parse(f
, s
, u_strlen(s
), &pos
, &status
);
135 if(U_FAILURE(status
)) {
136 log_data_err("FAIL: could not parse - exitting");
139 fmtChars
= myDateFormat(f
, d
);
141 log_verbose("%s\n", fmtChars
);
143 log_data_err("FAIL: could not format \n");
146 tryPat994(f
, "yy/MM/dd HH:mm:ss", "97/01/01 10:11:42", d
);
147 tryPat994(f
, "yy/MM/dd HH:mm:ss", "97/01/01 10:", null
);
148 tryPat994(f
, "yy/MM/dd HH:mm:ss", "97/01/01 10", null
);
149 tryPat994(f
, "yy/MM/dd HH:mm:ss", "97/01/01 ", null
);
150 tryPat994(f
, "yy/MM/dd HH:mm:ss", "97/01/01", null
);
157 void tryPat994(UDateFormat
* format
, const char* pattern
, const char* s
, UDate expected
)
164 UErrorCode status
= U_ZERO_ERROR
;
165 str
=(UChar
*)malloc(sizeof(UChar
) * (strlen(s
) + 1) );
167 pat
=(UChar
*)malloc(sizeof(UChar
) * (strlen(pattern
) + 1) );
168 u_uastrcpy(pat
, pattern
);
169 log_verbose("Pattern : %s ; String : %s\n", austrdup(pat
), austrdup(str
));
170 udat_applyPattern(format
, FALSE
, pat
, u_strlen(pat
));
172 date
= udat_parse(format
, str
, u_strlen(str
), &pos
, &status
);
173 if(U_FAILURE(status
) || date
== null
) {
174 log_verbose("ParseException: : %s\n", myErrorName(status
) );
175 if (expected
!= null
)
176 log_err("FAIL: Expected: %s\n", austrdup(myDateFormat(format
, expected
)) );
179 f
=myDateFormat(format
, date
);
180 log_verbose(" parse( %s ) -> %s\n", austrdup(str
), austrdup(f
));
181 if (expected
== null
|| date
!= expected
)
182 log_err("FAIL: Expected null for \"%s\"\n", s
);
183 if (u_strcmp(f
, str
) !=0)
184 log_err("FAIL: Expected : %s\n", austrdup(str
) );
193 * Verify the behavior of patterns in which digits for different fields run together
194 * without intervening separators.
196 void TestRunTogetherPattern985()
199 UChar
*pattern
=NULL
, *now
=NULL
, *then
=NULL
;
202 UErrorCode status
= U_ZERO_ERROR
;
203 pattern
=(UChar
*)malloc(sizeof(UChar
) * (strlen("yyyyMMddHHmmssSSS")+1) );
204 u_uastrcpy(pattern
, "yyyyMMddHHmmssSSS");
205 format
= udat_open(UDAT_PATTERN
, UDAT_PATTERN
, NULL
, NULL
, 0,pattern
, u_strlen(pattern
), &status
);
206 if(U_FAILURE(status
)){
207 log_data_err("FAIL: Error in date format construction with pattern: %s - (Are you missing data?)\n", myErrorName(status
));
210 date1
= ucal_getNow();
211 now
=myDateFormat(format
, date1
);
212 log_verbose("%s\n", austrdup(now
) );
214 date2
= udat_parse(format
, now
, u_strlen(now
), &pos
, &status
);
215 if (date2
== 0) log_verbose("Parse stopped at : %d\n", pos
);
216 else then
=myDateFormat(format
, date2
);
217 log_verbose("%s\n", austrdup(then
) );
218 if (!(date2
== date1
)) log_err("FAIL\n");
226 * Verify the handling of Czech June and July, which have the unique attribute that
227 * one is a proper prefix substring of the other.
229 void TestCzechMonths459()
232 UChar
*pattern
=NULL
, *tzID
=NULL
;
233 UChar
*juneStr
, *julyStr
;
237 UErrorCode status
= U_ZERO_ERROR
;
240 ctest_setTimeZone(NULL
, &status
);
241 fmt
= udat_open(UDAT_FULL
, UDAT_FULL
, "cs", NULL
, 0, NULL
, 0, &status
);
242 if(U_FAILURE(status
)){
243 log_data_err("Error in constructing the date format -> %s (Are you missing data?)\n", u_errorName(status
));
244 ctest_resetTimeZone();
248 lneed
=udat_toPattern(fmt
, TRUE
, NULL
, lneed
, &status
);
249 if(status
==U_BUFFER_OVERFLOW_ERROR
){
251 pattern
=(UChar
*)malloc(sizeof(UChar
) * (lneed
+1) );
252 udat_toPattern(fmt
, TRUE
, pattern
, lneed
+1, &status
);
254 if(U_FAILURE(status
)){ log_err("Error in extracting the pattern\n"); }
255 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
256 u_uastrcpy(tzID
, "GMT");
257 cal
=ucal_open(tzID
, u_strlen(tzID
), "cs", UCAL_GREGORIAN
, &status
);
258 if(U_FAILURE(status
)){ log_err("error in ucal_open caldef : %s\n", myErrorName(status
)); }
260 ucal_setDate(cal
, 1997, UCAL_JUNE
, 15, &status
);
261 june
=ucal_getMillis(cal
, &status
);
262 ucal_setDate(cal
, 1997, UCAL_JULY
, 15, &status
);
263 july
=ucal_getMillis(cal
, &status
);
265 juneStr
= myDateFormat(fmt
, june
);
266 julyStr
= myDateFormat(fmt
, july
);
268 if(juneStr
== NULL
) {
269 log_data_err("Can't load juneStr. Quitting.\n");
272 d
= udat_parse(fmt
, juneStr
, u_strlen(juneStr
), &pos
, &status
);
273 date
= myDateFormat(fmt
, d
);
275 if(U_SUCCESS(status
)){
276 UChar
* out1
= myDateFormat(fmt
, june
);
277 UChar
* out2
= myDateFormat(fmt
, d
);
278 if(u_strcmp(out1
, out2
) !=0)
279 log_err("Error in handling the czech month june\n");
281 log_verbose("Pass: Date = %s (czech month June)\n", aescstrdup(date
, -1));
283 log_err("udat_parse failed. Error. %s\n",u_errorName(status
));
286 d
= udat_parse(fmt
, julyStr
, u_strlen(julyStr
), &pos
, &status
);
287 date
= myDateFormat(fmt
, d
);
288 if(u_strcmp(myDateFormat(fmt
, july
), myDateFormat(fmt
, d
) ) !=0)
289 log_err("Error in handling the czech month july\n");
291 log_verbose("Pass: Date = %s (czech month July)\n", aescstrdup(date
, -1));
293 ctest_resetTimeZone();
301 * Test the handling of single quotes in patterns.
303 void TestQuotePattern161()
308 UChar
*pattern
, *tzID
, *exp
;
310 UErrorCode status
= U_ZERO_ERROR
;
311 const char* expStr
= "04/13/1999 at 10:42:28 AM ";
313 ctest_setTimeZone(NULL
, &status
);
315 pattern
=(UChar
*)malloc(sizeof(UChar
) * (strlen("MM/dd/yyyy 'at' hh:mm:ss a zzz")+1) );
316 u_uastrcpy(pattern
, "MM/dd/yyyy 'at' hh:mm:ss a zzz");
318 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
319 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
320 /* format= udat_openPattern(pattern, u_strlen(pattern), NULL, &status); */
321 format
= udat_open(UDAT_PATTERN
, UDAT_PATTERN
,"en_US", NULL
, 0,pattern
, u_strlen(pattern
), &status
);
322 if(U_FAILURE(status
)){
323 log_data_err("error in udat_open: %s - (Are you missing data?)\n", myErrorName(status
));
326 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
327 u_uastrcpy(tzID
, "PST");
328 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
329 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
330 /* cal=ucal_open(tzID, u_strlen(tzID), NULL, UCAL_TRADITIONAL, &status); */
331 cal
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
332 if(U_FAILURE(status
)){ log_err("error in ucal_open cal : %s\n", myErrorName(status
)); }
334 ucal_setDateTime(cal
, 1999, UCAL_APRIL
, 13, 10, 42, 28, &status
);
335 currentTime_1
= ucal_getMillis(cal
, &status
);
337 dateString
= myDateFormat(format
, currentTime_1
);
338 exp
=(UChar
*)malloc(sizeof(UChar
) * (strlen(expStr
) + 1) );
339 u_uastrcpy(exp
, expStr
);
341 log_verbose("%s\n", austrdup(dateString
) );
342 if(u_strncmp(dateString
, exp
, (int32_t)strlen(expStr
)) !=0)
343 log_err("Error in formatting a pattern with single quotes\n");
351 ctest_resetTimeZone();
355 * Testing udat_getBooleanAttribute and unum_setBooleanAttribute() to make sure basic C wrapper functionality is present
357 void TestBooleanAttributes(void)
360 UErrorCode status
=U_ZERO_ERROR
;
361 UBool initialState
= TRUE
;
362 UBool switchedState
= FALSE
;
364 log_verbose("\ncreating a date format with english locale\n");
365 en
= udat_open(UDAT_FULL
, UDAT_DEFAULT
, "en_US", NULL
, 0, NULL
, 0, &status
);
366 if(U_FAILURE(status
)) {
367 log_data_err("error in creating the dateformat -> %s (Are you missing data?)\n",
368 myErrorName(status
) );
373 initialState
= udat_getBooleanAttribute(en
, UDAT_PARSE_ALLOW_NUMERIC
, &status
);
374 if(initialState
!= TRUE
) switchedState
= TRUE
; // if it wasn't the default of TRUE, then flip what we expect
376 udat_setBooleanAttribute(en
, UDAT_PARSE_ALLOW_NUMERIC
, switchedState
, &status
);
377 if(switchedState
!= udat_getBooleanAttribute(en
, UDAT_PARSE_ALLOW_NUMERIC
, &status
)) {
378 log_err("unable to switch states!");
385 #endif /* #if !UCONFIG_NO_FORMATTING */