Skip to main content

acme_proxy_core/
datetime.rs

1//! Datetimes as the wire and the operator surfaces render them.
2//!
3//! Stored timestamps are epoch seconds; what a client, the admin API, a page or
4//! the CLI shows is RFC 3339 — the shape RFC 8555 uses for its datetime fields.
5//! One renderer, so a value cannot read differently on two surfaces.
6
7use time::OffsetDateTime;
8use time::format_description::well_known::Rfc3339;
9
10/// Renders epoch `secs` as an RFC 3339 datetime string, falling back to an
11/// empty string for the out-of-range timestamps that should never occur in
12/// practice.
13#[must_use]
14pub fn rfc3339(secs: i64) -> String {
15    OffsetDateTime::from_unix_timestamp(secs)
16        .ok()
17        .and_then(|dt| dt.format(&Rfc3339).ok())
18        .unwrap_or_default()
19}
20
21#[cfg(test)]
22mod tests {
23    use super::*;
24
25    #[test]
26    fn epoch_seconds_render_as_utc_rfc3339() {
27        assert_eq!(rfc3339(0), "1970-01-01T00:00:00Z");
28        assert_eq!(rfc3339(1_800_000_000), "2027-01-15T08:00:00Z");
29    }
30
31    #[test]
32    fn an_unrepresentable_instant_renders_empty() {
33        assert_eq!(rfc3339(i64::MAX), "");
34    }
35}