pub fn unix_to_rfc3339(secs: i64) -> String {
chrono::DateTime::<chrono::Utc>::from_timestamp(secs, 0)
.unwrap_or_else(|| {
chrono::DateTime::<chrono::Utc>::from_timestamp(0, 0)
.expect("unix epoch is in range")
})
.to_rfc3339()
}
pub fn unix_to_rfc3339_opt(secs: Option<i64>) -> Option<String> {
secs.map(unix_to_rfc3339)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn epoch_renders_rfc3339() {
assert_eq!(unix_to_rfc3339(0), "1970-01-01T00:00:00+00:00");
}
#[test]
fn option_passthrough() {
assert_eq!(unix_to_rfc3339_opt(None), None);
assert_eq!(
unix_to_rfc3339_opt(Some(0)),
Some("1970-01-01T00:00:00+00:00".to_string())
);
}
#[test]
fn out_of_range_falls_back_to_epoch_not_panic() {
assert_eq!(unix_to_rfc3339(i64::MAX), "1970-01-01T00:00:00+00:00");
}
}