For example, I have a timestamp 1662012433
and I want to convert it to a String with a local timezone (Hong Kong time UTC+8) which would be 2022-09-01 14:07:13 UTC+0800
or 2022-09-01 14:07:13 HKT
.
Asked
Active
Viewed 428 times
0

dexhunter
- 578
- 8
- 24
1 Answers
0
So I found the solution easiest with help of chrono-tz
use chrono::{DateTime, Utc, NaiveDateTime};
use chrono_tz::Tz;
let timestamp = NaiveDateTime::from_timestamp(1662012433, 0);
let utc_time = timestamp.and_local_timezone(Utc).unwrap();
let tz:Tz = "Asia/Hong_Kong".parse().unwrap();
let hkt = utc_time.with_timezone(&tz);
assert_eq!(hkt.to_string(), "2022-09-01 14:07:13 HKT".to_string());

dexhunter
- 578
- 8
- 24