nikody

nikody

Why does Phoenix use naive datetime for timestamps by default?

I found various topics discussing the differences and similarities between :naive_datetime and :utc_datetime, namely:

In addition, I looked through Ecto.Schema — Ecto v3.14.0, where it says:

All of those types [naive_datetime, naive_datetime_usec, utc_datetime, utc_datetime_usec] 
are represented by the same timestamp/datetime in the underlying data storage, 
the difference are in their precision and how the data is loaded into Elixir.

I’m guessing the difference in precision is related to the _usec datetime types.

But I’m still wondering - why does Phoenix use :naive_datetime by default for timestamps? From all these discussions I either get the impression that it does not matter or that it’s slightly better to use :utc_datetime. However, in this topic I’m not trying to start a conversation comparing the two like as much as understand the default choice for Phoenix.

Edit: Added link to the Ecto documentation, too.

Most Liked

peerreynders

peerreynders

In my view the difference is semantics.

  • :utc_datetime is very clear that UTC is it’s frame of reference
  • :naive_datetime retains ambiguity as to what the frame of reference is. It’s up to the application context to imply and/or supply the frame of reference.
    • In the absence of any other evidence UTC is the only sensible choice.
    • In the application context of being used as a Phoenix timestamp it is known to be UTC.

So just because it’s :naive_datetime doesn’t automatically imply it’s not UTC.


nikody

nikody

Thank you both for the replies once more, and, I must say, for your contributions to other topics too.

I noticed this comment by Michał Muskała, which also seems helpful for my question - Deprecation warning says to use :naive_datetime but I'd like to use :utc_datetime - #5 by michalmuskala

Just to clarify. The warning says to use :naive_datetime since that’s the type 
with exactly the same behaviour as the old Ecto.DateTime. 
The :utc_datetime has slightly different semantics.
peerreynders

peerreynders

You already linked to this

The reason why Ecto’s timestamps() are naive by default is backwards compatibility.

In what way doesn’t that address your question?

It is my impression that the core team by convention treats time data without a timezone as UTC.

Confusion typically arises with people coming from other backgrounds who may have been working with a different convention - assuming that timezone free data is recorded with reference to local server time or local client time - which may be immediately convenient in the local context but creates problems in a long term historical or global context.

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
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
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
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

Other popular topics Top

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
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