How to convert the chronology of `DateTime <UTC> 'to` DateTime <Local> `?

My goal is to convert utcto loc:

use chrono::{Local, UTC, TimeZone};

let utc = chrono::UTC::now();
let loc = chrono::Local::now();

println!("{:?}", utc);
println!("{:?}", loc);

println!("{:?}", utc.with_timezone(&Local));
println!("{:?}", Local.from_utc_datetime(&utc.naive_local()));

... which produced the following output:

2015-02-26T16:22:27.873593Z
2015-02-26T17:22:27.873663+01:00
2015-02-26T15:22:27.873593+00:00
2015-02-26T15:22:27.873593+00:00

The time locshown on the second line is what I want to see when converting utc.

How to convert an instance DateTime<UTC>to DateTime<Local>?

Meta h3>

I am using chrono 0.2.2 . In the method, DateTime.from_utche even tells me that I should use the tag TimeZone. However, I am missing something.

+5
source share
2 answers

, . # 26. Chrono 0.2.3.

, utc.with_timezone(&Local) . , utc.with_timezone(&Local).with_timezone(&UTC) utc ( , ).

+5

chrono 0.4.7, trait :

use chrono::prelude::*;

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

All Articles