Skip to content

Commit

Permalink
Fix for hardcoded future periods and negative off
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieuprog committed Jul 28, 2024
1 parent 46b8136 commit 6ca10b3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
32 changes: 27 additions & 5 deletions lib/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,33 @@ defmodule TzExtra.Compiler do

{utc_to_std_offset, utc_to_dst_offset, time_zone_abbr, dst_time_zone_abbr} =
case hd(periods) do
{_, {utc_offset, std_offset, time_zone_abbr}, _, nil} ->
{utc_offset, utc_offset + std_offset, time_zone_abbr, time_zone_abbr}
{utc_secs, {utc_to_std_offset, std_offset, time_zone_abbr},
{_, prev_std_offset, prev_zone_abbr}, nil} ->
hardcoded_dst_future_periods? =
DateTime.from_gregorian_seconds(utc_secs).year > Date.utc_today().year + 20

{_, {utc_offset, std_offset, time_zone_abbr}, {_, prev_std_offset, prev_zone_abbr}, _} ->
utc_to_dst_offset = utc_offset + max(std_offset, prev_std_offset)
if hardcoded_dst_future_periods? do
utc_to_dst_offset = utc_to_std_offset + max(std_offset, prev_std_offset)
utc_to_std_offset = utc_to_std_offset + min(std_offset, prev_std_offset)

{time_zone_abbr, dst_time_zone_abbr} =
cond do
std_offset < prev_std_offset ->
{time_zone_abbr, prev_zone_abbr}

std_offset > prev_std_offset ->
{prev_zone_abbr, time_zone_abbr}
end

{utc_to_std_offset, utc_to_dst_offset, time_zone_abbr, dst_time_zone_abbr}
else
{utc_to_std_offset, utc_to_std_offset + std_offset, time_zone_abbr, time_zone_abbr}
end

{_, {utc_to_std_offset, std_offset, time_zone_abbr},
{_, prev_std_offset, prev_zone_abbr}, _} ->
utc_to_dst_offset = utc_to_std_offset + max(std_offset, prev_std_offset)
utc_to_std_offset = utc_to_std_offset + min(std_offset, prev_std_offset)

{time_zone_abbr, dst_time_zone_abbr} =
cond do
Expand All @@ -308,7 +330,7 @@ defmodule TzExtra.Compiler do
{prev_zone_abbr, time_zone_abbr}
end

{utc_offset, utc_to_dst_offset, time_zone_abbr, dst_time_zone_abbr}
{utc_to_std_offset, utc_to_dst_offset, time_zone_abbr, dst_time_zone_abbr}
end

time_zone
Expand Down
10 changes: 10 additions & 0 deletions test/tz_extra_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ defmodule TzExtraTest do
|> Enum.count()

refute TzExtra.countries_time_zones() |> Enum.any?(&(&1.time_zone_id == "Etc/UTC"))

country_time_zone = TzExtra.country_time_zone!("MA", "Africa/Casablanca")

assert country_time_zone.utc_to_std_offset == 0
assert country_time_zone.utc_to_dst_offset == 3600

country_time_zone = TzExtra.country_time_zone!("IE", "Europe/Dublin")

assert country_time_zone.utc_to_std_offset == 0
assert country_time_zone.utc_to_dst_offset == 3600
end

test "time_zone_ids/1" do
Expand Down

0 comments on commit 6ca10b3

Please sign in to comment.