OTP Timezone not in sync with server

Hi guys,

I have been checking a small web application developed in Elixir 1.5.2, OTP 20, Phoenix Framework 1.3 and noticed that the application date was wrong.

I was checking the application before production and found out that the date was wrong as this shows:

ubuntu@ip-XXX-XXX-XXX-XXX:~/epr$ timedatectl
      Local time: Sun 2017-12-03 19:59:00 CST
  Universal time: Mon 2017-12-04 01:59:00 UTC
        RTC time: Mon 2017-12-04 01:58:59
       Time zone: America/Tegucigalpa (CST, -0600)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no
ubuntu@ip-XXX-XXX-XXX-XXX:~/epr$ iex
Erlang/OTP 20 [erts-9.1] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.5.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Date.utc_today()
~D[2017-12-04]
iex(2)> NaiveDateTime.utc_now()
~N[2017-12-04 02:09:28.813147]
iex(3)> 

Can anyone help me get the time and date in sync?

Thanks for your help.

Best regards,

1 Like

Dates and Times in elixir is always in UTC. Check out the Timex hex package if you want to use local datetime.

2 Likes

Thanks

1 Like

As a related note though: I would never, ever run a server in any timezone other than UTC.

3 Likes

@benwilson512 how can I adjust the date to use UTC. Apparently I am 12 hours out of sync.

Do I have to manually add or subtract hours depending on the time zone I need?

Thanks

1 Like

Where are you out of sync 12 hours. All I can see is a difference of 6 hours which in fact is exactly the offset between your timezone (America/Tegucigalpa (CST, -0600)) and UTC.

If you want to store timezone info with your timestamp you mustn’t use naïve date time.

1 Like

Oh, I am sorry for the confusion. I said 12 hours because of the date change between ubuntu local time and navie date:

Local time: Sun 2017-12-03 19:59:00 CST

iex(2)> NaiveDateTime.utc_now()
~N[2017-12-04 02:09:28.813147]

Notice the local date is 3 and the Naive date is 4 so I assumed it was 12 hours.

My issue is that transactions will have a different date in some point of the day as this example shows. Also, the user interface shows the wrong date so I wanted to fix it adjusting the Date and Time properly.

Shall I adjust time manually before saving and showing to the user? or is there a more proper way to handle this types of issues?

Thanks again.

1 Like

There is no difference between 20 CST on 3rd and 2 UTC on 4th.

It’s just how it looks like on a British clock when you call them when your clock shows 20:00.

Regarding presentation though, it’s common practice to store all times in UTC and convert to the users timezone for display.

2 Likes

Just in case anyone has the same issue, I found this post that might help:

https://www.amberbit.com/blog/2017/8/3/time-zones-in-postgresql-elixir-and-phoenix/

Thanks

1 Like