1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use chrono::offset::Local;
use chrono::{DateTime, TimeZone, Utc};

pub fn simple_timestamp() -> String {
    format_datetime(chrono::offset::Local::now())
}

pub fn format_datetime(dt: DateTime<Local>) -> String {
    dt.format("%Y%m%d-%H%M%S").to_string()
}

pub fn now() -> String {
    Local::now().to_rfc3339()
}

pub fn epoch_to_string(e: i64) -> String {
    Utc.timestamp_millis_opt(e).unwrap().to_rfc3339()
}

pub fn epoch_zero() -> String {
    epoch_to_string(0)
}

pub fn string_to_epoch(stamp: String) -> i64 {
    match DateTime::parse_from_rfc3339(&stamp) {
        Ok(dt) => dt.timestamp_millis(),
        Err(e) => {
            log::debug!("{:?}", e);
            Local::now().timestamp_millis()
        }
    }
}

#[cfg(test)]
mod tests {

    #[test]
    fn epoch_zero() {
        assert_eq!(super::epoch_zero(), "1970-01-01T00:00:00+00:00");
    }
}