std::string GlobalizationNDK::stringToDate(const std::string& args) { if (args.empty()) return errorInJson(PARSING_ERROR, "No dateString provided!"); Json::Reader reader; Json::Value root; bool parse = reader.parse(args, root); if (!parse) { slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::stringToDate: invalid json data: %s", args.c_str()); return errorInJson(PARSING_ERROR, "Parameters not valid json format!"); } Json::Value dateString = root["dateString"]; if (!dateString.isString()) { slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::stringToDate: invalid dateString type: %d", dateString.type()); return errorInJson(PARSING_ERROR, "dateString not a string!"); } std::string dateValue = dateString.asString(); if (dateValue.empty()) { slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::stringToDate: empty dateString."); return errorInJson(PARSING_ERROR, "dateString is empty!"); } Json::Value options = root["options"]; DateFormat::EStyle dstyle, tstyle; std::string error; if (!handleDateOptions(options, dstyle, tstyle, error)) return errorInJson(PARSING_ERROR, error); const Locale& loc = Locale::getDefault(); DateFormat* df = DateFormat::createDateTimeInstance(dstyle, tstyle, loc); if (!df) { slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::stringToDate: unable to create DateFormat instance!"); return errorInJson(UNKNOWN_ERROR, "Unable to create DateFormat instance!"); } std::auto_ptr<DateFormat> deleter(df); UnicodeString uDate = UnicodeString::fromUTF8(dateValue); UErrorCode status = U_ZERO_ERROR; UDate date = df->parse(uDate, status); // Note: not sure why U_ERROR_WARNING_START is returned when parse succeeded. if (status != U_ZERO_ERROR && status != U_ERROR_WARNING_START) { slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::stringToDate: DataFormat::parse error: %d: %s", status, dateValue.c_str()); return errorInJson(PARSING_ERROR, "Failed to parse dateString!"); } return resultDateInJson(date); }
UDate L10n::ParseDateTime(const std::string& dateTimeString, const std::string& dateTimeFormat, const Locale& locale) const { UErrorCode success = U_ZERO_ERROR; UnicodeString utf16DateTimeString = UnicodeString::fromUTF8(dateTimeString.c_str()); UnicodeString utf16DateTimeFormat = UnicodeString::fromUTF8(dateTimeFormat.c_str()); DateFormat* dateFormatter = new SimpleDateFormat(utf16DateTimeFormat, locale, success); UDate date = dateFormatter->parse(utf16DateTimeString, success); delete dateFormatter; return date; }
/** * This test checks various generic API methods in DateFormat to achieve 100% * API coverage. */ void IntlTestDateFormatAPI::testAPI(/* char* par */) { UErrorCode status = U_ZERO_ERROR; // ======= Test constructors logln("Testing DateFormat constructors"); DateFormat *def = DateFormat::createInstance(); DateFormat *fr = DateFormat::createTimeInstance(DateFormat::FULL, Locale::getFrench()); DateFormat *it = DateFormat::createDateInstance(DateFormat::MEDIUM, Locale::getItalian()); DateFormat *de = DateFormat::createDateTimeInstance(DateFormat::LONG, DateFormat::LONG, Locale::getGerman()); if (def == NULL || fr == NULL || it == NULL || de == NULL){ dataerrln("Error creating instnaces."); } // ======= Test equality if (fr != NULL && def != NULL) { logln("Testing equality operator"); if( *fr == *it ) { errln("ERROR: == failed"); } } // ======= Test various format() methods if (fr != NULL && it != NULL && de != NULL) { logln("Testing various format() methods"); UDate d = 837039928046.0; Formattable fD(d, Formattable::kIsDate); UnicodeString res1, res2, res3; FieldPosition pos1(0), pos2(0); status = U_ZERO_ERROR; res1 = fr->format(d, res1, pos1, status); if(U_FAILURE(status)) { errln("ERROR: format() failed (French)"); } logln( (UnicodeString) "" + d + " formatted to " + res1); res2 = it->format(d, res2, pos2); logln( (UnicodeString) "" + d + " formatted to " + res2); res3 = de->format(d, res3); logln( (UnicodeString) "" + d + " formatted to " + res3); } // ======= Test parse() if (def != NULL) { logln("Testing parse()"); UnicodeString text("02/03/76 2:50 AM, CST"); Formattable result1; UDate result2, result3; ParsePosition pos(0), pos01(0); def->parseObject(text, result1, pos); if(result1.getType() != Formattable::kDate) { errln("ERROR: parseObject() failed for " + text); } logln(text + " parsed into " + result1.getDate()); status = U_ZERO_ERROR; result2 = def->parse(text, status); if(U_FAILURE(status)) { errln("ERROR: parse() failed, stopping testing"); return; } logln(text + " parsed into " + result2); result3 = def->parse(text, pos01); logln(text + " parsed into " + result3); } // ======= Test getters and setters if (fr != NULL && it != NULL && de != NULL) { logln("Testing getters and setters"); int32_t count = 0; const Locale *locales = DateFormat::getAvailableLocales(count); logln((UnicodeString) "Got " + count + " locales" ); for(int32_t i = 0; i < count; i++) { UnicodeString name; name = locales[i].getName(); logln(name); } fr->setLenient(it->isLenient()); if(fr->isLenient() != it->isLenient()) { errln("ERROR: setLenient() failed"); } const Calendar *cal = def->getCalendar(); Calendar *newCal = cal->clone(); de->adoptCalendar(newCal); it->setCalendar(*newCal); if( *(de->getCalendar()) != *(it->getCalendar())) { errln("ERROR: adopt or set Calendar() failed"); } const NumberFormat *nf = def->getNumberFormat(); NumberFormat *newNf = (NumberFormat*) nf->clone(); de->adoptNumberFormat(newNf); it->setNumberFormat(*newNf); if( *(de->getNumberFormat()) != *(it->getNumberFormat())) { errln("ERROR: adopt or set NumberFormat() failed"); } const TimeZone& tz = def->getTimeZone(); TimeZone *newTz = tz.clone(); de->adoptTimeZone(newTz); it->setTimeZone(*newTz); if( de->getTimeZone() != it->getTimeZone()) { errln("ERROR: adopt or set TimeZone() failed"); } } // ======= Test getStaticClassID() logln("Testing getStaticClassID()"); status = U_ZERO_ERROR; DateFormat *test = new SimpleDateFormat(status); if(U_FAILURE(status)) { errln("ERROR: Couldn't create a DateFormat"); } if(test->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) { errln("ERROR: getDynamicClassID() didn't return the expected value"); } delete test; delete def; delete fr; delete it; delete de; }
void DataDrivenFormatTest::testConvertDate(TestData *testData, const DataMap * /* settings */, UBool fmt) { UnicodeString kPATTERN("PATTERN="); // TODO: static UnicodeString kMILLIS("MILLIS="); // TODO: static UnicodeString kRELATIVE_MILLIS("RELATIVE_MILLIS="); // TODO: static UnicodeString kRELATIVE_ADD("RELATIVE_ADD:"); // TODO: static UErrorCode status = U_ZERO_ERROR; SimpleDateFormat basicFmt(UnicodeString("EEE MMM dd yyyy / YYYY'-W'ww-ee"), status); if (U_FAILURE(status)) { dataerrln("FAIL: Couldn't create basic SimpleDateFormat: %s", u_errorName(status)); return; } const DataMap *currentCase= NULL; // Start the processing int n = 0; while (testData->nextCase(currentCase, status)) { char calLoc[256] = ""; DateTimeStyleSet styleSet; UnicodeString pattern; UBool usePattern = FALSE; (void)usePattern; // Suppress unused warning. CalendarFieldsSet fromSet; UDate fromDate = 0; UBool useDate = FALSE; UDate now = Calendar::getNow(); ++n; char theCase[200]; sprintf(theCase, "case %d:", n); UnicodeString caseString(theCase, ""); // load params UnicodeString locale = currentCase->getString("locale", status); if (U_FAILURE(status)) { errln("case %d: No 'locale' line.", n); continue; } UnicodeString zone = currentCase->getString("zone", status); if (U_FAILURE(status)) { errln("case %d: No 'zone' line.", n); continue; } UnicodeString spec = currentCase->getString("spec", status); if(U_FAILURE(status)) { errln("case %d: No 'spec' line.", n); continue; } UnicodeString date = currentCase->getString("date", status); if(U_FAILURE(status)) { errln("case %d: No 'date' line.", n); continue; } UnicodeString expectStr= currentCase->getString("str", status); if(U_FAILURE(status)) { errln("case %d: No 'str' line.", n); continue; } DateFormat *format = NULL; // Process: 'locale' locale.extract(0, locale.length(), calLoc, (const char*)0); // default codepage. Invariant codepage doesn't have '@'! Locale loc(calLoc); if(spec.startsWith(kPATTERN)) { pattern = UnicodeString(spec,kPATTERN.length()); usePattern = TRUE; format = new SimpleDateFormat(pattern, loc, status); if(U_FAILURE(status)) { errln("case %d: could not create SimpleDateFormat from pattern: %s", n, u_errorName(status)); continue; } } else { if(styleSet.parseFrom(spec, status)<0 || U_FAILURE(status)) { errln("case %d: could not parse spec as style fields: %s", n, u_errorName(status)); continue; } format = DateFormat::createDateTimeInstance((DateFormat::EStyle)styleSet.getDateStyle(), (DateFormat::EStyle)styleSet.getTimeStyle(), loc); if(format == NULL ) { errln("case %d: could not create SimpleDateFormat from styles.", n); continue; } } Calendar *cal = Calendar::createInstance(loc, status); if(U_FAILURE(status)) { errln("case %d: could not create calendar from %s", n, calLoc); } if (zone.length() > 0) { TimeZone * tz = TimeZone::createTimeZone(zone); cal->setTimeZone(*tz); format->setTimeZone(*tz); delete tz; } // parse 'date' if(date.startsWith(kMILLIS)) { UnicodeString millis = UnicodeString(date, kMILLIS.length()); useDate = TRUE; fromDate = udbg_stod(millis); } else if(date.startsWith(kRELATIVE_MILLIS)) { UnicodeString millis = UnicodeString(date, kRELATIVE_MILLIS.length()); useDate = TRUE; fromDate = udbg_stod(millis) + now; } else if(date.startsWith(kRELATIVE_ADD)) { UnicodeString add = UnicodeString(date, kRELATIVE_ADD.length()); // "add" is a string indicating which fields to add if(fromSet.parseFrom(add, status)<0 || U_FAILURE(status)) { errln("case %d: could not parse date as RELATIVE_ADD calendar fields: %s", n, u_errorName(status)); continue; } useDate=TRUE; cal->clear(); cal->setTime(now, status); for (int q=0; q<UCAL_FIELD_COUNT; q++) { if (fromSet.isSet((UCalendarDateFields)q)) { //int32_t oldv = cal->get((UCalendarDateFields)q, status); if (q == UCAL_DATE) { cal->add((UCalendarDateFields)q, fromSet.get((UCalendarDateFields)q), status); } else { cal->set((UCalendarDateFields)q, fromSet.get((UCalendarDateFields)q)); } //int32_t newv = cal->get((UCalendarDateFields)q, status); } } fromDate = cal->getTime(status); if(U_FAILURE(status)) { errln("case %d: could not apply date as RELATIVE_ADD calendar fields: %s", n, u_errorName(status)); continue; } } else if(fromSet.parseFrom(date, status)<0 || U_FAILURE(status)) { errln("case %d: could not parse date as calendar fields: %s", n, u_errorName(status)); continue; } // now, do it. if (fmt) { FieldPosition pos; // logln((UnicodeString)"#"+n+" "+locale+"/"+from+" >>> "+toCalLoc+"/" // +to); cal->clear(); UnicodeString output; output.remove(); if(useDate) { // cal->setTime(fromDate, status); // if(U_FAILURE(status)) { // errln("case %d: could not set time on calendar: %s", n, u_errorName(status)); // continue; // } format->format(fromDate, output, pos, status); } else { fromSet.setOnCalendar(cal, status); if(U_FAILURE(status)) { errln("case %d: could not set fields on calendar: %s", n, u_errorName(status)); continue; } format->format(*cal, output, pos); } // check erro result from 'format' if(U_FAILURE(status)) { errln("case %d: could not format(): %s", n, u_errorName(status)); // TODO: use 'pos' } // if(pos.getBeginIndex()==0 && pos.getEndIndex()==0) { // TODO: more precise error? // errln("WARNING: case %d: format's pos returned (0,0) - error ??", n); // } if(output == expectStr) { logln(caseString+": format: SUCCESS! "+UnicodeString("expect=output=")+output); } else { UnicodeString result; UnicodeString result2; errln(caseString+": format: output!=expectStr, got " + *udbg_escape(output, &result) + " expected " + *udbg_escape(expectStr, &result2)); } } else { cal->clear(); ParsePosition pos; format->parse(expectStr,*cal,pos); if(useDate) { UDate gotDate = cal->getTime(status); if(U_FAILURE(status)) { errln(caseString+": parse: could not get time on calendar: "+UnicodeString(u_errorName(status))); continue; } if(gotDate == fromDate) { logln(caseString+": parse: SUCCESS! "+UnicodeString("gotDate=parseDate=")+expectStr); } else { UnicodeString expectDateStr, gotDateStr; basicFmt.format(fromDate,expectDateStr); basicFmt.format(gotDate,gotDateStr); errln(caseString+": parse: FAIL. parsed '"+expectStr+"' and got "+gotDateStr+", expected " + expectDateStr); } } else { // Calendar *cal2 = cal->clone(); // cal2->clear(); // fromSet.setOnCalendar(cal2, status); if(U_FAILURE(status)) { errln("case %d: parse: could not set fields on calendar: %s", n, u_errorName(status)); continue; } CalendarFieldsSet diffSet; // diffSet.clear(); if (!fromSet.matches(cal, diffSet, status)) { UnicodeString diffs = diffSet.diffFrom(fromSet, status); errln((UnicodeString)"FAIL: "+caseString +", Differences: '"+ diffs +"', status: "+ u_errorName(status)); } else if (U_FAILURE(status)) { errln("FAIL: "+caseString+" parse SET SOURCE calendar Failed to match: " +u_errorName(status)); } else { logln("PASS: "******" parse."); } } } delete cal; delete format; } // delete basicFmt; }
/** * Parse date-time objects * * @param str * @param format * @param tz * @param lenient * @param locale * * @return character vector * * @version 0.5-1 (Marek Gagolewski, 2015-01-08) * @version 0.5-1 (Marek Gagolewski, 2015-01-11) lenient arg added * @version 0.5-1 (Marek Gagolewski, 2015-02-22) use tz * @version 0.5-1 (Marek Gagolewski, 2015-03-01) set tzone attrib on retval */ SEXP stri_datetime_parse(SEXP str, SEXP format, SEXP lenient, SEXP tz, SEXP locale) { PROTECT(str = stri_prepare_arg_string(str, "str")); const char* locale_val = stri__prepare_arg_locale(locale, "locale", true); const char* format_val = stri__prepare_arg_string_1_notNA(format, "format"); bool lenient_val = stri__prepare_arg_logical_1_notNA(lenient, "lenient"); if (!isNull(tz)) PROTECT(tz = stri_prepare_arg_string_1(tz, "tz")); else PROTECT(tz); /* needed to set tzone attrib */ // "format" may be one of: const char* format_opts[] = { "date_full", "date_long", "date_medium", "date_short", "date_relative_full", "date_relative_long", "date_relative_medium", "date_relative_short", "time_full", "time_long", "time_medium", "time_short", "time_relative_full", "time_relative_long", "time_relative_medium", "time_relative_short", "datetime_full", "datetime_long", "datetime_medium", "datetime_short", "datetime_relative_full", "datetime_relative_long", "datetime_relative_medium", "datetime_relative_short", NULL}; int format_cur = stri__match_arg(format_val, format_opts); TimeZone* tz_val = stri__prepare_arg_timezone(tz, "tz", true/*allowdefault*/); Calendar* cal = NULL; DateFormat* fmt = NULL; STRI__ERROR_HANDLER_BEGIN(2) R_len_t vectorize_length = LENGTH(str); StriContainerUTF16 str_cont(str, vectorize_length); UnicodeString format_str(format_val); UErrorCode status = U_ZERO_ERROR; if (format_cur >= 0) { DateFormat::EStyle style = DateFormat::kNone; switch (format_cur % 8) { case 0: style = DateFormat::kFull; break; case 1: style = DateFormat::kLong; break; case 2: style = DateFormat::kMedium; break; case 3: style = DateFormat::kShort; break; case 4: style = DateFormat::kFullRelative; break; case 5: style = DateFormat::kLongRelative; break; case 6: style = DateFormat::kMediumRelative; break; case 7: style = DateFormat::kShortRelative; break; default: style = DateFormat::kNone; break; } /* ICU 54.1: Relative time styles are not currently supported. */ switch (format_cur / 8) { case 0: fmt = DateFormat::createDateInstance(style, Locale::createFromName(locale_val)); break; case 1: fmt = DateFormat::createTimeInstance((DateFormat::EStyle)(style & ~DateFormat::kRelative), Locale::createFromName(locale_val)); break; case 2: fmt = DateFormat::createDateTimeInstance(style, (DateFormat::EStyle)(style & ~DateFormat::kRelative), Locale::createFromName(locale_val)); break; default: fmt = NULL; break; } } else fmt = new SimpleDateFormat(format_str, Locale::createFromName(locale_val), status); STRI__CHECKICUSTATUS_THROW(status, {/* do nothing special on err */}) status = U_ZERO_ERROR; cal = Calendar::createInstance(locale_val, status); STRI__CHECKICUSTATUS_THROW(status, {/* do nothing special on err */}) cal->adoptTimeZone(tz_val); tz_val = NULL; /* The Calendar takes ownership of the TimeZone. */ cal->setLenient(lenient_val); SEXP ret; STRI__PROTECT(ret = Rf_allocVector(REALSXP, vectorize_length)); for (R_len_t i=0; i<vectorize_length; ++i) { if (str_cont.isNA(i)) { REAL(ret)[i] = NA_REAL; continue; } status = U_ZERO_ERROR; ParsePosition pos; fmt->parse(str_cont.get(i), *cal, pos); if (pos.getErrorIndex() >= 0) REAL(ret)[i] = NA_REAL; else { status = U_ZERO_ERROR; REAL(ret)[i] = ((double)cal->getTime(status))/1000.0; if (U_FAILURE(status)) REAL(ret)[i] = NA_REAL; } } if (!isNull(tz)) Rf_setAttrib(ret, Rf_ScalarString(Rf_mkChar("tzone")), tz); stri__set_class_POSIXct(ret); if (tz_val) { delete tz_val; tz_val = NULL; } if (fmt) { delete fmt; fmt = NULL; } if (cal) { delete cal; cal = NULL; } STRI__UNPROTECT_ALL return ret; STRI__ERROR_HANDLER_END({ if (tz_val) { delete tz_val; tz_val = NULL; } if (fmt) { delete fmt; fmt = NULL; } if (cal) { delete cal; cal = NULL; } }) }