Parse A DateTime That Has A Time Zone With Rust's chrono Crate
Introduction
My Grimoire uses RFC 3339 timestamp strings1. They're like ISO 86012 but with a few more restrictions on them (e.g. years must be four digits).
This is the code I use to parse them with Rust's chrono3 crate looks like this:
The Code
---
[dependencies]
anyhow = "1.0.98"
chrono = "0.4.40"
---
use anyhow::Result;
use chrono::DateTime;
fn main() -> Result<()> {
let time_string = "2025-04-14T15:58:47-04:00";
let date = DateTime::parse_from_rfc3339(
time_string
)?;
println!("{}", date.to_rfc2822());
Ok(())
}
Output:
Mon, 14 Apr 2025 15:58:47 -0400
-- end of line --