krab-api 0.2.1

Kubernetes-compatible API server for krabkube
Documentation
use std::time::{SystemTime, UNIX_EPOCH};

pub fn now_secs() -> i64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs() as i64)
        .unwrap_or(0)
}

pub fn rfc3339(secs: i64) -> String {
    let days = secs.div_euclid(86400);
    let rem = secs.rem_euclid(86400);
    let z = days + 719468;
    let era = z.div_euclid(146097);
    let doe = z.rem_euclid(146097);
    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
    let y = yoe + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = doy - (153 * mp + 2) / 5 + 1;
    let m = if mp < 10 { mp + 3 } else { mp - 9 };
    let y = if m <= 2 { y + 1 } else { y };
    format!(
        "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
        y,
        m,
        d,
        rem / 3600,
        (rem % 3600) / 60,
        rem % 60
    )
}

pub fn parse_rfc3339(s: &str) -> Option<i64> {
    let b = s.as_bytes();
    if b.len() < 20 || b[4] != b'-' || b[7] != b'-' || (b[10] != b'T' && b[10] != b' ') {
        return None;
    }
    let n = |r: std::ops::Range<usize>| s.get(r)?.parse::<i64>().ok();
    let (y, m, d, hh, mm, ss) = (
        n(0..4)?,
        n(5..7)?,
        n(8..10)?,
        n(11..13)?,
        n(14..16)?,
        n(17..19)?,
    );
    let y2 = if m <= 2 { y - 1 } else { y };
    let era = y2.div_euclid(400);
    let yoe = y2.rem_euclid(400);
    let mp = if m > 2 { m - 3 } else { m + 9 };
    let doy = (153 * mp + 2) / 5 + d - 1;
    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
    let days = era * 146097 + doe - 719468;
    Some(days * 86400 + hh * 3600 + mm * 60 + ss)
}

pub fn age(secs: i64) -> String {
    let s = secs.max(0);
    if s < 120 {
        format!("{s}s")
    } else if s < 600 {
        format!("{}m{}s", s / 60, s % 60).replace("m0s", "m")
    } else if s < 3 * 3600 {
        format!("{}m", s / 60)
    } else if s < 8 * 3600 {
        let (h, m) = (s / 3600, (s % 3600) / 60);
        if m == 0 {
            format!("{h}h")
        } else {
            format!("{h}h{m}m")
        }
    } else if s < 48 * 3600 {
        format!("{}h", s / 3600)
    } else if s < 8 * 86400 {
        let (d, h) = (s / 86400, (s % 86400) / 3600);
        if h == 0 {
            format!("{d}d")
        } else {
            format!("{d}d{h}h")
        }
    } else {
        format!("{}d", s / 86400)
    }
}

pub fn uid_from(id: &str) -> String {
    let h: String = id
        .chars()
        .filter(|c| c.is_ascii_hexdigit())
        .chain(std::iter::repeat('0'))
        .take(32)
        .collect();
    format!(
        "{}-{}-{}-{}-{}",
        &h[0..8],
        &h[8..12],
        &h[12..16],
        &h[16..20],
        &h[20..32]
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn time_roundtrip() {
        for t in [0, 86399, 1_700_000_000, 1_795_000_000] {
            assert_eq!(parse_rfc3339(&rfc3339(t)), Some(t));
        }
        assert_eq!(rfc3339(0), "1970-01-01T00:00:00Z");
    }

    #[test]
    fn ages() {
        assert_eq!(age(5), "5s");
        assert_eq!(age(3600 * 5), "5h");
        assert_eq!(age(86400 * 20), "20d");
    }

    #[test]
    fn uid_format() {
        assert_eq!(uid_from("abcdef0123456789abcdef0123456789zz").len(), 36);
    }
}