From 428a224a819b3e329d76e303b36892d9d99fda22 Mon Sep 17 00:00:00 2001 From: kche0169 Date: Thu, 16 Jan 2025 17:32:09 +0800 Subject: [PATCH] add outer functions in datetime types --- src/function/scalar/day_of_month.cpp | 16 ++++++---------- src/function/scalar/day_of_week.cpp | 12 ++++-------- src/function/scalar/day_of_year.cpp | 13 +++++-------- src/parser/type/datetime/datetime_type.cpp | 10 ++++++++++ src/parser/type/datetime/datetime_type.h | 3 +++ 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/function/scalar/day_of_month.cpp b/src/function/scalar/day_of_month.cpp index 2f3082d6a1..c248905001 100644 --- a/src/function/scalar/day_of_month.cpp +++ b/src/function/scalar/day_of_month.cpp @@ -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; @@ -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; diff --git a/src/function/scalar/day_of_week.cpp b/src/function/scalar/day_of_week.cpp index 18c05df71e..0dbddb6b8a 100644 --- a/src/function/scalar/day_of_week.cpp +++ b/src/function/scalar/day_of_week.cpp @@ -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); @@ -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); diff --git a/src/function/scalar/day_of_year.cpp b/src/function/scalar/day_of_year.cpp index 3c64340b5f..252ff7d163 100644 --- a/src/function/scalar/day_of_year.cpp +++ b/src/function/scalar/day_of_year.cpp @@ -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; @@ -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); diff --git a/src/parser/type/datetime/datetime_type.cpp b/src/parser/type/datetime/datetime_type.cpp index 9edcf49db0..a5b8b4c78c 100644 --- a/src/parser/type/datetime/datetime_type.cpp +++ b/src/parser/type/datetime/datetime_type.cpp @@ -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 \ No newline at end of file diff --git a/src/parser/type/datetime/datetime_type.h b/src/parser/type/datetime/datetime_type.h index 281caa5afd..4e99066c3c 100644 --- a/src/parser/type/datetime/datetime_type.h +++ b/src/parser/type/datetime/datetime_type.h @@ -18,6 +18,7 @@ #include "parser_assert.h" #include "time_type.h" #include +#include namespace infinity { @@ -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);