Exadra37

Exadra37

Ecto3 Timestamps and NaiveDatetime

In my understanding when we use NaiveDatetime for timestamps in ecto we loose the timezone information, thus I am having an hard time understanding how they can be useful when I don’t know their timezone.

So let’s consider the case where your server is wrongly configured for the timezone, lets say UTC+5, and then you fix it to be UTC+0, then all your records in the database will overlap for that period, but if the timestamps were in UTC+0, then this wouldn’t be a problem.

The main point I want to understand is if I am using a NaiveDatetime that I don’t know from what timezone it was created, then how can I really used it in a useful way from the inserted_at and updated_at, because when reading that values they can represent any timezone, and even if I inspect the timezone currently used on the server, I don’t have any guarantee they where generated from that timezone.

Maybe I am really missing the point of not using UTC timestamps for the inserted_at and updated_at, and if so can someone point me to a good resource explaining why UTC is not being used.

Or Am I misunderstanding NaiveDatetime?

First 10 of 10 Posts Switch mode

dimitarvp

dimitarvp

Why don’t you simply use :utc_datetime instead? You will get transparent conversion to UTC upon saving your records and everything will be in one timezone. That’s what I tend to do and I defer the conversion to local timezone to the frontend.

To clarify, you’ll still get NaiveDateTime structs in your Elixir code but they will correspond to UTC and not your (or the server’s) timezone.

Exadra37

Exadra37 OP

You mean when configuring Ecto timestamp options?

LostKobrakai

LostKobrakai

I generally create a custom schema macro + migration config, which sets timestamps to :utc_datetime_usec when starting a project.

But I feel there are a few misconceptions to clear up as well:

  1. A the database level there’s no difference between :naive_datetime and :utc_datetime. Both are stored as timestamps. The difference is only at runtime, where on storage :utc_datetime does enforce that the datetime was supplied with Etc/UTC timezone, while the naive counterpart has no timezone to enforce. When loading data from the db :utc_datetime expects the timestamp to be in UTC.

  2. Postgres has a timestamptz, but contrary to it’s name it doesn’t store a timezone just like timestamp. The difference is that for timestamptz postgres will try to convert datetimes to (and from) utc on its own. So with ecto you’re better of not having that, as ecto handles that part at runtime and you’ve one thing less to worry about with timestamp columns. timestamptz can make sense if you’re using the db not just with ecto.

So to conclude. Your db never actually stores a timezone, so from that standpoint it’s completely irrelevant if you’re using :naive_datetime or :utc_datetime. As for why ecto defaults to :naive_datetime I’d guess it’s because of backwards compatibility (but that could’ve been changed with ecto 3) and maybe the fact that elixir core does not ship with a timezone database, so it can only handle UTC as a timezone.

dimitarvp

dimitarvp

Yes and not only. Just declare all your datetime fields as :utc_datetime_usec or :utc_datetime.

LostKobrakai

LostKobrakai

This is imo not good advice. There are various places where naive_datetime is enough (a wall time in a fixed place) or :utc_datetime not being enough (a wall time in a certain place, which needs to be compared to times in other places).

dimitarvp

dimitarvp

Seeing as [in my experience] most people want a quick advice to get started and not have to write DB timestamp fixing scripts later, that advice is good enough IMO. Nuances of course always exist.

Exadra37

Exadra37 OP

This is what I expected to be by default, not that I would need to have to remember to set up each time.

Exadra37

Exadra37 OP

Exactly the type of workarounds that should not be needed.

Need to play around with this in Postgres, because at first sight I don’t get it.

So if you tell me that each time we use NaiveDatetime we are indeed using something that was created from UTC+0, then I am ok with NaiveDatetimes as the default, otherwise I just need to keep using workarounds all over the place.

LostKobrakai

LostKobrakai

That’s the case. The api is named NaiveDateTime.utc_now for a reason. Without an external timezone db elixir doesn’t even know of timezones outside of Etc/UTC. You can still mess this up though if your machines clock is not correct though, but that’s nothing elixir can cater for.

Exadra37

Exadra37 OP

Thanks for pointing it out. It was not clear from reading the Ecto docs that the timestamps are being created with it.

timestamps(opts \\ [])
(macro)

Generates :inserted_at and :updated_at timestamp fields.

The fields generated by this macro will automatically be set to the current time when inserting and updating values in a repository.
Options

    :inserted_at - the ecto schema name of the field for insertion times or false
    :updated_at - the ecto schema name of the field for update times or false
    :inserted_at_source - the name of the database column for insertion times or false
    :updated_at_source - the name of the database column for update times or false
    :type - the timestamps type, defaults to :naive_datetime.
    :autogenerate - a module-function-args tuple used for generating both inserted_at and updated_at timestamps

All options can be pre-configured by setting @timestamps_opts.

And when I looked into NaiveDateTime docs It was not clear to me either, maybe because utc_now is the last one in that docs, and everything from the start in that page doesn’t lead you into UTC being used :slight_smile:

iex> naive = ~N[2000-01-01 23:00:07]

naive.year
2000

naive.second
7
— All posts loaded —

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement