diff --git a/CHANGELOG.md b/CHANGELOG.md index 4860438..4bcae0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +mail-parser 0.11.7 +================================ +- Fix: `DateTime::to_timezone` corrupting non-whole-hour offsets by storing leftover seconds in `tz_minute` (#158) + mail-parser 0.11.6 ================================ - Fix: Missing whitespace between a quoted name and a following encoded word (#150) diff --git a/Cargo.toml b/Cargo.toml index 0ce39bf..6dc507c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "mail-parser" description = "Fast and robust e-mail parsing library for Rust" -version = "0.11.6" +version = "0.11.7" edition = "2024" authors = [ "Stalwart Labs "] license = "Apache-2.0 OR MIT" diff --git a/src/parsers/fields/date.rs b/src/parsers/fields/date.rs index d46af88..c671721 100644 --- a/src/parsers/fields/date.rs +++ b/src/parsers/fields/date.rs @@ -258,7 +258,7 @@ impl DateTime { dt.tz_before_gmt = tz < 0; let tz = tz.abs(); dt.tz_hour = (tz / 3600) as u8; - dt.tz_minute = (tz % 3600) as u8; + dt.tz_minute = ((tz % 3600) / 60) as u8; dt } } @@ -539,4 +539,30 @@ mod tests { } } } + + #[test] + fn datetime_to_timezone() { + let dt = DateTime::parse_rfc3339("2021-01-01T00:00:00Z").unwrap(); + + for (tz, expected) in [ + (0i64, "2021-01-01T00:00:00Z"), + (3600, "2021-01-01T01:00:00+01:00"), + (-3600, "2020-12-31T23:00:00-01:00"), + (19800, "2021-01-01T05:30:00+05:30"), + (-12600, "2020-12-31T20:30:00-03:30"), + (20700, "2021-01-01T05:45:00+05:45"), + (16200, "2021-01-01T04:30:00+04:30"), + (34200, "2021-01-01T09:30:00+09:30"), + (-45900, "2020-12-31T11:15:00-12:45"), + ] { + let converted = dt.to_timezone(tz); + assert_eq!(converted.to_rfc3339(), expected, "failed for tz {tz}"); + assert!(converted.is_valid(), "invalid datetime for tz {tz}"); + assert_eq!( + converted.to_timestamp(), + dt.to_timestamp(), + "roundtrip failed for tz {tz}" + ); + } + } }