Add Display impls for ExtendedDateTime and ParsedDateTime

Provide a standard formatting path so callers (and tests) can use
`.to_string()` instead of hand-rolling offset formatting everywhere.
This commit is contained in:
Sylvestre Ledru
2026-03-31 09:13:56 +02:00
parent 72cada4740
commit f7aa4540c9
2 changed files with 25 additions and 0 deletions
+16
View File
@@ -1,6 +1,8 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use std::fmt;
use crate::GNU_MAX_YEAR;
const SECONDS_PER_DAY: i64 = 86_400;
@@ -289,6 +291,20 @@ impl ExtendedDateTime {
}
}
impl fmt::Display for ExtendedDateTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let sign = if self.offset_seconds < 0 { '-' } else { '+' };
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,
)
}
}
pub fn is_leap_year(year: u32) -> bool {
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
}
+9
View File
@@ -55,6 +55,15 @@ 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::Extended(dt) => write!(f, "{dt}"),
}
}
}
impl PartialEq<Zoned> for ParsedDateTime {
fn eq(&self, other: &Zoned) -> bool {
match self {