use chrono::{Datelike, TimeZone, Timelike, Utc};
use std::fmt::Write;
const DAYS: [&str; 7] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const MONTHS: [&str; 12] = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
];
pub fn format_http_date(timestamp: u64) -> String {
let dt = Utc
.timestamp_opt(timestamp as i64, 0)
.single()
.unwrap_or_else(|| Utc.timestamp_opt(0, 0).unwrap());
let day_name = DAYS[dt.weekday().num_days_from_sunday() as usize];
let month_name = MONTHS[(dt.month() - 1) as usize];
let mut s = String::with_capacity(29);
let _ = write!(
s,
"{}, {:02} {} {:04} {:02}:{:02}:{:02} GMT",
day_name,
dt.day(),
month_name,
dt.year(),
dt.hour(),
dt.minute(),
dt.second(),
);
s
}
pub fn parse_http_date(date_str: &str) -> Option<u64> {
if let Some(without_gmt) = date_str.strip_suffix(" GMT") {
if let Ok(naive) =
chrono::NaiveDateTime::parse_from_str(without_gmt, "%a, %d %b %Y %H:%M:%S")
{
let ts = Utc.from_utc_datetime(&naive).timestamp();
if ts >= 0 {
return Some(ts as u64);
}
}
if let Ok(naive) =
chrono::NaiveDateTime::parse_from_str(without_gmt, "%A, %d-%b-%y %H:%M:%S")
{
let ts = Utc.from_utc_datetime(&naive).timestamp();
if ts >= 0 {
return Some(ts as u64);
}
}
}
if let Ok(naive) = chrono::NaiveDateTime::parse_from_str(date_str, "%a %b %e %H:%M:%S %Y") {
let ts = Utc.from_utc_datetime(&naive).timestamp();
if ts >= 0 {
return Some(ts as u64);
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn formats_rfc_7231_shape() {
let ts = 784111777u64;
assert_eq!(format_http_date(ts), "Sun, 06 Nov 1994 08:49:37 GMT");
}
#[test]
fn formats_epoch() {
assert_eq!(format_http_date(0), "Thu, 01 Jan 1970 00:00:00 GMT");
}
#[test]
fn formats_fixed_width_29_bytes() {
for ts in [0u64, 1, 1_000_000, 784_111_777, 2_000_000_000] {
let s = format_http_date(ts);
assert_eq!(s.len(), 29, "wrong length for ts={}: {:?}", ts, s);
}
}
#[test]
fn parses_rfc_7231() {
assert_eq!(
parse_http_date("Sun, 06 Nov 1994 08:49:37 GMT"),
Some(784111777)
);
}
#[test]
fn parses_rfc_850() {
assert_eq!(
parse_http_date("Sunday, 06-Nov-94 08:49:37 GMT"),
Some(784111777)
);
}
#[test]
fn parses_asctime() {
assert_eq!(parse_http_date("Sun Nov 6 08:49:37 1994"), Some(784111777));
}
#[test]
fn rejects_bogus() {
assert_eq!(parse_http_date("not a date"), None);
assert_eq!(parse_http_date("Sun, 06 Nov 1994 08:49:37"), None); }
#[test]
fn round_trip() {
for ts in [0u64, 784_111_777, 1_700_000_000] {
let s = format_http_date(ts);
assert_eq!(parse_http_date(&s), Some(ts));
}
}
}