date-utilities 0.0.4

Lightweight utilities for working with dates
Documentation
use date_utilities::{Duration, FORMATS, Instant};

fn main() {
    let now = Instant::now_local();

    let mut args = std::env::args().skip(1);

    let next = args.next().unwrap();

    if let Ok(count) = str::parse::<u64>(&next) {
        let kind: String = args.next().expect("kind");

        let duration: Duration = match kind.as_str() {
            "seconds" => Duration::from_seconds(count),
            "minutes" => Duration::from_minutes(count),
            "hours" => Duration::from_hours(count),
            "days" => Duration::from_days(count),
            "weeks" => Duration::from_weeks(count),
            // TODO
            "months" => Duration::from_days(count * 30),
            // TODO
            "years" => Duration::from_days(count * 356),
            kind => {
                panic!("unknown {kind:?}");
            }
        };

        let then: Instant = now - duration;

        let after = args.next();
        let after: &str = after.as_deref().unwrap_or_default();
        let format = match after {
            "git" => "%year-%month-%date %hour:%minute:%second",
            "" => FORMATS::FULL_MINIMAL,
            unknown => {
                panic!("unknown {unknown:?}");
            }
        };

        let formatted = then.format(format);
        println!("{formatted}");
    } else if let "is" | "was" = next.as_str() {
        let then = Instant::parse_english(&mut args).unwrap();
        if let Some(next) = args.next()
            && next == "to"
        {
            let next = Instant::parse_english(&mut args).unwrap();
            let value = match next.difference(then) {
                Ok(future) => future,
                Err(past) => past,
            };
            println!("{value}", value = value.format());
        } else {
            match then.difference(now) {
                Ok(future) => {
                    println!("in {future} time", future = future.format());
                }
                Err(past) => {
                    println!("{past} ago", past = past.format());
                }
            }
        }
    } else {
        panic!("unknown {next:?}. expected data or is / when");
    }
}