/** * @bug 4162071 **/ void Test4162071() { int32_t pos; UDate x; UErrorCode status = U_ZERO_ERROR; UDateFormat *df; UChar datestr[30]; UChar format[50]; u_uastrcpy(datestr, "Thu, 30-Jul-1999 11:51:14 GMT"); u_uastrcpy(format, "EEE', 'dd-MMM-yyyy HH:mm:ss z"); /* RFC 822/1123 */ status = U_ZERO_ERROR; /* Can't hardcode the result to assume the default locale is "en_US". */ df = udat_open(UDAT_IGNORE,UDAT_IGNORE,"en_US",NULL,0,format, u_strlen(format),&status); if(U_FAILURE(status)) { log_data_err("ERROR: couldn't create date format: %s\n", myErrorName(status)); return; } pos=0; x = udat_parse(df, datestr, u_strlen(datestr), &pos, &status); if(U_FAILURE(status)) { log_data_err("ERROR : parse format %s fails : %s\n", austrdup(format), myErrorName(status)); } else { log_verbose("Parse format \"%s \" ok.\n", austrdup(format) ); } /*log_verbose("date= %s\n", austrdup(myFormatit(df, x)) );*/ udat_close(df); }
void tryPat994(UDateFormat* format, const char* pattern, const char* s, UDate expected) { UChar *f; UChar *str, *pat; UDate date; UDate null=0; int32_t pos; UErrorCode status = U_ZERO_ERROR; str=(UChar*)malloc(sizeof(UChar) * (strlen(s) + 1) ); u_uastrcpy(str, s); pat=(UChar*)malloc(sizeof(UChar) * (strlen(pattern) + 1) ); u_uastrcpy(pat, pattern); log_verbose("Pattern : %s ; String : %s\n", austrdup(pat), austrdup(str)); udat_applyPattern(format, FALSE, pat, u_strlen(pat)); pos=0; date = udat_parse(format, str, u_strlen(str), &pos, &status); if(U_FAILURE(status) || date == null) { log_verbose("ParseException: : %s\n", myErrorName(status) ); if (expected != null) log_err("FAIL: Expected: %s\n", austrdup(myDateFormat(format, expected)) ); } else { f=myDateFormat(format, date); log_verbose(" parse( %s ) -> %s\n", austrdup(str), austrdup(f)); if (expected == null || date != expected) log_err("FAIL: Expected null for \"%s\"\n", s); if (u_strcmp(f, str) !=0) log_err("FAIL: Expected : %s\n", austrdup(str) ); } free(str); free(pat); }
/* {{{ * Internal function which calls the udat_parse * param int store_error acts like a boolean * if set to 1 - store any error encountered in the parameter parse_error * if set to 0 - no need to store any error encountered in the parameter parse_error */ static void internal_parse_to_timestamp(IntlDateFormatter_object *dfo, char* text_to_parse, int32_t text_len, int32_t *parse_pos, zval *return_value) { double result = 0; UDate timestamp =0; UChar* text_utf16 = NULL; int32_t text_utf16_len = 0; /* Convert timezone to UTF-16. */ intl_convert_utf8_to_utf16(&text_utf16, &text_utf16_len, text_to_parse, text_len, &INTL_DATA_ERROR_CODE(dfo)); INTL_METHOD_CHECK_STATUS(dfo, "Error converting timezone to UTF-16" ); timestamp = udat_parse( DATE_FORMAT_OBJECT(dfo), text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo)); if( text_utf16 ){ efree(text_utf16); } INTL_METHOD_CHECK_STATUS( dfo, "Date parsing failed" ); /* Since return is in sec. */ result = (double)timestamp / U_MILLIS_PER_SECOND; if(result > LONG_MAX || result < -LONG_MAX) { ZVAL_DOUBLE(return_value, result<0?ceil(result):floor(result)); } else { ZVAL_LONG(return_value, (zend_long)result); } }
static Variant HHVM_METHOD(IntlDateFormatter, parse, const String& value, VRefParam position) { DATFMT_GET(data, this_, 0); int32_t pos = position.toInt64(); if (pos > value.size()) { return false; } UErrorCode error = U_ZERO_ERROR; String str(u16(value, error)); if (U_FAILURE(error)) { data->setError(error, "Error converting timezone to UTF-16"); return false; } error = U_ZERO_ERROR; UDate timestamp = udat_parse(data->datefmt(), (UChar*)str.c_str(), str.size() / sizeof(UChar), &pos, &error); position = (int64_t)pos; if (U_FAILURE(error)) { data->setError(error, "Date parsing failed"); return false; } double result = (double)timestamp / U_MILLIS_PER_SECOND; if ((result > LONG_MAX) || (result < -LONG_MAX)) { return (double)((result > 0) ? ceil(result) : floor(result)); } else { return (int64_t)result; } }
void Test_GEec(void) { UErrorCode status = U_ZERO_ERROR; UDateFormat * dtfmt = udat_open(UDAT_LONG, UDAT_LONG, "en", zonePST, -1, NULL, 0, &status); if ( U_SUCCESS(status) ) { const DatePatternAndText *patTextPtr; for (patTextPtr = datePatternsAndText; patTextPtr->pattern != NULL; ++patTextPtr) { UChar dmyGnText[DATE_TEXT_MAX_CHARS]; char byteText[3*DATE_TEXT_MAX_CHARS]; int32_t dmyGnTextLen; UDate dateResult; udat_applyPattern(dtfmt, FALSE, patTextPtr->pattern, -1); dmyGnTextLen = udat_format(dtfmt, july022008, dmyGnText, DATE_TEXT_MAX_CHARS, NULL, &status); if ( U_FAILURE(status) ) { log_err("FAIL: udat_format with %s: %s\n", patTextPtr->label, myErrorName(status) ); status = U_ZERO_ERROR; } else if ( u_strcmp(dmyGnText, patTextPtr->text) != 0 ) { log_err("FAIL: udat_format with %s: wrong UChar[] result %s\n", patTextPtr->label, u_austrcpy(byteText,dmyGnText) ); } dateResult = udat_parse(dtfmt, patTextPtr->text, -1, NULL, &status); /* no time, dateResult != july022008 by some hours */ if ( U_FAILURE(status) ) { log_err("FAIL: udat_parse with %s: %s\n", patTextPtr->label, myErrorName(status) ); status = U_ZERO_ERROR; } else if ( patTextPtr->label[0] != '*' && july022008 - dateResult > dayMillisec ) { log_err("FAIL: udat_parse with %s: wrong UDate result\n", patTextPtr->label ); } } udat_close(dtfmt); } else { log_data_err("FAIL: udat_open fails: %s (Are you missing data?)\n", myErrorName(status)); } }
double parseLocalizedDate(const String& input, DateComponents::Type type) { switch (type) { case DateComponents::Date: { if (input.length() > static_cast<unsigned>(numeric_limits<int32_t>::max())) break; int32_t inputLength = static_cast<int32_t>(input.length()); UDateFormat* dateFormat = createShortDateFormatter(); if (!dateFormat) break; UErrorCode status = U_ZERO_ERROR; int32_t parsePosition = 0; UDate date = udat_parse(dateFormat, input.characters(), inputLength, &parsePosition, &status); udat_close(dateFormat); if (parsePosition != inputLength || U_FAILURE(status)) break; // UDate, which is an alias of double, is compatible with our expectation. return date; } case DateComponents::DateTime: case DateComponents::DateTimeLocal: case DateComponents::Month: case DateComponents::Time: case DateComponents::Week: case DateComponents::Invalid: break; } return numeric_limits<double>::quiet_NaN(); }
void TestRunTogetherPattern985() { int32_t pos; UChar *pattern=NULL, *now=NULL, *then=NULL; UDateFormat *format; UDate date1, date2; UErrorCode status = U_ZERO_ERROR; pattern=(UChar*)malloc(sizeof(UChar) * (strlen("yyyyMMddHHmmssSSS")+1) ); u_uastrcpy(pattern, "yyyyMMddHHmmssSSS"); format = udat_open(UDAT_IGNORE, UDAT_IGNORE, NULL, NULL, 0,pattern, u_strlen(pattern), &status); if(U_FAILURE(status)){ log_err_status(status, "FAIL: Error in date format construction with pattern: %s\n", myErrorName(status)); return; } date1 = ucal_getNow(); now=myDateFormat(format, date1); log_verbose("%s\n", austrdup(now) ); pos = 0; date2 = udat_parse(format, now, u_strlen(now), &pos, &status); if (date2 == 0) log_verbose("Parse stopped at : %d\n", pos); else then=myDateFormat(format, date2); log_verbose("%s\n", austrdup(then) ); if (!(date2 == date1)) log_err("FAIL\n"); udat_close(format); free(pattern); }
/** * @bug 4061287 */ void Test4061287() { UBool ok; int32_t pos; UDateFormat *df; UErrorCode status = U_ZERO_ERROR; UDate myDate; UChar pattern[21], dateString[11]; u_uastrcpy(dateString, "35/13/1971"); u_uastrcpy(pattern, "dd/mm/yyyy"); status = U_ZERO_ERROR; log_verbose("Testing parsing by changing the attribute lenient\n"); df = udat_open(UDAT_IGNORE,UDAT_IGNORE,NULL,NULL,0,pattern, u_strlen(pattern),&status); if(U_FAILURE(status)) { log_data_err("ERROR: failure in open pattern of test4061287: %s - (Are you missing data?)\n", myErrorName(status)); return; } pos=0; udat_setLenient(df, FALSE); ok=udat_isLenient(df); if(ok==TRUE) log_err("setLenient nor working\n"); ok = FALSE; myDate = udat_parse(df, dateString, u_strlen(dateString), &pos, &status); if(U_FAILURE(status)) ok = TRUE; if(ok!=TRUE) log_err("Fail: Lenient not working: does lenient parsing in spite of setting Leninent as FALSE "); udat_close(df); }
void TestTwoDigitYearDSTParse() { UDateFormat *fullFmt, *fmt; UErrorCode status = U_ZERO_ERROR; UChar *pattern; UDate d; UChar *s; int32_t pos; ctest_setTimeZone(NULL, &status); pattern=(UChar*)malloc(sizeof(UChar) * (strlen("EEE MMM dd HH:mm:ss.SSS zzz yyyy G")+1 )); u_uastrcpy(pattern, "EEE MMM dd HH:mm:ss.SSS zzz yyyy G"); fullFmt= udat_open(UDAT_IGNORE, UDAT_IGNORE,"en_US",NULL,0,pattern, u_strlen(pattern),&status); if(U_FAILURE(status)) { log_err_status(status, "FAIL: Error in creating a date format using udat_openPattern %s\n", myErrorName(status) ); } else { log_verbose("PASS: creating dateformat using udat_openPattern() succesful\n"); u_uastrcpy(pattern, "dd-MMM-yy h:mm:ss 'o''clock' a z"); fmt= udat_open(UDAT_IGNORE,UDAT_IGNORE,"en_US", NULL, 0,pattern, u_strlen(pattern), &status); s=(UChar*)malloc(sizeof(UChar) * (strlen("03-Apr-04 2:20:47 o'clock AM PST")+1) ); u_uastrcpy(s, "03-Apr-04 2:20:47 o'clock AM PST"); pos=0; d = udat_parse(fmt, s, u_strlen(s), &pos, &status); if (U_FAILURE(status)) { log_err("FAIL: Could not parse \"%s\"\n", austrdup(s)); } else { UCalendar *cal = ucal_open(NULL, 0, uloc_getDefault(), UCAL_TRADITIONAL, &status); if (U_FAILURE(status)) { log_err_status(status, "FAIL: Could not open calendar: %s\n", u_errorName(status)); } else { int32_t h; ucal_setMillis(cal, d, &status); h = ucal_get(cal, UCAL_HOUR_OF_DAY, &status); if (U_FAILURE(status)) { log_err("FAIL: Some calendar operations failed"); } else if (h != 2) { log_err("FAIL: Parse of \"%s\" returned HOUR_OF_DAY %d\n", austrdup(s), h); } ucal_close(cal); } } udat_close(fullFmt); udat_close(fmt); free(s); } free(pattern); ctest_resetTimeZone(); }
double ICULocale::parseLocalizedDate(const String& input) { if (!initializeShortDateFormat()) return numeric_limits<double>::quiet_NaN(); if (input.length() > static_cast<unsigned>(numeric_limits<int32_t>::max())) return numeric_limits<double>::quiet_NaN(); int32_t inputLength = static_cast<int32_t>(input.length()); UErrorCode status = U_ZERO_ERROR; int32_t parsePosition = 0; UDate date = udat_parse(m_shortDateFormat, input.characters(), inputLength, &parsePosition, &status); if (parsePosition != inputLength || U_FAILURE(status)) return numeric_limits<double>::quiet_NaN(); // UDate, which is an alias of double, is compatible with our expectation. return date; }
/** * @bug 4060212 */ void Test4060212() { int32_t pos; UCalendar *cal; UDateFormat *formatter, *fmt; UErrorCode status = U_ZERO_ERROR; UDate myDate; UChar *myString; UChar dateString[30], pattern[20], tzID[4]; u_uastrcpy(dateString, "1995-040.05:01:29 -8"); u_uastrcpy(pattern, "yyyy-DDD.hh:mm:ss z"); log_verbose( "dateString= %s Using yyyy-DDD.hh:mm:ss\n", austrdup(dateString) ); status = U_ZERO_ERROR; u_uastrcpy(tzID, "PST"); formatter = udat_open(UDAT_IGNORE,UDAT_IGNORE,"en_US",tzID,-1,pattern, u_strlen(pattern), &status); pos=0; myDate = udat_parse(formatter, dateString, u_strlen(dateString), &pos, &status); fmt = udat_open(UDAT_FULL,UDAT_LONG ,NULL, tzID, -1, NULL, 0, &status); if(U_FAILURE(status)) { log_data_err("FAIL: error in creating the dateformat using default date and time style: %s - (Are you missing data?)\n", myErrorName(status) ); return; } myString = myFormatit(fmt, myDate); cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_GREGORIAN, &status); if(U_FAILURE(status)) { log_err("FAIL: error in ucal_open caldef : %s\n", myErrorName(status)); } ucal_setMillis(cal, myDate, &status); if ((ucal_get(cal, UCAL_DAY_OF_YEAR, &status) != 40)) { log_err("Fail: Got %d Expected 40\n", ucal_get(cal, UCAL_DAY_OF_YEAR, &status)); } udat_close(formatter); ucal_close(cal); udat_close(fmt); }
void TestPartialParse994() { int32_t pos; UDateFormat *f; UErrorCode status = U_ZERO_ERROR; UChar *s; UChar *fmtChars; UDate d, null; null=0; /* 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" */ /* f = udat_open(UDAT_DEFAULT, UDAT_SHORT, NULL, NULL, 0, &status); */ f = udat_open(UDAT_DEFAULT, UDAT_SHORT, "en_US", NULL, 0, NULL, 0,&status); if(U_FAILURE(status)){ log_data_err("FAIL: ErrorCode received during test: %s (Are you missing data?)\n", myErrorName(status)); return; } s=(UChar*)malloc(sizeof(UChar) * (strlen("01/01/1997 10:11:42 AM")+1) ); u_uastrcpy(s, "01/01/1997 10:11:42 AM"); pos=0; d = udat_parse(f, s, u_strlen(s), &pos, &status); if(U_FAILURE(status)) { log_data_err("FAIL: could not parse - exitting"); return; } fmtChars = myDateFormat(f, d); if(fmtChars) { log_verbose("%s\n", fmtChars); } else { log_data_err("FAIL: could not format \n"); return; } tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01 10:11:42", d); tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01 10:", null); tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01 10", null); tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01 ", null); tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/01", null); udat_close(f); free(s); }
/* Testing the DateFormat API */ static void TestDateFormat() { UDateFormat *def, *fr, *it, *de, *def1, *fr_pat; UDateFormat *any; UDateFormat *copy; UErrorCode status = U_ZERO_ERROR; UChar* result = NULL; const UCalendar *cal; const UNumberFormat *numformat1, *numformat2; UChar temp[50]; int32_t numlocales; UDate d1; int i; int32_t resultlength; int32_t resultlengthneeded; int32_t parsepos; UDate d = 837039928046.0; double num = -10456.37; /*const char* str="yyyy.MM.dd G 'at' hh:mm:ss z"; const char t[]="2/3/76 2:50 AM";*/ /*Testing udat_open() to open a dateformat */ ctest_setTimeZone(NULL, &status); log_verbose("\nTesting udat_open() with various parameters\n"); fr = udat_open(UDAT_FULL, UDAT_DEFAULT, "fr_FR", NULL,0, NULL, 0,&status); if(U_FAILURE(status)) { log_data_err("FAIL: error in creating the dateformat using full time style with french locale -> %s (Are you missing data?)\n", myErrorName(status) ); return; } /* 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" */ /* def = udat_open(UDAT_SHORT, UDAT_SHORT, NULL, NULL, 0, &status); */ def = udat_open(UDAT_SHORT, UDAT_SHORT, "en_US", NULL, 0,NULL, 0, &status); if(U_FAILURE(status)) { log_err("FAIL: error in creating the dateformat using short date and time style\n %s\n", myErrorName(status) ); return; } it = udat_open(UDAT_DEFAULT, UDAT_MEDIUM, "it_IT", NULL, 0, NULL, 0,&status); if(U_FAILURE(status)) { log_err("FAIL: error in creating the dateformat using medium date style with italian locale\n %s\n", myErrorName(status) ); return; } de = udat_open(UDAT_LONG, UDAT_LONG, "de_DE", NULL, 0, NULL, 0,&status); if(U_FAILURE(status)) { log_err("FAIL: error in creating the dateformat using long time and date styles with german locale\n %s\n", myErrorName(status)); return; } /*creating a default dateformat */ def1 = udat_open(UDAT_SHORT, UDAT_SHORT, NULL, NULL, 0,NULL, 0, &status); if(U_FAILURE(status)) { log_err("FAIL: error in creating the dateformat using short date and time style\n %s\n", myErrorName(status) ); return; } /*Testing udat_getAvailable() and udat_countAvailable()*/ log_verbose("\nTesting getAvailableLocales and countAvailable()\n"); numlocales=udat_countAvailable(); /* use something sensible w/o hardcoding the count */ if(numlocales < 0) log_data_err("FAIL: error in countAvailable\n"); log_verbose("The number of locales for which date/time formatting patterns are available is %d\n", numlocales); for(i=0;i<numlocales;i++) { UErrorCode subStatus = U_ZERO_ERROR; log_verbose("Testing open of %s\n", udat_getAvailable(i)); any = udat_open(UDAT_SHORT, UDAT_SHORT, udat_getAvailable(i), NULL ,0, NULL, 0, &subStatus); if(U_FAILURE(subStatus)) { log_data_err("FAIL: date format %s (getAvailable(%d)) is not instantiable: %s\n", udat_getAvailable(i), i, u_errorName(subStatus)); } udat_close(any); } /*Testing udat_clone()*/ log_verbose("\nTesting the udat_clone() function of date format\n"); copy=udat_clone(def, &status); if(U_FAILURE(status)){ log_err("Error in creating the clone using udat_clone: %s\n", myErrorName(status) ); } /*if(def != copy) log_err("Error in udat_clone");*/ /*how should i check for equality???? */ /*Testing udat_format()*/ log_verbose("\nTesting the udat_format() function of date format\n"); u_uastrcpy(temp, "7/10/96 4:05 PM"); /*format using def */ resultlength=0; resultlengthneeded=udat_format(def, d, NULL, resultlength, NULL, &status); if(status==U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultlengthneeded+1; if(result != NULL) { free(result); result = NULL; } result=(UChar*)malloc(sizeof(UChar) * resultlength); udat_format(def, d, result, resultlength, NULL, &status); } if(U_FAILURE(status) || !result) { log_err("FAIL: Error in formatting using udat_format(.....) %s\n", myErrorName(status) ); return; } else log_verbose("PASS: formatting successful\n"); if(u_strcmp(result, temp)==0) log_verbose("PASS: Date Format for US locale successful using udat_format()\n"); else { char xbuf[2048]; char gbuf[2048]; u_austrcpy(xbuf, temp); u_austrcpy(gbuf, result); log_err("FAIL: Date Format for US locale failed using udat_format() - expected %s got %s\n", xbuf, gbuf); } /*format using fr */ u_unescape("10 juil. 1996 16:05:28 heure avanc\\u00E9e du Pacifique", temp, 50); if(result != NULL) { free(result); result = NULL; } result=myDateFormat(fr, d); if(u_strcmp(result, temp)==0) log_verbose("PASS: Date Format for french locale successful using udat_format()\n"); else log_data_err("FAIL: Date Format for french locale failed using udat_format().\n" ); /*format using it */ u_uastrcpy(temp, "10/lug/1996 16:05:28"); { UChar *fmtted; char g[100]; char x[100]; fmtted = myDateFormat(it,d); u_austrcpy(g, fmtted); u_austrcpy(x, temp); if(u_strcmp(fmtted, temp)==0) { log_verbose("PASS: Date Format for italian locale successful uisng udat_format() - wanted %s, got %s\n", x, g); } else { log_data_err("FAIL: Date Format for italian locale failed using udat_format() - wanted %s, got %s\n", x, g); } } /*Testing parsing using udat_parse()*/ log_verbose("\nTesting parsing using udat_parse()\n"); u_uastrcpy(temp,"2/3/76 2:50 AM"); parsepos=0; status=U_ZERO_ERROR; d1=udat_parse(def, temp, u_strlen(temp), &parsepos, &status); if(U_FAILURE(status)) { log_err("FAIL: Error in parsing using udat_parse(.....) %s\n", myErrorName(status) ); } else log_verbose("PASS: parsing succesful\n"); /*format it back and check for equality */ if(u_strcmp(myDateFormat(def, d1),temp)!=0) log_err("FAIL: error in parsing\n"); /*Testing parsing using udat_parse()*/ log_verbose("\nTesting parsing using udat_parse()\n"); u_uastrcpy(temp,"2/Don't parse this part"); status=U_ZERO_ERROR; d1=udat_parse(def, temp, u_strlen(temp), NULL, &status); if(status != U_PARSE_ERROR) { log_err("FAIL: udat_parse(\"bad string\") passed when it should have failed\n"); } else log_verbose("PASS: parsing succesful\n"); /*Testing udat_openPattern() */ status=U_ZERO_ERROR; log_verbose("\nTesting the udat_openPattern with a specified pattern\n"); /*for french locale */ fr_pat=udat_open(UDAT_IGNORE, UDAT_IGNORE,"fr_FR",NULL,0,temp, u_strlen(temp), &status); if(U_FAILURE(status)) { log_err("FAIL: Error in creating a date format using udat_openPattern \n %s\n", myErrorName(status) ); } else log_verbose("PASS: creating dateformat using udat_openPattern() succesful\n"); /*Testing applyPattern and toPattern */ log_verbose("\nTesting applyPattern and toPattern()\n"); udat_applyPattern(def1, FALSE, temp, u_strlen(temp)); log_verbose("Extracting the pattern\n"); resultlength=0; resultlengthneeded=udat_toPattern(def1, FALSE, NULL, resultlength, &status); if(status==U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultlengthneeded + 1; result=(UChar*)malloc(sizeof(UChar) * resultlength); udat_toPattern(def1, FALSE, result, resultlength, &status); } if(U_FAILURE(status)) { log_err("FAIL: error in extracting the pattern from UNumberFormat\n %s\n", myErrorName(status) ); } if(u_strcmp(result, temp)!=0) log_err("FAIL: Error in extracting the pattern\n"); else log_verbose("PASS: applyPattern and toPattern work fine\n"); if(result != NULL) { free(result); result = NULL; } /*Testing getter and setter functions*/ /*isLenient and setLenient()*/ log_verbose("\nTesting the isLenient and setLenient properties\n"); udat_setLenient(fr, udat_isLenient(it)); if(udat_isLenient(fr) != udat_isLenient(it)) log_err("ERROR: setLenient() failed\n"); else log_verbose("PASS: setLenient() successful\n"); /*Test get2DigitYearStart set2DigitYearStart */ log_verbose("\nTesting the get and set 2DigitYearStart properties\n"); d1= udat_get2DigitYearStart(fr_pat,&status); if(U_FAILURE(status)) { log_err("ERROR: udat_get2DigitYearStart failed %s\n", myErrorName(status) ); } status = U_ZERO_ERROR; udat_set2DigitYearStart(def1 ,d1, &status); if(U_FAILURE(status)) { log_err("ERROR: udat_set2DigitYearStart failed %s\n", myErrorName(status) ); } if(udat_get2DigitYearStart(fr_pat, &status) != udat_get2DigitYearStart(def1, &status)) log_err("FAIL: error in set2DigitYearStart\n"); else log_verbose("PASS: set2DigitYearStart successful\n"); /*try setting it to another value */ udat_set2DigitYearStart(de, 2000.0, &status); if(U_FAILURE(status)){ log_verbose("ERROR: udat_set2DigitYearStart failed %s\n", myErrorName(status) ); } if(udat_get2DigitYearStart(de, &status) != 2000) log_err("FAIL: error in set2DigitYearStart\n"); else log_verbose("PASS: set2DigitYearStart successful\n"); /*Test getNumberFormat() and setNumberFormat() */ log_verbose("\nTesting the get and set NumberFormat properties of date format\n"); numformat1=udat_getNumberFormat(fr_pat); udat_setNumberFormat(def1, numformat1); numformat2=udat_getNumberFormat(def1); if(u_strcmp(myNumformat(numformat1, num), myNumformat(numformat2, num)) !=0) log_err("FAIL: error in setNumberFormat or getNumberFormat()\n"); else log_verbose("PASS:setNumberFormat and getNumberFormat succesful\n"); /*try setting the number format to another format */ numformat1=udat_getNumberFormat(def); udat_setNumberFormat(def1, numformat1); numformat2=udat_getNumberFormat(def1); if(u_strcmp(myNumformat(numformat1, num), myNumformat(numformat2, num)) !=0) log_err("FAIL: error in setNumberFormat or getNumberFormat()\n"); else log_verbose("PASS: setNumberFormat and getNumberFormat succesful\n"); /*Test getCalendar and setCalendar*/ log_verbose("\nTesting the udat_getCalendar() and udat_setCalendar() properties\n"); cal=udat_getCalendar(fr_pat); udat_setCalendar(def1, cal); if(!ucal_equivalentTo(udat_getCalendar(fr_pat), udat_getCalendar(def1))) log_err("FAIL: Error in setting and getting the calendar\n"); else log_verbose("PASS: getting and setting calendar successful\n"); if(result!=NULL) { free(result); } /*Closing the UDateForamt */ udat_close(def); udat_close(fr); udat_close(it); udat_close(de); udat_close(def1); udat_close(fr_pat); udat_close(copy); ctest_resetTimeZone(); }
* param int store_error acts like a boolean * if set to 1 - store any error encountered in the parameter parse_error * if set to 0 - no need to store any error encountered in the parameter parse_error */ static void internal_parse_to_timestamp(IntlDateFormatter_object *dfo, char* text_to_parse, int32_t text_len, int32_t *parse_pos, zval *return_value TSRMLS_DC) { double result = 0; UDate timestamp =0; UChar* text_utf16 = NULL; int32_t text_utf16_len = 0; /* Convert timezone to UTF-16. */ intl_convert_utf8_to_utf16(&text_utf16, &text_utf16_len, text_to_parse, text_len, &INTL_DATA_ERROR_CODE(dfo)); INTL_METHOD_CHECK_STATUS(dfo, "Error converting timezone to UTF-16" ); timestamp = udat_parse( DATE_FORMAT_OBJECT(dfo), text_utf16, text_utf16_len, parse_pos, &INTL_DATA_ERROR_CODE(dfo)); if( text_utf16 ){ efree(text_utf16); } INTL_METHOD_CHECK_STATUS( dfo, "Date parsing failed" ); /* Since return is in sec. */ result = (double)timestamp / U_MILLIS_PER_SECOND; if(result > LONG_MAX || result < -LONG_MAX) { ZVAL_DOUBLE(return_value, result<0?ceil(result):floor(result)); } else { ZVAL_LONG(return_value, (long)result); } } /* }}} */
/** * @bug 4029195 */ void Test4029195() { int32_t resultlength, resultlengthneeded; UChar *fmdt, *todayS, *rt; UChar *pat=NULL; UChar *temp; UDate today, d1; UDateFormat *df; int32_t parsepos; UErrorCode status = U_ZERO_ERROR; log_verbose("Testing date format and parse function in regression test\n"); today = ucal_getNow(); df = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,"en_US", NULL, 0, NULL, 0, &status); if(U_FAILURE(status)) { log_data_err("FAIL: error in creating the dateformat using default date and time style : %s (Are you missing data?)\n", myErrorName(status)); return; } resultlength=0; resultlengthneeded=udat_toPattern(df, TRUE, NULL, resultlength, &status); if(status==U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultlengthneeded + 1; pat=(UChar*)malloc(sizeof(UChar) * resultlength); udat_toPattern(df, TRUE, pat, resultlength, &status); } log_verbose("pattern: %s\n", austrdup(pat)); fmdt = myFormatit(df, today); if(fmdt) { log_verbose("today: %s\n", austrdup(fmdt)); } else { log_data_err("ERROR: couldn't format, exitting test"); return; } temp=(UChar*)malloc(sizeof(UChar) * 10); u_uastrcpy(temp, "M yyyy dd"); udat_applyPattern(df, TRUE, temp, u_strlen(temp)); todayS =myFormatit(df, today); log_verbose("After teh pattern is applied\n today: %s\n", austrdup(todayS) ); parsepos=0; d1=udat_parse(df, todayS, u_strlen(todayS), &parsepos, &status); if(U_FAILURE(status)) { log_err("FAIL: Error in parsing using udat_parse(.....): %s\n", myErrorName(status)); } rt =myFormatit(df, d1); log_verbose("today: %s\n", austrdup(rt) ); log_verbose("round trip: %s\n", austrdup(rt) ); if(u_strcmp(rt, todayS)!=0) { log_err("Fail: Want %s Got %s\n", austrdup(todayS), austrdup(rt) ); } else log_verbose("Pass: parse and format working fine\n"); udat_close(df); free(temp); if(pat != NULL) { free(pat); } }
/** * @bug 4073003 */ void Test4073003() { int32_t pos,i; UDate d,dd; UChar *datestr; UChar temp[15]; UErrorCode status = U_ZERO_ERROR; UDateFormat *fmt; UChar *result, *result2; const char* tests [] = { "12/25/61", "12/25/1961", "4/3/1999", "4/3/99" }; fmt= udat_open(UDAT_SHORT,UDAT_SHORT ,NULL, NULL, 0, NULL, 0, &status); if(U_FAILURE(status)) { log_data_err("FAIL: error in creating the dateformat using short date and time style: %s (Are you missing data?)\n", myErrorName(status)); return; } u_uastrcpy(temp, "m/D/yy"); udat_applyPattern(fmt, FALSE, temp, u_strlen(temp)); for(i= 0; i < 4; i+=2) { status=U_ZERO_ERROR; datestr=(UChar*)malloc(sizeof(UChar) * (strlen(tests[i])+1)); u_uastrcpy(datestr, tests[i]); pos=0; d = udat_parse(fmt, datestr, u_strlen(datestr), &pos, &status); if(U_FAILURE(status)) { log_err("ERROR : in test 4073003: %s\n", myErrorName(status)); } free(datestr); datestr=(UChar*)malloc(sizeof(UChar) * (strlen(tests[i+1])+1)); u_uastrcpy(datestr, tests[i+1]); pos=0; status=U_ZERO_ERROR; dd = udat_parse(fmt, datestr, u_strlen(datestr), &pos, &status); if(U_FAILURE(status)) { log_err("ERROR : in test 4073003: %s\n", myErrorName(status)); } free(datestr); result =myFormatit(fmt, d); result2 =myFormatit(fmt, dd); if(!result || !result2) { log_data_err("Fail: could not format - exitting test\n"); return; } if (u_strcmp(result, result2)!=0) { log_err("Fail: %s != %s\n", austrdup(result), austrdup(result2) ); } else { log_verbose("Ok: %s == %s\n", austrdup(result), austrdup(result2) ); } } udat_close(fmt); }
/** * @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); }
void TestCzechMonths459() { int32_t lneed, pos; UChar *pattern=NULL, *tzID=NULL; UChar *juneStr, *julyStr; UDateFormat *fmt; UCalendar *cal; UDate june, july, d; UErrorCode status = U_ZERO_ERROR; UChar *date; ctest_setTimeZone(NULL, &status); fmt = udat_open(UDAT_FULL, UDAT_FULL, "cs", NULL, 0, NULL, 0, &status); if(U_FAILURE(status)){ log_data_err("Error in constructing the date format -> %s (Are you missing data?)\n", u_errorName(status)); ctest_resetTimeZone(); return; } lneed=0; lneed=udat_toPattern(fmt, TRUE, NULL, lneed, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; pattern=(UChar*)malloc(sizeof(UChar) * (lneed+1) ); udat_toPattern(fmt, TRUE, pattern, lneed+1, &status); } if(U_FAILURE(status)){ log_err("Error in extracting the pattern\n"); } tzID=(UChar*)malloc(sizeof(UChar) * 4); u_uastrcpy(tzID, "GMT"); cal=ucal_open(tzID, u_strlen(tzID), "cs", UCAL_GREGORIAN, &status); if(U_FAILURE(status)){ log_err("error in ucal_open caldef : %s\n", myErrorName(status)); } ucal_setDate(cal, 1997, UCAL_JUNE, 15, &status); june=ucal_getMillis(cal, &status); ucal_setDate(cal, 1997, UCAL_JULY, 15, &status); july=ucal_getMillis(cal, &status); juneStr = myDateFormat(fmt, june); julyStr = myDateFormat(fmt, july); pos=0; if(juneStr == NULL) { log_data_err("Can't load juneStr. Quitting.\n"); return; } d = udat_parse(fmt, juneStr, u_strlen(juneStr), &pos, &status); date = myDateFormat(fmt, d); if(U_SUCCESS(status)){ UChar* out1 = myDateFormat(fmt, june); UChar* out2 = myDateFormat(fmt, d); if(u_strcmp(out1, out2) !=0) log_err("Error in handling the czech month june\n"); else log_verbose("Pass: Date = %s (czech month June)\n", aescstrdup(date, -1)); }else{ log_err("udat_parse failed. Error. %s\n",u_errorName(status)); } pos=0; d = udat_parse(fmt, julyStr, u_strlen(julyStr), &pos, &status); date = myDateFormat(fmt, d); if(u_strcmp(myDateFormat(fmt, july), myDateFormat(fmt, d) ) !=0) log_err("Error in handling the czech month july\n"); else log_verbose("Pass: Date = %s (czech month July)\n", aescstrdup(date, -1)); ctest_resetTimeZone(); udat_close(fmt); ucal_close(cal); free(pattern); free(tzID); }