From 7e79753478255a67313d0d39560e742ddef7b218 Mon Sep 17 00:00:00 2001 From: QuenKar <47681251+QuenKar@users.noreply.github.com> Date: Thu, 14 Sep 2023 22:18:24 +0800 Subject: [PATCH] refactor code. --- src/common/time/src/date.rs | 4 +++ src/common/time/src/time.rs | 13 ++++++++ src/datatypes/src/types/date_type.rs | 5 +-- src/datatypes/src/types/datetime_type.rs | 5 +-- src/datatypes/src/types/primitive_type.rs | 18 +++-------- src/datatypes/src/types/time_type.rs | 17 +++++++++- src/datatypes/src/types/timestamp_type.rs | 39 +++++++++++++---------- 7 files changed, 61 insertions(+), 40 deletions(-) diff --git a/src/common/time/src/date.rs b/src/common/time/src/date.rs index e9b1d76246c7..021085f06013 100644 --- a/src/common/time/src/date.rs +++ b/src/common/time/src/date.rs @@ -82,6 +82,10 @@ impl Date { pub fn to_chrono_date(&self) -> Option { NaiveDate::from_num_days_from_ce_opt(UNIX_EPOCH_FROM_CE + self.0) } + + pub fn to_secs(&self) -> i64 { + self.0 as i64 * 24 * 3600 + } } #[cfg(test)] diff --git a/src/common/time/src/time.rs b/src/common/time/src/time.rs index 7400e22c0fe5..197353501fbc 100644 --- a/src/common/time/src/time.rs +++ b/src/common/time/src/time.rs @@ -77,6 +77,19 @@ impl Time { self.value } + /// Convert a timestamp to given time unit. + /// Return `None` if conversion causes overflow. + pub fn convert_to(&self, unit: TimeUnit) -> Option