verctl_core/runtime/
http_date.rs1use std::time::{Duration, SystemTime, UNIX_EPOCH};
14
15const WEEKDAYS: [&str; 7] = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
17
18const MONTHS: [&str; 12] = [
20 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
21];
22
23const EPOCH_WEEKDAY: i64 = 3;
25
26#[must_use]
29pub fn wait(value: &str) -> Option<Duration> {
30 let value = value.trim();
31 if let Ok(seconds) = value.parse::<u64>() {
32 return Some(Duration::from_secs(seconds));
33 }
34 let when = parse(value)?;
35 Some(when.duration_since(SystemTime::now()).unwrap_or_default())
36}
37
38#[must_use]
46pub fn parse_prefix(text: &str) -> Option<(SystemTime, &str)> {
47 const IMF_FIXDATE_LEN: usize = 29;
48 let trimmed = text.trim_start();
49 let candidate = trimmed.get(..IMF_FIXDATE_LEN)?;
50 if let Some(next) = trimmed.as_bytes().get(IMF_FIXDATE_LEN)
51 && next.is_ascii_alphanumeric()
52 {
53 return None;
54 }
55 let when = parse(candidate)?;
56 Some((when, candidate))
57}
58
59#[must_use]
63pub fn parse(text: &str) -> Option<SystemTime> {
64 let mut parts = text.split(' ');
65 let weekday = parts.next()?.strip_suffix(',')?;
66 let day = digits(parts.next()?, 2)?;
67 let month = parts.next()?;
68 let year = digits(parts.next()?, 4)?;
69 let clock = parts.next()?;
70 let zone = parts.next()?;
71 if parts.next().is_some() || zone != "GMT" {
72 return None;
73 }
74 let weekday = WEEKDAYS
75 .iter()
76 .zip(0_i64..)
77 .find_map(|(name, index)| (*name == weekday).then_some(index))?;
78 let month = MONTHS
79 .iter()
80 .zip(1_i64..)
81 .find_map(|(name, index)| (*name == month).then_some(index))?;
82 if !(1..=days_in_month(year, month)).contains(&day) {
83 return None;
84 }
85 let mut clock = clock.split(':');
86 let hour = digits(clock.next()?, 2).filter(|hour| *hour < 24)?;
87 let minute = digits(clock.next()?, 2).filter(|minute| *minute < 60)?;
88 let second = digits(clock.next()?, 2).filter(|second| *second < 60)?;
89 if clock.next().is_some() {
90 return None;
91 }
92 let days = days_from_civil(year, month, day);
93 if (days + EPOCH_WEEKDAY).rem_euclid(7) != weekday {
94 return None;
95 }
96 let seconds = days * 86_400 + hour * 3_600 + minute * 60 + second;
97 UNIX_EPOCH.checked_add(Duration::from_secs(u64::try_from(seconds).ok()?))
98}
99
100fn digits(text: &str, width: usize) -> Option<i64> {
102 if text.len() != width || !text.bytes().all(|byte| byte.is_ascii_digit()) {
103 return None;
104 }
105 text.parse().ok()
106}
107
108fn days_in_month(year: i64, month: i64) -> i64 {
110 let leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
111 match month {
112 2 if leap => 29,
113 2 => 28,
114 4 | 6 | 9 | 11 => 30,
115 _ => 31,
116 }
117}
118
119fn days_from_civil(year: i64, month: i64, day: i64) -> i64 {
126 let year = if month <= 2 { year - 1 } else { year };
127 let era = year.div_euclid(400);
128 let year_of_era = year.rem_euclid(400);
129 let shifted_month = (month + 9) % 12;
130 let day_of_year = (153 * shifted_month + 2) / 5 + day - 1;
131 let day_of_era = year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year;
132 era * 146_097 + day_of_era - 719_468
133}
134
135#[cfg(test)]
136mod tests;