Skip to content

Commit

Permalink
Add next_period_start_in_year_span/1
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieuprog committed Jul 29, 2024
1 parent 977f7f4 commit a828fbc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
7 changes: 7 additions & 0 deletions lib/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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} =
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 @@ -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")
Expand Down

0 comments on commit a828fbc

Please sign in to comment.