Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
KeXiangWang committed Feb 20, 2024
1 parent 0fed6d0 commit dbd954a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
15 changes: 15 additions & 0 deletions e2e_test/batch/basic/make_time.slt.part
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
----
Expand Down Expand Up @@ -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);
----
Expand Down
28 changes: 12 additions & 16 deletions src/common/src/types/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,16 +328,11 @@ impl ToText for Date {
/// ```
fn write<W: std::fmt::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")
}
}

Expand Down Expand Up @@ -365,17 +360,18 @@ impl ToText for Time {
impl ToText for Timestamp {
fn write<W: std::fmt::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(())
}
}

Expand Down
16 changes: 9 additions & 7 deletions src/common/src/types/timestamptz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}

Expand Down

0 comments on commit dbd954a

Please sign in to comment.