Display: preserve fractional seconds when present

When nanoseconds are non-zero, include them in Display output as
`.NNNNNNNNN` between seconds and the offset. This avoids silently
dropping sub-second precision from the formatted representation.
This commit is contained in:
Sylvestre Ledru
2026-03-31 23:32:43 +02:00
parent 3767622cc3
commit 01687f304d
2 changed files with 30 additions and 6 deletions
+22 -5
View File
@@ -297,11 +297,28 @@ impl fmt::Display for ExtendedDateTime {
let abs = self.offset_seconds.unsigned_abs();
let oh = abs / 3600;
let om = (abs % 3600) / 60;
write!(
f,
"{:04}-{:02}-{:02} {:02}:{:02}:{:02}{}{:02}:{:02}",
self.year, self.month, self.day, self.hour, self.minute, self.second, sign, oh, om,
)
if self.nanosecond != 0 {
write!(
f,
"{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:09}{}{:02}:{:02}",
self.year,
self.month,
self.day,
self.hour,
self.minute,
self.second,
self.nanosecond,
sign,
oh,
om,
)
} else {
write!(
f,
"{:04}-{:02}-{:02} {:02}:{:02}:{:02}{}{:02}:{:02}",
self.year, self.month, self.day, self.hour, self.minute, self.second, sign, oh, om,
)
}
}
}
+8 -1
View File
@@ -63,7 +63,14 @@ impl ParsedDateTime {
impl fmt::Display for ParsedDateTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ParsedDateTime::InRange(z) => write!(f, "{}", z.strftime("%Y-%m-%d %H:%M:%S%:z")),
ParsedDateTime::InRange(z) => {
let ns = z.datetime().time().subsec_nanosecond();
if ns != 0 {
write!(f, "{}", z.strftime("%Y-%m-%d %H:%M:%S%.9f%:z"))
} else {
write!(f, "{}", z.strftime("%Y-%m-%d %H:%M:%S%:z"))
}
}
ParsedDateTime::Extended(dt) => write!(f, "{dt}"),
}
}