Rust中如何将本地时间转换为另一个时区?

22-09-21 banq

下面的程序将本地时间转换为指定的时区。代码如下:

use chrono::prelude::*;

fn main() {

 let local_time = Local::now();
 let utc = DateTime::<Utc>::from_utc(local_time.naive_utc(), Utc);

 let est = FixedOffset::east(5 * 3600);

 println!("Local Time: {} EAT", local_time);
 println!("UTC Time now: {}", utc);
 println!("EST Time Now: {}", utc.with_timezone(&est));

 // example output
 // Local Time: 2022-02-27 14:50:31.014429200 +03:00 EAT
 // UTC Time now: 2022-02-27 11:50:31.014429200 UTC
 // EST Time Now: 2022-02-27 16:50:31.014429200 +05:00

}