Try to parse date with ICU

Signed-off-by: Seonghyun Kim <sh8281.kim@samsung.com>
This commit is contained in:
Seonghyun Kim 2025-07-10 16:41:15 +09:00 committed by Hyukwoo Park
commit f2b6d3d5e0
3 changed files with 32 additions and 4 deletions

View file

@ -1048,6 +1048,32 @@ time64_t DateObject::parseStringToDate(ExecutionState& state, String* istr)
if (IS_VALID_TIME(primitiveValue) && IS_IN_TIME_RANGE(primitiveValue)) {
return primitiveValue;
} else {
// fallback. try to parse date with icu
#if defined(ENABLE_ICU)
UChar* buf = (UChar*)alloca(sizeof(UChar) * (state.context()->vmInstance()->timezoneID().size() + 1));
int32_t len = state.context()->vmInstance()->timezoneID().size();
buf[len] = 0;
for (int32_t i = 0; i < len; i++) {
buf[i] = state.context()->vmInstance()->timezoneID()[i];
}
auto u16 = istr->toUTF16StringData();
for (size_t i = 0; i <= UDateFormatStyle::UDAT_SHORT; i++) {
for (size_t j = 0; j <= UDateFormatStyle::UDAT_SHORT; j++) {
UErrorCode err = U_ZERO_ERROR;
UDateFormat* format = udat_open(static_cast<UDateFormatStyle>(i), static_cast<UDateFormatStyle>(j),
state.context()->vmInstance()->locale().data(), buf, len, nullptr, 0, &err);
if (U_FAILURE(err)) {
continue;
}
auto result = udat_parse(format, u16.data(), u16.length(), nullptr, &err);
udat_close(format);
if (!U_FAILURE(err)) {
return result;
}
}
}
#endif
return TIME64NAN;
}
}
@ -1398,11 +1424,11 @@ static String* formatDateTimeString(ExecutionState& state, DateObject* self, boo
UDateFormat* format = nullptr;
UErrorCode err = U_ZERO_ERROR;
if (isDate) {
udat_open(UDateFormatStyle::UDAT_NONE, UDateFormatStyle::UDAT_MEDIUM, state.context()->vmInstance()->locale().data(),
buf, len, nullptr, 0, &err);
format = udat_open(UDateFormatStyle::UDAT_NONE, UDateFormatStyle::UDAT_MEDIUM, state.context()->vmInstance()->locale().data(),
buf, len, nullptr, 0, &err);
} else {
udat_open(UDateFormatStyle::UDAT_MEDIUM, UDateFormatStyle::UDAT_NONE, state.context()->vmInstance()->locale().data(),
buf, len, nullptr, 0, &err);
format = udat_open(UDateFormatStyle::UDAT_MEDIUM, UDateFormatStyle::UDAT_NONE, state.context()->vmInstance()->locale().data(),
buf, len, nullptr, 0, &err);
}
UFieldPosition field;
field.field = -1;

View file

@ -96,6 +96,7 @@
#define udat_close RuntimeICUBinder::ICU::instance().udat_close
#define udat_getCalendar RuntimeICUBinder::ICU::instance().udat_getCalendar
#define udat_toPattern RuntimeICUBinder::ICU::instance().udat_toPattern
#define udat_parse RuntimeICUBinder::ICU::instance().udat_parse
#define uenum_count RuntimeICUBinder::ICU::instance().uenum_count
#define uenum_unext RuntimeICUBinder::ICU::instance().uenum_unext

View file

@ -117,6 +117,7 @@ namespace RuntimeICUBinder {
F(udat_formatForFields, int32_t (*)(const UDateFormat* format, UDate, UChar*, int32_t, UFieldPositionIterator*, UErrorCode*), int32_t) \
F(udat_getCalendar, const UCalendar* (*)(const UDateFormat* fmt), const UCalendar*) \
F(udat_toPattern, int32_t (*)(const UDateFormat* fmt, UBool localized, UChar* result, int32_t resultLength, UErrorCode* status), int32_t) \
F(udat_parse, UDate (*)(const UDateFormat*, const UChar*, int32_t, int32_t*, UErrorCode*), UDate) \
F(uenum_count, int32_t (*)(UEnumeration * en, UErrorCode * status), int32_t) \
F(uenum_unext, const UChar* (*)(UEnumeration * en, int32_t * resultLength, UErrorCode * status), const UChar*) \
F(uenum_next, const char* (*)(UEnumeration * en, int32_t * resultLength, UErrorCode * status), const char*) \