Skip to content

Commit

Permalink
Remove old daylightSavingTA() (facebook#1245)
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: facebook#1245

Reviewed By: avp

Differential Revision: D52579225

fbshipit-source-id: 42ff0485244e59b6d2447795b8f6d06180f02968
  • Loading branch information
lavenzg authored and facebook-github-bot committed Jun 26, 2024
1 parent 59ea64c commit 568a14c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 48 deletions.
4 changes: 0 additions & 4 deletions include/hermes/VM/JSLib/DateUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ double localTZA();
//===----------------------------------------------------------------------===//
// ES5.1 15.9.1.8

/// Daylight saving time adjustment, in milliseconds, at time \p t.
/// \param t timestamp in milliseconds.
double daylightSavingTA(double t);

namespace detail {
// Exposed for test only
int32_t equivalentTime(int64_t epochSecs);
Expand Down
44 changes: 0 additions & 44 deletions lib/VM/JSLib/DateUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,50 +372,6 @@ int32_t detail::equivalentTime(int64_t epochSecs) {
return (eqYearAsEpochDays + dayOfYear) * SECONDS_PER_DAY + secsOfDay;
}

double daylightSavingTA(double t) {
// The spec says LocalTime should only take finite time value and return 0 in
// case conversion fails. Once we enforce the finite input at the caller site,
// we should remove the below check or replace it with an assertion. For now,
// let's return NaN instead if it's not finite value.
if (!std::isfinite(t)) {
return std::numeric_limits<double>::quiet_NaN();
}

// Convert t to seconds and get the actual time needed.
const double seconds = t / MS_PER_SECOND;
// If the number of seconds is higher or lower than a unix timestamp can
// support, clamp it. This is not correct in all cases, but returning NaN (for
// Invalid Date) breaks date construction entirely. Clamping only results in
// small errors in daylight savings time. This is only a problem in systems
// with a 32-bit time_t, like some Android systems.
time_t local = 0;
if (seconds > TIME_RANGE_SECS || seconds < -TIME_RANGE_SECS) {
// Return NaN if input is outside Time Range allowed in ES5.1
return std::numeric_limits<double>::quiet_NaN();
}
// This will truncate any fractional seconds, which is ok for daylight
// savings time calculations.
local = detail::equivalentTime(static_cast<int64_t>(seconds));

std::tm tm;
#ifdef _WINDOWS
// The return value of localtime_s on Windows is an error code instead of
// a pointer to std::tm. For simplicity, we don't inspect the concrete error
// code and just return 0.
auto err = ::localtime_s(&tm, &local);
if (err) {
return 0;
}
#else
std::tm *brokenTime = ::localtime_r(&local, &tm);
if (!brokenTime) {
// Local time is invalid.
return 0;
}
#endif
return tm.tm_isdst ? MS_PER_HOUR : 0;
}

//===----------------------------------------------------------------------===//
// ES5.1 15.9.1.9

Expand Down
8 changes: 8 additions & 0 deletions unittests/VMRuntime/DateUtilTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,21 @@ TEST(DateUtilTest, EquivalentTimeTest) {
}

TEST(DateUtilTest, DaylightSavingTATest) {
LocalTimeOffsetCache localTimeOffsetCache;
/// A wrapper that implements the deleted daylightSavingTA() function.
auto daylightSavingTA = [&localTimeOffsetCache](double timeMs) {
return localTimeOffsetCache.daylightSavingOffsetInMs(timeMs);
};

setTimeZone("America/Los_Angeles");
localTimeOffsetCache.reset();
EXPECT_EQ(MS_PER_HOUR, daylightSavingTA(1489530532000)); // Mar 14, 2017
EXPECT_EQ(MS_PER_HOUR, daylightSavingTA(1019514530000)); // Apr 22, 2002
EXPECT_EQ(0, daylightSavingTA(1487111330000)); // Feb 14, 2017
EXPECT_EQ(0, daylightSavingTA(1017700130000)); // Apr 1, 2002

setTimeZone("America/Chicago");
localTimeOffsetCache.reset();
EXPECT_EQ(MS_PER_HOUR, daylightSavingTA(1489530532000)); // Mar 14, 2017
EXPECT_EQ(MS_PER_HOUR, daylightSavingTA(1019514530000)); // Apr 22, 2002
EXPECT_EQ(0, daylightSavingTA(1487111330000)); // Feb 14, 2017
Expand Down

0 comments on commit 568a14c

Please sign in to comment.