From dabb00ad4dda679e37dde1827f2b135c9183310c Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Sat, 26 Sep 2020 16:25:43 -0400 Subject: pianoman: support 9am or 9pm --- src/commands/today/pianoman.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/commands/today/pianoman.rs b/src/commands/today/pianoman.rs index 5cc7382..fa3ac7e 100644 --- a/src/commands/today/pianoman.rs +++ b/src/commands/today/pianoman.rs @@ -1,7 +1,8 @@ use super::prelude::*; lazy_static! { - static ref TARGET_TIME: chrono::NaiveTime = chrono::NaiveTime::from_hms(21, 0, 0); + static ref NINE_AM: chrono::NaiveTime = chrono::NaiveTime::from_hms(9, 0, 0); + static ref NINE_PM: chrono::NaiveTime = chrono::NaiveTime::from_hms(21, 0, 0); static ref PIANOMANS: Vec = vec![ by_url("https://www.youtube.com/watch?v=gxEPV4kolz0"), @@ -18,18 +19,20 @@ pub fn pianoman(dt: chrono::NaiveDateTime) -> TodayIter { return Box::new(empty()); } - let diff = { - let result = *TARGET_TIME - dt.time(); - if result < chrono::Duration::zero() { - -result - } else { - result - } - }; + let near_9am = duration_abs(*NINE_AM - dt.time()) <= Duration::minutes(5); + let near_9pm = duration_abs(*NINE_PM - dt.time()) <= Duration::minutes(5); - if diff > Duration::minutes(5) { + if !near_9am && !near_9pm { return Box::new(empty()); } Box::new(PIANOMANS.iter().cloned()) } + +fn duration_abs(d: Duration) -> Duration { + if d < chrono::Duration::zero() { + -d + } else { + d + } +} -- cgit v1.3.1