1use 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 {
137 use std::time::{Duration, UNIX_EPOCH};
138
139 use super::{parse, parse_prefix, wait};
140
141 fn seconds(text: &str) -> Option<u64> {
142 parse(text).map(|when| {
143 when.duration_since(UNIX_EPOCH)
144 .expect("after the epoch")
145 .as_secs()
146 })
147 }
148
149 #[test]
151 fn a_known_date_is_its_unix_seconds() {
152 assert_eq!(seconds("Sun, 06 Nov 1994 08:49:37 GMT"), Some(784_111_777));
153 assert_eq!(
154 seconds("Fri, 13 Nov 2026 15:10:00 GMT"),
155 Some(1_794_582_600)
156 );
157 }
158
159 #[test]
161 fn leap_days_and_the_epoch() {
162 assert_eq!(seconds("Thu, 01 Jan 1970 00:00:00 GMT"), Some(0));
163 assert_eq!(
164 seconds("Thu, 29 Feb 2024 12:00:00 GMT"),
165 Some(1_709_208_000)
166 );
167 assert_eq!(seconds("Thu, 29 Feb 2023 12:00:00 GMT"), None);
168 }
169
170 #[test]
171 fn a_bad_weekday_or_month_is_refused() {
172 assert_eq!(seconds("Fry, 13 Nov 2026 15:10:00 GMT"), None);
173 assert_eq!(seconds("Sat, 13 Nov 2026 15:10:00 GMT"), None);
174 assert_eq!(seconds("Fri, 13 Nob 2026 15:10:00 GMT"), None);
175 assert_eq!(seconds("Fri, 13 November 2026 15:10:00 GMT"), None);
176 }
177
178 #[test]
179 fn a_zone_other_than_gmt_is_refused() {
180 assert_eq!(seconds("Fri, 13 Nov 2026 15:10:00 UTC"), None);
181 assert_eq!(seconds("Fri, 13 Nov 2026 15:10:00 +0000"), None);
182 assert_eq!(seconds("Fri, 13 Nov 2026 15:10:00"), None);
183 }
184
185 #[test]
186 fn another_shape_is_refused() {
187 for text in [
188 "",
189 "Friday, 13-Nov-26 15:10:00 GMT",
190 "Fri Nov 13 15:10:00 2026",
191 "Fri, 13 Nov 2026 25:10:00 GMT",
192 "Fri, 13 Nov 2026 15:10 GMT",
193 "Fri, 13 Nov 2026 15:10:00 GMT extra",
194 ] {
195 assert_eq!(seconds(text), None, "{text:?}");
196 }
197 }
198
199 #[test]
201 fn a_retry_after_is_seconds_or_a_date() {
202 assert_eq!(wait("0"), Some(Duration::ZERO));
203 assert_eq!(wait(" 30 "), Some(Duration::from_secs(30)));
204 assert!(wait("Fri, 13 Nov 2026 15:10:00 GMT").is_some());
205 assert_eq!(wait("Sun, 06 Nov 1994 08:49:37 GMT"), Some(Duration::ZERO));
206 assert_eq!(wait("soon"), None);
207 assert_eq!(wait("-5"), None);
208 }
209
210 #[test]
211 fn parse_prefix_stops_after_imf_fixdate_regardless_of_trailing_prose() {
212 let current = parse_prefix(
213 "Wed, 02 Dec 2026 15:49:31 GMT and see https://crates.io/docs/rate-limits for more details.",
214 );
215 assert_eq!(
216 current
217 .map(|(when, slice)| (when.duration_since(UNIX_EPOCH).unwrap().as_secs(), slice)),
218 Some((1_796_226_571, "Wed, 02 Dec 2026 15:49:31 GMT"))
219 );
220 let legacy = parse_prefix("Mon, 19 Oct 2026 01:23:45 GMT or email help@crates.io.");
221 assert_eq!(
222 legacy.map(|(_, slice)| slice),
223 Some("Mon, 19 Oct 2026 01:23:45 GMT")
224 );
225 assert!(parse_prefix("Wed, 02 Dec 2026 15:49:31 GMTX").is_none());
226 assert!(parse_prefix("not a date").is_none());
227 }
228}