chrono icon indicating copy to clipboard operation
chrono copied to clipboard

`impl From<DateTime<Local>> for DateTime<FixedOffset>` drops timezone information

Open deviant opened this issue 4 years ago • 0 comments

For some reason, this impl uses a fixed offset of zero. This is surprising, because FixedOffset can represent Local offsets—indeed, Local is a FixedOffset internally—but instead this information is being lost. Perhaps it was introduced by a copy+paste of impl From<DateTime<Utc>>?

I found a way of converting them correctly, although I don't know if this is the optimal solution:

let now = Local::now();
let fixed = DateTime::<FixedOffset>::from_utc(now.naive_utc(), *now.offset());

Using now.offset().fix() as the offset argument also works.

Given that all timezones are required to be fixable, is there a reason why there isn't just a generic impl From<Tz: TimeZone> using that? Or perhaps a fix method on DateTime itself.

You can also do the following, which looks really weird but works (I think because TimeZone<Local>::Offset = FixedOffset):

let now = Local::now();
let fixed = now.with_timezone(now.offset());

deviant avatar Jul 23 '21 08:07 deviant