Skip to content

Commit

Permalink
add outer functions in datetime types
Browse files Browse the repository at this point in the history
  • Loading branch information
kche0169 committed Jan 16, 2025
1 parent 3683314 commit 428a224
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
16 changes: 6 additions & 10 deletions src/function/scalar/day_of_month.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ inline bool DayOfMonthFunction::Run(DateT left, BigIntT &result) {

template <>
inline bool DayOfMonthFunction::Run(DateTimeT left, BigIntT &result) {
auto given_year = DateTimeT::GetDateTimePart(left, TimeUnit::kYear);
auto given_month = DateTimeT::GetDateTimePart(left, TimeUnit::kMonth);
auto given_day = DateTimeT::GetDateTimePart(left, TimeUnit::kDay);
year_month_day ymd{year(given_year), month(given_month), day(given_day)};
year_month_day ymd;
DateTimeT::OuterDateTime2YMD(left.date, ymd);
sys_days sd = sys_days(ymd);
year_month_day start{year(given_year), month(given_month), day(1)};
year_month_day start{ymd.year(), ymd.month(), day(1)};
sys_days start_sd = sys_days(start);
auto days_diff = sd - start_sd;
result = days_diff.count() + 1;
Expand All @@ -65,12 +63,10 @@ inline bool DayOfMonthFunction::Run(DateTimeT left, BigIntT &result) {

template <>
inline bool DayOfMonthFunction::Run(TimestampT left, BigIntT &result) {
auto given_year = TimestampT::GetDateTimePart(left, TimeUnit::kYear);
auto given_month = TimestampT::GetDateTimePart(left, TimeUnit::kMonth);
auto given_day = TimestampT::GetDateTimePart(left, TimeUnit::kDay);
year_month_day ymd{year(given_year), month(given_month), day(given_day)};
year_month_day ymd;
TimestampT::OuterDateTime2YMD(left.date, ymd);
sys_days sd = sys_days(ymd);
year_month_day start{year(given_year), month(given_month), day(1)};
year_month_day start{ymd.year(), ymd.month(), day(1)};
sys_days start_sd = sys_days(start);
auto days_diff = sd - start_sd;
result = days_diff.count() + 1;
Expand Down
12 changes: 4 additions & 8 deletions src/function/scalar/day_of_week.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,8 @@ inline bool DayOfWeekFunction::Run(DateT left, BigIntT &result) {

template <>
inline bool DayOfWeekFunction::Run(DateTimeT left, BigIntT &result) {
auto given_year = DateTimeT::GetDateTimePart(left, TimeUnit::kYear);
auto given_month = DateTimeT::GetDateTimePart(left, TimeUnit::kMonth);
auto given_day = DateTimeT::GetDateTimePart(left, TimeUnit::kDay);
year_month_day ymd{year(given_year), month(given_month), day(given_day)};
year_month_day ymd;
DateTimeT::OuterDateTime2YMD(left.date, ymd);
weekday wd = weekday{ymd};
days diff = (wd - weekday{0}) % days{7};
sys_days ymd_sys_days = sys_days(ymd);
Expand All @@ -69,10 +67,8 @@ inline bool DayOfWeekFunction::Run(DateTimeT left, BigIntT &result) {

template <>
inline bool DayOfWeekFunction::Run(TimestampT left, BigIntT &result) {
auto given_year = TimestampT::GetDateTimePart(left, TimeUnit::kYear);
auto given_month = TimestampT::GetDateTimePart(left, TimeUnit::kMonth);
auto given_day = TimestampT::GetDateTimePart(left, TimeUnit::kDay);
year_month_day ymd{year(given_year), month(given_month), day(given_day)};
year_month_day ymd;
TimestampT::OuterDateTime2YMD(left.date, ymd);
weekday wd = weekday{ymd};
days diff = (wd - weekday{0}) % days{7};
sys_days ymd_sys_days = sys_days(ymd);
Expand Down
13 changes: 5 additions & 8 deletions src/function/scalar/day_of_year.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ inline bool DayOfYearFunction::Run(DateT left, BigIntT &result) {

template <>
inline bool DayOfYearFunction::Run(DateTimeT left, BigIntT &result) {
auto given_year = DateTimeT::GetDateTimePart(left, TimeUnit::kYear);
auto given_month = DateTimeT::GetDateTimePart(left, TimeUnit::kMonth);
auto given_day = DateTimeT::GetDateTimePart(left, TimeUnit::kDay);
year_month_day ymd{year(given_year), month(given_month), day(given_day)};
year_month_day ymd;
DateTimeT::OuterDateTime2YMD(left.date, ymd);
sys_days sd = sys_days(ymd);
year_month_day start{year(given_year), month(1), day(1)};
year_month_day start{ymd.year(), month(1), day(1)};
sys_days start_sd = sys_days(start);
auto days_diff = sd - start_sd;
result = days_diff.count() + 1;
Expand All @@ -68,9 +66,8 @@ inline bool DayOfYearFunction::Run(DateTimeT left, BigIntT &result) {
template <>
inline bool DayOfYearFunction::Run(TimestampT left, BigIntT &result) {
auto given_year = TimestampT::GetDateTimePart(left, TimeUnit::kYear);
auto given_month = TimestampT::GetDateTimePart(left, TimeUnit::kMonth);
auto given_day = TimestampT::GetDateTimePart(left, TimeUnit::kDay);
year_month_day ymd{year(given_year), month(given_month), day(given_day)};
year_month_day ymd;
TimestampT::OuterDateTime2YMD(left.date, ymd);
sys_days sd = sys_days(ymd);
year_month_day start{year(given_year), month(1), day(1)};
sys_days start_sd = sys_days(start);
Expand Down
10 changes: 10 additions & 0 deletions src/parser/type/datetime/datetime_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,14 @@ bool DateTimeType::IsDateTimeValid(int32_t year, int32_t month, int32_t day, int
return TimeType::IsTimeValid(hour, minute, second) and DateType::IsDateValid(year, month, day);
}


bool DateTimeType::OuterDateTime2YMD(int32_t days, std::chrono::year_month_day &ymd) {
int32_t year, month, day;
bool result = DateType::Date2YMD(days, year, month, day);
if (result) {
ymd = std::chrono::year_month_day(std::chrono::year(year), std::chrono::month(month), std::chrono::day(day));
}
return result;
}

} // namespace infinity
3 changes: 3 additions & 0 deletions src/parser/type/datetime/datetime_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "parser_assert.h"
#include "time_type.h"
#include <string>
#include <chrono>

namespace infinity {

Expand Down Expand Up @@ -77,6 +78,8 @@ struct DateTimeType {

int64_t GetEpochTime() const;

static bool OuterDateTime2YMD(int32_t days, std::chrono::year_month_day &ymd);

private:
static bool YMDHMS2DateTime(int32_t year, int32_t month, int32_t day, int32_t hour, int32_t minute, int32_t second, DateTimeType &datetime);

Expand Down

0 comments on commit 428a224

Please sign in to comment.