Skip to content

Commit de93dc2

Browse files
committed
Fix to time implementation to get the time of the day
1 parent b44aea0 commit de93dc2

4 files changed

Lines changed: 39 additions & 43 deletions

File tree

cpp/src/gandiva/precompiled/epoch_time_point.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,18 @@ class EpochTimePoint {
8787

8888
int64_t MillisSinceEpoch() const { return tp_.time_since_epoch().count(); }
8989

90-
private:
91-
arrow_vendored::date::year_month_day YearMonthDay() const {
92-
return arrow_vendored::date::year_month_day{
93-
arrow_vendored::date::floor<arrow_vendored::date::days>(tp_)}; // NOLINT
94-
}
95-
9690
arrow_vendored::date::time_of_day<std::chrono::milliseconds> TimeOfDay() const {
9791
auto millis_since_midnight =
9892
tp_ - arrow_vendored::date::floor<arrow_vendored::date::days>(tp_);
9993
return arrow_vendored::date::time_of_day<std::chrono::milliseconds>(
10094
millis_since_midnight);
10195
}
10296

97+
private:
98+
arrow_vendored::date::year_month_day YearMonthDay() const {
99+
return arrow_vendored::date::year_month_day{
100+
arrow_vendored::date::floor<arrow_vendored::date::days>(tp_)}; // NOLINT
101+
}
102+
103103
std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp_;
104104
};

cpp/src/gandiva/precompiled/time.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -851,10 +851,11 @@ gdv_int64 castBIGINT_daytimeinterval(gdv_day_time_interval in) {
851851
NUMERIC_TYPES(TO_TIMESTAMP)
852852

853853
// Convert the seconds since epoch argument to time
854-
#define TO_TIME(TYPE) \
855-
FORCE_INLINE \
856-
gdv_time32 to_time##_##TYPE(gdv_##TYPE seconds) { \
857-
return static_cast<gdv_time32>(seconds) * MILLIS_IN_SEC; \
854+
#define TO_TIME(TYPE) \
855+
FORCE_INLINE \
856+
gdv_timestamp to_time##_##TYPE(gdv_##TYPE seconds) { \
857+
EpochTimePoint tp(seconds * MILLIS_IN_SEC); \
858+
return static_cast<gdv_timestamp>(tp.TimeOfDay().to_duration().count()); \
858859
}
859860

860861
NUMERIC_TYPES(TO_TIME)

cpp/src/gandiva/precompiled/time_test.cc

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -776,35 +776,30 @@ TEST(TestTime, TestToTimestamp) {
776776
}
777777

778778
TEST(TestTime, TestToTimeNumeric) {
779-
auto millis = 0;
780-
EXPECT_EQ(millis, to_time_int32(0));
781-
EXPECT_EQ(millis, to_time_int64(0));
782-
EXPECT_EQ(millis, to_time_float32(0));
783-
EXPECT_EQ(millis, to_time_float64(0));
784-
785-
millis = 1000;
786-
EXPECT_EQ(millis, to_time_int32(1));
787-
EXPECT_EQ(millis, to_time_int64(1));
788-
EXPECT_EQ(millis, to_time_float32(1));
789-
EXPECT_EQ(millis, to_time_float64(1));
790-
791-
millis = 600000;
792-
EXPECT_EQ(millis, to_time_int32(600));
793-
EXPECT_EQ(millis, to_time_int64(600));
794-
EXPECT_EQ(millis, to_time_float32(600));
795-
EXPECT_EQ(millis, to_time_float64(600));
796-
797-
millis = 360000;
798-
EXPECT_EQ(millis, to_time_int32(360));
799-
EXPECT_EQ(millis, to_time_int64(360));
800-
EXPECT_EQ(millis, to_time_float32(360));
801-
EXPECT_EQ(millis, to_time_float64(360));
802-
803-
millis = 86400000;
804-
EXPECT_EQ(millis, to_time_int32(86400));
805-
EXPECT_EQ(millis, to_time_int64(86400));
806-
EXPECT_EQ(millis, to_time_float32(86400));
807-
EXPECT_EQ(millis, to_time_float64(86400));
779+
780+
auto ts = StringToTimestamp("1970-01-02 00:00:00") / 1000;
781+
EXPECT_EQ(0, to_time_int32(ts));
782+
EXPECT_EQ(0, to_time_int64(ts));
783+
EXPECT_EQ(0, to_time_float32(ts));
784+
EXPECT_EQ(0, to_time_float64(ts));
785+
786+
ts = StringToTimestamp("1970-01-01 00:00:01") / 1000;
787+
EXPECT_EQ(1000, to_time_int32(ts));
788+
EXPECT_EQ(1000, to_time_int64(ts));
789+
EXPECT_EQ(1000, to_time_float32(ts));
790+
EXPECT_EQ(1000, to_time_float64(ts));
791+
792+
ts = StringToTimestamp("1970-01-01 01:00:00") / 1000;
793+
EXPECT_EQ(3600000, to_time_int32(ts));
794+
EXPECT_EQ(3600000, to_time_int64(ts));
795+
EXPECT_EQ(3600000, to_time_float32(ts));
796+
EXPECT_EQ(3600000, to_time_float64(ts));
797+
798+
ts = StringToTimestamp("1970-01-01 23:59:59") / 1000;
799+
EXPECT_EQ(86399000, to_time_int32(ts));
800+
EXPECT_EQ(86399000, to_time_int64(ts));
801+
EXPECT_EQ(86399000, to_time_float32(ts));
802+
EXPECT_EQ(86399000, to_time_float64(ts));
808803
}
809804

810805
} // namespace gandiva

cpp/src/gandiva/precompiled/types.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ gdv_timestamp to_timestamp_int64(gdv_int64);
104104
gdv_timestamp to_timestamp_float32(gdv_float32);
105105
gdv_timestamp to_timestamp_float64(gdv_float64);
106106

107-
gdv_time32 to_time_int32(gdv_int32);
108-
gdv_time32 to_time_int64(gdv_int64);
109-
gdv_time32 to_time_float32(gdv_float32);
110-
gdv_time32 to_time_float64(gdv_float64);
107+
gdv_timestamp to_time_int32(gdv_int32);
108+
gdv_timestamp to_time_int64(gdv_int64);
109+
gdv_timestamp to_time_float32(gdv_float32);
110+
gdv_timestamp to_time_float64(gdv_float64);
111111

112112
gdv_int64 date_sub_timestamp_int32(gdv_timestamp, gdv_int32);
113113
gdv_int64 subtract_timestamp_int32(gdv_timestamp, gdv_int32);

0 commit comments

Comments
 (0)