From dbd954a5cd33e7eace6cb4fef4936a5ebec21629 Mon Sep 17 00:00:00 2001 From: Kexiang Wang Date: Mon, 19 Feb 2024 19:47:07 -0500 Subject: [PATCH] refactor --- e2e_test/batch/basic/make_time.slt.part | 15 +++++++++++++ src/common/src/types/datetime.rs | 28 +++++++++++-------------- src/common/src/types/timestamptz.rs | 16 +++++++------- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/e2e_test/batch/basic/make_time.slt.part b/e2e_test/batch/basic/make_time.slt.part index ae43f0293366c..ff1d4453e0efd 100644 --- a/e2e_test/batch/basic/make_time.slt.part +++ b/e2e_test/batch/basic/make_time.slt.part @@ -11,6 +11,11 @@ SELECT make_timestamptz(-1973, 07, 15, 08, 15, 55.33); ---- 1973-07-15 08:15:55.330+00:00 BC +query T +SELECT make_timestamptz(20240, 1, 26, 14, 20, 26); +---- +20240-01-26 14:20:26+00:00 + query error Invalid parameter year, month, day: invalid date: -3-2-29 SELECT make_timestamptz(-4, 02, 29, 08, 15, 55.33); @@ -105,6 +110,11 @@ SELECT make_date(2024, 1, 26); ---- 2024-01-26 +query T +SELECT make_date(20240, 1, 26); +---- +20240-01-26 + query T SELECT make_date(-2024, 1, 26); ---- @@ -146,6 +156,11 @@ SELECT make_timestamp(2024, 1, 26, 14, 20, 26); ---- 2024-01-26 14:20:26 +query T +SELECT make_timestamp(20240, 1, 26, 14, 20, 26); +---- +20240-01-26 14:20:26 + query T SELECT make_timestamp(-1973, 07, 15, 08, 15, 55.33); ---- diff --git a/src/common/src/types/datetime.rs b/src/common/src/types/datetime.rs index 5562447926a35..e8e5c1c5da4e2 100644 --- a/src/common/src/types/datetime.rs +++ b/src/common/src/types/datetime.rs @@ -328,16 +328,11 @@ impl ToText for Date { /// ``` fn write(&self, f: &mut W) -> std::fmt::Result { let (ce, year) = self.0.year_ce(); + write!(f, "{:04}-{:02}-{:02}", year, self.0.month(), self.0.day())?; if ce { - write!(f, "{}", self.0) + Ok(()) } else { - write!( - f, - "{:04}-{:02}-{:02} BC", - year, - self.0.month(), - self.0.day() - ) + f.write_str(" BC") } } @@ -365,17 +360,18 @@ impl ToText for Time { impl ToText for Timestamp { fn write(&self, f: &mut W) -> std::fmt::Result { let (ce, year) = self.0.year_ce(); + write!( + f, + "{:04}-{:02}-{:02} {}", + year, + self.0.month(), + self.0.day(), + self.0.time() + )?; if ce { - write!(f, "{}", self.0.date())?; + Ok(()) } else { - write!(f, "{:04}-{:02}-{:02}", year, self.0.month(), self.0.day())?; - } - f.write_char(' ')?; - write!(f, "{}", self.0.time())?; - if !ce { f.write_str(" BC") - } else { - Ok(()) } } diff --git a/src/common/src/types/timestamptz.rs b/src/common/src/types/timestamptz.rs index 5793511d1ab8e..c6913819f544f 100644 --- a/src/common/src/types/timestamptz.rs +++ b/src/common/src/types/timestamptz.rs @@ -207,16 +207,18 @@ pub fn write_date_time_tz( ) -> std::fmt::Result { let date = instant_local.date_naive(); let (ce, year) = date.year_ce(); + write!( + writer, + "{:04}-{:02}-{:02} {}", + year, + date.month(), + date.day(), + instant_local.format("%H:%M:%S%.f%:z") + )?; if ce { - write!(writer, "{}", date)?; + std::fmt::Result::Ok(()) } else { - write!(writer, "{:04}-{:02}-{:02}", year, date.month(), date.day())?; - } - write!(writer, "{}", instant_local.format(" %H:%M:%S%.f%:z"))?; - if !ce { writer.write_str(" BC") - } else { - std::fmt::Result::Ok(()) } }