itssasanka
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
The documentation above suggests that while NaiveDateTime doesn’t store any
TimeZone data associated with it, DateTime seems to, however the former, when used,
Ecto seems to store timestamps (inserted_at, updated_at) in according to UTC timezone anyway.
I read that using utc_datetime “ensures” that dates are always converted to utc before getting stored/retrieved from and to the database, however I am not sure what the significance of the “ensures” aspect here is, since naive_datetime seems to do it anyway.
And I also think storing timestamptz values in the database may not be really required, since I can just store utc timestamps everywhere and user timezone preferences in another table and use these two pieces of info to convert accordingly before presenting to the users.
Can someone throw some light on what the real world significance in using utc_datetime type for timestamps is? What am I missing here?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
You’re correct.
timestampcolumns are actually better for ecto (alone) as withtimestamptzpostgres will try to convert the timestamp to the clients timezone on its own. While fortimestampyou can be sure postgres just leaves timestamps as is, with ecto handling all the timezone stuff at runtime using the settings/timezone db of elixir.naive_datetimedoes store timestamps as UTC datetimes – that’s true. But that’s only “accidental” because it’s usingNaiveDateTime.utc_now()to generate timestamps. If you manually set such fields to a different naive datetime you build from another timezone there’s no way for ecto to detect that. NaiveDateTime structs just don’t have a timezone component, which is the reason why they’re called naive.For
utc_datetimeecto will enforce that any saved datetime uses theEtc/UTCtimezone. It’ll error otherwise. If you query the record you’ll also get back only datetimes with tthe timezone set to UTC. So the fact that datetimes use UTC changed from being implicit and prone to error withnaive_datetimeto explicit and enforced at runtime withutc_datetime.dimitarvp
This only works for automatically generated fields like
inserted_atandupdated_atand if you manually set other datetime fields toDateTime.utc_now(). But if you have any other Elixir code that is not timezone-aware and it fetches time in the local timezone (or have a legacy DB with non-UTC datetimes in it) then you’re in for trouble.Better to do all of these together:
utc_datetimetype for datetime fields in your Ecto schema modules.DateTime.utc_now()and/or do calculations with Elixir’s stdlib ortimex, always making sure you use UTC.wojtekmach
Definitely use :utc_datetime_*. In hindsight, we wish we made that the default in Ecto but now we can’t due to backwards compatibility (and there are no plans for Ecto 4)
Here are some resources:
itssasanka
Thank you all so much! this sums it up, once and for all !
So basically it’s just great that
Ectois enforcing saving DateTime values inutcwith theutc_datetime, which I feel really positive about since it’s a standard development practice. I’ve seen no other library/frameworkenforcingit.I made the following global changes to my
Phoenix 1.5app to useutc_datetimeinstead ofnaive_datetimebyEcto, both in schemas and migrations for anyone who may find it useful:In
config.exs, I added this:created a new
schema.exwith the following code:And in my
modelseverywhere, I replaceduse Ecto.Schemawithuse MyApp.SchemaAnd, done!
dimitarvp
I do almost the same:
You likely will have cases when you would need sub-second precision. Even if you never do I’d think this extra precision won’t cost you almost anything in terms of disk space.
henrik
I started with :utc_datetime but switched to :utc_datetime_usec just because it got annoying having to do
DateTime.utc_now() |> DateTime.truncate(:second)every time. I don’t really need the precision but it seemed to be less friction for the stuff I’ve done so far.itssasanka
Sounds great! then I’ll make this change in my app to get rid of this friction, nothin’ much to lose here anyway.
daduam
Why was the migration_timestamps change in your config.exs necessary?
dimitarvp
Because IIRC having
inserted_atandupdated_atbeing at a microsecond precision is not the default.itssasanka
Because
Ecto.SQL.Migrationneeds that configuration. AFAIK it is a different module fromEcto.Schema