Skip to content

Commit

Permalink
fix from_unixtime throw exception with overflowed argument and timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
NEUpanning committed May 16, 2024
1 parent 08ffe22 commit 187fa91
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion velox/functions/lib/DateTimeFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ int32_t DateTimeFormatter::format(
bool allowOverflow) const {
Timestamp t = timestamp;
if (timezone != nullptr) {
t.toTimezone(*timezone);
t.toTimezone(*timezone, allowOverflow);
}
const auto timePoint = t.toTimePoint(allowOverflow);
const auto daysTimePoint = date::floor<date::days>(timePoint);
Expand Down
6 changes: 3 additions & 3 deletions velox/functions/sparksql/tests/DateTimeFunctionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,17 +887,17 @@ TEST_F(DateTimeFunctionsTest, fromUnixtime) {
fromUnixTime(getUnixTime("2020-06-30 23:59:59"), "yyyy-MM-dd HH:mm:ss"),
"2020-06-30 23:59:59");

// 8 hours ahead UTC.
setQueryTimeZone("Asia/Shanghai");
// In debug mode, Timestamp constructor will throw exception if range check
// fails.
#ifdef NDEBUG
// Integer overflow in the internal conversion from seconds to milliseconds.
EXPECT_EQ(
fromUnixTime(std::numeric_limits<int64_t>::max(), "yyyy-MM-dd HH:mm:ss"),
"1969-12-31 23:59:59");
"1970-01-01 07:59:59");
#endif

// 8 hours ahead UTC.
setQueryTimeZone("Asia/Shanghai");
EXPECT_EQ(fromUnixTime(0, "yyyy-MM-dd HH:mm:ss"), "1970-01-01 08:00:00");
EXPECT_EQ(fromUnixTime(120, "yyyy-MM-dd HH:mm"), "1970-01-01 08:02");
EXPECT_EQ(fromUnixTime(-59, "yyyy-MM-dd HH:mm:ss"), "1970-01-01 07:59:01");
Expand Down
4 changes: 2 additions & 2 deletions velox/type/Timestamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ Timestamp::toTimePoint(bool allowOverflow) const {
return tp;
}

void Timestamp::toTimezone(const date::time_zone& zone) {
auto tp = toTimePoint();
void Timestamp::toTimezone(const date::time_zone& zone, bool allowOverflow) {
auto tp = toTimePoint(allowOverflow);
auto epoch = zone.to_local(tp).time_since_epoch();
// NOTE: Round down to get the seconds of the current time point.
seconds_ = std::chrono::floor<std::chrono::seconds>(epoch).count();
Expand Down
2 changes: 1 addition & 1 deletion velox/type/Timestamp.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ struct Timestamp {
// Example: Timestamp ts{0, 0};
// ts.Timezone("America/Los_Angeles");
// ts.toString() returns December 31, 1969 16:00:00
void toTimezone(const date::time_zone& zone);
void toTimezone(const date::time_zone& zone, bool allowOverflow = false);

// Same as above, but accepts PrestoDB time zone ID.
void toTimezone(int16_t tzID);
Expand Down

0 comments on commit 187fa91

Please sign in to comment.