From cb2c89197960d170f1ad5c463a31f300bf344fa4 Mon Sep 17 00:00:00 2001 From: Tpt Date: Mon, 20 Feb 2023 17:46:46 +0100 Subject: [PATCH] Fixes formatting of times with decimal second number lower than 10 --- lib/oxsdatatypes/src/date_time.rs | 34 +++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/oxsdatatypes/src/date_time.rs b/lib/oxsdatatypes/src/date_time.rs index 66829adb..0f3c66b6 100644 --- a/lib/oxsdatatypes/src/date_time.rs +++ b/lib/oxsdatatypes/src/date_time.rs @@ -243,12 +243,17 @@ impl fmt::Display for DateTime { } write!( f, - "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}", + "{:04}-{:02}-{:02}T{:02}:{:02}:{}{}", year.abs(), self.month(), self.day(), self.hour(), self.minute(), + if self.second().abs() >= 10.into() { + "" + } else { + "0" + }, self.second() )?; if let Some(timezone_offset) = self.timezone_offset() { @@ -439,9 +444,14 @@ impl fmt::Display for Time { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "{:02}:{:02}:{:02}", + "{:02}:{:02}:{}{}", self.hour(), self.minute(), + if self.second().abs() >= 10.into() { + "" + } else { + "0" + }, self.second() )?; if let Some(timezone_offset) = self.timezone_offset() { @@ -1933,6 +1943,26 @@ mod tests { DateTime::from_str("1899-03-01T00:00:00")?.to_string(), "1899-03-01T00:00:00" ); + assert_eq!( + DateTime::from_str("-0899-03-01T00:00:00")?.to_string(), + "-0899-03-01T00:00:00" + ); + assert_eq!( + DateTime::from_str("2000-01-01T00:00:00.1234567")?.to_string(), + "2000-01-01T00:00:00.1234567" + ); + assert_eq!( + DateTime::from_str("2000-01-01T00:00:12.1234567")?.to_string(), + "2000-01-01T00:00:12.1234567" + ); + assert_eq!( + Time::from_str("01:02:03.1234567")?.to_string(), + "01:02:03.1234567" + ); + assert_eq!( + Time::from_str("01:02:13.1234567")?.to_string(), + "01:02:13.1234567" + ); assert_eq!( DateTime::from_str("-1000000000-01-01T00:00:00")?.to_string(),