From f7aa4540c9dda4adfa3620f32728ff2347b0f2c6 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Tue, 31 Mar 2026 09:13:56 +0200 Subject: [PATCH] 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. --- src/extended.rs | 16 ++++++++++++++++ src/lib.rs | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/src/extended.rs b/src/extended.rs index cabf035..fd00777 100644 --- a/src/extended.rs +++ b/src/extended.rs @@ -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) } diff --git a/src/lib.rs b/src/lib.rs index 92e205f..74e376e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 for ParsedDateTime { fn eq(&self, other: &Zoned) -> bool { match self {