Functional Weave
Code in TypeScript

time.minutes-between@1.0.0

impl/rust.rs

1,312 bytes · the Rust implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

use super::funejson::Value;  ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one
use super::dates_add_days::epoch_day_from_iso;  ← from dates.add-days ^1.0.0 · built alongside by fune

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(),
    ))
}