diff --git a/README.md b/README.md index dacad36..42d8dea 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ * `TzExtra.utc_datetime_range/3` * `TzExtra.round_datetime/3` * `TzExtra.advances_clock?/1` +* `TzExtra.next_period_start_in_year_span/1` ### `TzExtra.countries_time_zones/0` diff --git a/lib/compiler.ex b/lib/compiler.ex index 6a800c4..ea00fb9 100644 --- a/lib/compiler.ex +++ b/lib/compiler.ex @@ -252,6 +252,13 @@ defmodule TzExtra.Compiler do true end end + + def next_period_start_in_year_span(%DateTime{} = datetime) do + {from, _, _, _} = Tz.PeriodsProvider.next_period(datetime) + + DateTime.from_gregorian_seconds(from) + |> DateTime.shift_zone!(datetime.time_zone, Tz.TimeZoneDatabase) + end end, for %{code: country_code} <- countries do {:ok, time_zones_for_country} = diff --git a/test/tz_extra_test.exs b/test/tz_extra_test.exs index d36d74b..8a7fbb0 100644 --- a/test/tz_extra_test.exs +++ b/test/tz_extra_test.exs @@ -70,6 +70,16 @@ defmodule TzExtraTest do refute TzExtra.advances_clock?("Asia/Tokyo") end + test "next_period_start_in_year_span/1" do + {:ambiguous, first_dt, second_dt} = DateTime.new(~D[2018-10-28], ~T[02:00:00], "Europe/Copenhagen", Tz.TimeZoneDatabase) + dt = TzExtra.next_period_start_in_year_span(DateTime.add(first_dt, -1, :day, Tz.TimeZoneDatabase)) + assert DateTime.compare(dt, second_dt) == :eq + + {:gap, dt_just_before, dt_just_after} = DateTime.new(~D[2019-03-31], ~T[02:30:00], "Europe/Copenhagen", Tz.TimeZoneDatabase) + dt = TzExtra.next_period_start_in_year_span(DateTime.add(dt_just_before, -1, :day, Tz.TimeZoneDatabase)) + assert DateTime.compare(dt, dt_just_after) == :eq + end + test "time_zone_id_exists?/1" do assert TzExtra.time_zone_id_exists?("Europe/Brussels") assert TzExtra.time_zone_id_exists?("Europe/Amsterdam")