use chrono::{NaiveTime, Timelike};

pub fn pprint(t: NaiveTime) -> String {
    if t.minute() == 0 {
        format!("{}h", t.hour())
    } else {
        format!("{}h{}", t.hour(), t.minute())
    }
}

pub fn parse(t: &str) -> Option<NaiveTime> {
    match t.split('h').collect::<Vec<&str>>()[..] {
        [hours, minutes] => {
            if minutes.trim().is_empty() {
                NaiveTime::from_hms_opt(hours.parse().ok()?, 0, 0)
            } else {
                NaiveTime::from_hms_opt(hours.parse().ok()?, minutes.parse().ok()?, 0)
            }
        }
        _ => None,
    }
}