Skip to content

Commit

Permalink
Use ucal_* API instead of vzone_* API of ICU
Browse files Browse the repository at this point in the history
Signed-off-by: Seonghyun Kim <[email protected]>
  • Loading branch information
ksh8281 authored and clover2123 committed Jun 19, 2023
1 parent 3418276 commit 3aaded1
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 33 deletions.
25 changes: 16 additions & 9 deletions src/runtime/DateObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,15 @@ static int equivalentYearForDST(int year)
time64_t DateObject::applyLocalTimezoneOffset(ExecutionState& state, time64_t t)
{
#if defined(ENABLE_ICU)
UErrorCode succ = U_ZERO_ERROR;
UErrorCode status = U_ZERO_ERROR;
#endif
int32_t stdOffset = 0, dstOffset = 0;

// roughly check range before calling yearFromTime function
#if defined(ENABLE_ICU) && !defined(OS_WINDOWS_UWP)
stdOffset = vzone_getRawOffset(state.context()->vmInstance()->vzone());
#if defined(ENABLE_ICU)
auto cal = state.context()->vmInstance()->calendar();
ucal_setMillis(cal, t, &status);
stdOffset = ucal_get(cal, UCAL_ZONE_OFFSET, &status);
#else
stdOffset = 0;
#endif
Expand All @@ -300,15 +302,17 @@ time64_t DateObject::applyLocalTimezoneOffset(ExecutionState& state, time64_t t)
time64_t msBetweenYears = (realYear != equivalentYear) ? (timeFromYear(equivalentYear) - timeFromYear(realYear)) : 0;

t += msBetweenYears;
#if defined(ENABLE_ICU) && !defined(OS_WINDOWS_UWP)
vzone_getOffset3(state.context()->vmInstance()->vzone(), t, true, stdOffset, dstOffset, succ);
#if defined(ENABLE_ICU)
ucal_setMillis(cal, t, &status);
stdOffset = ucal_get(cal, UCAL_ZONE_OFFSET, &status);
dstOffset = ucal_get(cal, UCAL_DST_OFFSET, &status);
#else
dstOffset = 0;
#endif
t -= msBetweenYears;
#if defined(ENABLE_ICU)
// range check should be completed by caller function
if (!U_FAILURE(succ)) {
if (!U_FAILURE(status)) {
return t - (stdOffset + dstOffset);
}
return TIME64NAN;
Expand Down Expand Up @@ -1241,9 +1245,12 @@ void DateObject::resolveCache(ExecutionState& state)
t += msBetweenYears;

int32_t stdOffset = 0, dstOffset = 0;
#if defined(ENABLE_ICU) && !defined(OS_WINDOWS_UWP)
UErrorCode succ = U_ZERO_ERROR;
vzone_getOffset3(state.context()->vmInstance()->vzone(), t, true, stdOffset, dstOffset, succ);
#if defined(ENABLE_ICU)
UErrorCode status = U_ZERO_ERROR;
auto cal = state.context()->vmInstance()->calendar();
ucal_setMillis(cal, t, &status);
stdOffset = ucal_get(cal, UCAL_ZONE_OFFSET, &status);
dstOffset = ucal_get(cal, UCAL_DST_OFFSET, &status);
#endif

m_cachedLocal.isdst = dstOffset == 0 ? 0 : 1;
Expand Down
12 changes: 11 additions & 1 deletion src/runtime/TemporalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2533,7 +2533,17 @@ Value TemporalTimeZoneObject::builtinTimeZoneGetOffsetStringFor(ExecutionState&
Value TemporalTimeZoneObject::getIANATimeZoneOffsetNanoseconds(ExecutionState& state, const Value& epochNanoseconds, const std::string& timeZoneIdentifier)
{
auto u16 = utf8StringToUTF16String(timeZoneIdentifier.data(), timeZoneIdentifier.size());
return epochNanoseconds.asBigInt()->addition(state, new BigInt((int64_t)vzone_getRawOffset(vzone_openID(u16.data(), u16.size()))));
UErrorCode status = U_ZERO_ERROR;
UCalendar* cal = ucal_open(u16.data(), u16.length(), "en", UCAL_DEFAULT, &status);
int64_t offset = 0;
if (U_SUCCESS(status)) {
if (U_SUCCESS(status)) {
offset = ucal_get(cal, UCAL_ZONE_OFFSET, &status);
}
ucal_close(cal);
}

return epochNanoseconds.asBigInt()->addition(state, new BigInt(offset));
}

std::map<TemporalObject::DateTimeUnits, int> TemporalTimeZoneObject::getIANATimeZoneDateTimeParts(ExecutionState& state, const Value& epochNanoseconds)
Expand Down
17 changes: 7 additions & 10 deletions src/runtime/VMInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,8 @@ VMInstance::~VMInstance()
}

clearCachesRelatedWithContext();
#if defined(ENABLE_ICU) && !defined(OS_WINDOWS_UWP)
vzone_close(m_timezone);
#if defined(ENABLE_ICU)
ucal_close(m_calendar);
#endif

#if defined(ENABLE_CODE_CACHE)
Expand Down Expand Up @@ -413,9 +413,7 @@ VMInstance::VMInstance(const char* locale, const char* timezone, const char* bas
memset(m_regexpOptionStringCache, 0, 64 * sizeof(ASCIIString*));

#if defined(ENABLE_ICU)
#if !defined(OS_WINDOWS_UWP)
m_timezone = nullptr;
#endif
m_calendar = nullptr;
if (timezone) {
m_timezoneID = timezone;
} else if (getenv("TZ")) {
Expand Down Expand Up @@ -571,14 +569,13 @@ static std::string findTimezone()
#endif


void VMInstance::ensureVZone()
void VMInstance::ensureCalendar()
{
ensureTimezoneID();

#if !defined(OS_WINDOWS_UWP)
auto u16 = utf8StringToUTF16String(timezoneID().data(), timezoneID().size());
m_timezone = vzone_openID(u16.data(), u16.size());
#endif
UErrorCode status = U_ZERO_ERROR;
m_calendar = ucal_open(u16.data(), u16.length(), "en", UCAL_DEFAULT, &status);
RELEASE_ASSERT(m_calendar);
}

void VMInstance::ensureTimezoneID()
Expand Down
13 changes: 6 additions & 7 deletions src/runtime/VMInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,13 @@ class VMInstance : public gc {
return m_locale;
}

#if !defined(OS_WINDOWS_UWP)
VZone* vzone()
UCalendar* calendar()
{
if (m_timezone == nullptr) {
ensureVZone();
if (m_calendar == nullptr) {
ensureCalendar();
}
return m_timezone;
return m_calendar;
}
#endif

const std::string& timezoneID()
{
Expand Down Expand Up @@ -462,12 +460,13 @@ class VMInstance : public gc {
// date object data
#ifdef ENABLE_ICU
std::string m_locale;
UCalendar* m_calendar;
#if !defined(OS_WINDOWS_UWP)
VZone* m_timezone;
#endif
std::string m_timezoneID;
void ensureTimezoneID();
void ensureVZone();
void ensureCalendar();
#endif
void ensureTzname();
std::string m_tzname[2];
Expand Down
2 changes: 2 additions & 0 deletions third_party/runtime_icu_binder/ICUPolyfill.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,12 @@
#define ucal_openTimeZoneIDEnumeration RuntimeICUBinder::ICU::instance().ucal_openTimeZoneIDEnumeration
#define ucal_openTimeZones RuntimeICUBinder::ICU::instance().ucal_openTimeZones
#define ucal_getCanonicalTimeZoneID RuntimeICUBinder::ICU::instance().ucal_getCanonicalTimeZoneID
#define ucal_get RuntimeICUBinder::ICU::instance().ucal_get
#define ucal_getType RuntimeICUBinder::ICU::instance().ucal_getType
#define ucal_getAttribute RuntimeICUBinder::ICU::instance().ucal_getAttribute
#define ucal_getDayOfWeekType RuntimeICUBinder::ICU::instance().ucal_getDayOfWeekType
#define ucal_setGregorianChange RuntimeICUBinder::ICU::instance().ucal_setGregorianChange
#define ucal_setMillis RuntimeICUBinder::ICU::instance().ucal_setMillis

#define udatpg_close RuntimeICUBinder::ICU::instance().udatpg_close
#define udatpg_open RuntimeICUBinder::ICU::instance().udatpg_open
Expand Down
2 changes: 2 additions & 0 deletions third_party/runtime_icu_binder/RuntimeICUBinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ namespace RuntimeICUBinder {
F(ucal_openTimeZoneIDEnumeration, UEnumeration* (*)(USystemTimeZoneType zoneType, const char* region, const int32_t* rawOffset, UErrorCode* ec), UEnumeration*) \
F(ucal_openTimeZones, UEnumeration* (*)(UErrorCode * ec), UEnumeration*) \
F(ucal_getCanonicalTimeZoneID, int32_t (*)(const UChar* id, int32_t len, UChar* result, int32_t resultCapacity, UBool* isSystemID, UErrorCode* status), int32_t) \
F(ucal_get, int32_t (*)(const UCalendar* cal, UCalendarDateFields field, UErrorCode* status), int32_t) \
F(ucal_getType, const char* (*)(const UCalendar* cal, UErrorCode* status), const char*) \
F(ucal_getAttribute, int32_t (*)(const UCalendar* cal, UCalendarAttribute attr), int32_t) \
F(ucal_getDayOfWeekType, UCalendarWeekdayType (*)(const UCalendar* cal, UCalendarDaysOfWeek dayOfWeek, UErrorCode* status), UCalendarWeekdayType) \
Expand Down Expand Up @@ -199,6 +200,7 @@ namespace RuntimeICUBinder {
F(unumsys_close, void (*)(UNumberingSystem*), void) \
F(ucal_close, void (*)(UCalendar * cal), void) \
F(ucal_setGregorianChange, void (*)(UCalendar * cal, UDate date, UErrorCode * pErrorCode), void) \
F(ucal_setMillis, void (*)(UCalendar * cal, UDate date, UErrorCode* status), void) \
F(udatpg_close, void (*)(UDateTimePatternGenerator * zone), void) \
F(unum_close, void (*)(UNumberFormat*), void) \
F(unum_setTextAttribute, void (*)(UNumberFormat * fmt, UNumberFormatTextAttribute tag, const UChar* newValue, int32_t newValueLength, UErrorCode* status), void) \
Expand Down
1 change: 1 addition & 0 deletions tools/test/chakracore/Date.rlexe.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<baseline>date_cache_bug.baseline</baseline>
<!-- test is timezone-sensitive; -->
<timezone-sensitive></timezone-sensitive>
<escargot-skip>Test output is wrong</escargot-skip>
</default>
</test>
<test>
Expand Down
6 changes: 3 additions & 3 deletions tools/test/chakracore/chakracore.x86.orig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ Running all tests
[Date] MillisecondTruncationCheckAfterCopyConstructor.js .......... Pass
[Date] Conversions.js .......... Skip (Implementation-dependent case)
[Date] Conversions.js .......... Skip (Implementation-dependent case)
[Date] date_cache_bug.js .......... Pass
[Date] date_cache_bug.js .......... Skip (Test output is wrong)
[Date] formatting.js .......... Skip (Implementation-dependent case)
[Error] errorProps.js .......... Skip (Non-standard feature)
[Error] ErrorCtorProps.js .......... Skip (different error message)
Expand Down Expand Up @@ -2040,7 +2040,7 @@ TESTNAME TOTAL PASS FAIL SKIP PASS_RATIO
Generated 110 108 0 2 (98%)
Closures 24 17 0 7 (70%)
Strings 38 26 0 12 (68%)
Date 12 4 0 8 (33%)
Date 12 3 0 9 (25%)
Error 25 1 0 24 (4%)
Boolean 7 6 0 1 (85%)
Number 8 6 0 2 (75%)
Expand Down Expand Up @@ -2074,4 +2074,4 @@ TESTNAME TOTAL PASS FAIL SKIP PASS_RATIO
switchStatement 28 24 0 4 (85%)
DebuggerCommon 205 68 0 137 (33%)
==========================================================
Total 1998 1154 0 844 (57%)
Total 1998 1153 0 845 (57%)
6 changes: 3 additions & 3 deletions tools/test/chakracore/chakracore.x86_64.orig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ Running all tests
[Date] MillisecondTruncationCheckAfterCopyConstructor.js .......... Pass
[Date] Conversions.js .......... Skip (Implementation-dependent case)
[Date] Conversions.js .......... Skip (Implementation-dependent case)
[Date] date_cache_bug.js .......... Pass
[Date] date_cache_bug.js .......... Skip (Test output is wrong)
[Date] formatting.js .......... Skip (Implementation-dependent case)
[Error] errorProps.js .......... Skip (Non-standard feature)
[Error] ErrorCtorProps.js .......... Skip (different error message)
Expand Down Expand Up @@ -2040,7 +2040,7 @@ TESTNAME TOTAL PASS FAIL SKIP PASS_RATIO
Generated 110 108 0 2 (98%)
Closures 24 17 0 7 (70%)
Strings 38 26 0 12 (68%)
Date 12 4 0 8 (33%)
Date 12 3 0 9 (25%)
Error 25 1 0 24 (4%)
Boolean 7 6 0 1 (85%)
Number 8 6 0 2 (75%)
Expand Down Expand Up @@ -2074,4 +2074,4 @@ TESTNAME TOTAL PASS FAIL SKIP PASS_RATIO
switchStatement 28 24 0 4 (85%)
DebuggerCommon 205 68 0 137 (33%)
==========================================================
Total 1998 1154 0 844 (57%)
Total 1998 1153 0 845 (57%)

0 comments on commit 3aaded1

Please sign in to comment.