Parse RSS pub_date with std

I want something like that:

use chrono::{Datelike, NaiveDateTime, Timelike};
use std::{fs::create_dir_all, path::MAIN_SEPARATOR};

let date_time = NaiveDateTime::parse_from_str(pub_date, "%a, %d %b %Y %H:%M:%S %Z")?;

let dir = format!(
    "{base}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}",
    date_time.year(),
    date_time.month(),
    date_time.day()
);

Do I really want the chrono crate here?
And this construction "%a, %d %b %Y %H:%M:%S %Z" just to parse RFC 2822 into the object

I think that wrote some kind of trash code :slight_smile:

Chrono has a dedicated routine for parsing RFC 2822: DateTime in chrono - Rust

As does Jiff: parse in jiff::fmt::rfc2822 - Rust

I don't know what you're trying to do, but yeah, you probably want a datetime crate for this. It also depends on what you want to do with the result, which is unclear from your post here.

2 Likes

Thank you, now it's much better!

let date_time = DateTime::parse_from_rfc2822(pub_date)?;

ps. I'm trying to extract y/m/d as the filename members for the feed cache

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.