@mathieuprog I want to run Datetime.shift_zone/2
using “Pacific/Auckland” time zone. The thing is that in the current month (November) they increase their time by one our for daylight saving purposes:
%{
coordinates: "-3652+17446",
country: %{code: "NZ", name: "New Zealand"},
dst_offset: 46800,
dst_zone_abbr: "NZDT",
pretty_dst_offset: "+13:00",
pretty_utc_offset: "+12:00",
time_zone: "Pacific/Auckland",
time_zone_links: [],
utc_offset: 43200,
zone_abbr: "NZST"
}
Should I always use the dst_offset
in case any other country has DST too? Also, suppose we are in a month where the DST ended, the dst_offset
for New Zealand will be the same as the utc_offset
?
This is the function I use to convert a NaiveDateTime
to a DateTime
with a specific time_zone
:
def shift_time_based_on_timezone(%NaiveDateTime{} = naive, timezone) do
datetime = DateTime.from_naive!(naive, timezone)
DateTime.add(datetime, datetime.utc_offset, :second)
end
PS: thanks for your fast response in my other reply
.
UPDATE
I’ve changed my function as follows and now I’m getting the right converted time:
defp shift_time_based_on_timezone(%NaiveDateTime{} = naive, timezone) do
datetime = DateTime.from_naive!(naive, timezone)
offset = datetime.utc_offset + datetime.std_offset # Added this line
DateTime.add(datetime, offset, :second)
end