chrono icon indicating copy to clipboard operation
chrono copied to clipboard

Parsing IsoWeek from a string

Open scootermon opened this issue 9 months ago • 1 comments

I find myself in the situation where I need to parse ISO weeks quite frequently. Especially when it comes to "manufacturing dates", which are very often given as iso weeks like 2024-W30 or even 202430.

My current workaround is to go through a NaiveDate first:

fn parse_iso_week(s: &str) -> ParseResult<IsoWeek> {
    let date = NaiveDate::parse_from_str(&format!("{s} 1"), "%G-W%V %u").unwrap();
    Ok(date.iso_week())
}

// parse_iso_week("2024-W30")

Do you think it would make sense to add this feature natively to chrono? Unfortunately the Parsed::to_naive_date implementation is quite daunting, otherwise I would've already created a proof of concept PR.

scootermon avatar Feb 05 '25 14:02 scootermon