use std::time::{SystemTime, UNIX_EPOCH};
pub fn imf_fixdate(time: SystemTime) -> String {
httpdate::fmt_http_date(time)
}
pub fn parse_http_date(value: &str) -> Option<SystemTime> {
httpdate::parse_http_date(value).ok()
}
pub fn now() -> String {
imf_fixdate(SystemTime::now())
}
pub fn epoch_seconds(time: SystemTime) -> u64 {
time.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trip_http_date() {
let original = SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_000_000);
let formatted = imf_fixdate(original);
let parsed = parse_http_date(&formatted).expect("should parse http date");
assert_eq!(epoch_seconds(parsed), epoch_seconds(original));
}
}