You've already forked parse_datetime
mirror of
https://github.com/uutils/parse_datetime.git
synced 2026-06-10 16:13:15 -07:00
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:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user