Exadra37

Exadra37

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?

Marked As Solved

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.

Also Liked

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

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.

Last Post!

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

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews