Roll-forward to next valid datetime in chrono-tz

31 Views Asked by At

If I have

// Cargo.toml
[package]
name = "tmp"
version = "0.1.0"
edition = "2021"

[dependencies]
chrono = "0.4.31"
chrono-tz = "0.8.3"
// src/main.rs
use chrono::{NaiveDate, NaiveDateTime, TimeZone};
use chrono_tz::Europe::London;

fn main() {
    let dt = NaiveDateTime::parse_from_str("2020-03-29T01:00:00", "%Y-%m-%dT%H:%M:%S").unwrap();
    let dt = London.from_local_datetime(&dt);
    println!("res: {:?}", dt);
}

and run cargo run, I get

res: None

Indeed, 2020-03-29T01:00:00 in Europe/London is a non-existent datetime due to daylight savings.

So, how can I get it to "roll forwards" to the next valid datetime in this time zone?

I can't just try adding an extra hour, unfortunately, as not all time zones go forwards by 1 hour during DST (e.g. Lord Howe goes forwards by half-an-hour). Furthermore, there may be non-existent datetimes due to calendar changes, where I'd need to go forwards more than half-an-hour.

Would just going forwards in half-hour-increments until I find a datetime that parses be a good solution?

0

There are 0 best solutions below