You've already forked parse_datetime
mirror of
https://github.com/uutils/parse_datetime.git
synced 2026-06-10 16:13:15 -07:00
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:
+22
-5
@@ -297,11 +297,28 @@ impl fmt::Display for ExtendedDateTime {
|
|||||||
let abs = self.offset_seconds.unsigned_abs();
|
let abs = self.offset_seconds.unsigned_abs();
|
||||||
let oh = abs / 3600;
|
let oh = abs / 3600;
|
||||||
let om = (abs % 3600) / 60;
|
let om = (abs % 3600) / 60;
|
||||||
write!(
|
if self.nanosecond != 0 {
|
||||||
f,
|
write!(
|
||||||
"{:04}-{:02}-{:02} {:02}:{:02}:{:02}{}{:02}:{:02}",
|
f,
|
||||||
self.year, self.month, self.day, self.hour, self.minute, self.second, sign, oh, om,
|
"{: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
@@ -63,7 +63,14 @@ impl ParsedDateTime {
|
|||||||
impl fmt::Display for ParsedDateTime {
|
impl fmt::Display for ParsedDateTime {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
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}"),
|
ParsedDateTime::Extended(dt) => write!(f, "{dt}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user