aboutsummaryrefslogtreecommitdiff
path: root/src/model/time.rs
blob: b29b74dc63a6dcccca3a9f4c5bc8a7f847a832d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use chrono::{NaiveTime, Timelike};

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

pub fn parse(t: &str) -> Option<NaiveTime> {
    match t.split(':').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,
    }
}