From 0c77774f34681ed1aa31403db5636578fefa78b3 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Tue, 30 Jan 2024 00:15:56 -0500 Subject: [PATCH] Add `display` methods for pretty print; TODO: Use naive conversion to timestamp when getting Weekday. Currently, since we convert to UTC the returned value is invalid if it is a different day in the offset timezone --- src/datetime.rs | 26 ++++++++++++++++++++++++++ src/month.rs | 17 +++++++++++++++++ src/weekday.rs | 34 ++++++++++++++++++++++++++++++++++ src/zone.rs | 7 +++++++ 4 files changed, 84 insertions(+) diff --git a/src/datetime.rs b/src/datetime.rs index a9b035a..c6905c4 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -146,6 +146,20 @@ impl DateTime { let rem = days % 7; rem.try_into().unwrap() } + + pub fn display(&self) -> String { + format!( + "{} {} {} {:0>2}:{:0>2}:{:0>2} {} {}", + self.weekday().abbreviate(), + self.month.abbreviate(), + self.day, + self.hour, + self.minute, + self.second, + self.zone.display(), + self.year(), + ) + } } impl fmt::Display for DateTime { @@ -260,4 +274,16 @@ mod tests { }; assert_eq!(dt.to_string(), "2024-01-29T23:38:02+04:15") } + + #[test] + fn display() { + let mut dt = DateTime::from_timestamp(1706571482); + assert_eq!(dt.display(), "Mon Jan 29 23:38:02 Utc 2024"); + dt.zone = TimeZone::Offset { + sign: crate::zone::Sign::Positive, + hours: 4, + minutes: 15, + }; + assert_eq!(dt.display(), "Mon Jan 29 23:38:02 +04:15 2024"); + } } diff --git a/src/month.rs b/src/month.rs index ba1b96d..2bb0158 100644 --- a/src/month.rs +++ b/src/month.rs @@ -62,6 +62,23 @@ impl Month { let n = *self as u8 - 1; n.try_into().ok() } + + pub fn abbreviate(&self) -> &'static str { + match self { + Self::Janurary => "Jan", + Self::February => "Feb", + Self::March => "Mar", + Self::April => "Apr", + Self::May => "May", + Self::June => "Jun", + Self::July => "Jul", + Self::August => "Aug", + Self::September => "Sep", + Self::October => "Oct", + Self::November => "Nov", + Self::December => "Dec", + } + } } impl TryFrom for Month { diff --git a/src/weekday.rs b/src/weekday.rs index 3c285bb..5e426c5 100644 --- a/src/weekday.rs +++ b/src/weekday.rs @@ -1,3 +1,5 @@ +use std::fmt; + use serde::{Deserialize, Serialize}; #[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)] @@ -29,5 +31,37 @@ impl TryFrom for Weekday { } } +impl fmt::Display for Weekday { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{}", + match self { + Self::Thursday => "Thursday", + Self::Friday => "Friday", + Self::Saturday => "Saturday", + Self::Sunday => "Sunday", + Self::Monday => "Monday", + Self::Tuesday => "Tuesday", + Self::Wednesday => "Wednesday", + } + ) + } +} + +impl Weekday { + pub fn abbreviate(&self) -> &'static str { + match self { + Self::Thursday => "Thu", + Self::Friday => "Fri", + Self::Saturday => "Sat", + Self::Sunday => "Sun", + Self::Monday => "Mon", + Self::Tuesday => "Tue", + Self::Wednesday => "Wed", + } + } +} + #[derive(Debug)] pub struct ParseWeekdayError; diff --git a/src/zone.rs b/src/zone.rs index 2432671..2effadc 100644 --- a/src/zone.rs +++ b/src/zone.rs @@ -94,6 +94,13 @@ impl TimeZone { } } } + + pub fn display(&self) -> String { + match self { + Self::Utc => "Utc".into(), + _ => format!("{self}"), + } + } } impl fmt::Display for TimeZone {