ERL_NIF_TERM date_get6(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { UErrorCode status = U_ZERO_ERROR; UCalendar* cal; cloner* ptr; int32_t year, month, day, hour, minute, second; if(!((argc == 7) && enif_get_resource(env, argv[0], calendar_type, (void**) &ptr) && enif_get_int(env, argv[1], &year) && enif_get_int(env, argv[2], &month) && enif_get_int(env, argv[3], &day) && enif_get_int(env, argv[4], &hour) && enif_get_int(env, argv[5], &minute) && enif_get_int(env, argv[6], &second))) { return enif_make_badarg(env); } month--; cal = (UCalendar*) cloner_get(ptr); CHECK_RES(env, cal); ucal_setDateTime(cal, year, month, day, hour, minute, second, &status); CHECK(env, status); return calendar_to_double(env, (const UCalendar*) cal); }
void TestQuotePattern161() { UDateFormat *format; UCalendar *cal; UDate currentTime_1; UChar *pattern, *tzID, *exp; UChar *dateString; UErrorCode status = U_ZERO_ERROR; const char* expStr = "04/13/1999 at 10:42:28 AM "; ctest_setTimeZone(NULL, &status); pattern=(UChar*)malloc(sizeof(UChar) * (strlen("MM/dd/yyyy 'at' hh:mm:ss a zzz")+1) ); u_uastrcpy(pattern, "MM/dd/yyyy 'at' hh:mm:ss a zzz"); /* this is supposed to open default date format, but later on it treats it like it is "en_US" - very bad if you try to run the tests on machine where default locale is NOT "en_US" */ /* format= udat_openPattern(pattern, u_strlen(pattern), NULL, &status); */ format= udat_open(UDAT_IGNORE, UDAT_IGNORE,"en_US", NULL, 0,pattern, u_strlen(pattern), &status); if(U_FAILURE(status)){ log_err_status(status, "error in udat_open: %s\n", myErrorName(status)); return; } tzID=(UChar*)malloc(sizeof(UChar) * 4); u_uastrcpy(tzID, "PST"); /* this is supposed to open default date format, but later on it treats it like it is "en_US" - very bad if you try to run the tests on machine where default locale is NOT "en_US" */ /* cal=ucal_open(tzID, u_strlen(tzID), NULL, UCAL_TRADITIONAL, &status); */ cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status); if(U_FAILURE(status)){ log_err("error in ucal_open cal : %s\n", myErrorName(status)); } ucal_setDateTime(cal, 1999, UCAL_APRIL, 13, 10, 42, 28, &status); currentTime_1 = ucal_getMillis(cal, &status); dateString = myDateFormat(format, currentTime_1); exp=(UChar*)malloc(sizeof(UChar) * (strlen(expStr) + 1) ); u_uastrcpy(exp, expStr); log_verbose("%s\n", austrdup(dateString) ); if(u_strncmp(dateString, exp, (int32_t)strlen(expStr)) !=0) log_err("Error in formatting a pattern with single quotes\n"); udat_close(format); ucal_close(cal); free(exp); free(tzID); free(pattern); ctest_resetTimeZone(); }
/** * call-seq: * calendar.set_date_time(year, month, date, hour, minute, second) * * Set a UCalendar's current date and time. */ VALUE icu4r_cal_set_date_time(VALUE obj,VALUE year, VALUE mon, VALUE date, VALUE hour, VALUE min, VALUE sec) { UErrorCode status = U_ZERO_ERROR; Check_Type(year, T_FIXNUM); Check_Type(mon, T_FIXNUM); Check_Type(date, T_FIXNUM); Check_Type(hour, T_FIXNUM); Check_Type(min, T_FIXNUM); Check_Type(sec, T_FIXNUM); ucal_setDateTime(UCALENDAR(obj), FIX2INT(year), FIX2INT(mon), FIX2INT(date), FIX2INT(hour), FIX2INT(min), FIX2INT(sec), &status); ICU_RAISE(status); return Qnil; }
/* {{{ * Internal function which sets UCalendar from the passed array and retrieves timestamp */ static UDate internal_get_timestamp(IntlDateFormatter_object *dfo, HashTable *hash_arr) { int32_t year, month, hour, minute, second, mday; UCalendar *pcal; UDate result; intl_error *err = &dfo->datef_data.error; #define INTL_GET_ELEM(elem) \ internal_get_arr_ele(dfo, hash_arr, (elem), err) /* Fetch values from the incoming array */ year = INTL_GET_ELEM(CALENDAR_YEAR) + 1900; /* tm_year is years since 1900 */ /* Month in ICU and PHP starts from January =0 */ month = INTL_GET_ELEM(CALENDAR_MON); hour = INTL_GET_ELEM(CALENDAR_HOUR); minute = INTL_GET_ELEM(CALENDAR_MIN); second = INTL_GET_ELEM(CALENDAR_SEC); /* For the ucal_setDateTime() function, this is the 'date' value */ mday = INTL_GET_ELEM(CALENDAR_MDAY); #undef INTL_GET_ELEM pcal = ucal_clone(udat_getCalendar(DATE_FORMAT_OBJECT(dfo)), &INTL_DATA_ERROR_CODE(dfo)); if (INTL_DATA_ERROR_CODE(dfo) != U_ZERO_ERROR) { intl_errors_set(err, INTL_DATA_ERROR_CODE(dfo), "datefmt_format: " "error cloning calendar", 0); return 0; } /* set the incoming values for the calendar */ ucal_setDateTime(pcal, year, month, mday, hour, minute, second, &INTL_DATA_ERROR_CODE(dfo)); /* actually, ucal_setDateTime cannot fail */ /* Fetch the timestamp from the UCalendar */ result = ucal_getMillis(pcal, &INTL_DATA_ERROR_CODE(dfo)); ucal_close(pcal); return result; }
static void TestRelativeDateFormat() { UDate today = 0.0; const UDateFormatStyle * stylePtr; const UChar ** monthPtnPtr; UErrorCode status = U_ZERO_ERROR; UCalendar * ucal = ucal_open(trdfZone, -1, trdfLocale, UCAL_GREGORIAN, &status); if ( U_SUCCESS(status) ) { int32_t year, month, day; ucal_setMillis(ucal, ucal_getNow(), &status); year = ucal_get(ucal, UCAL_YEAR, &status); month = ucal_get(ucal, UCAL_MONTH, &status); day = ucal_get(ucal, UCAL_DATE, &status); ucal_setDateTime(ucal, year, month, day, 18, 49, 0, &status); /* set to today at 18:49:00 */ today = ucal_getMillis(ucal, &status); ucal_close(ucal); } if ( U_FAILURE(status) || today == 0.0 ) { log_data_err("Generate UDate for a specified time today fails, error %s - (Are you missing data?)\n", myErrorName(status) ); return; } for (stylePtr = dateStylesList, monthPtnPtr = monthPatnsList; *stylePtr != UDAT_NONE; ++stylePtr, ++monthPtnPtr) { UDateFormat* fmtRelDateTime; UDateFormat* fmtRelDate; UDateFormat* fmtTime; int32_t dayOffset, limit; UFieldPosition fp; UChar strDateTime[kDateAndTimeOutMax]; UChar strDate[kDateOrTimeOutMax]; UChar strTime[kDateOrTimeOutMax]; UChar * strPtr; int32_t dtpatLen; fmtRelDateTime = udat_open(UDAT_SHORT, *stylePtr | UDAT_RELATIVE, trdfLocale, trdfZone, -1, NULL, 0, &status); if ( U_FAILURE(status) ) { log_data_err("udat_open timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s (Are you missing data?)\n", *stylePtr, myErrorName(status) ); continue; } fmtRelDate = udat_open(UDAT_NONE, *stylePtr | UDAT_RELATIVE, trdfLocale, trdfZone, -1, NULL, 0, &status); if ( U_FAILURE(status) ) { log_err("udat_open timeStyle NONE dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) ); udat_close(fmtRelDateTime); continue; } fmtTime = udat_open(UDAT_SHORT, UDAT_NONE, trdfLocale, trdfZone, -1, NULL, 0, &status); if ( U_FAILURE(status) ) { log_err("udat_open timeStyle SHORT dateStyle NONE fails, error %s\n", myErrorName(status) ); udat_close(fmtRelDateTime); udat_close(fmtRelDate); continue; } dtpatLen = udat_toPatternRelativeDate(fmtRelDateTime, strDate, kDateAndTimeOutMax, &status); if ( U_FAILURE(status) ) { log_err("udat_toPatternRelativeDate timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) ); status = U_ZERO_ERROR; } else if ( u_strstr(strDate, *monthPtnPtr) == NULL || dtpatLen != u_strlen(strDate) ) { log_err("udat_toPatternRelativeDate timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) date pattern incorrect\n", *stylePtr ); } dtpatLen = udat_toPatternRelativeTime(fmtRelDateTime, strTime, kDateAndTimeOutMax, &status); if ( U_FAILURE(status) ) { log_err("udat_toPatternRelativeTime timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) ); status = U_ZERO_ERROR; } else if ( u_strstr(strTime, minutesPatn) == NULL || dtpatLen != u_strlen(strTime) ) { log_err("udat_toPatternRelativeTime timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) time pattern incorrect\n", *stylePtr ); } dtpatLen = udat_toPattern(fmtRelDateTime, FALSE, strDateTime, kDateAndTimeOutMax, &status); if ( U_FAILURE(status) ) { log_err("udat_toPattern timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) ); status = U_ZERO_ERROR; } else if ( u_strstr(strDateTime, strDate) == NULL || u_strstr(strDateTime, strTime) == NULL || dtpatLen != u_strlen(strDateTime) ) { log_err("udat_toPattern timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) dateTime pattern incorrect\n", *stylePtr ); } udat_applyPatternRelative(fmtRelDateTime, strDate, u_strlen(strDate), newTimePatn, u_strlen(newTimePatn), &status); if ( U_FAILURE(status) ) { log_err("udat_applyPatternRelative timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) ); status = U_ZERO_ERROR; } else { udat_toPattern(fmtRelDateTime, FALSE, strDateTime, kDateAndTimeOutMax, &status); if ( U_FAILURE(status) ) { log_err("udat_toPattern timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) ); status = U_ZERO_ERROR; } else if ( u_strstr(strDateTime, newTimePatn) == NULL ) { log_err("udat_applyPatternRelative timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) didn't update time pattern\n", *stylePtr ); } } udat_applyPatternRelative(fmtRelDateTime, strDate, u_strlen(strDate), strTime, u_strlen(strTime), &status); /* restore original */ fp.field = UDAT_MINUTE_FIELD; for (dayOffset = -2, limit = 2; dayOffset <= limit; ++dayOffset) { UDate dateToUse = today + (float)dayOffset*dayInterval; udat_format(fmtRelDateTime, dateToUse, strDateTime, kDateAndTimeOutMax, &fp, &status); if ( U_FAILURE(status) ) { log_err("udat_format timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) ); status = U_ZERO_ERROR; } else { udat_format(fmtRelDate, dateToUse, strDate, kDateOrTimeOutMax, NULL, &status); if ( U_FAILURE(status) ) { log_err("udat_format timeStyle NONE dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) ); status = U_ZERO_ERROR; } else if ( u_strstr(strDateTime, strDate) == NULL ) { log_err("relative date string not found in udat_format timeStyle SHORT dateStyle (%d | UDAT_RELATIVE)\n", *stylePtr ); } udat_format(fmtTime, dateToUse, strTime, kDateOrTimeOutMax, NULL, &status); if ( U_FAILURE(status) ) { log_err("udat_format timeStyle SHORT dateStyle NONE fails, error %s\n", myErrorName(status) ); status = U_ZERO_ERROR; } else if ( u_strstr(strDateTime, strTime) == NULL ) { log_err("time string not found in udat_format timeStyle SHORT dateStyle (%d | UDAT_RELATIVE)\n", *stylePtr ); } strPtr = u_strstr(strDateTime, minutesStr); if ( strPtr != NULL ) { int32_t beginIndex = strPtr - strDateTime; if ( fp.beginIndex != beginIndex ) { log_err("UFieldPosition beginIndex %d, expected %d, in udat_format timeStyle SHORT dateStyle (%d | UDAT_RELATIVE)\n", fp.beginIndex, beginIndex, *stylePtr ); } } else { log_err("minutes string not found in udat_format timeStyle SHORT dateStyle (%d | UDAT_RELATIVE)\n", *stylePtr ); } } } udat_close(fmtRelDateTime); udat_close(fmtRelDate); udat_close(fmtTime); } }
/* Test u_formatMessage() and u_parseMessage() , format and parse sequence and round trip */ static void TestSampleFormatAndParse(void) { UChar *result, *tzID, *str; UChar pattern[100]; UChar expected[100]; int32_t resultLengthOut, resultlength; UCalendar *cal; UDate d1,d; UDateFormat *def1; UErrorCode status = U_ZERO_ERROR; int32_t value = 0; UChar ret[30]; ctest_setTimeZone(NULL, &status); log_verbose("Testing format and parse\n"); str=(UChar*)malloc(sizeof(UChar) * 25); u_uastrcpy(str, "disturbance in force"); tzID=(UChar*)malloc(sizeof(UChar) * 4); u_uastrcpy(tzID, "PST"); cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status); if(U_FAILURE(status)){ log_data_err("error in ucal_open caldef : %s - (Are you missing data?)\n", myErrorName(status) ); } ucal_setDateTime(cal, 1999, UCAL_MARCH, 18, 0, 0, 0, &status); d1=ucal_getMillis(cal, &status); if(U_FAILURE(status)){ log_data_err("Error: failure in get millis: %s - (Are you missing data?)\n", myErrorName(status) ); } log_verbose("\nTesting with pattern test#4"); u_uastrcpy(pattern, "On {0, date, long}, there was a {1} on planet {2,number,integer}"); u_uastrcpy(expected, "On March 18, 1999, there was a disturbance in force on planet 7"); resultlength=1; result=(UChar*)malloc(sizeof(UChar) * resultlength); resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, d1, str, 7); if(status==U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultLengthOut+1; result=(UChar*)realloc(result, sizeof(UChar) * resultlength); u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, d1, str, 7); } if(U_FAILURE(status)){ log_data_err("ERROR: failure in message format test#4: %s (Are you missing data?)\n", myErrorName(status)); } else if(u_strcmp(result, expected)==0) log_verbose("PASS: MessagFormat successful on test#4\n"); else{ log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result), austrdup(expected) ); } /*try to parse this and check*/ log_verbose("\nTesting the parse Message test#5\n"); u_parseMessage("en_US", pattern, u_strlen(pattern), result, u_strlen(result), &status, &d, ret, &value); if(U_FAILURE(status)){ log_data_err("ERROR: error in parsing: test#5: %s (Are you missing data?)\n", myErrorName(status)); } else if(value!=7 && u_strcmp(str,ret)!=0) log_err("FAIL: Error in parseMessage on test#5 \n"); else log_verbose("PASS: parseMessage successful on test#5\n"); def1 = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, NULL,0,&status); if(U_FAILURE(status)) { log_data_err("error in creating the dateformat using short date and time style: %s (Are you missing data?)\n", myErrorName(status)); }else{ if(u_strcmp(myDateFormat(def1, d), myDateFormat(def1, d1))==0) log_verbose("PASS: parseMessage successful test#5\n"); else{ log_err("FAIL: parseMessage didn't parse the date successfully\n GOT: %s EXPECTED %s\n", austrdup(myDateFormat(def1,d)), austrdup(myDateFormat(def1,d1)) ); } } udat_close(def1); ucal_close(cal); free(result); free(str); free(tzID); ctest_resetTimeZone(); }
/** * @bug 4056591 * Verify the function of the [s|g]et2DigitYearStart() API. */ void Test4056591() { int i; UCalendar *cal; UDateFormat *def; UDate start,exp,got; UChar s[10]; UChar *gotdate, *expdate; UChar pat[10]; UDate d[4]; UErrorCode status = U_ZERO_ERROR; const char* strings[] = { "091225", "091224", "611226", "991227" }; log_verbose("Testing s[get] 2 digit year start regressively\n"); cal=ucal_open(NULL, 0, "en_US", UCAL_GREGORIAN, &status); if(U_FAILURE(status)) { log_data_err("error in ucal_open caldef : %s - (Are you missing data?)\n", myErrorName(status)); return; } ucal_setDateTime(cal, 1809, UCAL_DECEMBER, 25, 17, 40, 30, &status); d[0]=ucal_getMillis(cal, &status); if(U_FAILURE(status)) { log_err("Error: failure in get millis: %s\n", myErrorName(status)); } ucal_setDateTime(cal, 1909, UCAL_DECEMBER, 24, 17, 40, 30, &status); d[1]=ucal_getMillis(cal, &status); ucal_setDateTime(cal, 1861, UCAL_DECEMBER, 26, 17, 40, 30, &status); d[2]=ucal_getMillis(cal, &status); ucal_setDateTime(cal, 1999, UCAL_DECEMBER, 27, 17, 40, 30, &status); d[3]=ucal_getMillis(cal, &status); u_uastrcpy(pat, "yyMMdd"); def = udat_open(UDAT_IGNORE,UDAT_IGNORE,NULL, NULL, 0, pat, u_strlen(pat), &status); if(U_FAILURE(status)) { log_err_status(status, "FAIL: error in creating the dateformat using u_openPattern(): %s\n", myErrorName(status)); return; } start = 1800; udat_set2DigitYearStart(def, start, &status); if(U_FAILURE(status)) log_err("ERROR: in setTwoDigitStartDate: %s\n", myErrorName(status)); if( (udat_get2DigitYearStart(def, &status) != start)) log_err("ERROR: get2DigitYearStart broken\n"); for(i = 0; i < 4; ++i) { u_uastrcpy(s, strings[i]); exp = d[i]; got = udat_parse(def, s, u_strlen(s), 0, &status); gotdate=myFormatit(def, got); expdate=myFormatit(def, exp); if (gotdate == NULL || expdate == NULL) { log_err("myFormatit failed!\n"); } else if(u_strcmp(gotdate, expdate) !=0) { log_err("set2DigitYearStart broken for %s \n got: %s, expected: %s\n", austrdup(s), austrdup(gotdate), austrdup(expdate) ); } } udat_close(def); ucal_close(cal); }