简体   繁体   English

Rust Chrono date parse from RFC2822 allow time zone string at the end of the string

[英]Rust Chrono date parse from RFC2822 allow time zone string 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 .我正在尝试使用 Chrono 解析日期,方法是给它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) .问题是它无法解析格式为2 Nov 2021 14:26:12 +0000 (UTC)的字符串,问题似乎是最后一部分(UTC) How can I make Chrono parse also these strings?我怎样才能让 Chrono 也解析这些字符串?

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"?我可以使用正则表达式来删除最后一部分,但是否可以在没有这个“hack”的情况下解析它?

This was a bug in chrono .chrono中的一个错误

It got fixed and will potentially be released in chrono version 0.4.20 .它已得到修复,可能会在 chrono 版本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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM