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
In my view the difference is semantics.
:utc_datetimeis very clear that UTC is it’s frame of reference:naive_datetimeretains 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
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
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








