Timezone and Database config

Hello, I am trying to use Brazil Timezone but I don’t know how to config it as default on my phoenix project.
I tried to use Tzdata but I am not sure if it is what I need.
I am new to this area and I would like to know if there is any solution to set the entire application to UTC-3 which is the standard in Brazil or if I must configure it each time a value is inserted and retrieved in the database.

Elixir’s ecosystem works with UTC datetimes or naive datetimes, so you have two options:

  • use naive datetimes with your server time set to GMT-3. In this way, datetimes will not have a timezone attached, and they will just use the server’s time.

  • work with UTC datetimes and convert them to correct timezone when appropriate.

It’s kinda tricky, what worked for me was playing a bit with timezone conversions, timex and iso8601 datetimes from user inputs.

This really depends on your server and your DB configuration. If they are in sync and are in the Brazil timezone then you can just use the :naive_datime or :naive_datetime_usec Ecto types and everything should be in your local timezone.

1 Like

I did a recent presentation on Time Zones that you might find helpful:

http://mikezornek.com/posts/2020/3/working-with-time-zones-in-an-elixir-phoenix-app/

Good luck!

1 Like