use chrono::Local;
use croner::Cron;
use super::super::model::Routine;
pub const MAX_TTL_SECS: u64 = 60 * 60;
impl Routine {
pub fn effective_ttl_secs(&self) -> u64 {
let ceiling = MAX_TTL_SECS.min(self.cron_interval_secs().unwrap_or(MAX_TTL_SECS));
self.ttl_secs.map_or(ceiling, |t| t.min(ceiling))
}
fn cron_interval_secs(&self) -> Option<u64> {
let cron = self.schedule.parse::<Cron>().ok()?;
let mut fires = cron.iter_after(Local::now());
let first = fires.next()?;
let second = fires.next()?;
u64::try_from((second - first).num_seconds()).ok()
}
}