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
This commit is contained in:
parent
34edb7522d
commit
0c77774f34
@ -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");
|
||||
}
|
||||
}
|
||||
|
17
src/month.rs
17
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<u8> for Month {
|
||||
|
@ -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<i64> 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;
|
||||
|
@ -94,6 +94,13 @@ impl TimeZone {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn display(&self) -> String {
|
||||
match self {
|
||||
Self::Utc => "Utc".into(),
|
||||
_ => format!("{self}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for TimeZone {
|
||||
|
Loading…
Reference in New Issue
Block a user