1
2
3
4
5
6
7
8
9
10
11
12
13
14
//! Misc local utilities
use chrono::{offset::Utc, DateTime, NaiveDateTime};

pub fn epoch_to_iso(epoch: i32) -> String {
    // Chrono is a little silly and can't easily convert from epoch to utc timezone
    let d: DateTime<Utc> = DateTime::from_utc(NaiveDateTime::from_timestamp_opt(epoch as i64, 0).expect("epoch timestamp too high"), Utc);
    d.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
}

pub fn iso_to_epoch(iso: &str) -> u32 {
    DateTime::parse_from_rfc3339(iso)
        .map(|x| x.timestamp() as u32)
        .unwrap_or(0)
}