Utc_datetime vs naive_datetime, which one to use for ecto timestamps?

Hi all,

Did some research on this forum and found information on naive_datetime and utc_datetime. My understanding is that the naive_datetime does not store the timezone info in it. I am using postgres, and in my migrations I simply said timestamps(), Looks like the values getting stored here are NaiveDateTime.

There are suggestions here to use UtcDateTime for Ecto timestamps() but I am wondering if NaiveDateTime is a concern really?
Aren’t the values getting stored in the table UTC anyway?
On my system, DateTime.utc_now, and NaiveDateTime.utc_now seem to yield the same UTC current time. Am I missing anything here?

I would appreciate some expert insight into this one. Thank you a ton everyone!

From the db perspective both types are the same and use the same data/column in postgres. The difference is that naive_datetime in elixir does not have timezone data, while datetime values have one and ecto makes sure only UTC ones can be saved to the db.

only utc ones are saved to the db

Do you mean to say ecto converts to utc before writing them?

You can specify in migrations and schema the type of timestamps

timestamps(type: :utc_datetime)
1 Like

It doesn‘t, but it fails it the datetime is not set to UTC (no current elixir version can convert between timezones without 3rd party dependencies).