use std::time::{Duration, SystemTime, UNIX_EPOCH};
const WEEKDAYS: [&str; 7] = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
const MONTHS: [&str; 12] = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
];
const EPOCH_WEEKDAY: i64 = 3;
#[must_use]
pub fn wait(value: &str) -> Option<Duration> {
let value = value.trim();
if let Ok(seconds) = value.parse::<u64>() {
return Some(Duration::from_secs(seconds));
}
let when = parse(value)?;
Some(when.duration_since(SystemTime::now()).unwrap_or_default())
}
#[must_use]
pub fn parse(text: &str) -> Option<SystemTime> {
let mut parts = text.split(' ');
let weekday = parts.next()?.strip_suffix(',')?;
let day = digits(parts.next()?, 2)?;
let month = parts.next()?;
let year = digits(parts.next()?, 4)?;
let clock = parts.next()?;
let zone = parts.next()?;
if parts.next().is_some() || zone != "GMT" {
return None;
}
let weekday = WEEKDAYS
.iter()
.zip(0_i64..)
.find_map(|(name, index)| (*name == weekday).then_some(index))?;
let month = MONTHS
.iter()
.zip(1_i64..)
.find_map(|(name, index)| (*name == month).then_some(index))?;
if !(1..=days_in_month(year, month)).contains(&day) {
return None;
}
let mut clock = clock.split(':');
let hour = digits(clock.next()?, 2).filter(|hour| *hour < 24)?;
let minute = digits(clock.next()?, 2).filter(|minute| *minute < 60)?;
let second = digits(clock.next()?, 2).filter(|second| *second < 60)?;
if clock.next().is_some() {
return None;
}
let days = days_from_civil(year, month, day);
if (days + EPOCH_WEEKDAY).rem_euclid(7) != weekday {
return None;
}
let seconds = days * 86_400 + hour * 3_600 + minute * 60 + second;
UNIX_EPOCH.checked_add(Duration::from_secs(u64::try_from(seconds).ok()?))
}
fn digits(text: &str, width: usize) -> Option<i64> {
if text.len() != width || !text.bytes().all(|byte| byte.is_ascii_digit()) {
return None;
}
text.parse().ok()
}
fn days_in_month(year: i64, month: i64) -> i64 {
let leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
match month {
2 if leap => 29,
2 => 28,
4 | 6 | 9 | 11 => 30,
_ => 31,
}
}
fn days_from_civil(year: i64, month: i64, day: i64) -> i64 {
let year = if month <= 2 { year - 1 } else { year };
let era = year.div_euclid(400);
let year_of_era = year.rem_euclid(400);
let shifted_month = (month + 9) % 12;
let day_of_year = (153 * shifted_month + 2) / 5 + day - 1;
let day_of_era = year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year;
era * 146_097 + day_of_era - 719_468
}
#[cfg(test)]
mod tests {
use std::time::{Duration, UNIX_EPOCH};
use super::{parse, wait};
fn seconds(text: &str) -> Option<u64> {
parse(text).map(|when| {
when.duration_since(UNIX_EPOCH)
.expect("after the epoch")
.as_secs()
})
}
#[test]
fn a_known_date_is_its_unix_seconds() {
assert_eq!(seconds("Sun, 06 Nov 1994 08:49:37 GMT"), Some(784_111_777));
assert_eq!(
seconds("Fri, 13 Nov 2026 15:10:00 GMT"),
Some(1_794_582_600)
);
}
#[test]
fn leap_days_and_the_epoch() {
assert_eq!(seconds("Thu, 01 Jan 1970 00:00:00 GMT"), Some(0));
assert_eq!(
seconds("Thu, 29 Feb 2024 12:00:00 GMT"),
Some(1_709_208_000)
);
assert_eq!(seconds("Thu, 29 Feb 2023 12:00:00 GMT"), None);
}
#[test]
fn a_bad_weekday_or_month_is_refused() {
assert_eq!(seconds("Fry, 13 Nov 2026 15:10:00 GMT"), None);
assert_eq!(seconds("Sat, 13 Nov 2026 15:10:00 GMT"), None);
assert_eq!(seconds("Fri, 13 Nob 2026 15:10:00 GMT"), None);
assert_eq!(seconds("Fri, 13 November 2026 15:10:00 GMT"), None);
}
#[test]
fn a_zone_other_than_gmt_is_refused() {
assert_eq!(seconds("Fri, 13 Nov 2026 15:10:00 UTC"), None);
assert_eq!(seconds("Fri, 13 Nov 2026 15:10:00 +0000"), None);
assert_eq!(seconds("Fri, 13 Nov 2026 15:10:00"), None);
}
#[test]
fn another_shape_is_refused() {
for text in [
"",
"Friday, 13-Nov-26 15:10:00 GMT",
"Fri Nov 13 15:10:00 2026",
"Fri, 13 Nov 2026 25:10:00 GMT",
"Fri, 13 Nov 2026 15:10 GMT",
"Fri, 13 Nov 2026 15:10:00 GMT extra",
] {
assert_eq!(seconds(text), None, "{text:?}");
}
}
#[test]
fn a_retry_after_is_seconds_or_a_date() {
assert_eq!(wait("0"), Some(Duration::ZERO));
assert_eq!(wait(" 30 "), Some(Duration::from_secs(30)));
assert!(wait("Fri, 13 Nov 2026 15:10:00 GMT").is_some());
assert_eq!(wait("Sun, 06 Nov 1994 08:49:37 GMT"), Some(Duration::ZERO));
assert_eq!(wait("soon"), None);
assert_eq!(wait("-5"), None);
}
}