Ecto.DateTime and Time Zones

Is there a way to set up Ecto.DateTime objects for timezone? I would like to read Ecto.DateTime from the database, but then configure it for a user-assigned time zone?

In addition, I would like to accept the data entry and convert it back using the time zone setting.

I was looking for a library or technique to do this, but did not find anything obvious.

+4
source share
1 answer

Ecto does not include any such features. There are several libraries available for Elixir that can do this. I have used timex. To store Timex values ​​in a database using Ecto, you can use the package timex_ecto. Here's how you change the time zone values Timex.DateTimeto America/Los_Angeles:

iex(1)> original = Timex.now
#<DateTime(2016-08-17T06:24:03.015339Z Etc/UTC)>
iex(2)> timezone = Timex.Timezone.get("America/Los_Angeles", original)
#<TimezoneInfo(America/Los_Angeles - PDT (-07:00:00))>
iex(3)> converted = Timex.Timezone.convert(original, timezone)
#<DateTime(2016-08-16T23:24:03.015339-07:00 America/Los_Angeles)>
iex(4)> Timex.format!(original, "{ISO:Extended}")
"2016-08-17T06:24:03.015339+00:00"
iex(5)> Timex.format!(converted, "{ISO:Extended}")
"2016-08-16T23:24:03.015339-07:00"
+5
source

Source: https://habr.com/ru/post/1651491/


All Articles