1

我正在编写一些代码来解析 RSS 提要,但我遇到了Abstruse Goose RSS 提要的问题。如果您查看该提要,日期编码为Mon, 06 Aug 2018 00:00:00 UTC. 对我来说,它看起来像 RFC 2822。

我尝试使用 chrono's 解析它DateTime::parse_from_rfc2822,但我得到了ParseError(NotEnough).

let pub_date = entry.pub_date().unwrap().to_owned();
return rfc822_sanitizer::parse_from_rfc2822_with_fallback(&pub_date)
    .unwrap_or_else(|e| {
        panic!(
            "pub_date for item {:?} (value is {:?}) can't be parsed due to error {:?}",
            &entry, pub_date, e
        )
    })
    .naive_utc();

有什么我做错了吗?我必须以某种方式破解它吗?

我使用rfc822_sa​​nitizer,它可以很好地修复错误的书写错误(大部分时间)。我不认为它会影响解析......但谁知道呢?

4

1 回答 1

1

日期/时间格式在RFC2822RFC 中被很好地编码为以下格式:

date-time       =       [ day-of-week "," ] date FWS time [CFWS]
day-of-week     =       ([FWS] day-name) / obs-day-of-week
day-name        =       "Mon" / "Tue" / "Wed" / "Thu" /
                        "Fri" / "Sat" / "Sun"
date            =       day month year
year            =       4*DIGIT / obs-year
month           =       (FWS month-name FWS) / obs-month
month-name      =       "Jan" / "Feb" / "Mar" / "Apr" /
                        "May" / "Jun" / "Jul" / "Aug" /
                        "Sep" / "Oct" / "Nov" / "Dec"
day             =       ([FWS] 1*2DIGIT) / obs-day
time            =       time-of-day FWS zone
time-of-day     =       hour ":" minute [ ":" second ]
hour            =       2DIGIT / obs-hour
minute          =       2DIGIT / obs-minute
second          =       2DIGIT / obs-second
zone            =       (( "+" / "-" ) 4DIGIT) / obs-zone

其中obs-zone定义如下:

obs-zone        =       "UT" / "GMT" /          ; Universal Time
                                                ; North American UT
                                                ; offsets
                        "EST" / "EDT" /         ; Eastern:  - 5/ - 4
                        "CST" / "CDT" /         ; Central:  - 6/ - 5
                        "MST" / "MDT" /         ; Mountain: - 7/ - 6
                        "PST" / "PDT" /         ; Pacific:  - 8/ - 7
                        %d65-73 /               ; Military zones - "A"
                        %d75-90 /               ; through "I" and "K"
                        %d97-105 /              ; through "Z", both
                        %d107-122               ; upper and lower case

很多人在滚动他们自己的时间戳生成库时都会出错,这就是这一点——如何正确标记RFC2822TZ 偏移量。原因UT是因为UTCUT不完全相同(一个有额外的秒数,另一个有......四个变体!并且 RFC 没有定义使用哪一个;它们都有细微的不同)。

于 2019-09-11T17:40:42.423 回答