]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/cintltst/ccaltst.c
ICU-62141.0.1.tar.gz
[apple/icu.git] / icuSources / test / cintltst / ccaltst.c
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f 3/********************************************************************
2ca993e8 4 * Copyright (c) 1997-2016, International Business Machines
46f4442e
A
5 * Corporation and others. All Rights Reserved.
6 ********************************************************************
7 *
8 * File CCALTST.C
9 *
10 * Modification History:
11 * Name Description
12 * Madhu Katragadda Creation
13 ********************************************************************
14 */
b75a7d8f
A
15
16/* C API AND FUNCTIONALITY TEST FOR CALENDAR (ucol.h)*/
17
18#include "unicode/utypes.h"
19
20#if !UCONFIG_NO_FORMATTING
21
22#include <stdlib.h>
46f4442e 23#include <string.h>
b75a7d8f
A
24
25#include "unicode/uloc.h"
26#include "unicode/ucal.h"
27#include "unicode/udat.h"
28#include "unicode/ustring.h"
29#include "cintltst.h"
30#include "ccaltst.h"
31#include "cformtst.h"
2ca993e8 32#include "cmemory.h"
46f4442e 33#include "cstring.h"
729e4ab9 34#include "ulist.h"
b75a7d8f 35
73c04bcf 36void TestGregorianChange(void);
729e4ab9 37void TestFieldDifference(void);
51004dcb
A
38void TestAddRollEra0AndEraBounds(void);
39void TestGetTZTransition(void);
73c04bcf 40
57a6839d
A
41void TestGetWindowsTimeZoneID(void);
42void TestGetTimeZoneIDByWindowsID(void);
b331163b 43void TestClear(void);
3bb97ae2 44void TestPersianCalOverflow(void);
b331163b 45void TestGetDayPeriods(); /* Apple-specific */
1546d4af 46void TestJCalHeiseiNextEra(); /* Apple-specific */
57a6839d 47
b75a7d8f
A
48void addCalTest(TestNode** root);
49
50void addCalTest(TestNode** root)
51{
52
53 addTest(root, &TestCalendar, "tsformat/ccaltst/TestCalendar");
54 addTest(root, &TestGetSetDateAPI, "tsformat/ccaltst/TestGetSetDateAPI");
55 addTest(root, &TestFieldGetSet, "tsformat/ccaltst/TestFieldGetSet");
56 addTest(root, &TestAddRollExtensive, "tsformat/ccaltst/TestAddRollExtensive");
57 addTest(root, &TestGetLimits, "tsformat/ccaltst/TestGetLimits");
58 addTest(root, &TestDOWProgression, "tsformat/ccaltst/TestDOWProgression");
59 addTest(root, &TestGMTvsLocal, "tsformat/ccaltst/TestGMTvsLocal");
73c04bcf 60 addTest(root, &TestGregorianChange, "tsformat/ccaltst/TestGregorianChange");
729e4ab9
A
61 addTest(root, &TestGetKeywordValuesForLocale, "tsformat/ccaltst/TestGetKeywordValuesForLocale");
62 addTest(root, &TestWeekend, "tsformat/ccaltst/TestWeekend");
63 addTest(root, &TestFieldDifference, "tsformat/ccaltst/TestFieldDifference");
4388f060 64 addTest(root, &TestAmbiguousWallTime, "tsformat/ccaltst/TestAmbiguousWallTime");
51004dcb
A
65 addTest(root, &TestAddRollEra0AndEraBounds, "tsformat/ccaltst/TestAddRollEra0AndEraBounds");
66 addTest(root, &TestGetTZTransition, "tsformat/ccaltst/TestGetTZTransition");
57a6839d
A
67 addTest(root, &TestGetWindowsTimeZoneID, "tsformat/ccaltst/TestGetWindowsTimeZoneID");
68 addTest(root, &TestGetTimeZoneIDByWindowsID, "tsformat/ccaltst/TestGetTimeZoneIDByWindowsID");
b331163b 69 addTest(root, &TestClear, "tsformat/ccaltst/TestClear");
3bb97ae2 70 addTest(root, &TestPersianCalOverflow, "tsformat/ccaltst/TestPersianCalOverflow");
b331163b 71 addTest(root, &TestGetDayPeriods, "tsformat/ccaltst/TestGetDayPeriods"); /* Apple-specific */
1546d4af 72 addTest(root, &TestJCalHeiseiNextEra, "tsformat/ccaltst/TestJCalHeiseiNextEra"); /* Apple-specific */
b75a7d8f
A
73}
74
75/* "GMT" */
76static const UChar fgGMTID [] = { 0x0047, 0x004d, 0x0054, 0x0000 };
77
78/* "PST" */
79static const UChar PST[] = {0x50, 0x53, 0x54, 0x00}; /* "PST" */
80
81static const UChar EUROPE_PARIS[] = {0x45, 0x75, 0x72, 0x6F, 0x70, 0x65, 0x2F, 0x50, 0x61, 0x72, 0x69, 0x73, 0x00}; /* "Europe/Paris" */
82
46f4442e
A
83static const UChar AMERICA_LOS_ANGELES[] = {0x41, 0x6D, 0x65, 0x72, 0x69, 0x63, 0x61, 0x2F,
84 0x4C, 0x6F, 0x73, 0x5F, 0x41, 0x6E, 0x67, 0x65, 0x6C, 0x65, 0x73, 0x00}; /* America/Los_Angeles */
85
86typedef struct {
87 const char * locale;
88 UCalendarType calType;
89 const char * expectedResult;
90} UCalGetTypeTest;
91
92static const UCalGetTypeTest ucalGetTypeTests[] = {
93 { "en_US", UCAL_GREGORIAN, "gregorian" },
94 { "ja_JP@calendar=japanese", UCAL_DEFAULT, "japanese" },
95 { "th_TH", UCAL_GREGORIAN, "gregorian" },
96 { "th_TH", UCAL_DEFAULT, "buddhist" },
729e4ab9
A
97 { "th-TH-u-ca-gregory", UCAL_DEFAULT, "gregorian" },
98 { "ja_JP@calendar=japanese", UCAL_GREGORIAN, "gregorian" },
2ca993e8
A
99 { "fr_CH", UCAL_DEFAULT, "gregorian" },
100 { "fr_SA", UCAL_DEFAULT, "islamic-umalqura" },
101 { "fr_CH@rg=sazzzz", UCAL_DEFAULT, "islamic-umalqura" },
102 { "fr_CH@calendar=japanese;rg=sazzzz", UCAL_DEFAULT, "japanese" },
103 { "fr_TH@rg=SA", UCAL_DEFAULT, "buddhist" }, /* ignore malformed rg tag */
104 { "th@rg=SA", UCAL_DEFAULT, "buddhist" }, /* ignore malformed rg tag */
729e4ab9
A
105 { "", UCAL_GREGORIAN, "gregorian" },
106 { NULL, UCAL_GREGORIAN, "gregorian" },
46f4442e
A
107 { NULL, 0, NULL } /* terminator */
108};
109
b75a7d8f
A
110static void TestCalendar()
111{
46f4442e 112 UCalendar *caldef = 0, *caldef2 = 0, *calfr = 0, *calit = 0, *calfrclone = 0;
b75a7d8f 113 UEnumeration* uenum = NULL;
374ca955 114 int32_t count, count2, i,j;
46f4442e 115 UChar tzID[4];
b75a7d8f
A
116 UChar *tzdname = 0;
117 UErrorCode status = U_ZERO_ERROR;
118 UDate now;
119 UDateFormat *datdef = 0;
120 UChar *result = 0;
121 int32_t resultlength, resultlengthneeded;
0f5d89e8 122 char tempMsgBuf[1024]; // u_austrcpy() of some formatted dates & times.
b75a7d8f 123 UChar zone1[32], zone2[32];
46f4442e
A
124 const char *tzver = 0;
125 UChar canonicalID[64];
126 UBool isSystemID = FALSE;
127 const UCalGetTypeTest * ucalGetTypeTestPtr;
b75a7d8f 128
374ca955 129#ifdef U_USE_UCAL_OBSOLETE_2_8
b75a7d8f 130 /*Testing countAvailableTimeZones*/
374ca955 131 int32_t offset=0;
b75a7d8f
A
132 log_verbose("\nTesting ucal_countAvailableTZIDs\n");
133 count=ucal_countAvailableTZIDs(offset);
134 log_verbose("The number of timezone id's present with offset 0 are %d:\n", count);
135 if(count < 5) /* Don't hard code an exact == test here! */
136 log_err("FAIL: error in the ucal_countAvailableTZIDs - got %d expected at least 5 total\n", count);
137
138 /*Testing getAvailableTZIDs*/
139 log_verbose("\nTesting ucal_getAvailableTZIDs");
140 for(i=0;i<count;i++){
141 ucal_getAvailableTZIDs(offset, i, &status);
142 if(U_FAILURE(status)){
143 log_err("FAIL: ucal_getAvailableTZIDs returned %s\n", u_errorName(status));
144 }
145 log_verbose("%s\n", u_austrcpy(tempMsgBuf, ucal_getAvailableTZIDs(offset, i, &status)));
146 }
147 /*get Illegal TZID where index >= count*/
148 ucal_getAvailableTZIDs(offset, i, &status);
149 if(status != U_INDEX_OUTOFBOUNDS_ERROR){
150 log_err("FAIL:for TZID index >= count Expected INDEX_OUTOFBOUNDS_ERROR Got %s\n", u_errorName(status));
151 }
152 status=U_ZERO_ERROR;
374ca955 153#endif
b75a7d8f 154
4388f060
A
155 /*Test ucal_openTimeZones, ucal_openCountryTimeZones and ucal_openTimeZoneIDEnumeration */
156 for (j=0; j<6; ++j) {
157 const char *api = "?";
158 const int32_t offsetMinus5 = -5*60*60*1000;
159 switch (j) {
160 case 0:
161 api = "ucal_openTimeZones()";
162 uenum = ucal_openTimeZones(&status);
163 break;
164 case 1:
165 api = "ucal_openCountryTimeZones(US)";
166 uenum = ucal_openCountryTimeZones("US", &status);
167 break;
168 case 2:
169 api = "ucal_openTimeZoneIDEnumerarion(UCAL_ZONE_TYPE_CANONICAL, NULL, NULL)";
170 uenum = ucal_openTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL, NULL, NULL, &status);
171 break;
172 case 3:
173 api = "ucal_openTimeZoneIDEnumerarion(UCAL_ZONE_TYPE_CANONICAL_LOCATION, CA, NULL)";
174 uenum = ucal_openTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL_LOCATION, "CA", NULL, &status);
175 break;
176 case 4:
177 api = "ucal_openTimeZoneIDEnumerarion(UCAL_ZONE_TYPE_ANY, NULL, -5 hour)";
178 uenum = ucal_openTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, NULL, &offsetMinus5, &status);
179 break;
180 case 5:
181 api = "ucal_openTimeZoneIDEnumerarion(UCAL_ZONE_TYPE_ANY, US, -5 hour)";
182 uenum = ucal_openTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, "US", &offsetMinus5, &status);
183 break;
184 }
b75a7d8f 185 if (U_FAILURE(status)) {
4388f060 186 log_err_status(status, "FAIL: %s failed with %s\n", api,
b75a7d8f
A
187 u_errorName(status));
188 } else {
189 const char* id;
190 int32_t len;
191 count = uenum_count(uenum, &status);
192 log_verbose("%s returned %d timezone id's:\n", api, count);
193 if (count < 5) { /* Don't hard code an exact == test here! */
729e4ab9 194 log_data_err("FAIL: in %s, got %d, expected at least 5 -> %s (Are you missing data?)\n", api, count, u_errorName(status));
b75a7d8f
A
195 }
196 uenum_reset(uenum, &status);
197 if (U_FAILURE(status)){
198 log_err("FAIL: uenum_reset for %s returned %s\n",
199 api, u_errorName(status));
200 }
201 for (i=0; i<count; i++) {
202 id = uenum_next(uenum, &len, &status);
203 if (U_FAILURE(status)){
204 log_err("FAIL: uenum_next for %s returned %s\n",
205 api, u_errorName(status));
206 } else {
207 log_verbose("%s\n", id);
208 }
209 }
210 /* Next one should be NULL */
211 id = uenum_next(uenum, &len, &status);
212 if (id != NULL) {
213 log_err("FAIL: uenum_next for %s returned %s, expected NULL\n",
214 api, id);
215 }
216 }
217 uenum_close(uenum);
218 }
219
220 /*Test ucal_getDSTSavings*/
221 status = U_ZERO_ERROR;
222 i = ucal_getDSTSavings(fgGMTID, &status);
223 if (U_FAILURE(status)) {
224 log_err("FAIL: ucal_getDSTSavings(GMT) => %s\n",
225 u_errorName(status));
226 } else if (i != 0) {
729e4ab9 227 log_data_err("FAIL: ucal_getDSTSavings(GMT) => %d, expect 0 (Are you missing data?)\n", i);
b75a7d8f
A
228 }
229 i = ucal_getDSTSavings(PST, &status);
230 if (U_FAILURE(status)) {
231 log_err("FAIL: ucal_getDSTSavings(PST) => %s\n",
232 u_errorName(status));
233 } else if (i != 1*60*60*1000) {
234 log_err("FAIL: ucal_getDSTSavings(PST) => %d, expect %d\n", i, 1*60*60*1000);
235 }
236
237 /*Test ucal_set/getDefaultTimeZone*/
238 status = U_ZERO_ERROR;
2ca993e8 239 i = ucal_getDefaultTimeZone(zone1, UPRV_LENGTHOF(zone1), &status);
b75a7d8f
A
240 if (U_FAILURE(status)) {
241 log_err("FAIL: ucal_getDefaultTimeZone() => %s\n",
242 u_errorName(status));
243 } else {
244 ucal_setDefaultTimeZone(EUROPE_PARIS, &status);
245 if (U_FAILURE(status)) {
246 log_err("FAIL: ucal_setDefaultTimeZone(Europe/Paris) => %s\n",
247 u_errorName(status));
248 } else {
2ca993e8 249 i = ucal_getDefaultTimeZone(zone2, UPRV_LENGTHOF(zone2), &status);
b75a7d8f
A
250 if (U_FAILURE(status)) {
251 log_err("FAIL: ucal_getDefaultTimeZone() => %s\n",
252 u_errorName(status));
253 } else {
254 if (u_strcmp(zone2, EUROPE_PARIS) != 0) {
729e4ab9 255 log_data_err("FAIL: ucal_getDefaultTimeZone() did not return Europe/Paris (Are you missing data?)\n");
b75a7d8f
A
256 }
257 }
258 }
259 status = U_ZERO_ERROR;
260 ucal_setDefaultTimeZone(zone1, &status);
261 }
46f4442e
A
262
263 /*Test ucal_getTZDataVersion*/
264 status = U_ZERO_ERROR;
265 tzver = ucal_getTZDataVersion(&status);
266 if (U_FAILURE(status)) {
729e4ab9 267 log_err_status(status, "FAIL: ucal_getTZDataVersion() => %s\n", u_errorName(status));
5ea0322b 268 } else if (uprv_strlen(tzver) < 5 || uprv_strlen(tzver) > 7 /*4 digits + 1-3 letters*/) {
46f4442e
A
269 log_err("FAIL: Bad version string was returned by ucal_getTZDataVersion\n");
270 } else {
271 log_verbose("PASS: ucal_getTZDataVersion returned %s\n", tzver);
272 }
273
274 /*Testing ucal_getCanonicalTimeZoneID*/
275 status = U_ZERO_ERROR;
276 resultlength = ucal_getCanonicalTimeZoneID(PST, -1,
2ca993e8 277 canonicalID, UPRV_LENGTHOF(canonicalID), &isSystemID, &status);
46f4442e 278 if (U_FAILURE(status)) {
57a6839d 279 log_data_err("FAIL: error in ucal_getCanonicalTimeZoneID : %s\n", u_errorName(status));
46f4442e
A
280 } else {
281 if (u_strcmp(AMERICA_LOS_ANGELES, canonicalID) != 0) {
729e4ab9 282 log_data_err("FAIL: ucal_getCanonicalTimeZoneID(%s) returned %s : expected - %s (Are you missing data?)\n",
46f4442e
A
283 PST, canonicalID, AMERICA_LOS_ANGELES);
284 }
285 if (!isSystemID) {
729e4ab9 286 log_data_err("FAIL: ucal_getCanonicalTimeZoneID(%s) set %d to isSystemID (Are you missing data?)\n",
46f4442e
A
287 PST, isSystemID);
288 }
289 }
b75a7d8f
A
290
291 /*Testing the ucal_open() function*/
46f4442e 292 status = U_ZERO_ERROR;
b75a7d8f 293 log_verbose("\nTesting the ucal_open()\n");
b75a7d8f
A
294 u_uastrcpy(tzID, "PST");
295 caldef=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
296 if(U_FAILURE(status)){
729e4ab9 297 log_data_err("FAIL: error in ucal_open caldef : %s\n - (Are you missing data?)", u_errorName(status));
b75a7d8f
A
298 }
299
300 caldef2=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
301 if(U_FAILURE(status)){
729e4ab9 302 log_data_err("FAIL: error in ucal_open caldef : %s - (Are you missing data?)\n", u_errorName(status));
b75a7d8f
A
303 }
304 u_strcpy(tzID, fgGMTID);
305 calfr=ucal_open(tzID, u_strlen(tzID), "fr_FR", UCAL_TRADITIONAL, &status);
306 if(U_FAILURE(status)){
729e4ab9 307 log_data_err("FAIL: error in ucal_open calfr : %s - (Are you missing data?)\n", u_errorName(status));
b75a7d8f
A
308 }
309 calit=ucal_open(tzID, u_strlen(tzID), "it_IT", UCAL_TRADITIONAL, &status);
310 if(U_FAILURE(status)) {
729e4ab9 311 log_data_err("FAIL: error in ucal_open calit : %s - (Are you missing data?)\n", u_errorName(status));
b75a7d8f 312 }
46f4442e
A
313
314 /*Testing the clone() function*/
315 calfrclone = ucal_clone(calfr, &status);
316 if(U_FAILURE(status)){
729e4ab9 317 log_data_err("FAIL: error in ucal_clone calfr : %s - (Are you missing data?)\n", u_errorName(status));
46f4442e 318 }
b75a7d8f
A
319
320 /*Testing udat_getAvailable() and udat_countAvailable()*/
321 log_verbose("\nTesting getAvailableLocales and countAvailable()\n");
322 count=ucal_countAvailable();
323 /* use something sensible w/o hardcoding the count */
324 if(count > 0) {
325 log_verbose("PASS: ucal_countAvailable() works fine\n");
326 log_verbose("The no: of locales for which calendars are avilable are %d\n", count);
327 } else {
328 log_data_err("FAIL: Error in countAvailable()\n");
329 }
330
331 for(i=0;i<count;i++) {
332 log_verbose("%s\n", ucal_getAvailable(i));
333 }
334
335
336 /*Testing the equality between calendar's*/
337 log_verbose("\nTesting ucal_equivalentTo()\n");
338 if(caldef && caldef2 && calfr && calit) {
339 if(ucal_equivalentTo(caldef, caldef2) == FALSE || ucal_equivalentTo(caldef, calfr)== TRUE ||
46f4442e 340 ucal_equivalentTo(caldef, calit)== TRUE || ucal_equivalentTo(calfr, calfrclone) == FALSE) {
729e4ab9 341 log_data_err("FAIL: Error. equivalentTo test failed (Are you missing data?)\n");
b75a7d8f
A
342 } else {
343 log_verbose("PASS: equivalentTo test passed\n");
344 }
345 }
346
347
348 /*Testing the current time and date using ucal_getnow()*/
349 log_verbose("\nTesting the ucal_getNow function to check if it is fetching tbe current time\n");
350 now=ucal_getNow();
351 /* open the date format and format the date to check the output */
352 datdef=udat_open(UDAT_FULL,UDAT_FULL ,NULL, NULL, 0,NULL,0,&status);
353 if(U_FAILURE(status)){
729e4ab9 354 log_data_err("FAIL: error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
b75a7d8f
A
355 return;
356 }
357 log_verbose("PASS: The current date and time fetched is %s\n", u_austrcpy(tempMsgBuf, myDateFormat(datdef, now)) );
358
359
360 /*Testing the TimeZoneDisplayName */
361 log_verbose("\nTesting the fetching of time zone display name\n");
362 /*for the US locale */
363 resultlength=0;
364 resultlengthneeded=ucal_getTimeZoneDisplayName(caldef, UCAL_DST, "en_US", NULL, resultlength, &status);
365
366 if(status==U_BUFFER_OVERFLOW_ERROR)
367 {
368 status=U_ZERO_ERROR;
369 resultlength=resultlengthneeded+1;
370 result=(UChar*)malloc(sizeof(UChar) * resultlength);
371 ucal_getTimeZoneDisplayName(caldef, UCAL_DST, "en_US", result, resultlength, &status);
372 }
373 if(U_FAILURE(status)) {
374 log_err("FAIL: Error in getting the timezone display name : %s\n", u_errorName(status));
375 }
376 else{
377 log_verbose("PASS: getting the time zone display name successful : %s, %d needed \n",
378 u_errorName(status), resultlengthneeded);
379 }
380
381
382#define expectPDT "Pacific Daylight Time"
383
384 tzdname=(UChar*)malloc(sizeof(UChar) * (sizeof(expectPDT)+1));
385 u_uastrcpy(tzdname, expectPDT);
386 if(u_strcmp(tzdname, result)==0){
387 log_verbose("PASS: got the correct time zone display name %s\n", u_austrcpy(tempMsgBuf, result) );
388 }
389 else{
390 log_err("FAIL: got the wrong time zone(DST) display name %s, wanted %s\n", austrdup(result) , expectPDT);
391 }
392
393 ucal_getTimeZoneDisplayName(caldef, UCAL_SHORT_DST, "en_US", result, resultlength, &status);
394 u_uastrcpy(tzdname, "PDT");
395 if(u_strcmp(tzdname, result) != 0){
396 log_err("FAIL: got the wrong time zone(SHORT_DST) display name %s, wanted %s\n", austrdup(result), austrdup(tzdname));
397 }
398
399 ucal_getTimeZoneDisplayName(caldef, UCAL_STANDARD, "en_US", result, resultlength, &status);
400 u_uastrcpy(tzdname, "Pacific Standard Time");
401 if(u_strcmp(tzdname, result) != 0){
402 log_err("FAIL: got the wrong time zone(STANDARD) display name %s, wanted %s\n", austrdup(result), austrdup(tzdname));
403 }
404
405 ucal_getTimeZoneDisplayName(caldef, UCAL_SHORT_STANDARD, "en_US", result, resultlength, &status);
406 u_uastrcpy(tzdname, "PST");
407 if(u_strcmp(tzdname, result) != 0){
408 log_err("FAIL: got the wrong time zone(SHORT_STANDARD) display name %s, wanted %s\n", austrdup(result), austrdup(tzdname));
409 }
410
411
412 /*testing the setAttributes and getAttributes of a UCalendar*/
413 log_verbose("\nTesting the getAttributes and set Attributes\n");
414 count=ucal_getAttribute(calit, UCAL_LENIENT);
415 count2=ucal_getAttribute(calfr, UCAL_LENIENT);
416 ucal_setAttribute(calit, UCAL_LENIENT, 0);
417 ucal_setAttribute(caldef, UCAL_LENIENT, count2);
418 if( ucal_getAttribute(calit, UCAL_LENIENT) !=0 ||
419 ucal_getAttribute(calfr, UCAL_LENIENT)!=ucal_getAttribute(caldef, UCAL_LENIENT) )
420 log_err("FAIL: there is an error in getAttributes or setAttributes\n");
421 else
422 log_verbose("PASS: attribute set and got successfully\n");
423 /*set it back to orginal value */
424 log_verbose("Setting it back to normal\n");
425 ucal_setAttribute(calit, UCAL_LENIENT, count);
426 if(ucal_getAttribute(calit, UCAL_LENIENT)!=count)
427 log_err("FAIL: Error in setting the attribute back to normal\n");
428
429 /*setting the first day of the week to other values */
430 count=ucal_getAttribute(calit, UCAL_FIRST_DAY_OF_WEEK);
431 for (i=1; i<=7; ++i) {
432 ucal_setAttribute(calit, UCAL_FIRST_DAY_OF_WEEK,i);
433 if (ucal_getAttribute(calit, UCAL_FIRST_DAY_OF_WEEK) != i)
434 log_err("FAIL: set/getFirstDayOfWeek failed\n");
435 }
436 /*get bogus Attribute*/
437 count=ucal_getAttribute(calit, (UCalendarAttribute)99); /* BOGUS_ATTRIBUTE */
438 if(count != -1){
439 log_err("FAIL: get/bogus attribute should return -1\n");
440 }
441
442 /*set it back to normal */
443 ucal_setAttribute(calit, UCAL_FIRST_DAY_OF_WEEK,count);
444 /*setting minimal days of the week to other values */
445 count=ucal_getAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK);
446 for (i=1; i<=7; ++i) {
447 ucal_setAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,i);
448 if (ucal_getAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK) != i)
449 log_err("FAIL: set/getMinimalDaysInFirstWeek failed\n");
450 }
451 /*set it back to normal */
452 ucal_setAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,count);
453
454
455 /*testing if the UCalendar's timezone is currently in day light saving's time*/
456 log_verbose("\nTesting if the UCalendar is currently in daylight saving's time\n");
457 ucal_setDateTime(caldef, 1999, UCAL_MARCH, 3, 10, 45, 20, &status);
458 ucal_inDaylightTime(caldef, &status );
459 if(U_FAILURE(status)) {
460 log_err("Error in ucal_inDaylightTime: %s\n", u_errorName(status));
461 }
462 if(!ucal_inDaylightTime(caldef, &status))
729e4ab9 463 log_verbose("PASS: It is not in daylight saving's time\n");
b75a7d8f
A
464 else
465 log_err("FAIL: It is not in daylight saving's time\n");
466
b75a7d8f
A
467 /*closing the UCalendar*/
468 ucal_close(caldef);
469 ucal_close(caldef2);
470 ucal_close(calfr);
471 ucal_close(calit);
46f4442e
A
472 ucal_close(calfrclone);
473
474 /*testing ucal_getType, and ucal_open with UCAL_GREGORIAN*/
729e4ab9
A
475 for (ucalGetTypeTestPtr = ucalGetTypeTests; ucalGetTypeTestPtr->expectedResult != NULL; ++ucalGetTypeTestPtr) {
476 const char * localeToDisplay = (ucalGetTypeTestPtr->locale != NULL)? ucalGetTypeTestPtr->locale: "<NULL>";
46f4442e
A
477 status = U_ZERO_ERROR;
478 caldef = ucal_open(NULL, 0, ucalGetTypeTestPtr->locale, ucalGetTypeTestPtr->calType, &status);
479 if ( U_SUCCESS(status) ) {
480 const char * calType = ucal_getType(caldef, &status);
481 if ( U_SUCCESS(status) && calType != NULL ) {
51004dcb 482 if ( uprv_strcmp( calType, ucalGetTypeTestPtr->expectedResult ) != 0 ) {
729e4ab9 483 log_err("FAIL: ucal_open %s type %d does not return %s calendar\n", localeToDisplay,
46f4442e
A
484 ucalGetTypeTestPtr->calType, ucalGetTypeTestPtr->expectedResult);
485 }
486 } else {
729e4ab9 487 log_err("FAIL: ucal_open %s type %d, then ucal_getType fails\n", localeToDisplay, ucalGetTypeTestPtr->calType);
46f4442e
A
488 }
489 ucal_close(caldef);
490 } else {
729e4ab9 491 log_err("FAIL: ucal_open %s type %d fails\n", localeToDisplay, ucalGetTypeTestPtr->calType);
46f4442e
A
492 }
493 }
494
b75a7d8f
A
495 /*closing the UDateFormat used */
496 udat_close(datdef);
b75a7d8f
A
497 free(result);
498 free(tzdname);
b75a7d8f
A
499}
500
501/*------------------------------------------------------*/
502/*Testing the getMillis, setMillis, setDate and setDateTime functions extensively*/
503
504static void TestGetSetDateAPI()
505{
51004dcb 506 UCalendar *caldef = 0, *caldef2 = 0, *caldef3 = 0;
46f4442e 507 UChar tzID[4];
b75a7d8f
A
508 UDate d1;
509 int32_t hour;
510 int32_t zoneOffset;
511 UDateFormat *datdef = 0;
512 UErrorCode status=U_ZERO_ERROR;
513 UDate d2= 837039928046.0;
514 UChar temp[30];
51004dcb
A
515 double testMillis;
516 int32_t dateBit;
517 UChar id[4];
518 int32_t idLen;
b75a7d8f
A
519
520 log_verbose("\nOpening the calendars()\n");
b75a7d8f
A
521 u_strcpy(tzID, fgGMTID);
522 /*open the calendars used */
523 caldef=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
524 caldef2=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
51004dcb 525 caldef3=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
b75a7d8f
A
526 /*open the dateformat */
527 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
528 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
529 /*datdef=udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL,fgGMTID,-1, &status);*/
530 datdef=udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,"en_US",fgGMTID,-1,NULL,0, &status);
531 if(U_FAILURE(status))
532 {
729e4ab9 533 log_data_err("error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
b75a7d8f
A
534 return;
535 }
b75a7d8f
A
536
537 /*Testing getMillis and setMillis */
538 log_verbose("\nTesting the date and time fetched in millis for a calendar using getMillis\n");
539 d1=ucal_getMillis(caldef, &status);
540 if(U_FAILURE(status)){
541 log_err("Error in getMillis : %s\n", u_errorName(status));
542 }
543
544 /*testing setMillis */
545 log_verbose("\nTesting the set date and time function using setMillis\n");
546 ucal_setMillis(caldef, d2, &status);
547 if(U_FAILURE(status)){
548 log_err("Error in setMillis : %s\n", u_errorName(status));
549 }
550
551 /*testing if the calendar date is set properly or not */
552 d1=ucal_getMillis(caldef, &status);
553 if(u_strcmp(myDateFormat(datdef, d1), myDateFormat(datdef, d2))!=0)
554 log_err("error in setMillis or getMillis\n");
555 /*-------------------*/
556
51004dcb
A
557 /*testing large negative millis*/
558 /*test a previously failed millis and beyond the lower bounds - ICU trac #9403 */
559 // -184303902611600000.0 - just beyond lower bounds (#9403 sets U_ILLEGAL_ARGUMENT_ERROR in strict mode)
560 // -46447814188001000.0 - fixed by #9403
561
562 log_verbose("\nTesting very large valid millis & invalid setMillis values (in both strict & lienent modes) detected\n");
563
564 testMillis = -46447814188001000.0; // point where floorDivide in handleComputeFields failed as per #9403
565 log_verbose("using value[%lf]\n", testMillis);
566 ucal_setAttribute(caldef3, UCAL_LENIENT, 0);
567 ucal_setMillis(caldef3, testMillis, &status);
568 if(U_FAILURE(status)){
569 log_err("Fail: setMillis incorrectly detected invalid value : for millis : %e : returned : %s\n", testMillis, u_errorName(status));
570 status = U_ZERO_ERROR;
571 }
572
573 log_verbose("\nTesting invalid setMillis values detected\n");
574 testMillis = -184303902611600000.0;
575 log_verbose("using value[%lf]\n", testMillis);
576 ucal_setAttribute(caldef3, UCAL_LENIENT, 1);
577 ucal_setMillis(caldef3, testMillis, &status);
578 if(U_FAILURE(status)){
579 log_err("Fail: setMillis incorrectly detected invalid value : for millis : %e : returned : %s\n", testMillis, u_errorName(status));
580 status = U_ZERO_ERROR;
581 } else {
582 dateBit = ucal_get(caldef2, UCAL_MILLISECOND, &status);
583 if(testMillis == dateBit)
584 {
585 log_err("Fail: error in setMillis, allowed invalid value %e : returns millisecond : %d", testMillis, dateBit);
586 } else {
587 log_verbose("Pass: setMillis correctly pinned min, returned : %d", dateBit);
588 }
589 }
590
591 log_verbose("\nTesting invalid setMillis values detected\n");
592 testMillis = -184303902611600000.0;
593 log_verbose("using value[%lf]\n", testMillis);
594 ucal_setAttribute(caldef3, UCAL_LENIENT, 0);
595 ucal_setMillis(caldef3, testMillis, &status);
596 if(U_FAILURE(status)){
597 log_verbose("Pass: Illegal argument error as expected : for millis : %e : returned : %s\n", testMillis, u_errorName(status));
598 status = U_ZERO_ERROR;
599 } else {
600 dateBit = ucal_get(caldef3, UCAL_DAY_OF_MONTH, &status);
601 log_err("Fail: error in setMillis, allowed invalid value %e : returns DayOfMonth : %d", testMillis, dateBit);
602 }
603 /*-------------------*/
b75a7d8f
A
604
605
374ca955
A
606 ctest_setTimeZone(NULL, &status);
607
51004dcb
A
608 /*testing ucal_setTimeZone() and ucal_getTimeZoneID function*/
609 log_verbose("\nTesting if the function ucal_setTimeZone() and ucal_getTimeZoneID work fine\n");
2ca993e8 610 idLen = ucal_getTimeZoneID(caldef2, id, UPRV_LENGTHOF(id), &status);
51004dcb
A
611 (void)idLen; /* Suppress set but not used warning. */
612 if (U_FAILURE(status)) {
613 log_err("Error in getTimeZoneID : %s\n", u_errorName(status));
614 } else if (u_strcmp(id, fgGMTID) != 0) {
615 log_err("FAIL: getTimeZoneID returns a wrong ID: actual=%d, expected=%s\n", austrdup(id), austrdup(fgGMTID));
616 } else {
617 log_verbose("PASS: getTimeZoneID works fine\n");
618 }
619
b75a7d8f
A
620 ucal_setMillis(caldef2, d2, &status);
621 if(U_FAILURE(status)){
51004dcb 622 log_err("Error in getMillis : %s\n", u_errorName(status));
b75a7d8f
A
623 }
624 hour=ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status);
625
626 u_uastrcpy(tzID, "PST");
627 ucal_setTimeZone(caldef2,tzID, 3, &status);
628 if(U_FAILURE(status)){
629 log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status));
630 }
631 else
632 log_verbose("ucal_setTimeZone worked fine\n");
51004dcb 633
2ca993e8 634 idLen = ucal_getTimeZoneID(caldef2, id, UPRV_LENGTHOF(id), &status);
51004dcb
A
635 if (U_FAILURE(status)) {
636 log_err("Error in getTimeZoneID : %s\n", u_errorName(status));
637 } else if (u_strcmp(id, tzID) != 0) {
638 log_err("FAIL: getTimeZoneID returns a wrong ID: actual=%d, expected=%s\n", austrdup(id), austrdup(tzID));
639 } else {
640 log_verbose("PASS: getTimeZoneID works fine\n");
641 }
642
b75a7d8f
A
643 if(hour == ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status))
644 log_err("FAIL: Error setting the time zone doesn't change the represented time\n");
645 else if((hour-8 + 1) != ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status)) /*because it is not in daylight savings time */
646 log_err("FAIL: Error setTimeZone doesn't change the represented time correctly with 8 hour offset\n");
647 else
648 log_verbose("PASS: setTimeZone works fine\n");
649
650 /*testing setTimeZone roundtrip */
651 log_verbose("\nTesting setTimeZone() roundtrip\n");
652 u_strcpy(tzID, fgGMTID);
653 ucal_setTimeZone(caldef2, tzID, 3, &status);
654 if(U_FAILURE(status)){
655 log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status));
656 }
657 if(d2==ucal_getMillis(caldef2, &status))
658 log_verbose("PASS: setTimeZone roundtrip test passed\n");
659 else
660 log_err("FAIL: setTimeZone roundtrip test failed\n");
661
662 zoneOffset = ucal_get(caldef2, UCAL_ZONE_OFFSET, &status);
663 if(U_FAILURE(status)){
664 log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone(): %s\n", u_errorName(status));
665 }
666 else if (zoneOffset != 0) {
667 log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone() offset=%d\n", zoneOffset);
668 }
669
670 ucal_setTimeZone(caldef2, NULL, -1, &status);
671 if(U_FAILURE(status)){
672 log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status));
673 }
674 if(ucal_getMillis(caldef2, &status))
675 log_verbose("PASS: setTimeZone roundtrip test passed\n");
676 else
677 log_err("FAIL: setTimeZone roundtrip test failed\n");
678
679 zoneOffset = ucal_get(caldef2, UCAL_ZONE_OFFSET, &status);
680 if(U_FAILURE(status)){
681 log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone(): %s\n", u_errorName(status));
682 }
683 else if (zoneOffset != -28800000) {
684 log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone() offset=%d\n", zoneOffset);
685 }
374ca955
A
686
687 ctest_resetTimeZone();
688
b75a7d8f
A
689/*----------------------------* */
690
691
692
693 /*Testing if setDate works fine */
694 log_verbose("\nTesting the ucal_setDate() function \n");
f3c0d7a5 695 u_uastrcpy(temp, "Dec 17, 1971 at 11:05:28 PM");
b75a7d8f
A
696 ucal_setDate(caldef,1971, UCAL_DECEMBER, 17, &status);
697 if(U_FAILURE(status)){
698 log_err("error in setting the calendar date : %s\n", u_errorName(status));
699 }
700 /*checking if the calendar date is set properly or not */
701 d1=ucal_getMillis(caldef, &status);
702 if(u_strcmp(myDateFormat(datdef, d1), temp)==0)
703 log_verbose("PASS:setDate works fine\n");
704 else
705 log_err("FAIL:Error in setDate()\n");
706
707
708 /* Testing setDate Extensively with various input values */
709 log_verbose("\nTesting ucal_setDate() extensively\n");
710 ucal_setDate(caldef, 1999, UCAL_JANUARY, 10, &status);
711 verify1("1999 10th day of January is :", caldef, datdef, 1999, UCAL_JANUARY, 10);
712 ucal_setDate(caldef, 1999, UCAL_DECEMBER, 3, &status);
713 verify1("1999 3rd day of December is :", caldef, datdef, 1999, UCAL_DECEMBER, 3);
714 ucal_setDate(caldef, 2000, UCAL_MAY, 3, &status);
715 verify1("2000 3rd day of May is :", caldef, datdef, 2000, UCAL_MAY, 3);
716 ucal_setDate(caldef, 1999, UCAL_AUGUST, 32, &status);
717 verify1("1999 32th day of August is :", caldef, datdef, 1999, UCAL_SEPTEMBER, 1);
718 ucal_setDate(caldef, 1999, UCAL_MARCH, 0, &status);
719 verify1("1999 0th day of March is :", caldef, datdef, 1999, UCAL_FEBRUARY, 28);
720 ucal_setDate(caldef, 0, UCAL_MARCH, 12, &status);
721
722 /*--------------------*/
723
724 /*Testing if setDateTime works fine */
725 log_verbose("\nTesting the ucal_setDateTime() function \n");
f3c0d7a5 726 u_uastrcpy(temp, "May 3, 1972 at 4:30:42 PM");
b75a7d8f
A
727 ucal_setDateTime(caldef,1972, UCAL_MAY, 3, 16, 30, 42, &status);
728 if(U_FAILURE(status)){
729 log_err("error in setting the calendar date : %s\n", u_errorName(status));
730 }
731 /*checking if the calendar date is set properly or not */
732 d1=ucal_getMillis(caldef, &status);
733 if(u_strcmp(myDateFormat(datdef, d1), temp)==0)
734 log_verbose("PASS: setDateTime works fine\n");
735 else
736 log_err("FAIL: Error in setDateTime\n");
737
738
739
740 /*Testing setDateTime extensively with various input values*/
741 log_verbose("\nTesting ucal_setDateTime() function extensively\n");
742 ucal_setDateTime(caldef, 1999, UCAL_OCTOBER, 10, 6, 45, 30, &status);
743 verify2("1999 10th day of October at 6:45:30 is :", caldef, datdef, 1999, UCAL_OCTOBER, 10, 6, 45, 30, 0 );
744 ucal_setDateTime(caldef, 1999, UCAL_MARCH, 3, 15, 10, 55, &status);
745 verify2("1999 3rd day of March at 15:10:55 is :", caldef, datdef, 1999, UCAL_MARCH, 3, 3, 10, 55, 1);
746 ucal_setDateTime(caldef, 1999, UCAL_MAY, 3, 25, 30, 45, &status);
747 verify2("1999 3rd day of May at 25:30:45 is :", caldef, datdef, 1999, UCAL_MAY, 4, 1, 30, 45, 0);
748 ucal_setDateTime(caldef, 1999, UCAL_AUGUST, 32, 22, 65, 40, &status);
749 verify2("1999 32th day of August at 22:65:40 is :", caldef, datdef, 1999, UCAL_SEPTEMBER, 1, 11, 5, 40,1);
750 ucal_setDateTime(caldef, 1999, UCAL_MARCH, 12, 0, 0, 0,&status);
751 verify2("1999 12th day of March at 0:0:0 is :", caldef, datdef, 1999, UCAL_MARCH, 12, 0, 0, 0, 0);
752 ucal_setDateTime(caldef, 1999, UCAL_MARCH, 12, -10, -10,0, &status);
753 verify2("1999 12th day of March is at -10:-10:0 :", caldef, datdef, 1999, UCAL_MARCH, 11, 1, 50, 0, 1);
754
755
756
757 /*close caldef and datdef*/
758 ucal_close(caldef);
759 ucal_close(caldef2);
51004dcb 760 ucal_close(caldef3);
b75a7d8f 761 udat_close(datdef);
b75a7d8f
A
762}
763
764/*----------------------------------------------------------- */
765/**
766 * Confirm the functioning of the calendar field related functions.
767 */
768static void TestFieldGetSet()
769{
770 UCalendar *cal = 0;
46f4442e 771 UChar tzID[4];
b75a7d8f 772 UDateFormat *datdef = 0;
51004dcb 773 UDate d1 = 0;
b75a7d8f 774 UErrorCode status=U_ZERO_ERROR;
51004dcb 775 (void)d1; /* Suppress set but not used warning. */
b75a7d8f 776 log_verbose("\nFetching pointer to UCalendar using the ucal_open()\n");
b75a7d8f
A
777 u_strcpy(tzID, fgGMTID);
778 /*open the calendar used */
779 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
780 if (U_FAILURE(status)) {
729e4ab9 781 log_data_err("ucal_open failed: %s - (Are you missing data?)\n", u_errorName(status));
b75a7d8f
A
782 return;
783 }
784 datdef=udat_open(UDAT_SHORT,UDAT_SHORT ,NULL,fgGMTID,-1,NULL, 0, &status);
785 if(U_FAILURE(status))
786 {
729e4ab9 787 log_data_err("error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
b75a7d8f
A
788 }
789
790 /*Testing ucal_get()*/
791 log_verbose("\nTesting the ucal_get() function of Calendar\n");
792 ucal_setDateTime(cal, 1999, UCAL_MARCH, 12, 5, 25, 30, &status);
793 if(U_FAILURE(status)){
729e4ab9 794 log_data_err("error in the setDateTime() : %s (Are you missing data?)\n", u_errorName(status));
b75a7d8f
A
795 }
796 if(ucal_get(cal, UCAL_YEAR, &status)!=1999 || ucal_get(cal, UCAL_MONTH, &status)!=2 ||
797 ucal_get(cal, UCAL_DATE, &status)!=12 || ucal_get(cal, UCAL_HOUR, &status)!=5)
729e4ab9 798 log_data_err("error in ucal_get() -> %s (Are you missing data?)\n", u_errorName(status));
b75a7d8f 799 else if(ucal_get(cal, UCAL_DAY_OF_WEEK_IN_MONTH, &status)!=2 || ucal_get(cal, UCAL_DAY_OF_WEEK, &status)!=6
4388f060 800 || ucal_get(cal, UCAL_WEEK_OF_MONTH, &status)!=2 || ucal_get(cal, UCAL_WEEK_OF_YEAR, &status)!= 11)
b75a7d8f
A
801 log_err("FAIL: error in ucal_get()\n");
802 else
803 log_verbose("PASS: ucal_get() works fine\n");
804
805 /*Testing the ucal_set() , ucal_clear() functions of calendar*/
806 log_verbose("\nTesting the set, and clear field functions of calendar\n");
807 ucal_setAttribute(cal, UCAL_LENIENT, 0);
808 ucal_clear(cal);
809 ucal_set(cal, UCAL_YEAR, 1997);
810 ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
811 ucal_set(cal, UCAL_DATE, 3);
812 verify1("1997 third day of June = ", cal, datdef, 1997, UCAL_JUNE, 3);
813 ucal_clear(cal);
814 ucal_set(cal, UCAL_YEAR, 1997);
815 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
816 ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
817 ucal_set(cal, UCAL_DAY_OF_WEEK_IN_MONTH, 1);
818 verify1("1997 first Tuesday in June = ", cal, datdef, 1997, UCAL_JUNE, 3);
819 ucal_clear(cal);
820 ucal_set(cal, UCAL_YEAR, 1997);
821 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
822 ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
823 ucal_set(cal, UCAL_DAY_OF_WEEK_IN_MONTH, - 1);
824 verify1("1997 last Tuesday in June = ", cal, datdef,1997, UCAL_JUNE, 24);
825 /*give undesirable input */
826 status = U_ZERO_ERROR;
46f4442e
A
827 ucal_clear(cal);
828 ucal_set(cal, UCAL_YEAR, 1997);
829 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
830 ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
831 ucal_set(cal, UCAL_DAY_OF_WEEK_IN_MONTH, 0);
832 d1 = ucal_getMillis(cal, &status);
833 if (status != U_ILLEGAL_ARGUMENT_ERROR) {
834 log_err("FAIL: U_ILLEGAL_ARGUMENT_ERROR was not returned for : 1997 zero-th Tuesday in June\n");
835 } else {
836 log_verbose("PASS: U_ILLEGAL_ARGUMENT_ERROR as expected\n");
837 }
838 status = U_ZERO_ERROR;
b75a7d8f
A
839 ucal_clear(cal);
840 ucal_set(cal, UCAL_YEAR, 1997);
841 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
842 ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
843 ucal_set(cal, UCAL_WEEK_OF_MONTH, 1);
844 verify1("1997 Tuesday in week 1 of June = ", cal,datdef, 1997, UCAL_JUNE, 3);
845 ucal_clear(cal);
846 ucal_set(cal, UCAL_YEAR, 1997);
847 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
848 ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
849 ucal_set(cal, UCAL_WEEK_OF_MONTH, 5);
850 verify1("1997 Tuesday in week 5 of June = ", cal,datdef, 1997, UCAL_JULY, 1);
851 status = U_ZERO_ERROR;
852 ucal_clear(cal);
853 ucal_set(cal, UCAL_YEAR, 1997);
854 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
855 ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
856 ucal_set(cal, UCAL_WEEK_OF_MONTH, 0);
729e4ab9 857 ucal_setAttribute(cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,1);
46f4442e
A
858 d1 = ucal_getMillis(cal,&status);
859 if (status != U_ILLEGAL_ARGUMENT_ERROR){
860 log_err("FAIL: U_ILLEGAL_ARGUMENT_ERROR was not returned for : 1997 Tuesday zero-th week in June\n");
861 } else {
862 log_verbose("PASS: U_ILLEGAL_ARGUMENT_ERROR as expected\n");
863 }
864 status = U_ZERO_ERROR;
b75a7d8f
A
865 ucal_clear(cal);
866 ucal_set(cal, UCAL_YEAR_WOY, 1997);
867 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
868 ucal_set(cal, UCAL_WEEK_OF_YEAR, 1);
869 verify1("1997 Tuesday in week 1 of year = ", cal, datdef,1996, UCAL_DECEMBER, 31);
870 ucal_clear(cal);
871 ucal_set(cal, UCAL_YEAR, 1997);
872 ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
873 ucal_set(cal, UCAL_WEEK_OF_YEAR, 10);
874 verify1("1997 Tuesday in week 10 of year = ", cal,datdef, 1997, UCAL_MARCH, 4);
875 ucal_clear(cal);
876 ucal_set(cal, UCAL_YEAR, 1999);
877 ucal_set(cal, UCAL_DAY_OF_YEAR, 1);
878 verify1("1999 1st day of the year =", cal, datdef, 1999, UCAL_JANUARY, 1);
879 ucal_set(cal, UCAL_MONTH, -3);
46f4442e
A
880 d1 = ucal_getMillis(cal,&status);
881 if (status != U_ILLEGAL_ARGUMENT_ERROR){
882 log_err("FAIL: U_ILLEGAL_ARGUMENT_ERROR was not returned for : 1999 -3th month\n");
883 } else {
884 log_verbose("PASS: U_ILLEGAL_ARGUMENT_ERROR as expected\n");
885 }
886
b75a7d8f
A
887 ucal_setAttribute(cal, UCAL_LENIENT, 1);
888
889 ucal_set(cal, UCAL_MONTH, -3);
890 verify1("1999 -3th month should be", cal, datdef, 1998, UCAL_OCTOBER, 1);
891
892
893 /*testing isSet and clearField()*/
894 if(!ucal_isSet(cal, UCAL_WEEK_OF_YEAR))
895 log_err("FAIL: error in isSet\n");
896 else
897 log_verbose("PASS: isSet working fine\n");
898 ucal_clearField(cal, UCAL_WEEK_OF_YEAR);
899 if(ucal_isSet(cal, UCAL_WEEK_OF_YEAR))
900 log_err("FAIL: there is an error in clearField or isSet\n");
901 else
902 log_verbose("PASS :clearField working fine\n");
903
904 /*-------------------------------*/
905
906 ucal_close(cal);
907 udat_close(datdef);
b75a7d8f
A
908}
909
4388f060
A
910typedef struct {
911 const char * zone;
912 int32_t year;
913 int32_t month;
914 int32_t day;
915 int32_t hour;
916} TransitionItem;
917
918static const TransitionItem transitionItems[] = {
919 { "America/Caracas", 2007, UCAL_DECEMBER, 8, 10 }, /* day before change in UCAL_ZONE_OFFSET */
920 { "US/Pacific", 2011, UCAL_MARCH, 12, 10 }, /* day before change in UCAL_DST_OFFSET */
921 { NULL, 0, 0, 0, 0 }
922};
b75a7d8f 923
b75a7d8f
A
924/* ------------------------------------- */
925/**
926 * Execute adding and rolling in Calendar extensively,
927 */
928static void TestAddRollExtensive()
929{
4388f060 930 const TransitionItem * itemPtr;
b75a7d8f
A
931 UCalendar *cal = 0;
932 int32_t i,limit;
4388f060 933 UChar tzID[32];
b75a7d8f
A
934 UCalendarDateFields e;
935 int32_t y,m,d,hr,min,sec,ms;
936 int32_t maxlimit = 40;
937 UErrorCode status = U_ZERO_ERROR;
938 y = 1997; m = UCAL_FEBRUARY; d = 1; hr = 1; min = 1; sec = 0; ms = 0;
939
940 log_verbose("Testing add and roll extensively\n");
941
b75a7d8f
A
942 u_uastrcpy(tzID, "PST");
943 /*open the calendar used */
944 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_GREGORIAN, &status);;
945 if (U_FAILURE(status)) {
729e4ab9 946 log_data_err("ucal_open() failed : %s - (Are you missing data?)\n", u_errorName(status));
b75a7d8f
A
947 return;
948 }
949
950 ucal_set(cal, UCAL_YEAR, y);
951 ucal_set(cal, UCAL_MONTH, m);
952 ucal_set(cal, UCAL_DATE, d);
729e4ab9 953 ucal_setAttribute(cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,1);
b75a7d8f
A
954
955 /* Confirm that adding to various fields works.*/
956 log_verbose("\nTesting to confirm that adding to various fields works with ucal_add()\n");
957 checkDate(cal, y, m, d);
958 ucal_add(cal,UCAL_YEAR, 1, &status);
959 if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status)); return; }
960 y++;
961 checkDate(cal, y, m, d);
962 ucal_add(cal,UCAL_MONTH, 12, &status);
963 if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
964 y+=1;
965 checkDate(cal, y, m, d);
966 ucal_add(cal,UCAL_DATE, 1, &status);
967 if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
968 d++;
969 checkDate(cal, y, m, d);
970 ucal_add(cal,UCAL_DATE, 2, &status);
971 if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
972 d += 2;
973 checkDate(cal, y, m, d);
974 ucal_add(cal,UCAL_DATE, 28, &status);
975 if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
976 ++m;
977 checkDate(cal, y, m, d);
978 ucal_add(cal, (UCalendarDateFields)-1, 10, &status);
979 if(status==U_ILLEGAL_ARGUMENT_ERROR)
980 log_verbose("Pass: Illegal argument error as expected\n");
981 else{
982 log_err("Fail: No, illegal argument error as expected. Got....: %s\n", u_errorName(status));
983 }
984 status=U_ZERO_ERROR;
985
986
987 /*confirm that applying roll to various fields works fine*/
988 log_verbose("\nTesting to confirm that ucal_roll() works\n");
989 ucal_roll(cal, UCAL_DATE, -1, &status);
990 if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
991 d -=1;
992 checkDate(cal, y, m, d);
993 ucal_roll(cal, UCAL_MONTH, -2, &status);
994 if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
995 m -=2;
996 checkDate(cal, y, m, d);
997 ucal_roll(cal, UCAL_DATE, 1, &status);
998 if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
999 d +=1;
1000 checkDate(cal, y, m, d);
1001 ucal_roll(cal, UCAL_MONTH, -12, &status);
1002 if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
1003 checkDate(cal, y, m, d);
1004 ucal_roll(cal, UCAL_YEAR, -1, &status);
1005 if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
1006 y -=1;
1007 checkDate(cal, y, m, d);
1008 ucal_roll(cal, UCAL_DATE, 29, &status);
1009 if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
1010 d = 2;
1011 checkDate(cal, y, m, d);
1012 ucal_roll(cal, (UCalendarDateFields)-1, 10, &status);
1013 if(status==U_ILLEGAL_ARGUMENT_ERROR)
1014 log_verbose("Pass: illegal arguement error as expected\n");
1015 else{
1016 log_err("Fail: no illegal argument error got..: %s\n", u_errorName(status));
1017 return;
1018 }
1019 status=U_ZERO_ERROR;
73c04bcf 1020 ucal_clear(cal);
b75a7d8f
A
1021 ucal_setDateTime(cal, 1999, UCAL_FEBRUARY, 28, 10, 30, 45, &status);
1022 if(U_FAILURE(status)){
1023 log_err("error is setting the datetime: %s\n", u_errorName(status));
1024 }
1025 ucal_add(cal, UCAL_MONTH, 1, &status);
1026 checkDate(cal, 1999, UCAL_MARCH, 28);
1027 ucal_add(cal, UCAL_MILLISECOND, 1000, &status);
1028 checkDateTime(cal, 1999, UCAL_MARCH, 28, 10, 30, 46, 0, UCAL_MILLISECOND);
1029
1030 ucal_close(cal);
1031/*--------------- */
1032 status=U_ZERO_ERROR;
1033 /* Testing add and roll extensively */
1034 log_verbose("\nTesting the ucal_add() and ucal_roll() functions extensively\n");
1035 y = 1997; m = UCAL_FEBRUARY; d = 1; hr = 1; min = 1; sec = 0; ms = 0;
1036 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
1037 if (U_FAILURE(status)) {
1038 log_err("ucal_open failed: %s\n", u_errorName(status));
1039 return;
1040 }
1041 ucal_set(cal, UCAL_YEAR, y);
1042 ucal_set(cal, UCAL_MONTH, m);
1043 ucal_set(cal, UCAL_DATE, d);
1044 ucal_set(cal, UCAL_HOUR, hr);
1045 ucal_set(cal, UCAL_MINUTE, min);
1046 ucal_set(cal, UCAL_SECOND,sec);
1047 ucal_set(cal, UCAL_MILLISECOND, ms);
729e4ab9 1048 ucal_setAttribute(cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,1);
b75a7d8f
A
1049 status=U_ZERO_ERROR;
1050
1051 log_verbose("\nTesting UCalendar add...\n");
1052 for(e = UCAL_YEAR;e < UCAL_FIELD_COUNT; e=(UCalendarDateFields)((int32_t)e + 1)) {
1053 limit = maxlimit;
1054 status = U_ZERO_ERROR;
1055 for (i = 0; i < limit; i++) {
1056 ucal_add(cal, e, 1, &status);
1057 if (U_FAILURE(status)) { limit = i; status = U_ZERO_ERROR; }
1058 }
1059 for (i = 0; i < limit; i++) {
1060 ucal_add(cal, e, -1, &status);
1061 if (U_FAILURE(status)) {
1062 log_err("ucal_add -1 failed: %s\n", u_errorName(status));
1063 return;
1064 }
1065 }
1066 checkDateTime(cal, y, m, d, hr, min, sec, ms, e);
1067 }
1068 log_verbose("\nTesting calendar ucal_roll()...\n");
1069 for(e = UCAL_YEAR;e < UCAL_FIELD_COUNT; e=(UCalendarDateFields)((int32_t)e + 1)) {
1070 limit = maxlimit;
1071 status = U_ZERO_ERROR;
1072 for (i = 0; i < limit; i++) {
1073 ucal_roll(cal, e, 1, &status);
1074 if (U_FAILURE(status)) {
1075 limit = i;
1076 status = U_ZERO_ERROR;
1077 }
1078 }
1079 for (i = 0; i < limit; i++) {
1080 ucal_roll(cal, e, -1, &status);
1081 if (U_FAILURE(status)) {
1082 log_err("ucal_roll -1 failed: %s\n", u_errorName(status));
1083 return;
1084 }
1085 }
1086 checkDateTime(cal, y, m, d, hr, min, sec, ms, e);
1087 }
1088
1089 ucal_close(cal);
4388f060
A
1090/*--------------- */
1091 log_verbose("\nTesting ucal_add() across ZONE_OFFSET and DST_OFFSE transitions.\n");
1092 for (itemPtr = transitionItems; itemPtr->zone != NULL; itemPtr++) {
1093 status=U_ZERO_ERROR;
1094 u_uastrcpy(tzID, itemPtr->zone);
1095 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_GREGORIAN, &status);
1096 if (U_FAILURE(status)) {
1097 log_err("ucal_open failed for zone %s: %s\n", itemPtr->zone, u_errorName(status));
1098 continue;
1099 }
1100 ucal_setDateTime(cal, itemPtr->year, itemPtr->month, itemPtr->day, itemPtr->hour, 0, 0, &status);
1101 ucal_add(cal, UCAL_DATE, 1, &status);
1102 hr = ucal_get(cal, UCAL_HOUR_OF_DAY, &status);
1103 if ( U_FAILURE(status) ) {
1104 log_err("ucal_add failed adding day across transition for zone %s: %s\n", itemPtr->zone, u_errorName(status));
1105 } else if ( hr != itemPtr->hour ) {
1106 log_err("ucal_add produced wrong hour %d when adding day across transition for zone %s\n", hr, itemPtr->zone);
1107 } else {
1108 ucal_add(cal, UCAL_DATE, -1, &status);
1109 hr = ucal_get(cal, UCAL_HOUR_OF_DAY, &status);
1110 if ( U_FAILURE(status) ) {
1111 log_err("ucal_add failed subtracting day across transition for zone %s: %s\n", itemPtr->zone, u_errorName(status));
1112 } else if ( hr != itemPtr->hour ) {
1113 log_err("ucal_add produced wrong hour %d when subtracting day across transition for zone %s\n", hr, itemPtr->zone);
1114 }
1115 }
1116 ucal_close(cal);
1117 }
b75a7d8f
A
1118}
1119
1120/*------------------------------------------------------ */
1121/*Testing the Limits for various Fields of Calendar*/
1122static void TestGetLimits()
1123{
1124 UCalendar *cal = 0;
1125 int32_t min, max, gr_min, le_max, ac_min, ac_max, val;
46f4442e 1126 UChar tzID[4];
b75a7d8f
A
1127 UErrorCode status = U_ZERO_ERROR;
1128
1129
b75a7d8f
A
1130 u_uastrcpy(tzID, "PST");
1131 /*open the calendar used */
1132 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_GREGORIAN, &status);;
1133 if (U_FAILURE(status)) {
729e4ab9 1134 log_data_err("ucal_open() for gregorian calendar failed in TestGetLimits: %s - (Are you missing data?)\n", u_errorName(status));
b75a7d8f
A
1135 return;
1136 }
1137
1138 log_verbose("\nTesting the getLimits function for various fields\n");
1139
1140
1141
1142 ucal_setDate(cal, 1999, UCAL_MARCH, 5, &status); /* Set the date to be March 5, 1999 */
1143 val = ucal_get(cal, UCAL_DAY_OF_WEEK, &status);
1144 min = ucal_getLimit(cal, UCAL_DAY_OF_WEEK, UCAL_MINIMUM, &status);
1145 max = ucal_getLimit(cal, UCAL_DAY_OF_WEEK, UCAL_MAXIMUM, &status);
73c04bcf 1146 if ( (min != UCAL_SUNDAY || max != UCAL_SATURDAY ) && (min > val && val > max) && (val != UCAL_FRIDAY)){
b75a7d8f
A
1147 log_err("FAIL: Min/max bad\n");
1148 log_err("FAIL: Day of week %d out of range\n", val);
1149 log_err("FAIL: FAIL: Day of week should be SUNDAY Got %d\n", val);
1150 }
1151 else
1152 log_verbose("getLimits successful\n");
1153
1154 val = ucal_get(cal, UCAL_DAY_OF_WEEK_IN_MONTH, &status);
1155 min = ucal_getLimit(cal, UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_MINIMUM, &status);
1156 max = ucal_getLimit(cal, UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_MAXIMUM, &status);
73c04bcf 1157 if ( (min != 0 || max != 5 ) && (min > val && val > max) && (val != 1)){
b75a7d8f
A
1158 log_err("FAIL: Min/max bad\n");
1159 log_err("FAIL: Day of week in month %d out of range\n", val);
1160 log_err("FAIL: FAIL: Day of week in month should be SUNDAY Got %d\n", val);
1161
1162 }
1163 else
1164 log_verbose("getLimits successful\n");
1165
1166 min=ucal_getLimit(cal, UCAL_MONTH, UCAL_MINIMUM, &status);
1167 max=ucal_getLimit(cal, UCAL_MONTH, UCAL_MAXIMUM, &status);
1168 gr_min=ucal_getLimit(cal, UCAL_MONTH, UCAL_GREATEST_MINIMUM, &status);
1169 le_max=ucal_getLimit(cal, UCAL_MONTH, UCAL_LEAST_MAXIMUM, &status);
1170 ac_min=ucal_getLimit(cal, UCAL_MONTH, UCAL_ACTUAL_MINIMUM, &status);
1171 ac_max=ucal_getLimit(cal, UCAL_MONTH, UCAL_ACTUAL_MAXIMUM, &status);
1172 if(U_FAILURE(status)){
1173 log_err("Error in getLimits: %s\n", u_errorName(status));
1174 }
1175 if(min!=0 || max!=11 || gr_min!=0 || le_max!=11 || ac_min!=0 || ac_max!=11)
1176 log_err("There is and error in getLimits in fetching the values\n");
1177 else
1178 log_verbose("getLimits successful\n");
1179
1180 ucal_setDateTime(cal, 1999, UCAL_MARCH, 5, 4, 10, 35, &status);
1181 val=ucal_get(cal, UCAL_HOUR_OF_DAY, &status);
1182 min=ucal_getLimit(cal, UCAL_HOUR_OF_DAY, UCAL_MINIMUM, &status);
1183 max=ucal_getLimit(cal, UCAL_HOUR_OF_DAY, UCAL_MAXIMUM, &status);
1184 gr_min=ucal_getLimit(cal, UCAL_MINUTE, UCAL_GREATEST_MINIMUM, &status);
1185 le_max=ucal_getLimit(cal, UCAL_MINUTE, UCAL_LEAST_MAXIMUM, &status);
1186 ac_min=ucal_getLimit(cal, UCAL_MINUTE, UCAL_ACTUAL_MINIMUM, &status);
1187 ac_max=ucal_getLimit(cal, UCAL_SECOND, UCAL_ACTUAL_MAXIMUM, &status);
1188 if( (min!=0 || max!= 11 || gr_min!=0 || le_max!=60 || ac_min!=0 || ac_max!=60) &&
73c04bcf 1189 (min>val && val>max) && val!=4){
b75a7d8f
A
1190
1191 log_err("FAIL: Min/max bad\n");
1192 log_err("FAIL: Hour of Day %d out of range\n", val);
1193 log_err("FAIL: HOUR_OF_DAY should be 4 Got %d\n", val);
1194 }
1195 else
1196 log_verbose("getLimits successful\n");
1197
1198
1199 /*get BOGUS_LIMIT type*/
1200 val=ucal_getLimit(cal, UCAL_SECOND, (UCalendarLimitType)99, &status);
1201 if(val != -1){
1202 log_err("FAIL: ucal_getLimit() with BOGUS type should return -1\n");
1203 }
1204 status=U_ZERO_ERROR;
1205
1206
1207 ucal_close(cal);
b75a7d8f
A
1208}
1209
1210
1211
1212/* ------------------------------------- */
1213
1214/**
1215 * Test that the days of the week progress properly when add is called repeatedly
1216 * for increments of 24 days.
1217 */
1218static void TestDOWProgression()
1219{
1220 int32_t initialDOW, DOW, newDOW, expectedDOW;
1221 UCalendar *cal = 0;
1222 UDateFormat *datfor = 0;
1223 UDate date1;
1224 int32_t delta=24;
1225 UErrorCode status = U_ZERO_ERROR;
46f4442e 1226 UChar tzID[4];
b75a7d8f 1227 char tempMsgBuf[256];
b75a7d8f
A
1228 u_strcpy(tzID, fgGMTID);
1229 /*open the calendar used */
1230 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);;
1231 if (U_FAILURE(status)) {
729e4ab9 1232 log_data_err("ucal_open failed: %s - (Are you missing data?)\n", u_errorName(status));
b75a7d8f
A
1233 return;
1234 }
1235
1236 datfor=udat_open(UDAT_MEDIUM,UDAT_MEDIUM ,NULL, fgGMTID,-1,NULL, 0, &status);
1237 if(U_FAILURE(status)){
729e4ab9 1238 log_data_err("error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
b75a7d8f
A
1239 }
1240
1241
1242 ucal_setDate(cal, 1999, UCAL_JANUARY, 1, &status);
1243
1244 log_verbose("\nTesting the DOW progression\n");
1245
1246 initialDOW = ucal_get(cal, UCAL_DAY_OF_WEEK, &status);
729e4ab9 1247 if (U_FAILURE(status)) { log_data_err("ucal_get() failed: %s (Are you missing data?)\n", u_errorName(status) ); return; }
b75a7d8f
A
1248 newDOW = initialDOW;
1249 do {
1250 DOW = newDOW;
1251 log_verbose("DOW = %d...\n", DOW);
1252 date1=ucal_getMillis(cal, &status);
1253 if(U_FAILURE(status)){ log_err("ucal_getMiilis() failed: %s\n", u_errorName(status)); return;}
1254 log_verbose("%s\n", u_austrcpy(tempMsgBuf, myDateFormat(datfor, date1)));
1255
1256 ucal_add(cal,UCAL_DAY_OF_WEEK, delta, &status);
1257 if (U_FAILURE(status)) { log_err("ucal_add() failed: %s\n", u_errorName(status)); return; }
1258
1259 newDOW = ucal_get(cal, UCAL_DAY_OF_WEEK, &status);
1260 if (U_FAILURE(status)) { log_err("ucal_get() failed: %s\n", u_errorName(status)); return; }
1261 expectedDOW = 1 + (DOW + delta - 1) % 7;
1262 date1=ucal_getMillis(cal, &status);
1263 if(U_FAILURE(status)){ log_err("ucal_getMiilis() failed: %s\n", u_errorName(status)); return;}
1264 if (newDOW != expectedDOW) {
1265 log_err("Day of week should be %d instead of %d on %s", expectedDOW, newDOW,
1266 u_austrcpy(tempMsgBuf, myDateFormat(datfor, date1)) );
1267 return;
1268 }
1269 }
1270 while (newDOW != initialDOW);
1271
1272 ucal_close(cal);
1273 udat_close(datfor);
b75a7d8f
A
1274}
1275
1276/* ------------------------------------- */
1277
1278/**
1279 * Confirm that the offset between local time and GMT behaves as expected.
1280 */
1281static void TestGMTvsLocal()
1282{
1283 log_verbose("\nTesting the offset between the GMT and local time\n");
1284 testZones(1999, 1, 1, 12, 0, 0);
1285 testZones(1999, 4, 16, 18, 30, 0);
1286 testZones(1998, 12, 17, 19, 0, 0);
1287}
1288
1289/* ------------------------------------- */
1290
1291static void testZones(int32_t yr, int32_t mo, int32_t dt, int32_t hr, int32_t mn, int32_t sc)
1292{
1293 int32_t offset,utc, expected;
1294 UCalendar *gmtcal = 0, *cal = 0;
1295 UDate date1;
1296 double temp;
1297 UDateFormat *datfor = 0;
1298 UErrorCode status = U_ZERO_ERROR;
46f4442e 1299 UChar tzID[4];
b75a7d8f
A
1300 char tempMsgBuf[256];
1301
b75a7d8f
A
1302 u_strcpy(tzID, fgGMTID);
1303 gmtcal=ucal_open(tzID, 3, "en_US", UCAL_TRADITIONAL, &status);;
1304 if (U_FAILURE(status)) {
729e4ab9 1305 log_data_err("ucal_open failed: %s - (Are you missing data?)\n", u_errorName(status));
b75a7d8f
A
1306 return;
1307 }
1308 u_uastrcpy(tzID, "PST");
1309 cal = ucal_open(tzID, 3, "en_US", UCAL_TRADITIONAL, &status);
1310 if (U_FAILURE(status)) {
1311 log_err("ucal_open failed: %s\n", u_errorName(status));
1312 return;
1313 }
1314
1315 datfor=udat_open(UDAT_MEDIUM,UDAT_MEDIUM ,NULL, fgGMTID,-1,NULL, 0, &status);
1316 if(U_FAILURE(status)){
729e4ab9 1317 log_data_err("error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
b75a7d8f
A
1318 }
1319
1320 ucal_setDateTime(gmtcal, yr, mo - 1, dt, hr, mn, sc, &status);
1321 if (U_FAILURE(status)) {
729e4ab9 1322 log_data_err("ucal_setDateTime failed: %s (Are you missing data?)\n", u_errorName(status));
b75a7d8f
A
1323 return;
1324 }
1325 ucal_set(gmtcal, UCAL_MILLISECOND, 0);
1326 date1 = ucal_getMillis(gmtcal, &status);
1327 if (U_FAILURE(status)) {
1328 log_err("ucal_getMillis failed: %s\n", u_errorName(status));
1329 return;
1330 }
1331 log_verbose("date = %s\n", u_austrcpy(tempMsgBuf, myDateFormat(datfor, date1)) );
1332
1333
1334 ucal_setMillis(cal, date1, &status);
1335 if (U_FAILURE(status)) {
1336 log_err("ucal_setMillis() failed: %s\n", u_errorName(status));
1337 return;
1338 }
1339
1340 offset = ucal_get(cal, UCAL_ZONE_OFFSET, &status);
1341 offset += ucal_get(cal, UCAL_DST_OFFSET, &status);
1342
1343 if (U_FAILURE(status)) {
1344 log_err("ucal_get() failed: %s\n", u_errorName(status));
1345 return;
1346 }
1347 temp=(double)((double)offset / 1000.0 / 60.0 / 60.0);
1348 /*printf("offset for %s %f hr\n", austrdup(myDateFormat(datfor, date1)), temp);*/
1349
1350 utc = ((ucal_get(cal, UCAL_HOUR_OF_DAY, &status) * 60 +
1351 ucal_get(cal, UCAL_MINUTE, &status)) * 60 +
1352 ucal_get(cal, UCAL_SECOND, &status)) * 1000 +
1353 ucal_get(cal, UCAL_MILLISECOND, &status) - offset;
1354 if (U_FAILURE(status)) {
1355 log_err("ucal_get() failed: %s\n", u_errorName(status));
1356 return;
1357 }
1358
1359 expected = ((hr * 60 + mn) * 60 + sc) * 1000;
1360 if (utc != expected) {
1361 temp=(double)(utc - expected)/ 1000 / 60 / 60.0;
1362 log_err("FAIL: Discrepancy of %d millis = %fhr\n", utc-expected, temp );
1363 }
1364 else
1365 log_verbose("PASS: the offset between local and GMT is correct\n");
1366 ucal_close(gmtcal);
1367 ucal_close(cal);
1368 udat_close(datfor);
b75a7d8f
A
1369}
1370
1371/* ------------------------------------- */
1372
1373
1374
1375
1376/* INTERNAL FUNCTIONS USED */
1377/*------------------------------------------------------------------------------------------- */
1378
1379/* ------------------------------------- */
1380static void checkDateTime(UCalendar* c,
1381 int32_t y, int32_t m, int32_t d,
1382 int32_t hr, int32_t min, int32_t sec,
1383 int32_t ms, UCalendarDateFields field)
1384
1385{
1386 UErrorCode status = U_ZERO_ERROR;
1387 if (ucal_get(c, UCAL_YEAR, &status) != y ||
1388 ucal_get(c, UCAL_MONTH, &status) != m ||
1389 ucal_get(c, UCAL_DATE, &status) != d ||
1390 ucal_get(c, UCAL_HOUR, &status) != hr ||
1391 ucal_get(c, UCAL_MINUTE, &status) != min ||
1392 ucal_get(c, UCAL_SECOND, &status) != sec ||
1393 ucal_get(c, UCAL_MILLISECOND, &status) != ms) {
1394 log_err("U_FAILURE for field %d, Expected y/m/d h:m:s:ms of %d/%d/%d %d:%d:%d:%d got %d/%d/%d %d:%d:%d:%d\n",
1395 (int32_t)field, y, m + 1, d, hr, min, sec, ms,
1396 ucal_get(c, UCAL_YEAR, &status),
1397 ucal_get(c, UCAL_MONTH, &status) + 1,
1398 ucal_get(c, UCAL_DATE, &status),
1399 ucal_get(c, UCAL_HOUR, &status),
1400 ucal_get(c, UCAL_MINUTE, &status) + 1,
1401 ucal_get(c, UCAL_SECOND, &status),
1402 ucal_get(c, UCAL_MILLISECOND, &status) );
1403
1404 if (U_FAILURE(status)){
1405 log_err("ucal_get failed: %s\n", u_errorName(status));
1406 return;
1407 }
1408
1409 }
1410 else
1411 log_verbose("Confirmed: %d/%d/%d %d:%d:%d:%d\n", y, m + 1, d, hr, min, sec, ms);
1412
1413}
1414
1415/* ------------------------------------- */
1416static void checkDate(UCalendar* c, int32_t y, int32_t m, int32_t d)
1417{
1418 UErrorCode status = U_ZERO_ERROR;
1419 if (ucal_get(c,UCAL_YEAR, &status) != y ||
1420 ucal_get(c, UCAL_MONTH, &status) != m ||
1421 ucal_get(c, UCAL_DATE, &status) != d) {
1422
1423 log_err("FAILURE: Expected y/m/d of %d/%d/%d got %d/%d/%d\n", y, m + 1, d,
1424 ucal_get(c, UCAL_YEAR, &status),
1425 ucal_get(c, UCAL_MONTH, &status) + 1,
1426 ucal_get(c, UCAL_DATE, &status) );
1427
1428 if (U_FAILURE(status)) {
1429 log_err("ucal_get failed: %s\n", u_errorName(status));
1430 return;
1431 }
1432 }
1433 else
1434 log_verbose("Confirmed: %d/%d/%d\n", y, m + 1, d);
1435
1436
1437}
1438
1439/* ------------------------------------- */
1440
1441/* ------------------------------------- */
1442
1443static void verify1(const char* msg, UCalendar* c, UDateFormat* dat, int32_t year, int32_t month, int32_t day)
1444{
1445 UDate d1;
1446 UErrorCode status = U_ZERO_ERROR;
1447 if (ucal_get(c, UCAL_YEAR, &status) == year &&
1448 ucal_get(c, UCAL_MONTH, &status) == month &&
1449 ucal_get(c, UCAL_DATE, &status) == day) {
1450 if (U_FAILURE(status)) {
1451 log_err("FAIL: Calendar::get failed: %s\n", u_errorName(status));
1452 return;
1453 }
1454 log_verbose("PASS: %s\n", msg);
1455 d1=ucal_getMillis(c, &status);
1456 if (U_FAILURE(status)) {
1457 log_err("ucal_getMillis failed: %s\n", u_errorName(status));
1458 return;
1459 }
1460 /*log_verbose(austrdup(myDateFormat(dat, d1)) );*/
1461 }
1462 else {
1463 log_err("FAIL: %s\n", msg);
1464 d1=ucal_getMillis(c, &status);
1465 if (U_FAILURE(status)) {
1466 log_err("ucal_getMillis failed: %s\n", u_errorName(status) );
1467 return;
1468 }
1469 log_err("Got %s Expected %d/%d/%d \n", austrdup(myDateFormat(dat, d1)), year, month + 1, day );
1470 return;
1471 }
1472
1473
1474}
1475
1476/* ------------------------------------ */
1477static void verify2(const char* msg, UCalendar* c, UDateFormat* dat, int32_t year, int32_t month, int32_t day,
1478 int32_t hour, int32_t min, int32_t sec, int32_t am_pm)
1479{
1480 UDate d1;
1481 UErrorCode status = U_ZERO_ERROR;
1482 char tempMsgBuf[256];
1483
1484 if (ucal_get(c, UCAL_YEAR, &status) == year &&
1485 ucal_get(c, UCAL_MONTH, &status) == month &&
1486 ucal_get(c, UCAL_DATE, &status) == day &&
1487 ucal_get(c, UCAL_HOUR, &status) == hour &&
1488 ucal_get(c, UCAL_MINUTE, &status) == min &&
1489 ucal_get(c, UCAL_SECOND, &status) == sec &&
1490 ucal_get(c, UCAL_AM_PM, &status) == am_pm ){
1491 if (U_FAILURE(status)) {
1492 log_err("FAIL: Calendar::get failed: %s\n", u_errorName(status));
1493 return;
1494 }
1495 log_verbose("PASS: %s\n", msg);
1496 d1=ucal_getMillis(c, &status);
1497 if (U_FAILURE(status)) {
1498 log_err("ucal_getMillis failed: %s\n", u_errorName(status));
1499 return;
1500 }
1501 log_verbose("%s\n" , u_austrcpy(tempMsgBuf, myDateFormat(dat, d1)) );
1502 }
1503 else {
1504 log_err("FAIL: %s\n", msg);
1505 d1=ucal_getMillis(c, &status);
1506 if (U_FAILURE(status)) {
1507 log_err("ucal_getMillis failed: %s\n", u_errorName(status));
1508 return;
1509 }
1510 log_err("Got %s Expected %d/%d/%d/ %d:%d:%d %s\n", austrdup(myDateFormat(dat, d1)),
1511 year, month + 1, day, hour, min, sec, (am_pm==0) ? "AM": "PM");
1512
1513 return;
1514 }
1515
1516
1517}
1518
73c04bcf
A
1519void TestGregorianChange() {
1520 static const UChar utc[] = { 0x45, 0x74, 0x63, 0x2f, 0x47, 0x4d, 0x54, 0 }; /* "Etc/GMT" */
1521 const int32_t dayMillis = 86400 * INT64_C(1000); /* 1 day = 86400 seconds */
1522 UCalendar *cal;
1523 UDate date;
1524 UErrorCode errorCode = U_ZERO_ERROR;
1525
1526 /* Test ucal_setGregorianChange() on a Gregorian calendar. */
1527 errorCode = U_ZERO_ERROR;
1528 cal = ucal_open(utc, -1, "", UCAL_GREGORIAN, &errorCode);
1529 if(U_FAILURE(errorCode)) {
729e4ab9 1530 log_data_err("ucal_open(UTC) failed: %s - (Are you missing data?)\n", u_errorName(errorCode));
73c04bcf
A
1531 return;
1532 }
1533 ucal_setGregorianChange(cal, -365 * (dayMillis * (UDate)1), &errorCode);
1534 if(U_FAILURE(errorCode)) {
1535 log_err("ucal_setGregorianChange(1969) failed: %s\n", u_errorName(errorCode));
1536 } else {
1537 date = ucal_getGregorianChange(cal, &errorCode);
1538 if(U_FAILURE(errorCode) || date != -365 * (dayMillis * (UDate)1)) {
1539 log_err("ucal_getGregorianChange() failed: %s, date = %f\n", u_errorName(errorCode), date);
1540 }
1541 }
1542 ucal_close(cal);
1543
1544 /* Test ucal_setGregorianChange() on a non-Gregorian calendar where it should fail. */
1545 errorCode = U_ZERO_ERROR;
1546 cal = ucal_open(utc, -1, "th@calendar=buddhist", UCAL_TRADITIONAL, &errorCode);
1547 if(U_FAILURE(errorCode)) {
1548 log_err("ucal_open(UTC, non-Gregorian) failed: %s\n", u_errorName(errorCode));
1549 return;
1550 }
1551 ucal_setGregorianChange(cal, -730 * (dayMillis * (UDate)1), &errorCode);
1552 if(errorCode != U_UNSUPPORTED_ERROR) {
1553 log_err("ucal_setGregorianChange(non-Gregorian calendar) did not yield U_UNSUPPORTED_ERROR but %s\n",
1554 u_errorName(errorCode));
1555 }
1556 errorCode = U_ZERO_ERROR;
1557 date = ucal_getGregorianChange(cal, &errorCode);
1558 if(errorCode != U_UNSUPPORTED_ERROR) {
1559 log_err("ucal_getGregorianChange(non-Gregorian calendar) did not yield U_UNSUPPORTED_ERROR but %s\n",
1560 u_errorName(errorCode));
1561 }
1562 ucal_close(cal);
1563}
1564
729e4ab9 1565static void TestGetKeywordValuesForLocale() {
2ca993e8 1566#define PREFERRED_SIZE 16
57a6839d 1567#define MAX_NUMBER_OF_KEYWORDS 5
729e4ab9 1568 const char *PREFERRED[PREFERRED_SIZE][MAX_NUMBER_OF_KEYWORDS+1] = {
57a6839d
A
1569 { "root", "gregorian", NULL, NULL, NULL, NULL },
1570 { "und", "gregorian", NULL, NULL, NULL, NULL },
1571 { "en_US", "gregorian", NULL, NULL, NULL, NULL },
1572 { "en_029", "gregorian", NULL, NULL, NULL, NULL },
1573 { "th_TH", "buddhist", "gregorian", NULL, NULL, NULL },
1574 { "und_TH", "buddhist", "gregorian", NULL, NULL, NULL },
1575 { "en_TH", "buddhist", "gregorian", NULL, NULL, NULL },
1576 { "he_IL", "gregorian", "hebrew", "islamic", "islamic-civil", "islamic-tbla" },
1577 { "ar_EG", "gregorian", "coptic", "islamic", "islamic-civil", "islamic-tbla" },
1578 { "ja", "gregorian", "japanese", NULL, NULL, NULL },
1579 { "ps_Guru_IN", "gregorian", "indian", NULL, NULL, NULL },
1580 { "th@calendar=gregorian", "buddhist", "gregorian", NULL, NULL, NULL },
1581 { "en@calendar=islamic", "gregorian", NULL, NULL, NULL, NULL },
1582 { "zh_TW", "gregorian", "roc", "chinese", NULL, NULL },
1583 { "ar_IR", "persian", "gregorian", "islamic", "islamic-civil", "islamic-tbla" },
2ca993e8 1584 { "th@rg=SAZZZZ", "islamic-umalqura", "islamic-rgsa", "islamic", "gregorian", NULL },
729e4ab9 1585 };
2ca993e8 1586 const int32_t EXPECTED_SIZE[PREFERRED_SIZE] = { 1, 1, 1, 1, 2, 2, 2, 5, 5, 2, 2, 2, 1, 3, 5, 4 };
729e4ab9
A
1587 UErrorCode status = U_ZERO_ERROR;
1588 int32_t i, size, j;
1589 UEnumeration *all, *pref;
1590 const char *loc = NULL;
1591 UBool matchPref, matchAll;
1592 const char *value;
1593 int32_t valueLength;
1594 UList *ALLList = NULL;
1595
1596 UEnumeration *ALL = ucal_getKeywordValuesForLocale("calendar", uloc_getDefault(), FALSE, &status);
1597 if (U_SUCCESS(status)) {
1598 for (i = 0; i < PREFERRED_SIZE; i++) {
1599 pref = NULL;
1600 all = NULL;
1601 loc = PREFERRED[i][0];
1602 pref = ucal_getKeywordValuesForLocale("calendar", loc, TRUE, &status);
1603 matchPref = FALSE;
1604 matchAll = FALSE;
1605
1606 value = NULL;
1607 valueLength = 0;
1608
1609 if (U_SUCCESS(status) && uenum_count(pref, &status) == EXPECTED_SIZE[i]) {
1610 matchPref = TRUE;
1611 for (j = 0; j < EXPECTED_SIZE[i]; j++) {
1612 if ((value = uenum_next(pref, &valueLength, &status)) != NULL && U_SUCCESS(status)) {
1613 if (uprv_strcmp(value, PREFERRED[i][j+1]) != 0) {
1614 matchPref = FALSE;
1615 break;
1616 }
1617 } else {
1618 matchPref = FALSE;
1619 log_err("ERROR getting keyword value for locale \"%s\"\n", loc);
1620 break;
1621 }
1622 }
1623 }
1624
1625 if (!matchPref) {
1626 log_err("FAIL: Preferred values for locale \"%s\" does not match expected.\n", loc);
1627 break;
1628 }
1629 uenum_close(pref);
1630
1631 all = ucal_getKeywordValuesForLocale("calendar", loc, FALSE, &status);
1632
1633 size = uenum_count(all, &status);
1634
1635 if (U_SUCCESS(status) && size == uenum_count(ALL, &status)) {
1636 matchAll = TRUE;
1637 ALLList = ulist_getListFromEnum(ALL);
1638 for (j = 0; j < size; j++) {
1639 if ((value = uenum_next(all, &valueLength, &status)) != NULL && U_SUCCESS(status)) {
1640 if (!ulist_containsString(ALLList, value, uprv_strlen(value))) {
1641 log_err("Locale %s have %s not in ALL\n", loc, value);
1642 matchAll = FALSE;
1643 break;
1644 }
1645 } else {
1646 matchAll = FALSE;
1647 log_err("ERROR getting \"all\" keyword value for locale \"%s\"\n", loc);
1648 break;
1649 }
1650 }
1651 }
1652 if (!matchAll) {
1653 log_err("FAIL: All values for locale \"%s\" does not match expected.\n", loc);
1654 }
1655
1656 uenum_close(all);
1657 }
1658 } else {
1659 log_err_status(status, "Failed to get ALL keyword values for default locale %s: %s.\n", uloc_getDefault(), u_errorName(status));
1660 }
1661 uenum_close(ALL);
1662}
1663
1664/*
1665 * Weekend tests, ported from
1666 * icu4j/trunk/main/tests/core/src/com/ibm/icu/dev/test/calendar/IBMCalendarTest.java
1667 * and extended a bit. Notes below from IBMCalendarTest.java ...
1668 * This test tests for specific locale data. This is probably okay
1669 * as far as US data is concerned, but if the Arabic/Yemen data
1670 * changes, this test will have to be updated.
1671 */
1672
1673typedef struct {
1674 int32_t year;
1675 int32_t month;
1676 int32_t day;
1677 int32_t hour;
1678 int32_t millisecOffset;
1679 UBool isWeekend;
1680} TestWeekendDates;
1681typedef struct {
1682 const char * locale;
1683 const TestWeekendDates * dates;
1684 int32_t numDates;
1685} TestWeekendDatesList;
1686
1687static const TestWeekendDates weekendDates_en_US[] = {
1688 { 2000, UCAL_MARCH, 17, 23, 0, 0 }, /* Fri 23:00 */
1689 { 2000, UCAL_MARCH, 18, 0, -1, 0 }, /* Fri 23:59:59.999 */
1690 { 2000, UCAL_MARCH, 18, 0, 0, 1 }, /* Sat 00:00 */
1691 { 2000, UCAL_MARCH, 18, 15, 0, 1 }, /* Sat 15:00 */
1692 { 2000, UCAL_MARCH, 19, 23, 0, 1 }, /* Sun 23:00 */
1693 { 2000, UCAL_MARCH, 20, 0, -1, 1 }, /* Sun 23:59:59.999 */
1694 { 2000, UCAL_MARCH, 20, 0, 0, 0 }, /* Mon 00:00 */
1695 { 2000, UCAL_MARCH, 20, 8, 0, 0 }, /* Mon 08:00 */
1696};
57a6839d 1697static const TestWeekendDates weekendDates_ar_OM[] = {
729e4ab9
A
1698 { 2000, UCAL_MARCH, 15, 23, 0, 0 }, /* Wed 23:00 */
1699 { 2000, UCAL_MARCH, 16, 0, -1, 0 }, /* Wed 23:59:59.999 */
b331163b
A
1700 { 2000, UCAL_MARCH, 16, 0, 0, 0 }, /* Thu 00:00 */
1701 { 2000, UCAL_MARCH, 16, 15, 0, 0 }, /* Thu 15:00 */
729e4ab9
A
1702 { 2000, UCAL_MARCH, 17, 23, 0, 1 }, /* Fri 23:00 */
1703 { 2000, UCAL_MARCH, 18, 0, -1, 1 }, /* Fri 23:59:59.999 */
b331163b
A
1704 { 2000, UCAL_MARCH, 18, 0, 0, 1 }, /* Sat 00:00 */
1705 { 2000, UCAL_MARCH, 18, 8, 0, 1 }, /* Sat 08:00 */
729e4ab9
A
1706};
1707static const TestWeekendDatesList testDates[] = {
2ca993e8
A
1708 { "en_US", weekendDates_en_US, UPRV_LENGTHOF(weekendDates_en_US) },
1709 { "ar_OM", weekendDates_ar_OM, UPRV_LENGTHOF(weekendDates_ar_OM) },
729e4ab9
A
1710};
1711
1712typedef struct {
1713 UCalendarDaysOfWeek dayOfWeek;
1714 UCalendarWeekdayType dayType;
1715 int32_t transition; /* transition time if dayType is UCAL_WEEKEND_ONSET or UCAL_WEEKEND_CEASE; else must be 0 */
1716} TestDaysOfWeek;
1717typedef struct {
1718 const char * locale;
1719 const TestDaysOfWeek * days;
1720 int32_t numDays;
1721} TestDaysOfWeekList;
1722
1723static const TestDaysOfWeek daysOfWeek_en_US[] = {
1724 { UCAL_MONDAY, UCAL_WEEKDAY, 0 },
1725 { UCAL_FRIDAY, UCAL_WEEKDAY, 0 },
1726 { UCAL_SATURDAY, UCAL_WEEKEND, 0 },
51004dcb 1727 { UCAL_SUNDAY, UCAL_WEEKEND, 0 },
729e4ab9 1728};
b331163b 1729static const TestDaysOfWeek daysOfWeek_ar_OM[] = { /* Friday:Saturday */
729e4ab9 1730 { UCAL_WEDNESDAY,UCAL_WEEKDAY, 0 },
b331163b 1731 { UCAL_THURSDAY, UCAL_WEEKDAY, 0 },
51004dcb 1732 { UCAL_FRIDAY, UCAL_WEEKEND, 0 },
b331163b 1733 { UCAL_SATURDAY, UCAL_WEEKEND, 0 },
51004dcb
A
1734};
1735static const TestDaysOfWeek daysOfWeek_hi_IN[] = { /* Sunday only */
1736 { UCAL_MONDAY, UCAL_WEEKDAY, 0 },
1737 { UCAL_FRIDAY, UCAL_WEEKDAY, 0 },
1738 { UCAL_SATURDAY, UCAL_WEEKDAY, 0 },
1739 { UCAL_SUNDAY, UCAL_WEEKEND, 0 },
729e4ab9
A
1740};
1741static const TestDaysOfWeekList testDays[] = {
2ca993e8
A
1742 { "en_US", daysOfWeek_en_US, UPRV_LENGTHOF(daysOfWeek_en_US) },
1743 { "ar_OM", daysOfWeek_ar_OM, UPRV_LENGTHOF(daysOfWeek_ar_OM) },
1744 { "hi_IN", daysOfWeek_hi_IN, UPRV_LENGTHOF(daysOfWeek_hi_IN) },
1745 { "en_US@rg=OMZZZZ", daysOfWeek_ar_OM, UPRV_LENGTHOF(daysOfWeek_ar_OM) },
1746 { "hi@rg=USZZZZ", daysOfWeek_en_US, UPRV_LENGTHOF(daysOfWeek_en_US) },
729e4ab9
A
1747};
1748
1749static const UChar logDateFormat[] = { 0x0045,0x0045,0x0045,0x0020,0x004D,0x004D,0x004D,0x0020,0x0064,0x0064,0x0020,0x0079,
1750 0x0079,0x0079,0x0079,0x0020,0x0047,0x0020,0x0048,0x0048,0x003A,0x006D,0x006D,0x003A,
1751 0x0073,0x0073,0x002E,0x0053,0x0053,0x0053,0 }; /* "EEE MMM dd yyyy G HH:mm:ss.SSS" */
2ca993e8 1752enum { kFormattedDateMax = 2*UPRV_LENGTHOF(logDateFormat) };
729e4ab9
A
1753
1754static void TestWeekend() {
1755 const TestWeekendDatesList * testDatesPtr = testDates;
1756 const TestDaysOfWeekList * testDaysPtr = testDays;
1757 int32_t count, subCount;
1758
1759 UErrorCode fmtStatus = U_ZERO_ERROR;
1760 UDateFormat * fmt = udat_open(UDAT_NONE, UDAT_NONE, "en", NULL, 0, NULL, 0, &fmtStatus);
1761 if (U_SUCCESS(fmtStatus)) {
1762 udat_applyPattern(fmt, FALSE, logDateFormat, -1);
4388f060
A
1763 } else {
1764 log_data_err("Unable to create UDateFormat - %s\n", u_errorName(fmtStatus));
1765 return;
729e4ab9 1766 }
2ca993e8 1767 for (count = UPRV_LENGTHOF(testDates); count-- > 0; ++testDatesPtr) {
729e4ab9 1768 UErrorCode status = U_ZERO_ERROR;
51004dcb
A
1769 UCalendar * cal = ucal_open(NULL, 0, testDatesPtr->locale, UCAL_GREGORIAN, &status);
1770 log_verbose("locale: %s\n", testDatesPtr->locale);
1771 if (U_SUCCESS(status)) {
1772 const TestWeekendDates * weekendDatesPtr = testDatesPtr->dates;
1773 for (subCount = testDatesPtr->numDates; subCount--; ++weekendDatesPtr) {
1774 UDate dateToTest;
1775 UBool isWeekend;
1776 char fmtDateBytes[kFormattedDateMax] = "<could not format test date>"; /* initialize for failure */
1777
1778 ucal_clear(cal);
1779 ucal_setDateTime(cal, weekendDatesPtr->year, weekendDatesPtr->month, weekendDatesPtr->day,
1780 weekendDatesPtr->hour, 0, 0, &status);
1781 dateToTest = ucal_getMillis(cal, &status) + weekendDatesPtr->millisecOffset;
1782 isWeekend = ucal_isWeekend(cal, dateToTest, &status);
1783 if (U_SUCCESS(fmtStatus)) {
1784 UChar fmtDate[kFormattedDateMax];
1785 (void)udat_format(fmt, dateToTest, fmtDate, kFormattedDateMax, NULL, &fmtStatus);
1786 if (U_SUCCESS(fmtStatus)) {
1787 u_austrncpy(fmtDateBytes, fmtDate, kFormattedDateMax);
1788 fmtDateBytes[kFormattedDateMax-1] = 0;
1789 } else {
1790 fmtStatus = U_ZERO_ERROR;
1791 }
1792 }
1793 if ( U_FAILURE(status) ) {
1794 log_err("FAIL: locale %s date %s isWeekend() status %s\n", testDatesPtr->locale, fmtDateBytes, u_errorName(status) );
1795 status = U_ZERO_ERROR;
1796 } else if ( (isWeekend!=0) != (weekendDatesPtr->isWeekend!=0) ) {
1797 log_err("FAIL: locale %s date %s isWeekend %d, expected the opposite\n", testDatesPtr->locale, fmtDateBytes, isWeekend );
1798 } else {
1799 log_verbose("OK: locale %s date %s isWeekend %d\n", testDatesPtr->locale, fmtDateBytes, isWeekend );
1800 }
1801 }
1802 ucal_close(cal);
1803 } else {
1804 log_data_err("FAIL: ucal_open for locale %s failed: %s - (Are you missing data?)\n", testDatesPtr->locale, u_errorName(status) );
1805 }
1806 }
729e4ab9
A
1807 if (U_SUCCESS(fmtStatus)) {
1808 udat_close(fmt);
1809 }
1810
2ca993e8 1811 for (count = UPRV_LENGTHOF(testDays); count-- > 0; ++testDaysPtr) {
729e4ab9
A
1812 UErrorCode status = U_ZERO_ERROR;
1813 UCalendar * cal = ucal_open(NULL, 0, testDaysPtr->locale, UCAL_GREGORIAN, &status);
1814 log_verbose("locale: %s\n", testDaysPtr->locale);
1815 if (U_SUCCESS(status)) {
1816 const TestDaysOfWeek * daysOfWeekPtr = testDaysPtr->days;
1817 for (subCount = testDaysPtr->numDays; subCount--; ++daysOfWeekPtr) {
1818 int32_t transition = 0;
1819 UCalendarWeekdayType dayType = ucal_getDayOfWeekType(cal, daysOfWeekPtr->dayOfWeek, &status);
1820 if ( dayType == UCAL_WEEKEND_ONSET || dayType == UCAL_WEEKEND_CEASE ) {
1821 transition = ucal_getWeekendTransition(cal, daysOfWeekPtr->dayOfWeek, &status);
1822 }
1823 if ( U_FAILURE(status) ) {
51004dcb
A
1824 log_err("FAIL: locale %s DOW %d getDayOfWeekType() status %s\n", testDaysPtr->locale, daysOfWeekPtr->dayOfWeek, u_errorName(status) );
1825 status = U_ZERO_ERROR;
729e4ab9 1826 } else if ( dayType != daysOfWeekPtr->dayType || transition != daysOfWeekPtr->transition ) {
51004dcb 1827 log_err("FAIL: locale %s DOW %d type %d, expected %d\n", testDaysPtr->locale, daysOfWeekPtr->dayOfWeek, dayType, daysOfWeekPtr->dayType );
729e4ab9 1828 } else {
51004dcb 1829 log_verbose("OK: locale %s DOW %d type %d\n", testDaysPtr->locale, daysOfWeekPtr->dayOfWeek, dayType );
729e4ab9
A
1830 }
1831 }
1832 ucal_close(cal);
1833 } else {
1834 log_data_err("FAIL: ucal_open for locale %s failed: %s - (Are you missing data?)\n", testDaysPtr->locale, u_errorName(status) );
1835 }
1836 }
1837}
1838
1839/**
1840 * TestFieldDifference
1841 */
1842
1843typedef struct {
1844 const UChar * timezone;
1845 const char * locale;
1846 UDate start;
1847 UDate target;
51004dcb 1848 UBool progressive; /* TRUE to compute progressive difference for each field, FALSE to reset calendar after each call */
729e4ab9
A
1849 int32_t yDiff;
1850 int32_t MDiff;
1851 int32_t dDiff;
1852 int32_t HDiff;
1853 int32_t mDiff;
4388f060 1854 int32_t sDiff; /* 0x7FFFFFFF indicates overflow error expected */
729e4ab9
A
1855} TFDItem;
1856
1857static const UChar tzUSPacific[] = { 0x55,0x53,0x2F,0x50,0x61,0x63,0x69,0x66,0x69,0x63,0 }; /* "US/Pacific" */
4388f060 1858static const UChar tzGMT[] = { 0x47,0x4D,0x54,0 }; /* "GMT" */
729e4ab9
A
1859
1860static const TFDItem tfdItems[] = {
51004dcb 1861 /* timezone locale start target progres yDf MDf dDf HDf mDf sDf */
4388f060 1862 /* For these we compute the progressive difference for each field - not resetting the calendar after each call */
51004dcb
A
1863 { tzUSPacific, "en_US", 1267459800000.0, 1277772600000.0, TRUE, 0, 3, 27, 9, 40, 0 }, /* 2010-Mar-01 08:10 -> 2010-Jun-28 17:50 */
1864 { tzUSPacific, "en_US", 1267459800000.0, 1299089280000.0, TRUE, 1, 0, 1, 1, 58, 0 }, /* 2010-Mar-01 08:10 -> 2011-Mar-02 10:08 */
4388f060 1865 /* For these we compute the total difference for each field - resetting the calendar after each call */
51004dcb
A
1866 { tzGMT, "en_US", 0.0, 1073692800000.0, FALSE, 34, 408, 12427, 298248, 17894880, 1073692800 }, /* 1970-Jan-01 00:00 -> 2004-Jan-10 00:00 */
1867 { tzGMT, "en_US", 0.0, 1073779200000.0, FALSE, 34, 408, 12428, 298272, 17896320, 1073779200 }, /* 1970-Jan-01 00:00 -> 2004-Jan-11 00:00 */
1868 { tzGMT, "en_US", 0.0, 2147472000000.0, FALSE, 68, 816, 24855, 596520, 35791200, 2147472000 }, /* 1970-Jan-01 00:00 -> 2038-Jan-19 00:00 */
1869 { tzGMT, "en_US", 0.0, 2147558400000.0, FALSE, 68, 816, 24856, 596544, 35792640, 0x7FFFFFFF }, /* 1970-Jan-01 00:00 -> 2038-Jan-20 00:00, seconds diff overflow */
1870 { tzGMT, "en_US", 0.0, -1073692800000.0, FALSE, -34,-408,-12427,-298248,-17894880,-1073692800 }, /* 1970-Jan-01 00:00 -> 1935-Dec-24 00:00 */
1871 { tzGMT, "en_US", 0.0, -1073779200000.0, FALSE, -34,-408,-12428,-298272,-17896320,-1073779200 }, /* 1970-Jan-01 00:00 -> 1935-Dec-23 00:00 */
1872 /* check fwd/backward on either side of era boundary and across era boundary */
1873 { tzGMT, "en_US", -61978089600000.0,-61820409600000.0, FALSE, 4, 59, 1825, 43800, 2628000, 157680000 }, /* CE 5-Dec-31 00:00 -> CE 10-Dec-30 00:00 */
1874 { tzGMT, "en_US", -61820409600000.0,-61978089600000.0, FALSE, -4, -59, -1825, -43800, -2628000, -157680000 }, /* CE 10-Dec-30 00:00 -> CE 5-Dec-31 00:00 */
1875 { tzGMT, "en_US", -62451129600000.0,-62293449600000.0, FALSE, 4, 59, 1825, 43800, 2628000, 157680000 }, /* BCE 10-Jan-04 00:00 -> BCE 5-Jan-03 00:00 */
1876 { tzGMT, "en_US", -62293449600000.0,-62451129600000.0, FALSE, -4, -59, -1825, -43800, -2628000, -157680000 }, /* BCE 5-Jan-03 00:00 -> BCE 10-Jan-04 00:00 */
1877 { tzGMT, "en_US", -62293449600000.0,-61978089600000.0, FALSE, 9, 119, 3650, 87600, 5256000, 315360000 }, /* BCE 5-Jan-03 00:00 -> CE 5-Dec-31 00:00 */
1878 { tzGMT, "en_US", -61978089600000.0,-62293449600000.0, FALSE, -9,-119, -3650, -87600, -5256000, -315360000 }, /* CE 5-Dec-31 00:00 -> BCE 5-Jan-03 00:00 */
1879 { tzGMT, "en@calendar=roc", -1672704000000.0, -1515024000000.0, FALSE, 4, 59, 1825, 43800, 2628000, 157680000 }, /* MG 5-Dec-30 00:00 -> MG 10-Dec-29 00:00 */
1880 { tzGMT, "en@calendar=roc", -1515024000000.0, -1672704000000.0, FALSE, -4, -59, -1825, -43800, -2628000, -157680000 }, /* MG 10-Dec-29 00:00 -> MG 5-Dec-30 00:00 */
1881 { tzGMT, "en@calendar=roc", -2145744000000.0, -1988064000000.0, FALSE, 4, 59, 1825, 43800, 2628000, 157680000 }, /* BMG 10-Jan-03 00:00 -> BMG 5-Jan-02 00:00 */
1882 { tzGMT, "en@calendar=roc", -1988064000000.0, -2145744000000.0, FALSE, -4, -59, -1825, -43800, -2628000, -157680000 }, /* BMG 5-Jan-02 00:00 -> BMG 10-Jan-03 00:00 */
1883 { tzGMT, "en@calendar=roc", -1988064000000.0, -1672704000000.0, FALSE, 9, 119, 3650, 87600, 5256000, 315360000 }, /* BMG 5-Jan-02 00:00 -> MG 5-Dec-30 00:00 */
1884 { tzGMT, "en@calendar=roc", -1672704000000.0, -1988064000000.0, FALSE, -9,-119, -3650, -87600, -5256000, -315360000 }, /* MG 5-Dec-30 00:00 -> BMG 5-Jan-02 00:00 */
1885 { tzGMT, "en@calendar=coptic",-53026531200000.0,-52868851200000.0, FALSE, 4, 64, 1825, 43800, 2628000, 157680000 }, /* Er1 5-Nas-05 00:00 -> Er1 10-Nas-04 00:00 */
1886 { tzGMT, "en@calendar=coptic",-52868851200000.0,-53026531200000.0, FALSE, -4, -64, -1825, -43800, -2628000, -157680000 }, /* Er1 10-Nas-04 00:00 -> Er1 5-Nas-05 00:00 */
1887 { tzGMT, "en@calendar=coptic",-53499571200000.0,-53341891200000.0, FALSE, 4, 64, 1825, 43800, 2628000, 157680000 }, /* Er0 10-Tou-04 00:00 -> Er0 5-Tou-02 00:00 */
1888 { tzGMT, "en@calendar=coptic",-53341891200000.0,-53499571200000.0, FALSE, -4, -64, -1825, -43800, -2628000, -157680000 }, /* Er0 5-Tou-02 00:00 -> Er0 10-Tou-04 00:00 */
1889 { tzGMT, "en@calendar=coptic",-53341891200000.0,-53026531200000.0, FALSE, 9, 129, 3650, 87600, 5256000, 315360000 }, /* Er0 5-Tou-02 00:00 -> Er1 5-Nas-05 00:00 */
1890 { tzGMT, "en@calendar=coptic",-53026531200000.0,-53341891200000.0, FALSE, -9,-129, -3650, -87600, -5256000, -315360000 }, /* Er1 5-Nas-05 00:00 -> Er0 5-Tou-02 00:00 */
1891 { NULL, NULL, 0.0, 0.0, FALSE, 0, 0, 0, 0, 0, 0 } /* terminator */
729e4ab9
A
1892};
1893
1894void TestFieldDifference() {
1895 const TFDItem * tfdItemPtr;
1896 for (tfdItemPtr = tfdItems; tfdItemPtr->timezone != NULL; tfdItemPtr++) {
1897 UErrorCode status = U_ZERO_ERROR;
1898 UCalendar* ucal = ucal_open(tfdItemPtr->timezone, -1, tfdItemPtr->locale, UCAL_DEFAULT, &status);
1899 if (U_FAILURE(status)) {
1900 log_err("FAIL: for locale \"%s\", ucal_open had status %s\n", tfdItemPtr->locale, u_errorName(status) );
1901 } else {
4388f060 1902 int32_t yDf, MDf, dDf, HDf, mDf, sDf;
51004dcb 1903 if (tfdItemPtr->progressive) {
4388f060
A
1904 ucal_setMillis(ucal, tfdItemPtr->start, &status);
1905 yDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_YEAR, &status);
1906 MDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_MONTH, &status);
1907 dDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_DATE, &status);
1908 HDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_HOUR, &status);
1909 mDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_MINUTE, &status);
1910 sDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_SECOND, &status);
1911 if (U_FAILURE(status)) {
1912 log_err("FAIL: for locale \"%s\", start %.1f, target %.1f, ucal_setMillis or ucal_getFieldDifference had status %s\n",
1913 tfdItemPtr->locale, tfdItemPtr->start, tfdItemPtr->target, u_errorName(status) );
1914 } else if ( yDf != tfdItemPtr->yDiff ||
1915 MDf != tfdItemPtr->MDiff ||
1916 dDf != tfdItemPtr->dDiff ||
1917 HDf != tfdItemPtr->HDiff ||
1918 mDf != tfdItemPtr->mDiff ||
1919 sDf != tfdItemPtr->sDiff ) {
1920 log_data_err("FAIL: for locale \"%s\", start %.1f, target %.1f, expected y-M-d-H-m-s progressive diffs %d-%d-%d-%d-%d-%d, got %d-%d-%d-%d-%d-%d\n",
1921 tfdItemPtr->locale, tfdItemPtr->start, tfdItemPtr->target,
1922 tfdItemPtr->yDiff, tfdItemPtr->MDiff, tfdItemPtr->dDiff, tfdItemPtr->HDiff, tfdItemPtr->mDiff, tfdItemPtr->sDiff,
1923 yDf, MDf, dDf, HDf, mDf, sDf);
1924 }
1925 } else {
1926 ucal_setMillis(ucal, tfdItemPtr->start, &status);
1927 yDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_YEAR, &status);
1928 ucal_setMillis(ucal, tfdItemPtr->start, &status);
1929 MDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_MONTH, &status);
1930 ucal_setMillis(ucal, tfdItemPtr->start, &status);
1931 dDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_DATE, &status);
1932 ucal_setMillis(ucal, tfdItemPtr->start, &status);
1933 HDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_HOUR, &status);
1934 ucal_setMillis(ucal, tfdItemPtr->start, &status);
1935 mDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_MINUTE, &status);
1936 if (U_FAILURE(status)) {
1937 log_err("FAIL: for locale \"%s\", start %.1f, target %.1f, ucal_setMillis or ucal_getFieldDifference (y-M-d-H-m) had status %s\n",
1938 tfdItemPtr->locale, tfdItemPtr->start, tfdItemPtr->target, u_errorName(status) );
1939 } else if ( yDf != tfdItemPtr->yDiff ||
1940 MDf != tfdItemPtr->MDiff ||
1941 dDf != tfdItemPtr->dDiff ||
1942 HDf != tfdItemPtr->HDiff ||
1943 mDf != tfdItemPtr->mDiff ) {
1944 log_data_err("FAIL: for locale \"%s\", start %.1f, target %.1f, expected y-M-d-H-m total diffs %d-%d-%d-%d-%d, got %d-%d-%d-%d-%d\n",
1945 tfdItemPtr->locale, tfdItemPtr->start, tfdItemPtr->target,
1946 tfdItemPtr->yDiff, tfdItemPtr->MDiff, tfdItemPtr->dDiff, tfdItemPtr->HDiff, tfdItemPtr->mDiff,
1947 yDf, MDf, dDf, HDf, mDf);
1948 }
1949 ucal_setMillis(ucal, tfdItemPtr->start, &status);
1950 sDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_SECOND, &status);
1951 if (tfdItemPtr->sDiff != 0x7FFFFFFF) {
1952 if (U_FAILURE(status)) {
1953 log_err("FAIL: for locale \"%s\", start %.1f, target %.1f, ucal_setMillis or ucal_getFieldDifference (seconds) had status %s\n",
1954 tfdItemPtr->locale, tfdItemPtr->start, tfdItemPtr->target, u_errorName(status) );
1955 } else if (sDf != tfdItemPtr->sDiff) {
1956 log_data_err("FAIL: for locale \"%s\", start %.1f, target %.1f, expected seconds progressive diff %d, got %d\n",
1957 tfdItemPtr->locale, tfdItemPtr->start, tfdItemPtr->target, tfdItemPtr->sDiff, sDf);
1958 }
1959 } else if (!U_FAILURE(status)) {
1960 log_err("FAIL: for locale \"%s\", start %.1f, target %.1f, for ucal_getFieldDifference (seconds) expected overflow error, got none\n",
1961 tfdItemPtr->locale, tfdItemPtr->start, tfdItemPtr->target );
1962 }
729e4ab9
A
1963 }
1964 ucal_close(ucal);
1965 }
1966 }
1967}
1968
4388f060
A
1969void TestAmbiguousWallTime() {
1970 UErrorCode status = U_ZERO_ERROR;
1971 UChar tzID[32];
1972 UCalendar* ucal;
1973 UDate t, expected;
1974
1975 u_uastrcpy(tzID, "America/New_York");
0f5d89e8 1976 ucal = ucal_open(tzID, -1, "en_US", UCAL_DEFAULT, &status);
4388f060
A
1977 if (U_FAILURE(status)) {
1978 log_err("FAIL: Failed to create a calendar");
1979 return;
1980 }
1981
1982 if (ucal_getAttribute(ucal, UCAL_REPEATED_WALL_TIME) != UCAL_WALLTIME_LAST) {
1983 log_err("FAIL: Default UCAL_REPEATED_WALL_TIME value is not UCAL_WALLTIME_LAST");
1984 }
1985
1986 if (ucal_getAttribute(ucal, UCAL_SKIPPED_WALL_TIME) != UCAL_WALLTIME_LAST) {
1987 log_err("FAIL: Default UCAL_SKIPPED_WALL_TIME value is not UCAL_WALLTIME_LAST");
1988 }
1989
1990 /* UCAL_WALLTIME_FIRST on US fall transition */
1991 ucal_setAttribute(ucal, UCAL_REPEATED_WALL_TIME, UCAL_WALLTIME_FIRST);
1992 ucal_clear(ucal);
1993 ucal_setDateTime(ucal, 2011, 11-1, 6, 1, 30, 0, &status);
1994 t = ucal_getMillis(ucal, &status);
1995 expected = 1320557400000.0; /* 2011-11-06T05:30:00Z */
1996 if (U_FAILURE(status)) {
1997 log_err("FAIL: Calculating time 2011-11-06 01:30:00 with UCAL_WALLTIME_FIRST - %s\n", u_errorName(status));
1998 status = U_ZERO_ERROR;
1999 } else if (t != expected) {
2000 log_data_err("FAIL: 2011-11-06 01:30:00 with UCAL_WALLTIME_FIRST - got: %f, expected: %f\n", t, expected);
2001 }
2002
2003 /* UCAL_WALLTIME_LAST on US fall transition */
2004 ucal_setAttribute(ucal, UCAL_REPEATED_WALL_TIME, UCAL_WALLTIME_LAST);
2005 ucal_clear(ucal);
2006 ucal_setDateTime(ucal, 2011, 11-1, 6, 1, 30, 0, &status);
2007 t = ucal_getMillis(ucal, &status);
2008 expected = 1320561000000.0; /* 2011-11-06T06:30:00Z */
2009 if (U_FAILURE(status)) {
2010 log_err("FAIL: Calculating time 2011-11-06 01:30:00 with UCAL_WALLTIME_LAST - %s\n", u_errorName(status));
2011 status = U_ZERO_ERROR;
2012 } else if (t != expected) {
2013 log_data_err("FAIL: 2011-11-06 01:30:00 with UCAL_WALLTIME_LAST - got: %f, expected: %f\n", t, expected);
2014 }
2015
2016 /* UCAL_WALLTIME_FIRST on US spring transition */
2017 ucal_setAttribute(ucal, UCAL_SKIPPED_WALL_TIME, UCAL_WALLTIME_FIRST);
2018 ucal_clear(ucal);
2019 ucal_setDateTime(ucal, 2011, 3-1, 13, 2, 30, 0, &status);
2020 t = ucal_getMillis(ucal, &status);
2021 expected = 1299997800000.0; /* 2011-03-13T06:30:00Z */
2022 if (U_FAILURE(status)) {
2023 log_err("FAIL: Calculating time 2011-03-13 02:30:00 with UCAL_WALLTIME_FIRST - %s\n", u_errorName(status));
2024 status = U_ZERO_ERROR;
2025 } else if (t != expected) {
2026 log_data_err("FAIL: 2011-03-13 02:30:00 with UCAL_WALLTIME_FIRST - got: %f, expected: %f\n", t, expected);
2027 }
2028
2029 /* UCAL_WALLTIME_LAST on US spring transition */
2030 ucal_setAttribute(ucal, UCAL_SKIPPED_WALL_TIME, UCAL_WALLTIME_LAST);
2031 ucal_clear(ucal);
2032 ucal_setDateTime(ucal, 2011, 3-1, 13, 2, 30, 0, &status);
2033 t = ucal_getMillis(ucal, &status);
2034 expected = 1300001400000.0; /* 2011-03-13T07:30:00Z */
2035 if (U_FAILURE(status)) {
2036 log_err("FAIL: Calculating time 2011-03-13 02:30:00 with UCAL_WALLTIME_LAST - %s\n", u_errorName(status));
2037 status = U_ZERO_ERROR;
2038 } else if (t != expected) {
2039 log_data_err("FAIL: 2011-03-13 02:30:00 with UCAL_WALLTIME_LAST - got: %f, expected: %f\n", t, expected);
2040 }
2041
2042 /* UCAL_WALLTIME_NEXT_VALID on US spring transition */
2043 ucal_setAttribute(ucal, UCAL_SKIPPED_WALL_TIME, UCAL_WALLTIME_NEXT_VALID);
2044 ucal_clear(ucal);
2045 ucal_setDateTime(ucal, 2011, 3-1, 13, 2, 30, 0, &status);
2046 t = ucal_getMillis(ucal, &status);
2047 expected = 1299999600000.0; /* 2011-03-13T07:00:00Z */
2048 if (U_FAILURE(status)) {
2049 log_err("FAIL: Calculating time 2011-03-13 02:30:00 with UCAL_WALLTIME_NEXT_VALID - %s\n", u_errorName(status));
2050 status = U_ZERO_ERROR;
2051 } else if (t != expected) {
2052 log_data_err("FAIL: 2011-03-13 02:30:00 with UCAL_WALLTIME_NEXT_VALID - got: %f, expected: %f\n", t, expected);
2053 }
2054
2055 /* non-lenient on US spring transition */
2056 ucal_setAttribute(ucal, UCAL_LENIENT, 0);
2057 ucal_clear(ucal);
2058 ucal_setDateTime(ucal, 2011, 3-1, 13, 2, 30, 0, &status);
2059 t = ucal_getMillis(ucal, &status);
2060 if (U_SUCCESS(status)) {
2061 /* must return error */
2062 log_data_err("FAIL: Non-lenient did not fail with 2011-03-13 02:30:00\n");
2063 status = U_ZERO_ERROR;
2064 }
2065
2066 ucal_close(ucal);
2067}
2068
51004dcb
A
2069/**
2070 * TestAddRollEra0AndEraBounds, for #9226
2071 */
2072
2073 typedef struct {
2074 const char * locale;
2075 UBool era0YearsGoBackwards; /* until we have API to get this, per #9393 */
2076 } EraTestItem;
2077
2078static const EraTestItem eraTestItems[] = {
2079 /* calendars with non-modern era 0 that goes backwards, max era == 1 */
2080 { "en@calendar=gregorian", TRUE },
2081 { "en@calendar=roc", TRUE },
2082 { "en@calendar=coptic", TRUE },
2083 /* calendars with non-modern era 0 that goes forwards, max era > 1 */
2084 { "en@calendar=japanese", FALSE },
2085 { "en@calendar=chinese", FALSE },
2086 /* calendars with non-modern era 0 that goes forwards, max era == 1 */
2087 { "en@calendar=ethiopic", FALSE },
2088 /* calendars with only one era = 0, forwards */
2089 { "en@calendar=buddhist", FALSE },
2090 { "en@calendar=hebrew", FALSE },
2091 { "en@calendar=islamic", FALSE },
2092 { "en@calendar=indian", FALSE },
2093 { "en@calendar=persian", FALSE },
2094 { "en@calendar=ethiopic-amete-alem", FALSE },
2095 { NULL, FALSE }
2096};
2097
2098static const UChar zoneGMT[] = { 0x47,0x4D,0x54,0 };
2099
2100void TestAddRollEra0AndEraBounds() {
2101 const EraTestItem * eraTestItemPtr;
2102 for (eraTestItemPtr = eraTestItems; eraTestItemPtr->locale != NULL; eraTestItemPtr++) {
2103 UErrorCode status = U_ZERO_ERROR;
2104 UCalendar *ucalTest = ucal_open(zoneGMT, -1, eraTestItemPtr->locale, UCAL_DEFAULT, &status);
2105 if ( U_SUCCESS(status) ) {
2106 int32_t yrBefore, yrAfter, yrMax, eraAfter, eraMax, eraNow;
2107
2108 status = U_ZERO_ERROR;
2109 ucal_clear(ucalTest);
2110 ucal_set(ucalTest, UCAL_YEAR, 2);
2111 ucal_set(ucalTest, UCAL_ERA, 0);
2112 yrBefore = ucal_get(ucalTest, UCAL_YEAR, &status);
2113 ucal_add(ucalTest, UCAL_YEAR, 1, &status);
2114 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2115 if (U_FAILURE(status)) {
2116 log_err("FAIL: set era 0 year 2 then add 1 year and get year for %s, error %s\n",
2117 eraTestItemPtr->locale, u_errorName(status));
2118 } else if ( (eraTestItemPtr->era0YearsGoBackwards && yrAfter>yrBefore) ||
2119 (!eraTestItemPtr->era0YearsGoBackwards && yrAfter<yrBefore) ) {
2120 log_err("FAIL: era 0 add 1 year does not move forward in time for %s\n", eraTestItemPtr->locale);
2121 }
2122
2123 status = U_ZERO_ERROR;
2124 ucal_clear(ucalTest);
2125 ucal_set(ucalTest, UCAL_YEAR, 2);
2126 ucal_set(ucalTest, UCAL_ERA, 0);
2127 yrBefore = ucal_get(ucalTest, UCAL_YEAR, &status);
2128 ucal_roll(ucalTest, UCAL_YEAR, 1, &status);
2129 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2130 if (U_FAILURE(status)) {
2131 log_err("FAIL: set era 0 year 2 then roll 1 year and get year for %s, error %s\n",
2132 eraTestItemPtr->locale, u_errorName(status));
2133 } else if ( (eraTestItemPtr->era0YearsGoBackwards && yrAfter>yrBefore) ||
2134 (!eraTestItemPtr->era0YearsGoBackwards && yrAfter<yrBefore) ) {
2135 log_err("FAIL: era 0 roll 1 year does not move forward in time for %s\n", eraTestItemPtr->locale);
2136 }
2137
2138 status = U_ZERO_ERROR;
2139 ucal_clear(ucalTest);
2140 ucal_set(ucalTest, UCAL_YEAR, 1);
2141 ucal_set(ucalTest, UCAL_ERA, 0);
2142 if (eraTestItemPtr->era0YearsGoBackwards) {
2143 ucal_roll(ucalTest, UCAL_YEAR, 1, &status); /* roll forward in time to era 0 boundary */
2144 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2145 eraAfter = ucal_get(ucalTest, UCAL_ERA, &status);
2146 if (U_FAILURE(status)) {
2147 log_err("FAIL: set era 0 year 1 then roll 1 year and get year,era for %s, error %s\n",
2148 eraTestItemPtr->locale, u_errorName(status));
2149 /* all calendars with era0YearsGoBackwards have "unbounded" era0 year values, so we should pin at yr 1 */
2150 } else if (eraAfter != 0 || yrAfter != 1) {
2151 log_err("FAIL: era 0 roll 1 year from year 1 does not stay within era or pin to year 1 for %s (get era %d year %d)\n",
2152 eraTestItemPtr->locale, eraAfter, yrAfter);
2153 }
2154 } else {
2155 /* roll backward in time to where era 0 years go negative, except for the Chinese
2156 calendar, which uses negative eras instead of having years outside the range 1-60 */
2157 const char * calType = ucal_getType(ucalTest, &status);
2158 ucal_roll(ucalTest, UCAL_YEAR, -2, &status);
2159 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2160 eraAfter = ucal_get(ucalTest, UCAL_ERA, &status);
2161 if (U_FAILURE(status)) {
2162 log_err("FAIL: set era 0 year 1 then roll -2 years and get year,era for %s, error %s\n",
2163 eraTestItemPtr->locale, u_errorName(status));
2164 } else if ( uprv_strcmp(calType,"chinese")!=0 && (eraAfter != 0 || yrAfter != -1) ) {
2165 log_err("FAIL: era 0 roll -2 years from year 1 does not stay within era or produce year -1 for %s (get era %d year %d)\n",
2166 eraTestItemPtr->locale, eraAfter, yrAfter);
2167 }
2168 }
2169
2170 status = U_ZERO_ERROR;
2171 ucal_clear(ucalTest);
2172 {
2173 int32_t eraMin = ucal_getLimit(ucalTest, UCAL_ERA, UCAL_MINIMUM, &status);
2174 const char * calType = ucal_getType(ucalTest, &status);
2175 if (eraMin != 0 && uprv_strcmp(calType, "chinese") != 0) {
2176 log_err("FAIL: ucal_getLimit returns minimum era %d (should be 0) for calType %s, error %s\n", eraMin, calType, u_errorName(status));
2177 }
2178 }
2179
2180 status = U_ZERO_ERROR;
2181 ucal_clear(ucalTest);
2182 ucal_set(ucalTest, UCAL_YEAR, 1);
2183 ucal_set(ucalTest, UCAL_ERA, 0);
2184 eraMax = ucal_getLimit(ucalTest, UCAL_ERA, UCAL_MAXIMUM, &status);
2185 if ( U_SUCCESS(status) && eraMax > 0 ) {
2186 /* try similar tests for era 1 (if calendar has it), in which years always go forward */
2187 status = U_ZERO_ERROR;
2188 ucal_clear(ucalTest);
2189 ucal_set(ucalTest, UCAL_YEAR, 2);
2190 ucal_set(ucalTest, UCAL_ERA, 1);
2191 yrBefore = ucal_get(ucalTest, UCAL_YEAR, &status);
2192 ucal_add(ucalTest, UCAL_YEAR, 1, &status);
2193 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2194 if (U_FAILURE(status)) {
2195 log_err("FAIL: set era 1 year 2 then add 1 year and get year for %s, error %s\n",
2196 eraTestItemPtr->locale, u_errorName(status));
2197 } else if ( yrAfter<yrBefore ) {
2198 log_err("FAIL: era 1 add 1 year does not move forward in time for %s\n", eraTestItemPtr->locale);
2199 }
2200
2201 status = U_ZERO_ERROR;
2202 ucal_clear(ucalTest);
2203 ucal_set(ucalTest, UCAL_YEAR, 2);
2204 ucal_set(ucalTest, UCAL_ERA, 1);
2205 yrBefore = ucal_get(ucalTest, UCAL_YEAR, &status);
2206 ucal_roll(ucalTest, UCAL_YEAR, 1, &status);
2207 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2208 if (U_FAILURE(status)) {
2209 log_err("FAIL: set era 1 year 2 then roll 1 year and get year for %s, error %s\n",
2210 eraTestItemPtr->locale, u_errorName(status));
2211 } else if ( yrAfter<yrBefore ) {
2212 log_err("FAIL: era 1 roll 1 year does not move forward in time for %s\n", eraTestItemPtr->locale);
2213 }
2214
2215 status = U_ZERO_ERROR;
2216 ucal_clear(ucalTest);
2217 ucal_set(ucalTest, UCAL_YEAR, 1);
2218 ucal_set(ucalTest, UCAL_ERA, 1);
2219 yrMax = ucal_getLimit(ucalTest, UCAL_YEAR, UCAL_ACTUAL_MAXIMUM, &status); /* max year value for era 1 */
2220 ucal_roll(ucalTest, UCAL_YEAR, -1, &status); /* roll down which should pin or wrap to end */
2221 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2222 eraAfter = ucal_get(ucalTest, UCAL_ERA, &status);
2223 if (U_FAILURE(status)) {
2224 log_err("FAIL: set era 1 year 1 then roll -1 year and get year,era for %s, error %s\n",
2225 eraTestItemPtr->locale, u_errorName(status));
2226 /* if yrMax is reasonable we should wrap to that, else we should pin at yr 1 */
2227 } else if (yrMax >= 32768) {
2228 if (eraAfter != 1 || yrAfter != 1) {
2229 log_err("FAIL: era 1 roll -1 year from year 1 does not stay within era or pin to year 1 for %s (get era %d year %d)\n",
2230 eraTestItemPtr->locale, eraAfter, yrAfter);
2231 }
2232 } else if (eraAfter != 1 || yrAfter != yrMax) {
2233 log_err("FAIL: era 1 roll -1 year from year 1 does not stay within era or wrap to year %d for %s (get era %d year %d)\n",
2234 yrMax, eraTestItemPtr->locale, eraAfter, yrAfter);
2235 } else {
2236 /* now roll up which should wrap to beginning */
2237 ucal_roll(ucalTest, UCAL_YEAR, 1, &status); /* now roll up which should wrap to beginning */
2238 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2239 eraAfter = ucal_get(ucalTest, UCAL_ERA, &status);
2240 if (U_FAILURE(status)) {
2241 log_err("FAIL: era 1 roll 1 year from end and get year,era for %s, error %s\n",
2242 eraTestItemPtr->locale, u_errorName(status));
2243 } else if (eraAfter != 1 || yrAfter != 1) {
2244 log_err("FAIL: era 1 roll 1 year from year %d does not stay within era or wrap to year 1 for %s (get era %d year %d)\n",
2245 yrMax, eraTestItemPtr->locale, eraAfter, yrAfter);
2246 }
2247 }
2248
2249 /* if current era > 1, try the same roll tests for current era */
2250 ucal_setMillis(ucalTest, ucal_getNow(), &status);
2251 eraNow = ucal_get(ucalTest, UCAL_ERA, &status);
2252 if ( U_SUCCESS(status) && eraNow > 1 ) {
2253 status = U_ZERO_ERROR;
2254 ucal_clear(ucalTest);
2255 ucal_set(ucalTest, UCAL_YEAR, 1);
2256 ucal_set(ucalTest, UCAL_ERA, eraNow);
2257 yrMax = ucal_getLimit(ucalTest, UCAL_YEAR, UCAL_ACTUAL_MAXIMUM, &status); /* max year value for this era */
2258 ucal_roll(ucalTest, UCAL_YEAR, -1, &status);
2259 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2260 eraAfter = ucal_get(ucalTest, UCAL_ERA, &status);
2261 if (U_FAILURE(status)) {
2262 log_err("FAIL: set era %d year 1 then roll -1 year and get year,era for %s, error %s\n",
2263 eraNow, eraTestItemPtr->locale, u_errorName(status));
2264 /* if yrMax is reasonable we should wrap to that, else we should pin at yr 1 */
2265 } else if (yrMax >= 32768) {
2266 if (eraAfter != eraNow || yrAfter != 1) {
2267 log_err("FAIL: era %d roll -1 year from year 1 does not stay within era or pin to year 1 for %s (get era %d year %d)\n",
2268 eraNow, eraTestItemPtr->locale, eraAfter, yrAfter);
2269 }
2270 } else if (eraAfter != eraNow || yrAfter != yrMax) {
2271 log_err("FAIL: era %d roll -1 year from year 1 does not stay within era or wrap to year %d for %s (get era %d year %d)\n",
2272 eraNow, yrMax, eraTestItemPtr->locale, eraAfter, yrAfter);
2273 } else {
2274 /* now roll up which should wrap to beginning */
2275 ucal_roll(ucalTest, UCAL_YEAR, 1, &status); /* now roll up which should wrap to beginning */
2276 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2277 eraAfter = ucal_get(ucalTest, UCAL_ERA, &status);
2278 if (U_FAILURE(status)) {
2279 log_err("FAIL: era %d roll 1 year from end and get year,era for %s, error %s\n",
2280 eraNow, eraTestItemPtr->locale, u_errorName(status));
2281 } else if (eraAfter != eraNow || yrAfter != 1) {
2282 log_err("FAIL: era %d roll 1 year from year %d does not stay within era or wrap to year 1 for %s (get era %d year %d)\n",
2283 eraNow, yrMax, eraTestItemPtr->locale, eraAfter, yrAfter);
2284 }
2285 }
2286 }
2287 }
2288
2289 ucal_close(ucalTest);
2290 } else {
2291 log_data_err("FAIL: ucal_open fails for zone GMT, locale %s, UCAL_DEFAULT\n", eraTestItemPtr->locale);
2292 }
2293 }
2294}
2295
2296/**
2297 * TestGetTZTransition, for #9606
2298 */
2299
2300typedef struct {
2301 const char *descrip; /* test description */
2302 const UChar * zoneName; /* pointer to zero-terminated zone name */
2303 int32_t year; /* starting point for test is gregorian calendar noon on day specified by y,M,d here */
2304 int32_t month;
2305 int32_t day;
2306 UBool hasPrev; /* does it have a previous transition from starting point? If so we test inclusive from that */
2307 UBool hasNext; /* does it have a next transition from starting point? If so we test inclusive from that */
2308} TZTransitionItem;
2309
2310/* have zoneGMT above */
2311static const UChar zoneUSPacific[] = { 0x55,0x53,0x2F,0x50,0x61,0x63,0x69,0x66,0x69,0x63,0 }; /* "US/Pacific" */
2312static const UChar zoneCairo[] = { 0x41,0x66,0x72,0x69,0x63,0x61,0x2F,0x43,0x61,0x69,0x72,0x6F,0 }; /* "Africa/Cairo", DST cancelled since 2011 */
2313static const UChar zoneIceland[] = { 0x41,0x74,0x6C,0x61,0x6E,0x74,0x69,0x63,0x2F,0x52,0x65,0x79,0x6B,0x6A,0x61,0x76,0x69,0x6B,0 }; /* "Atlantic/Reykjavik", always on DST (since when?) */
2314
2315static const TZTransitionItem tzTransitionItems[] = {
2316 { "USPacific mid 2012", zoneUSPacific, 2012, UCAL_JULY, 1, TRUE , TRUE },
2317 { "USPacific mid 100", zoneUSPacific, 100, UCAL_JULY, 1, FALSE, TRUE }, /* no transitions before 100 CE... */
57a6839d 2318 { "Cairo mid 2012", zoneCairo, 2012, UCAL_JULY, 1, TRUE , TRUE }, /* DST cancelled since 2011 (Changed since 2014c) */
51004dcb
A
2319 { "Iceland mid 2012", zoneIceland, 2012, UCAL_JULY, 1, TRUE , FALSE }, /* always on DST */
2320 { NULL, NULL, 0, 0, 0, FALSE, FALSE } /* terminator */
2321};
2322
2323void TestGetTZTransition() {
2324 UErrorCode status = U_ZERO_ERROR;
2325 UCalendar * ucal = ucal_open(zoneGMT, -1, "en", UCAL_GREGORIAN, &status);
2326 if ( U_SUCCESS(status) ) {
2327 const TZTransitionItem * itemPtr;
2328 for (itemPtr = tzTransitionItems; itemPtr->descrip != NULL; itemPtr++) {
2329 UDate curMillis;
2330 ucal_setTimeZone(ucal, itemPtr->zoneName, -1, &status);
2331 ucal_setDateTime(ucal, itemPtr->year, itemPtr->month, itemPtr->day, 12, 0, 0, &status);
2332 curMillis = ucal_getMillis(ucal, &status);
2333 (void)curMillis; /* Suppress set but not used warning. */
2334 if ( U_SUCCESS(status) ) {
2335 UDate transition1, transition2;
2336 UBool result;
2337
2338 result = ucal_getTimeZoneTransitionDate(ucal, UCAL_TZ_TRANSITION_PREVIOUS, &transition1, &status);
2339 if (U_FAILURE(status) || result != itemPtr->hasPrev) {
2340 log_data_err("FAIL: %s ucal_getTimeZoneTransitionDate prev status %s, expected result %d but got %d\n",
2341 itemPtr->descrip, u_errorName(status), itemPtr->hasPrev, result);
2342 } else if (result) {
2343 ucal_setMillis(ucal, transition1, &status);
2344 result = ucal_getTimeZoneTransitionDate(ucal, UCAL_TZ_TRANSITION_PREVIOUS_INCLUSIVE, &transition2, &status);
2345 if (U_FAILURE(status) || !result || transition2 != transition1) {
2346 log_err("FAIL: %s ucal_getTimeZoneTransitionDate prev_inc status %s, result %d, expected date %.1f but got %.1f\n",
2347 itemPtr->descrip, u_errorName(status), result, transition1, transition2);
2348 }
2349 }
2350 status = U_ZERO_ERROR;
2351
2352 result = ucal_getTimeZoneTransitionDate(ucal, UCAL_TZ_TRANSITION_NEXT, &transition1, &status);
2353 if (U_FAILURE(status) || result != itemPtr->hasNext) {
2354 log_data_err("FAIL: %s ucal_getTimeZoneTransitionDate next status %s, expected result %d but got %d\n",
2355 itemPtr->descrip, u_errorName(status), itemPtr->hasNext, result);
2356 } else if (result) {
2357 ucal_setMillis(ucal, transition1, &status);
2358 result = ucal_getTimeZoneTransitionDate(ucal, UCAL_TZ_TRANSITION_NEXT_INCLUSIVE, &transition2, &status);
2359 if (U_FAILURE(status) || !result || transition2 != transition1) {
2360 log_err("FAIL: %s ucal_getTimeZoneTransitionDate next_inc status %s, result %d, expected date %.1f but got %.1f\n",
2361 itemPtr->descrip, u_errorName(status), result, transition1, transition2);
2362 }
2363 }
2364 status = U_ZERO_ERROR;
2365 } else {
2366 log_data_err("FAIL setup: can't setup calendar for %s, status %s\n",
2367 itemPtr->descrip, u_errorName(status));
2368 status = U_ZERO_ERROR;
2369 }
2370 }
2371 ucal_close(ucal);
2372 } else {
2373 log_data_err("FAIL setup: ucal_open status %s\n", u_errorName(status));
2374 }
2375}
2376
57a6839d
A
2377static const UChar winEastern[] = /* Eastern Standard Time */
2378 {0x45,0x61,0x73,0x74,0x65,0x72,0x6E,0x20,0x53,0x74,0x61,0x6E,0x64,0x61,0x72,0x64,0x20,0x54,0x69,0x6D,0x65,0x00};
2379
2380static const UChar tzNewYork[] = /* America/New_York */
2381 {0x41,0x6D,0x65,0x72,0x69,0x63,0x61,0x2F,0x4E,0x65,0x77,0x5F,0x59,0x6F,0x72,0x6B,0x00};
2382static const UChar tzTronto[] = /* America/Toronto */
2383 {0x41,0x6D,0x65,0x72,0x69,0x63,0x61,0x2F,0x54,0x6F,0x72,0x6F,0x6E,0x74,0x6F,0x00};
2384
2385static const UChar sBogus[] = /* Bogus */
2386 {0x42,0x6F,0x67,0x75,0x73,0x00};
2387
2388void TestGetWindowsTimeZoneID() {
2389 UErrorCode status;
2390 UChar winID[64];
2391 int32_t len;
2392
2393 {
2394 status = U_ZERO_ERROR;
2ca993e8 2395 len = ucal_getWindowsTimeZoneID(tzNewYork, u_strlen(tzNewYork), winID, UPRV_LENGTHOF(winID), &status);
57a6839d
A
2396 if (U_FAILURE(status)) {
2397 log_data_err("FAIL: Windows ID for America/New_York, status %s\n", u_errorName(status));
2398 } else if (len != u_strlen(winEastern) || u_strncmp(winID, winEastern, len) != 0) {
2399 log_data_err("FAIL: Windows ID for America/New_York\n");
2400 }
2401 }
2402 {
2403 status = U_ZERO_ERROR;
2ca993e8 2404 len = ucal_getWindowsTimeZoneID(tzTronto, u_strlen(tzTronto), winID, UPRV_LENGTHOF(winID), &status);
57a6839d
A
2405 if (U_FAILURE(status)) {
2406 log_data_err("FAIL: Windows ID for America/Toronto, status %s\n", u_errorName(status));
2407 } else if (len != u_strlen(winEastern) || u_strncmp(winID, winEastern, len) != 0) {
2408 log_data_err("FAIL: Windows ID for America/Toronto\n");
2409 }
2410 }
2411 {
2412 status = U_ZERO_ERROR;
2ca993e8 2413 len = ucal_getWindowsTimeZoneID(sBogus, u_strlen(sBogus), winID, UPRV_LENGTHOF(winID), &status);
57a6839d
A
2414 if (U_FAILURE(status)) {
2415 log_data_err("FAIL: Windows ID for Bogus, status %s\n", u_errorName(status));
2416 } else if (len != 0) {
2417 log_data_err("FAIL: Windows ID for Bogus\n");
2418 }
2419 }
2420}
2421
2422void TestGetTimeZoneIDByWindowsID() {
2423 UErrorCode status;
2424 UChar tzID[64];
2425 int32_t len;
2426
2427 {
2428 status = U_ZERO_ERROR;
2ca993e8 2429 len = ucal_getTimeZoneIDForWindowsID(winEastern, -1, NULL, tzID, UPRV_LENGTHOF(tzID), &status);
57a6839d
A
2430 if (U_FAILURE(status)) {
2431 log_data_err("FAIL: TZ ID for Eastern Standard Time, status %s\n", u_errorName(status));
2432 } else if (len != u_strlen(tzNewYork) || u_strncmp(tzID, tzNewYork, len) != 0) {
2433 log_err("FAIL: TZ ID for Eastern Standard Time\n");
2434 }
2435 }
2436 {
2437 status = U_ZERO_ERROR;
2ca993e8 2438 len = ucal_getTimeZoneIDForWindowsID(winEastern, u_strlen(winEastern), "US", tzID, UPRV_LENGTHOF(tzID), &status);
57a6839d
A
2439 if (U_FAILURE(status)) {
2440 log_data_err("FAIL: TZ ID for Eastern Standard Time - US, status %s\n", u_errorName(status));
2441 } else if (len != u_strlen(tzNewYork) || u_strncmp(tzID, tzNewYork, len) != 0) {
2442 log_err("FAIL: TZ ID for Eastern Standard Time - US\n");
2443 }
2444 }
2445 {
2446 status = U_ZERO_ERROR;
2ca993e8 2447 len = ucal_getTimeZoneIDForWindowsID(winEastern, u_strlen(winEastern), "CA", tzID, UPRV_LENGTHOF(tzID), &status);
57a6839d
A
2448 if (U_FAILURE(status)) {
2449 log_data_err("FAIL: TZ ID for Eastern Standard Time - CA, status %s\n", u_errorName(status));
2450 } else if (len != u_strlen(tzTronto) || u_strncmp(tzID, tzTronto, len) != 0) {
2451 log_err("FAIL: TZ ID for Eastern Standard Time - CA\n");
2452 }
2453 }
2454
2455 {
2456 status = U_ZERO_ERROR;
2ca993e8 2457 len = ucal_getTimeZoneIDForWindowsID(sBogus, -1, NULL, tzID, UPRV_LENGTHOF(tzID), &status);
57a6839d
A
2458 if (U_FAILURE(status)) {
2459 log_data_err("FAIL: TZ ID for Bogus, status %s\n", u_errorName(status));
2460 } else if (len != 0) {
2461 log_err("FAIL: TZ ID for Bogus\n");
2462 }
2463 }
2464}
2465
b331163b
A
2466typedef struct {
2467 const char * localeWithCal;
2468 UDate clearDate;
2469} LocaleWithCalendarAndClearDate;
2470
2471static const LocaleWithCalendarAndClearDate calAndClearDates[] = {
2472// ucal_clear sets era grego
2473// this date for GMT in cal date
2474 { "en@calendar=gregorian", 0.0 }, // 1 1970-01-01
2475 { "en@calendar=iso8601", 0.0 }, // 1 1970-01-01
2476 { "en@calendar=buddhist", 0.0 }, // 0 1970-01-01
2477 { "en@calendar=japanese", 0.0 }, // 234 1970-01-01
2478 { "en@calendar=roc", 0.0 }, // 1 1970-01-01
2479 { "en@calendar=chinese", 444528000000.0 }, // 78 1984-02-02
2480 { "en@calendar=dangi", 444528000000.0 }, // 78 1984-02-02
2481 { "en@calendar=coptic", -53184211200000.0 }, // 1 284-08-29
2482 { "en@calendar=ethiopic", -61894108800000.0 }, // 1 8-08-29
2483 { "en@calendar=ethiopic-amete-alem", -61894108800000.0 }, // 0 8-08-29
2484 { "en@calendar=hebrew", -180799776000000.0 }, // 0 3761-10-07
2485 { "en@calendar=indian", -59667235200000.0 }, // 0 79-03-24
2486 { "en@calendar=islamic", -42521673600000.0 }, // 0 622-07-15
2487 { "en@calendar=islamic-civil", -42521587200000.0 }, // 0 622-07-16
2488 { "en@calendar=islamic-tbla", -42521673600000.0 }, // 0 622-07-15
2489 { "en@calendar=islamic-umalqura", -42521587200000.0 }, // 0 622-07-16
2490 { "en@calendar=persian", -42531955200000.0 }, // 0 622-03-18
2491 { NULL, 0.0 }
2492};
2493
2494void TestClear() {
2495 const LocaleWithCalendarAndClearDate * calAndClearDatesPtr;
2496 for (calAndClearDatesPtr = calAndClearDates; calAndClearDatesPtr->localeWithCal != NULL; calAndClearDatesPtr++) {
2497 UErrorCode status = U_ZERO_ERROR;
2498 UCalendar * ucal = ucal_open(zoneGMT, -1, calAndClearDatesPtr->localeWithCal, UCAL_DEFAULT, &status);
2499 if ( U_FAILURE(status) ) {
2500 log_data_err("FAIL: ucal_open for locale %s, status %s\n", calAndClearDatesPtr->localeWithCal, u_errorName(status));
2501 } else {
2502 UDate date;
2503 ucal_clear(ucal);
2504 date = ucal_getMillis(ucal, &status);
2505 if ( U_FAILURE(status) ) {
2506 log_err("FAIL: ucal_clear, ucal_getMillis for locale %s, status %s\n", calAndClearDatesPtr->localeWithCal, u_errorName(status));
2507 } else if (date != calAndClearDatesPtr->clearDate) {
2508 log_err("FAIL: ucal_clear, ucal_getMillis for locale %s, expected %.1f, got %.1f\n", calAndClearDatesPtr->localeWithCal, calAndClearDatesPtr->clearDate, date);
2509 }
2ca993e8 2510 ucal_close(ucal);
b331163b
A
2511 }
2512 }
2513}
2514
3bb97ae2
A
2515void TestPersianCalOverflow() {
2516 const char * locale = "bs_Cyrl@calendar=persian";
2517 UErrorCode status = U_ZERO_ERROR;
2518 UCalendar * ucal = ucal_open(NULL, 0, locale, UCAL_DEFAULT, &status);
2519 if ( U_FAILURE(status) ) {
2520 log_data_err("FAIL: ucal_open for locale %s, status %s\n", locale, u_errorName(status));
2521 } else {
2522 int32_t maxMonth = ucal_getLimit(ucal, UCAL_MONTH, UCAL_MAXIMUM, &status);
2523 int32_t maxDayOfMonth = ucal_getLimit(ucal, UCAL_DATE, UCAL_MAXIMUM, &status);
2524 if ( U_FAILURE(status) ) {
2525 log_err("FAIL: ucal_getLimit MONTH/DATE for locale %s, status %s\n", locale, u_errorName(status));
2526 } else {
2527 int32_t jd, month, dayOfMonth;
2528 for (jd = 67023580; jd <= 67023584; jd++) { // year 178171, int32_t overflow if jd >= 67023582
2529 status = U_ZERO_ERROR;
2530 ucal_clear(ucal);
2531 ucal_set(ucal, UCAL_JULIAN_DAY, jd);
2532 month = ucal_get(ucal, UCAL_MONTH, &status);
2533 dayOfMonth = ucal_get(ucal, UCAL_DATE, &status);
2534 if ( U_FAILURE(status) ) {
2535 log_err("FAIL: ucal_get MONTH/DATE for locale %s, julianDay %d, status %s\n", locale, jd, u_errorName(status));
2536 } else if (month > maxMonth || dayOfMonth > maxDayOfMonth) {
2537 log_err("FAIL: locale %s, julianDay %d; maxMonth %d, got month %d; maxDayOfMonth %d, got dayOfMonth %d\n",
2538 locale, jd, maxMonth, month, maxDayOfMonth, dayOfMonth);
2539 }
2540 }
2541 }
2542 ucal_close(ucal);
2543 }
2544}
2545
b331163b
A
2546/* Apple-specific */
2547typedef struct {
2548 const char * locale;
2549 UBool formatStyle;
2550 UADayPeriod expected[12]; // expected results for 0..22 in 2-hour increments
2551} DayPeriodTestItem;
2552
2553static const DayPeriodTestItem dpItems[] = {
2554 { "en", FALSE, { UADAYPERIOD_NIGHT1, UADAYPERIOD_NIGHT1, UADAYPERIOD_NIGHT1, UADAYPERIOD_MORNING1, UADAYPERIOD_MORNING1, UADAYPERIOD_MORNING1,
2555 UADAYPERIOD_AFTERNOON1, UADAYPERIOD_AFTERNOON1, UADAYPERIOD_AFTERNOON1, UADAYPERIOD_EVENING1, UADAYPERIOD_EVENING1, UADAYPERIOD_NIGHT1 } },
2556 { "en", TRUE, { UADAYPERIOD_MIDNIGHT, UADAYPERIOD_NIGHT1, UADAYPERIOD_NIGHT1, UADAYPERIOD_MORNING1, UADAYPERIOD_MORNING1, UADAYPERIOD_MORNING1,
2557 UADAYPERIOD_NOON, UADAYPERIOD_AFTERNOON1, UADAYPERIOD_AFTERNOON1, UADAYPERIOD_EVENING1, UADAYPERIOD_EVENING1, UADAYPERIOD_NIGHT1 } },
2558 { "ta", FALSE, { UADAYPERIOD_NIGHT1, UADAYPERIOD_NIGHT1, UADAYPERIOD_MORNING1, UADAYPERIOD_MORNING2, UADAYPERIOD_MORNING2, UADAYPERIOD_MORNING2,
2559 UADAYPERIOD_AFTERNOON1, UADAYPERIOD_AFTERNOON2, UADAYPERIOD_EVENING1, UADAYPERIOD_EVENING2, UADAYPERIOD_EVENING2, UADAYPERIOD_NIGHT1 } },
2560 // test fallback for languages with no data. Should be to root, but that is broken in the data, so to en for now.
2561 { "tlh", FALSE, { UADAYPERIOD_NIGHT1, UADAYPERIOD_NIGHT1, UADAYPERIOD_NIGHT1, UADAYPERIOD_MORNING1, UADAYPERIOD_MORNING1, UADAYPERIOD_MORNING1,
2562 UADAYPERIOD_AFTERNOON1, UADAYPERIOD_AFTERNOON1, UADAYPERIOD_AFTERNOON1, UADAYPERIOD_EVENING1, UADAYPERIOD_EVENING1, UADAYPERIOD_NIGHT1 } },
2563 { NULL, FALSE, { 0 } }
2564};
2565
2566void TestGetDayPeriods() {
2567 static const DayPeriodTestItem * dpItemPtr;
2568 for (dpItemPtr = dpItems; dpItemPtr->locale != NULL; dpItemPtr++) {
2569 int32_t hourIndex;
2570 for (hourIndex = 0; hourIndex < 12; hourIndex++) {
2571 UErrorCode status = U_ZERO_ERROR;
2572 UADayPeriod dp = uacal_getDayPeriod(dpItemPtr->locale, hourIndex*2, 0, dpItemPtr->formatStyle, &status);
2573 if ( U_FAILURE(status) ) {
2574 log_err("FAIL: uacal_getDayPeriod, locale %s, hour %d, formatStyle %d, status %s\n",
2575 dpItemPtr->locale, hourIndex*2, dpItemPtr->formatStyle, u_errorName(status));
2576 } else if (dp != dpItemPtr->expected[hourIndex]) {
2577 log_err("FAIL: uacal_getDayPeriod, locale %s, hour %d, formatStyle %d, expected dp %d, got %d\n",
2578 dpItemPtr->locale, hourIndex*2, dpItemPtr->formatStyle, dpItemPtr->expected[hourIndex], dp);
2579 }
2580 }
2581 }
2582}
57a6839d 2583
1546d4af
A
2584void TestJCalHeiseiNextEra() {
2585 UErrorCode status = U_ZERO_ERROR;
2586 UCalendar *jCal = ucal_open(NULL, 0, "ja_JP@calendar=japanese", UCAL_DEFAULT, &status);
2587 if ( U_FAILURE(status) ) {
2588 log_data_err("FAIL: ucal_open for ja_JP@calendar=japanese, status %s\n", u_errorName(status));
2589 } else {
2590 ucal_clear(jCal); // This sets to 1970, in Showa
2591 int32_t sEra = ucal_get(jCal, UCAL_ERA, &status); // Don't assume Showa is 234
2592 if ( U_FAILURE(status) ) {
2593 log_data_err("FAIL: ucal_get ERA for Showa, status %s\n", u_errorName(status));
2594 } else {
2595 int32_t iEra, eYear;
2596 int32_t startYears[] = { 1926, 1989, 2019, 0 };
2597 for (iEra = 1; iEra < 4; iEra++) {
2598 status = U_ZERO_ERROR;
2599 ucal_clear(jCal);
2600 ucal_set(jCal, UCAL_ERA, sEra+iEra);
2601 eYear = ucal_get(jCal, UCAL_EXTENDED_YEAR, &status);
2602 if ( U_FAILURE(status) ) {
2603 log_err("FAIL: set %d, ucal_get EXTENDED_YEAR, status %s\n", iEra, u_errorName(status));
2604 } else if (startYears[iEra] == 0) {
2605 // invalid era, start should be in the far future with non-negative millis
2606 if (eYear < 10000) {
2607 log_err("ERROR: set %d, invalid era should have faraway start year, but get %d\n", iEra, eYear);
2608 }
2609 UDate date = ucal_getMillis(jCal, &status);
2610 if ( U_FAILURE(status) ) {
2611 log_err("FAIL: set %d, ucal_getMillis, status %s\n", iEra, u_errorName(status));
2612 } else if (date < 0) {
2613 log_err("ERROR: set %d, ucal_getMillis should be positive, but get %.1f\n", iEra, date);
2614 }
2615 } else if (eYear != startYears[iEra]) {
2616 log_err("ERROR: set %d, expected start year %d but get %d\n", iEra, startYears[iEra], eYear);
2617 } else {
2618 ucal_add(jCal, UCAL_ERA, 1, &status);
2619 if ( U_FAILURE(status) ) {
2620 log_err("FAIL: set %d, ucal_add ERA 1, status %s\n", iEra, u_errorName(status));
2621 } else {
2622 eYear = ucal_get(jCal, UCAL_EXTENDED_YEAR, &status);
2623 if ( U_FAILURE(status) ) {
2624 log_err("FAIL: set %d then add ERA 1, ucal_get EXTENDED_YEAR, status %s\n", iEra, u_errorName(status));
2625 } else {
2626 // If this is the last valid era, we expect adding an era to pin to the current era
2627 int32_t nextEraStart = (startYears[iEra+1] == 0)? startYears[iEra]: startYears[iEra+1];
2628 if (eYear != nextEraStart) {
2629 log_err("ERROR: set %d then add ERA 1, expected start year %d but get %d\n", iEra, nextEraStart, eYear);
2630 }
2631 }
2632 }
2633 }
2634 }
2635 }
2636 ucal_close(jCal);
2637 }
2638}
2639
b75a7d8f 2640#endif /* #if !UCONFIG_NO_FORMATTING */