use chrono::NaiveDate; #[derive(Debug)] pub struct Time { pub hour: u8, pub min: u8, } impl Time { pub fn new(hour: u32, min: u32) -> Option { if hour < 24 && min < 60 || hour == 24 && min == 0 { Some(Self { hour: hour as u8, min: min as u8, }) } else { None } } } #[derive(Debug, Clone, Copy)] pub enum Weekday { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday, } impl Weekday { pub fn name(&self) -> &'static str { match self { Weekday::Monday => "mon", Weekday::Tuesday => "tue", Weekday::Wednesday => "wed", Weekday::Thursday => "thu", Weekday::Friday => "fri", Weekday::Saturday => "sat", Weekday::Sunday => "sun", } } } #[derive(Debug)] pub enum DeltaStep { /// `y`, move by a year, keeping the same month and day Year(i32), /// `m`, move by a month, keeping the same day `d` Month(i32), /// `M`, move by a month, keeping the same day `D` MonthReverse(i32), /// `d` Day(i32), /// `w`, move by 7 days Week(i32), /// `h` Hour(i32), /// `m` Minute(i32), /// `mon`, `tue`, `wed`, `thu`, `fri`, `sat`, `sun` /// /// Move to the next occurrence of the specified weekday Weekday(i32, Weekday), } impl DeltaStep { pub fn amount(&self) -> i32 { match self { DeltaStep::Year(i) => *i, DeltaStep::Month(i) => *i, DeltaStep::MonthReverse(i) => *i, DeltaStep::Day(i) => *i, DeltaStep::Week(i) => *i, DeltaStep::Hour(i) => *i, DeltaStep::Minute(i) => *i, DeltaStep::Weekday(i, _) => *i, } } pub fn name(&self) -> &'static str { match self { DeltaStep::Year(_) => "y", DeltaStep::Month(_) => "m", DeltaStep::MonthReverse(_) => "M", DeltaStep::Day(_) => "d", DeltaStep::Week(_) => "w", DeltaStep::Hour(_) => "h", DeltaStep::Minute(_) => "min", DeltaStep::Weekday(_, wd) => wd.name(), } } } #[derive(Debug)] pub struct Delta(pub Vec); #[derive(Debug)] pub struct DateSpec { pub start: NaiveDate, pub start_delta: Option, pub start_time: Option