This only works for automatically generated fields like inserted_at
and updated_at
and if you manually set other datetime fields to DateTime.utc_now()
. But if you have any other Elixir code that is not timezone-aware and it fetches time in the local timezone (or have a legacy DB with non-UTC datetimes in it) then you’re in for trouble.
Better to do all of these together:
- Use
utc_datetime
type for datetime fields in your Ecto schema modules. - When setting times manually, always start with
DateTime.utc_now()
and/or do calculations with Elixir’s stdlib ortimex
, always making sure you use UTC. - If you have to work with an inherited legacy database, just make a one-off script to convert any non-UTC datetimes to UTC. It will save you a world of trouble in the future.