ashish173

ashish173

I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the timezone too.

I read a little more here and found out that on the DB end both the fields are the same which is utc time without timezone.

Now, I am a little confused about when should I use :naive_datetime and when :utc_datetime.

Showing Posts 1 to 10

Nicd

Nicd

You should use :utc_datetime unless you have a reason not to. The reason why Ecto’s timestamps() are naive by default is backwards compatibility. Since they are always UTC anyway, you should use :utc_datetime for them (and I believe the default will be changed to that in the future).

The difference between :utc_datetime and :naive_datetime is that the former will ensure that you can only insert UTC DateTimes and will read back UTC DateTime structs from the DB, whereas the latter will remove timezone information when writing and return NaiveDateTimes when you read from the DB.

For most cases, you should use UTC.

BTW, if you need to store the timezone too (in PostgreSQL), you have no option but to use a naive datetime field and store the offset or timezone information separately. This is because PostgreSQL does not have a data type for storing a timestamp with timezone.

There is the confusingly named timestamp with timezone but all it does is convert your input timestamp to UTC and convert it back to whatever your DB connection’s timezone is when reading (so for most cases it’s useless). It does not even store the offset/timezone. So you need to do that yourself somehow.

27
Post #1
OvermindDL1

OvermindDL1

Also, use naive/UTC to store datetimes in the past, use timezones only when storing datetimes in the future, don’t mix them (personal experience). ^.^;

outlog

outlog

for datetime in a specific location you generally need “wall time” - as timezones and/or daylight saving time changes..

something like calecto GitHub - lau/calecto: Adapter for the Calendar library in Ecto · GitHub will get you wall time..

time is fun (not!)

OvermindDL1

OvermindDL1

Exactly, that is why future times should always have a time zone. :slight_smile:

SZJX

SZJX

The “timestamp with timezone” type still seems to be useful since it makes all clients that try to read from this database interpret it as UTC time. Otherwise each client might try to interpret the timestamp in its local time, which would be a total mess. I find this article to summarize everything related to timezone in Elixir + Postgres pretty well: Time zones in PostgreSQL, Elixir and Phoenix | AmberBit Sp. z o. o.

LostKobrakai

LostKobrakai

The following argues that the exact same mess would happen when using timestamptz as well and that being the reason to not use it in ecto in the first place: utc_datetime having no affect on PostgreSQL DDL · Issue #1868 · elixir-ecto/ecto · GitHub

SZJX

SZJX

Interesting. The author of the article I linked to actually seems to think that this is the desired behavior:

While this will be true when you connect to PostgreSQL from your Elixir application, as soon as you connect using different client (psql, Ruby driver etc), the problems will start creeping in. SQL queries like:

SELECT * FROM events WHERE events.start_date < CURRENT_TIMESTAMP;

will have different meaning when you are in Europe/Warsaw time zone, and very much different when you are in PDT (California, USA), because of the 9 hour difference. If you have a reporting or maintenance script that you run against this database, depending on your client locale settings, you will miss more or less events from the query, or include unwanted ones in results. It would only be correct if your client’s settings were UTC.

How can you fix this issue? Use timestamptz data type in PostgreSQL, which is a shortcut to timestamp with time zone.

i.e. because the timestamp will then be guaranteed to be output in local time, the SQL queries will behave consistently across different clients.

I haven’t explored it myself yet so I’m not sure if this would indeed result in consistent behaviors for SQL queries, as the author claimed.

Nicd

Nicd

Wrong, timestamptz will return the timestamp in the connection’s local timezone. They explicitly won’t be getting UTC from the database, unless they have set the connection to UTC. Quoting:

When a timestamp with time zone value is output, it is always converted from UTC to the current timezone zone, and displayed as local time in that zone.

This is what I never want to happen.

Now it’s true that if you fire up psql or some script and compare the timestamp values to for example NOW(), you may end up with unexpected results if you didn’t remember to set the connection timezone. For me, using timestamptz here would be optimising for a special case and making the general case worse. It’s also reliant on magic (and frankly unintuitive) behaviour from PostgreSQL, and I feel being explicit in what you do (like you have to be when using plain timestamp) is better.

timestamp with time zone is a misnomer and IMO a misfeature that shouldn’t be in PostgreSQL.

SZJX

SZJX

Thanks a lot for the answer. This is definitely quite a rabbit hole to jump into and time representation is always a nightmare to deal with. At least one universal consensus seems to be to always use :utc_datetime in Ecto when there are no backwards compatibility worries. I’ll do that.

cnck1387

cnck1387

You’ve only reached the first tunnel of the rabbit hole because there’s timex and timex_ecto too.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
psy-q
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New

Other Trending Topics Top

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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews