Skip to content

Commit effbb45

Browse files
committed
Introduce decreasing_range helper function and compile conditionally based on Elixir version.
1 parent 953a20b commit effbb45

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

lib/tzdata/util.ex

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
defmodule Tzdata.Util do
22
@moduledoc false
33

4+
@elixir_newer_1_12 Version.match?(System.version(), ">= 1.12.0")
5+
46
@doc """
57
Take strings of amounts and convert them to ints of seconds.
68
For instance useful for strings from TZ gmt offsets.
@@ -64,7 +66,7 @@ defmodule Tzdata.Util do
6466
def last_weekday_of_month(year, month, weekday) do
6567
weekday = weekday_string_to_number!(weekday)
6668
days_in_month = day_count_for_month(year, month)
67-
day_list = Enum.to_list(days_in_month..1//-1)
69+
day_list = Enum.to_list(decreasing_range(days_in_month, 1))
6870
{:ok, day} = first_matching_weekday_in_month(year, month, weekday, day_list)
6971
day
7072
end
@@ -96,7 +98,7 @@ defmodule Tzdata.Util do
9698
# Can be in the previous month if no matching date and weekday is found in the specified month
9799
defp first_weekday_of_month_at_most(year, month, weekday, maximum_date) do
98100
weekday = weekday_string_to_number!(weekday)
99-
day_list = Enum.to_list(maximum_date..1//-1)
101+
day_list = Enum.to_list(decreasing_range(maximum_date, 1))
100102

101103
case first_matching_weekday_in_month(year, month, weekday, day_list) do
102104
{:ok, day} when is_integer(day) ->
@@ -565,4 +567,19 @@ defmodule Tzdata.Util do
565567
_ -> false
566568
end
567569
end
570+
571+
if @elixir_newer_1_12 do
572+
# See PR #154.
573+
# Elixir 1.17 and 1.18 deprecated using decreasing Ranges without explicit steps.
574+
# On the other hand, older elixir versions, before 1.12, don't know the `first..last//-1` syntax.
575+
# As long as we want to support those versions, we need to compile conditionally.
576+
577+
defp decreasing_range(upper, lower) when upper >= lower do
578+
upper..lower//-1
579+
end
580+
else
581+
defp decreasing_range(upper, lower) when upper >= lower do
582+
upper..lower
583+
end
584+
end
568585
end

0 commit comments

Comments
 (0)