kalkatfyodor

kalkatfyodor

Why isn't Phoenix using `utc_datetime` and `timestamptz`?

But instead using native_datetime and timestamp.

I have searched the Internet and the overwhelming majority is saying that timestamp should NOT be used and instead one should use timestamptz.

So why does Phoenix use native_datetime and timestamp?

Sources:

https://elixirguides.com/2019/06/what-is-the-difference-between-utc_datetime-and-naive_datetime-in-ecto/

Most Liked

Nicd

Nicd

Please use the search facilities of this forum, just a search for timestamptz returns several topics that have details about this.

The reason why Ecto’s timestamps() are naive by default is backwards compatibility. Back when Ecto was created, Elixir didn’t even have datetime types and Ecto didn’t have timezone support.

Also quoting my earlier answer in an earlier topic:

BTW, if you need to store the timezone too (in PostgreSQL), you have no option but to use a naive datetime field and store the offset or timezone information separately. This is because PostgreSQL does not have a data type for storing a timestamp with timezone.

There is the confusingly named timestamp with timezone but all it does is convert your input timestamp to UTC and convert it back to whatever your DB connection’s timezone is when reading. It does not even store the offset/timezone. So you need to do that yourself somehow.

Personally I don’t find much use in timestamptz, but that’s up to everyone to decide for themselves. It’s easy to override this in your migrations. In your schema you would use something like timestamps(type: :utc_datetime) and in migration timestamps(type: :timestamptz).

If you’re using just Elixir and your application is the only one connecting to the database, then it doesn’t make a difference if you use timestamp or timestamptz. If you use other applications to also connect to the same database, timestamptz will have different implications.

dodo

dodo

No idea, I always use timestamptz.

In migrations: timestamps(type: :timestamptz)

In schemas: timestamps(type: :utc_datetime_usec)

al2o3cr

al2o3cr

One of the answers in that linked StackOverflow post points to an official Postgres FAQ that has a decent-sounding reason:

(in response to "Don’t use timestamp without TZ to store UTC"):

When should you?

If compatibility with non-timezone-supporting databases trumps all other considerations.

Ecto already converts values to UTC, and needs to support other databases that don’t have timestamp with time zone.

Where Next?

Popular in Questions Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
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
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement