How do I convert a chrono `DateTime<UTC>` instance to `DateTime<Local>`?

Oops, thank you for reporting. This is a bug and registered as the issue #26. This should be fixed in Chrono 0.2.3.

Besides from the bug, utc.with_timezone(&Local) is indeed a correct way to convert to the local time. There is an important identity that utc.with_timezone(&Local).with_timezone(&UTC) should be equal to utc (except for the exceptional case, where the local time zone has been changed).


Starting with chrono 0.4.7 you can convert them between with using from trait in a simpler way:

use chrono::prelude::*;

fn main() {
    let utc = Utc::now();
    let local = Local::now();
    let converted: DateTime<Local> = DateTime::from(utc);
}

Tags:

Datetime

Rust