Skip to content

Commit

Permalink
Add advances_clock?/1
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieuprog committed Jul 29, 2024
1 parent 6ca10b3 commit dfb6a11
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* `TzExtra.new_resolved_datetime!/4`
* `TzExtra.utc_datetime_range/3`
* `TzExtra.round_datetime/3`
* `TzExtra.advances_clock?/1`

### `TzExtra.countries_time_zones/0`

Expand Down
16 changes: 16 additions & 0 deletions lib/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,22 @@ defmodule TzExtra.Compiler do

Enum.map(start_unix_time..end_unix_time//step_in_seconds, &DateTime.from_unix!(&1))
end

def advances_clock?(time_zone_id) when time_zone_id != nil do
case Tz.PeriodsProvider.periods(time_zone_id) do
{:error, :time_zone_not_found} ->
raise "invalid time zone #{time_zone_id}"

{:ok, [{utc_secs, _, _, nil} | _]} ->
hardcoded_dst_future_periods? =
DateTime.from_gregorian_seconds(utc_secs).year > Tz.PeriodsProvider.compiled_at().year + 20

hardcoded_dst_future_periods?

{:ok, [{_, _, _, _} | _]} ->
true
end
end
end,
for %{code: country_code} <- countries do
{:ok, time_zones_for_country} =
Expand Down
8 changes: 8 additions & 0 deletions test/tz_extra_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ defmodule TzExtraTest do
assert TzExtra.iana_version() == Tz.iana_version()
end

test "advances_clock?/1" do
assert TzExtra.advances_clock?("Europe/Brussels")
assert TzExtra.advances_clock?("Africa/Casablanca")

refute TzExtra.advances_clock?("Asia/Manila")
refute TzExtra.advances_clock?("Asia/Tokyo")
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 dfb6a11

Please sign in to comment.