Fixes formatting of times with decimal second number lower than 10

pull/389/head
Tpt 2 years ago committed by Thomas Tanon
parent 7a0c457867
commit cb2c891979
  1. 34
      lib/oxsdatatypes/src/date_time.rs

@ -243,12 +243,17 @@ impl fmt::Display for DateTime {
} }
write!( write!(
f, f,
"{:04}-{:02}-{:02}T{:02}:{:02}:{:02}", "{:04}-{:02}-{:02}T{:02}:{:02}:{}{}",
year.abs(), year.abs(),
self.month(), self.month(),
self.day(), self.day(),
self.hour(), self.hour(),
self.minute(), self.minute(),
if self.second().abs() >= 10.into() {
""
} else {
"0"
},
self.second() self.second()
)?; )?;
if let Some(timezone_offset) = self.timezone_offset() { 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 { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!( write!(
f, f,
"{:02}:{:02}:{:02}", "{:02}:{:02}:{}{}",
self.hour(), self.hour(),
self.minute(), self.minute(),
if self.second().abs() >= 10.into() {
""
} else {
"0"
},
self.second() self.second()
)?; )?;
if let Some(timezone_offset) = self.timezone_offset() { if let Some(timezone_offset) = self.timezone_offset() {
@ -1933,6 +1943,26 @@ mod tests {
DateTime::from_str("1899-03-01T00:00:00")?.to_string(), DateTime::from_str("1899-03-01T00:00:00")?.to_string(),
"1899-03-01T00:00:00" "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!( assert_eq!(
DateTime::from_str("-1000000000-01-01T00:00:00")?.to_string(), DateTime::from_str("-1000000000-01-01T00:00:00")?.to_string(),

Loading…
Cancel
Save