Time events
This commit is contained in:
parent
e4843fa900
commit
2eaccce83c
2 changed files with 77 additions and 0 deletions
|
|
@ -1,3 +1,7 @@
|
||||||
|
mod time_event;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct ScheduleApp {}
|
||||||
|
|
|
||||||
73
src/time_event.rs
Normal file
73
src/time_event.rs
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
use chrono::{DateTime, Datelike, Local, NaiveTime, TimeDelta, WeekdaySet};
|
||||||
|
|
||||||
|
pub struct TimeEvent {
|
||||||
|
event_type: TimeEventType,
|
||||||
|
duration: TimeDelta,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TimeEvent {
|
||||||
|
pub fn oneshot(start: DateTime<Local>, duration: f64) -> Self {
|
||||||
|
Self {
|
||||||
|
event_type: TimeEventType::Oneshot { start },
|
||||||
|
duration: TimeDelta::seconds((duration * 3600.) as i64),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn periodic(weekdays: WeekdaySet, start_time: NaiveTime, duration: f64) -> Self {
|
||||||
|
Self {
|
||||||
|
event_type: TimeEventType::Periodic {
|
||||||
|
weekdays,
|
||||||
|
start: start_time,
|
||||||
|
},
|
||||||
|
duration: TimeDelta::seconds((duration * 3600.) as i64),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn status(&self) -> TimeEventStatus {
|
||||||
|
let now = Local::now();
|
||||||
|
match self.event_type {
|
||||||
|
TimeEventType::Oneshot { start } => {
|
||||||
|
let end = start.checked_add_signed(self.duration).unwrap_or(start);
|
||||||
|
if start < now {
|
||||||
|
TimeEventStatus::Planned
|
||||||
|
} else if now > end {
|
||||||
|
TimeEventStatus::Ended
|
||||||
|
} else {
|
||||||
|
TimeEventStatus::Active
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TimeEventType::Periodic { weekdays, start } => {
|
||||||
|
if weekdays.contains(now.weekday()) {
|
||||||
|
let start_date = now.with_time(start).unwrap();
|
||||||
|
if now < start_date {
|
||||||
|
TimeEventStatus::Planned
|
||||||
|
} else if now
|
||||||
|
> start_date
|
||||||
|
.checked_add_signed(self.duration)
|
||||||
|
.unwrap_or(start_date)
|
||||||
|
{
|
||||||
|
TimeEventStatus::Ended
|
||||||
|
} else {
|
||||||
|
TimeEventStatus::Active
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
TimeEventStatus::Planned
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum TimeEventType {
|
||||||
|
Periodic {
|
||||||
|
weekdays: WeekdaySet,
|
||||||
|
start: NaiveTime,
|
||||||
|
},
|
||||||
|
Oneshot {
|
||||||
|
start: DateTime<Local>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
enum TimeEventStatus {
|
||||||
|
Planned,
|
||||||
|
Active,
|
||||||
|
Ended,
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue