feat: add cargo today command (#43)

This commit is contained in:
Tom Van Eyck
2023-12-09 14:43:37 +01:00
committed by GitHub
parent b696aa45da
commit f8a1368765
6 changed files with 238 additions and 6 deletions

View File

@@ -2,6 +2,9 @@ use std::error::Error;
use std::fmt::Display;
use std::str::FromStr;
#[cfg(feature = "today")]
use chrono::{Datelike, Local};
/// A valid day number of advent (i.e. an integer in range 1 to 25).
///
/// # Display
@@ -37,6 +40,19 @@ impl Day {
}
}
#[cfg(feature = "today")]
impl Day {
/// Returns the current day if it's between the 1st and the 25th of december, `None` otherwise.
pub fn today() -> Option<Self> {
let today = Local::now();
if today.month() == 12 && today.day() <= 25 {
Self::new(u8::try_from(today.day()).ok()?)
} else {
None
}
}
}
impl Display for Day {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:02}", self.0)