I want to return the local time at a timezone using chrono and chrono-tz. NaiveDate
and NaiveDateTime
can be converted to DateTime<Tz>
, but I can't convert DateTime<FixedOffset>
to DateTime<Tz>
.
// tz is Asia/Tokyo
// 2004-02-13 => 2004-02-13T00:00:00+09:00
// 2004-02-13T04:00:00 => 2004-02-13T04:00:00+09:00
// 2004-02-13T04:00:00 => 2004-02-13T04:00:00+09:00
// 2004-02-13T04:00:00+09:00 => 2004-02-13T04:00:00+09:00
// 2004-02-13T04:00:00+10:00 => 2004-02-13T03:00:00+09:00
fn local_datetime(s: &str, tz: &str) -> Option<DateTime<Tz>> {
let tz: Tz = tz.parse().expect("Check your config.json Timezone");
match s.len() {
10 => NaiveDate::parse_from_str(s, "%Y-%m-%d")
.ok()
.and_then(|dt| Some(dt.and_hms(0, 0, 0)))
.and_then(|dt| Some(tz.from_local_datetime(&dt).unwrap())),
19 => NaiveDateTime::parse_from_str(s, "%Y-%m-%dT%H:%M:%S")
.ok()
.and_then(|dt| Some(tz.from_local_datetime(&dt).unwrap())),
25 => DateTime::parse_from_rfc3339(s), // ParseResult<DateTime<FixedOffset>>
_ => None,
}
}