Search code examples
rustrfc2822rust-chrono

How do I parse a date from RFC2822 allowing a time zone at the end of the string?


I am trying to parse mail headers. I am trying to parse the date with Chrono, by giving it the RFC2822 strings. The problem is that it is not able to parse strings on the format 2 Nov 2021 14:26:12 +0000 (UTC), where the problem seems to be the last part (UTC). How can I make Chrono parse also these strings?

use chrono::prelude::DateTime; // 0.4.19
use regex::Regex;              // 1.6.0

let date = "2 Nov 2021 14:26:12 +0000"; // does work
let date = "2 Nov 2021 14:26:12 +0000 (UTC)"; // does not work

// regex parses "[+-]dddd (www)" => " "[+-]dddd"
let re = Regex::new(r"([+-]?\d{4}) \(\w+\)$").unwrap();
let date = DateTime::parse_from_rfc2822(
        &re.replace(date_rfc2822_str, "$1")
    )
    .unwrap()
);

I can use regex to just remove the last part, but is it possible to parse it without this "hack"?


Solution

  • This was a bug in chrono.

    It got fixed and will potentially be released in chrono version 0.4.20.

    use chrono::prelude::DateTime; // main branch
    
    fn main() {
        let date = "2 Nov 2021 14:26:12 +0000 (UTC)";
        println!("{}", DateTime::parse_from_rfc2822(date).unwrap());
    }
    
    2021-11-02 14:26:12 +00:00