From d94015d676f448c6dfbb5f3d586d3ff0f05d5e61 Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Sat, 21 Nov 2026 13:48:38 -0500 Subject: [PATCH 1/3] Extract apply_timezone helper in dateparse Move the Timezone arm into a shared helper with no behavior change. --- src/dateparse.rs | 110 ++++++++++++++++++++++++----------------------- 1 file changed, 57 insertions(+), 53 deletions(-) diff --git a/src/dateparse.rs b/src/dateparse.rs index eb35380..c3c20a7 100644 --- a/src/dateparse.rs +++ b/src/dateparse.rs @@ -66,6 +66,62 @@ fn seconds_to_date(year: i64, month: i64, day: i64) -> i64 { result + 86400 * (day - 1) } +fn apply_timezone(result: i64, tok: &str) -> Result { + let (tz, tz_sign) = match tok.parse::() { + Ok(v) if !(-2400..=2400).contains(&v) => { + return Err(MailParseError::Generic("Invalid timezone")); + } + Ok(v) if v < 0 => (-v, -1), + Ok(v) => (v, 1), + Err(_) => { + match tok.to_uppercase().as_str() { + // This list taken from IETF RFC 822 + "UTC" | "UT" | "GMT" | "Z" => (0, 1), + "EDT" => (400, -1), + "EST" | "CDT" => (500, -1), + "CST" | "MDT" => (600, -1), + "MST" | "PDT" => (700, -1), + "PST" => (800, -1), + // Military time zones (RFC 822, RFC 5322). A-M (except J) are negative offsets, N-Y are positive. J is not used. + "A" => (100, -1), + "B" => (200, -1), + "C" => (300, -1), + "D" => (400, -1), + "E" => (500, -1), + "F" => (600, -1), + "G" => (700, -1), + "H" => (800, -1), + "I" => (900, -1), + // "J" is not used + "K" => (1000, -1), + "L" => (1100, -1), + "M" => (1200, -1), + "N" => (100, 1), + "O" => (200, 1), + "P" => (300, 1), + "Q" => (400, 1), + "R" => (500, 1), + "S" => (600, 1), + "T" => (700, 1), + "U" => (800, 1), + "V" => (900, 1), + "W" => (1000, 1), + "X" => (1100, 1), + "Y" => (1200, 1), + _ => return Err(MailParseError::Generic("Invalid timezone")), + } + } + }; + let tz_hours = tz / 100; + let tz_mins = tz % 100; + let tz_delta = (tz_hours * 3600) + (tz_mins * 60); + if tz_sign < 0 { + Ok(result + i64::from(tz_delta)) + } else { + Ok(result - i64::from(tz_delta)) + } +} + /// Convert a date field from an email header into a UNIX epoch timestamp. /// This function handles the most common formatting of date fields found in /// email headers. It may fail to parse some of the more creative formattings. @@ -155,59 +211,7 @@ pub fn dateparse(date: &str) -> Result { continue; } DateParseState::Timezone => { - let (tz, tz_sign) = match tok.parse::() { - Ok(v) if !(-2400..=2400).contains(&v) => { - return Err(MailParseError::Generic("Invalid timezone")); - } - Ok(v) if v < 0 => (-v, -1), - Ok(v) => (v, 1), - Err(_) => { - match tok.to_uppercase().as_str() { - // This list taken from IETF RFC 822 - "UTC" | "UT" | "GMT" | "Z" => (0, 1), - "EDT" => (400, -1), - "EST" | "CDT" => (500, -1), - "CST" | "MDT" => (600, -1), - "MST" | "PDT" => (700, -1), - "PST" => (800, -1), - // Military time zones (RFC 822, RFC 5322). A-M (except J) are negative offsets, N-Y are positive. J is not used. - "A" => (100, -1), - "B" => (200, -1), - "C" => (300, -1), - "D" => (400, -1), - "E" => (500, -1), - "F" => (600, -1), - "G" => (700, -1), - "H" => (800, -1), - "I" => (900, -1), - // "J" is not used - "K" => (1000, -1), - "L" => (1100, -1), - "M" => (1200, -1), - "N" => (100, 1), - "O" => (200, 1), - "P" => (300, 1), - "Q" => (400, 1), - "R" => (500, 1), - "S" => (600, 1), - "T" => (700, 1), - "U" => (800, 1), - "V" => (900, 1), - "W" => (1000, 1), - "X" => (1100, 1), - "Y" => (1200, 1), - _ => return Err(MailParseError::Generic("Invalid timezone")), - } - } - }; - let tz_hours = tz / 100; - let tz_mins = tz % 100; - let tz_delta = (tz_hours * 3600) + (tz_mins * 60); - if tz_sign < 0 { - result += i64::from(tz_delta); - } else { - result -= i64::from(tz_delta); - } + result = apply_timezone(result, tok)?; break; } } From 4969eb48c45df482d979c98e1d81e2d5ddbc6708 Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Sat, 21 Nov 2026 13:48:48 -0500 Subject: [PATCH 2/3] Accept optional seconds in dateparse RFC 5322 section 3.3 makes seconds optional in the time production (hour ":" minute [":" second]). The Second state previously always parsed the next token as u8 seconds, so a numeric zone like +0100 was read as 100 seconds and zones like -0500 or GMT failed. Only treat a token of one or two ASCII digits as seconds; otherwise handle the token as a timezone. Add tests for the issue #154 cases. --- src/dateparse.rs | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/dateparse.rs b/src/dateparse.rs index c3c20a7..a8b1574 100644 --- a/src/dateparse.rs +++ b/src/dateparse.rs @@ -202,13 +202,18 @@ pub fn dateparse(date: &str) -> Result { continue; } DateParseState::Second => { - let second = match tok.parse::() { - Ok(v) => v, - Err(_) => return Err(MailParseError::Generic("Invalid second")), - }; - result += i64::from(second); - state = DateParseState::Timezone; - continue; + // RFC 5322 section 3.3: seconds are optional. Only treat a + // token of one or two ASCII digits as seconds; otherwise this + // token is the timezone. + if tok.len() <= 2 && tok.as_bytes().iter().all(u8::is_ascii_digit) { + let second = tok + .parse::() + .expect("one or two ASCII digits always parse as u8"); + result += i64::from(second); + state = DateParseState::Timezone; + continue; + } + return apply_timezone(result, tok); } DateParseState::Timezone => { result = apply_timezone(result, tok)?; @@ -259,6 +264,23 @@ mod tests { 1354323443 ); + // RFC 5322 section 3.3: seconds are optional (hour ":" minute [":" second]). + assert_eq!( + dateparse("Sat, 14 Dec 1985 11:43:00 +0100").unwrap(), + 503404980 + ); + assert_eq!( + dateparse("Sat, 14 Dec 1985 11:43 +0100").unwrap(), + 503404980 + ); + assert_eq!( + dateparse("Sat, 14 Dec 1985 11:43 -0500").unwrap(), + 503426580 + ); + assert_eq!(dateparse("Sat, 14 Dec 1985 11:43 GMT").unwrap(), 503408580); + // No timezone after minutes: same as before (interpreted as UTC). + assert_eq!(dateparse("Sat, 14 Dec 1985 11:43").unwrap(), 503408580); + // Day cannot be zero. assert!(dateparse("Wed, 0 Jan 1970 00:00:00 +0000").is_err()); From 6d8df38eb3bc6c8e19295f1c435f1c6e4d974b01 Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Sat, 21 Nov 2026 13:48:59 -0500 Subject: [PATCH 3/3] Split date tokens on folding whitespace RFC 5322 folding whitespace (FWS) in Date fields can include tabs and CRLF, but dateparse only split on spaces and colons, so dates with tabs failed to parse (issue #154). Also split on tab, CR, and LF, and test a tab-separated date against its space-separated equivalent. --- src/dateparse.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/dateparse.rs b/src/dateparse.rs index a8b1574..50a4374 100644 --- a/src/dateparse.rs +++ b/src/dateparse.rs @@ -136,7 +136,7 @@ pub fn dateparse(date: &str) -> Result { let mut month = 0; let mut day_of_month = 0; let mut state = DateParseState::Date; - for tok in date.split([' ', ':']) { + for tok in date.split([' ', '\t', '\r', '\n', ':']) { if tok.is_empty() { continue; } @@ -281,6 +281,16 @@ mod tests { // No timezone after minutes: same as before (interpreted as UTC). assert_eq!(dateparse("Sat, 14 Dec 1985 11:43").unwrap(), 503408580); + // RFC 5322 FWS in Date fields can include tabs. + assert_eq!( + dateparse("Tue, 12 Aug 2003 05:39:03 -0330").unwrap(), + 1060679343 + ); + assert_eq!( + dateparse("Tue,\t12 Aug\t2003\t05:39:03\t-0330").unwrap(), + dateparse("Tue, 12 Aug 2003 05:39:03 -0330").unwrap() + ); + // Day cannot be zero. assert!(dateparse("Wed, 0 Jan 1970 00:00:00 +0000").is_err());