use super::funejson::Value; use super::dates_add_days::epoch_day_from_iso; fn minute_of_day(time: &str) -> i64 { let b = time.as_bytes(); let ok = b.len() == 5 && b[2] == b':' && [0, 1, 3, 4].iter().all(|&i| b[i].is_ascii_digit()); let digit = |i: usize| i64::from(b[i] - b'0'); let (hours, minutes) = if ok { (digit(0) * 10 + digit(1), digit(3) * 10 + digit(4)) } else { (-1, -1) }; if !ok || hours > 23 || minutes > 59 { panic!("\"{}\" is not a time of day (HH:MM, 00:00 to 23:59)", time); } hours * 60 + minutes } /// Minutes from one local date and time to another: the difference between two /// wall-clock readings. It knows nothing of time zones or daylight saving, so /// across a clock change it differs from real elapsed time by the change. /// /// # Panics /// Panics on a malformed date or time. pub fn minutes_between(start_date: &str, start_time: &str, end_date: &str, end_time: &str) -> i64 { let days = epoch_day_from_iso(end_date) - epoch_day_from_iso(start_date); days * 1440 + minute_of_day(end_time) - minute_of_day(start_time) } pub fn fune_vector(args: &[Value]) -> Value { Value::Int(minutes_between( args[0].as_str(), args[1].as_str(), args[2].as_str(), args[3].as_str(), )) }