eqon133

eqon133

Ecto, Phoenix and utc datetime

Hello,

I have a Phoenix app using Html.Form datetime_select
I fill user input using Timex zone convert in Europe/Tallinn (UTC+2):

Saved result is same as input

Shouldn’t it be saved as UTC, since all times are UTC in db?

field(:planned_start_at, :utc_datetime)

Q: Where should this convert to UTC? Should i create custom Ecto.Type for this? or in changeset before insert? or can Ecto handle it itself?

inserted_at and updated_at are correctly in UTC
Phoenix 1.4, Ecto 3.0, Postgres

Thanks,

Marked As Solved

eqon133

eqon133

I will provide my solution here. So i created custom Ecto.Type which will receive local datetime and timezone. And that will convert it to utc.

  @behaviour Ecto.Type
  def type(), do: :utc_datetime
..
  def cast(%{"datetimelocal" => datetimelocal, "tz" => tz}) do
    case Timex.parse(datetimelocal, "{ISO:Extended}") do
      {:error, _error} ->
        Ecto.Type.cast(type(), nil)
      {:ok, value} ->
        Ecto.Type.cast(type(), Timex.to_datetime(value, tz))
    end
  end
  ..
  def cast(data), do: Ecto.Type.cast(type(), data)

Also Liked

wojtekmach

wojtekmach

Hex Core Team

When casting a non-UTC %DateTime{} to :utc_datetime it’s gonna be converted to UTC timezone as we can do this safely. After casting it’s all UTC.

As far as storing future date times in UTC that’s indeed dangerous. See @Lau’s excellent blog posts: http://www.creativedeletion.com

OvermindDL1

OvermindDL1

As I recall :utc_datetime stores timezone information where :naive_datetime does not and stores things as utc always. I think… ^.^;

Regardless, datetimes in the future you want to store with timezone information. Timezones change all the time and if someone plans something on, say, March 18th at 5pm, and daylight savings starts on March 20th but then that gets changed to start on March 12th then if you stored the datetime as utc in the database then you are off by an hour where if you stored it with the proper timezone then it will still be correct. :slight_smile:

Past times are of course fine to store timezoneless.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement