travisf
Timex.now(timezone)
I’m sending a daily report which should be showing the current time set to mountain daylight time. However, after daylight savings time the report has been consistently an hour behind. I if I run Timex.now("MDT") in the terminal I will get the current time in MDT. Our report is generated much the same way: {:ok, date} = Timex.format(Timex.now("MDT"), "{h12}:{m} {am}") Again, this works in my terminal but not on the report that gets sent out; it’s an hour behind.
However, if I change Timex.now("EST") I will get Eastern Time but an hour behind (right now it’s 11:15 EST but I’m getting 10:15).
Any ideas on what the cause of this might be? Is there a Phoenix config somewhere that sets time?
Marked As Solved
travisf
I think I’ve realized the issue. Our code was actually using Timex.now("MST") I thought MST and MDT were interchangeable or that Timex would account for daylight saving time. It seems like MST will be incorrect about 8 months out of the year, I can’t verify MDT seems like America/Denver is the way to go.
@dimitarvp your daylight savings mention got me on this track.
Also Liked
thojanssens1
Perfect ![]()
Just to show you what I meant:
iex> Tzdata.zone_exists?("MST")
true
iex> Tzdata.zone_exists?("MDT")
false # <- see here
iex> Timex.now("MDT")
#DateTime<2020-03-26 19:55:03.637000-06:00 MDT MST7MDT>
“MDT” doesn’t exist as a time zone ID, but Timex will find a time zone ID that contains the string “MDT”, and finds MST7MDT, whish does change according to US DST rules.
Ouch… I don’t know why Timex would not return an error instead honestly, that leads to bugs ;-p
If one ever needs a fixed offset, better use a time zone ID such as Etc/GMT-7.
Anyway, it was just to complete the information. You use the right time zone ID now.
thojanssens1
I think I confuse you more:) but the IANA tz database can be very confusing.
“MST” can be used as a time zone ID; but for example “MDT” seems not to be a valid time zone ID.
^ So this should actually throw an error
Anyway, this is not what you want. You want to use “America/Denver” as a time zone ID.
When you use “America/Denver” as a time zone ID, the offset will change throughout the year:
-07:00/MST and -06:00/MDT
LostKobrakai
This is actually how timezones work. Timezone names alike MDT are names for certain UTC offsets. Area names like “America/Denver” are the names, which define which of those timezones is in effect in a certain area of the world at a certain time.
Last Post!
thojanssens1
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









