Skip to main content

base/
clock.rs

1//! Wall-clock readings shared by every crate: epoch milliseconds and RFC 3339.
2
3pub fn unix_ms() -> i64 {
4    std::time::SystemTime::now()
5        .duration_since(std::time::UNIX_EPOCH)
6        .map(|d| d.as_millis() as i64)
7        .unwrap_or(0)
8}
9
10pub fn now_rfc3339() -> String {
11    time::OffsetDateTime::now_utc()
12        .format(&time::format_description::well_known::Rfc3339)
13        .unwrap_or_else(|_| "unknown".to_string())
14}
15
16#[cfg(test)]
17mod tests {
18    use super::*;
19
20    #[test]
21    fn rfc3339_parses_back() {
22        let s = now_rfc3339();
23        assert!(
24            time::OffsetDateTime::parse(&s, &time::format_description::well_known::Rfc3339).is_ok()
25        );
26        assert!(unix_ms() > 1_600_000_000_000);
27    }
28}