简体   繁体   中英

using Timezone.convert when dates are on different sides of DST

Dates are passed to the backend in UTC, and I want to convert time to a given TZ. I'm running into a bug as March 10th is in a different DST for UTC than my test TZ (America/Los_Angeles).

date = #DateTime<2019-03-10 02:25:19.464342Z>
timezone = Timezone.get("America/Los_Angeles", d2)
Timezone.convert(date, timezone)

Line 2 gives the following error

{:error, {:could_not_resolve_timezone, "America/Los_Angeles", 63719403919, :wall}}

I understand that this is because of DST, but at the given time, LA exists in some other datetime, which is what I'm looking for, how would I get that datetime?

How about Timezone.resolve/3 ?

{:ok, date, _} = DateTime.from_iso8601("2019-03-10 02:25:19.464342Z")
unix = DateTime.to_unix(date)
timezone = Timex.Timezone.resolve("America/Los_Angeles", unix)
Timex.Timezone.convert(date, timezone)

Output:

#DateTime<2019-03-09 18:25:19.464342-08:00 PST America/Los_Angeles>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM