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.
Trending in Questions
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
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
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
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
Other Trending Topics
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
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
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
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
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
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
- #ai
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Nicd
You should use
:utc_datetimeunless you have a reason not to. The reason why Ecto’stimestamps()are naive by default is backwards compatibility. Since they are always UTC anyway, you should use:utc_datetimefor them (and I believe the default will be changed to that in the future).The difference between
:utc_datetimeand:naive_datetimeis that the former will ensure that you can only insert UTCDateTimes and will read back UTCDateTimestructs from the DB, whereas the latter will remove timezone information when writing and returnNaiveDateTimes 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.
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
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
Exactly, that is why future times should always have a time zone.
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
The following argues that the exact same mess would happen when using
timestamptzas 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 · GitHubSZJX
Interesting. The author of the article I linked to actually seems to think that this is the desired behavior:
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
Wrong,
timestamptzwill 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:This is what I never want to happen.
Now it’s true that if you fire up
psqlor some script and compare thetimestampvalues to for exampleNOW(), you may end up with unexpected results if you didn’t remember to set the connection timezone. For me, usingtimestamptzhere 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 plaintimestamp) is better.timestamp with time zoneis a misnomer and IMO a misfeature that shouldn’t be in PostgreSQL.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_datetimein Ecto when there are no backwards compatibility worries. I’ll do that.cnck1387
You’ve only reached the first tunnel of the rabbit hole because there’s timex and timex_ecto too.