diff --git a/lib/tzdata/period_builder.ex b/lib/tzdata/period_builder.ex index d95303c..efcb832 100644 --- a/lib/tzdata/period_builder.ex +++ b/lib/tzdata/period_builder.ex @@ -107,6 +107,15 @@ defmodule Tzdata.PeriodBuilder do # get rules {rules_type, rules_value} = zone_hd_rules + # A rule from an earlier year can still be in effect when this zone line + # starts (e.g. the US war time rule of 1942 for America/Phoenix in 1944). + # Start out with that rule's save and letter. + {std_off, letter} = + case rule_in_effect_before_year(btz_data, zone_hd_rules, from, from_standard_time_year) do + nil -> {std_off, letter} + rule -> {rule.save, rule.letter} + end + calc_rule_periods_h( btz_data, rules_type, @@ -120,6 +129,22 @@ defmodule Tzdata.PeriodBuilder do ) end + # The last rule of the named rule set that took effect in a year before + # `year`, or nil if there is none. + defp rule_in_effect_before_year(btz_data, {:named_rules, rules_value}, from, year) + when is_integer(from) do + {:ok, rules} = get_rules(btz_data, rules_value) + first_year = rules |> Enum.map(& &1.from) |> Enum.min() + + if year > first_year do + Enum.find_value((year - 1)..first_year//-1, fn y -> + rules |> TzUtil.rules_for_year(y) |> sort_rules_by_time(y) |> List.last() + end) + end + end + + defp rule_in_effect_before_year(_btz_data, _zone_rules, _from, _year), do: nil + # Helper function for function calc_periods with no rules # When the zone line tail is empty we are at the last zone line. # As this should only be called when there are no rules, we assume that diff --git a/test/tz_period_builder_test.exs b/test/tz_period_builder_test.exs index 3cca489..2bfb2e9 100644 --- a/test/tz_period_builder_test.exs +++ b/test/tz_period_builder_test.exs @@ -486,6 +486,34 @@ defmodule Tzdata.PeriodBuilderTest do } end + test "a rule from an earlier year can be in effect when a zone line starts", %{map: map} do + find_period = fn zone, instant -> + calc_periods(map, zone) + |> Enum.find(fn p -> + p.from.utc != :min and p.until.utc != :max and + p.from.utc <= instant and instant < p.until.utc + end) + end + + # The zone line starting 1944-04-01 uses the US rules, whose last change + # was war time (MWT) in 1942. `zdump America/Phoenix` shows MWT from + # 1944-04-01 07:01 UTC to 1944-10-01 06:01 UTC. + period = find_period.("America/Phoenix", ~G[1944-06-01T00:00:00]) + assert period.zone_abbr == "MWT" + assert period.utc_off + period.std_off == -21600 + assert period.from.utc == ~G[1944-04-01T07:01:00] + assert period.until.utc == ~G[1944-10-01T06:01:00] + + # The zone line starting 1941-04-07 23:00 uses the C-Eur rules, whose + # last change was summer time from 1940-04-01. `zdump Europe/Budapest` + # shows CEST from 1941-04-07 22:00 UTC to 1942-11-02 01:00 UTC. + period = find_period.("Europe/Budapest", ~G[1942-01-01T00:00:00]) + assert period.zone_abbr == "CEST" + assert period.utc_off + period.std_off == 7200 + assert period.from.utc == ~G[1941-04-07T22:00:00] + assert period.until.utc == ~G[1942-11-02T01:00:00] + end + test "no zone periods go back in time", %{map: map} do invalid_periods = for zone <- map.zone_list,