|
1 | 1 | defmodule Tzdata.Util do |
2 | 2 | @moduledoc false |
3 | 3 |
|
| 4 | + @elixir_newer_1_12 Version.match?(System.version(), ">= 1.12.0") |
| 5 | + |
4 | 6 | @doc """ |
5 | 7 | Take strings of amounts and convert them to ints of seconds. |
6 | 8 | For instance useful for strings from TZ gmt offsets. |
@@ -64,7 +66,7 @@ defmodule Tzdata.Util do |
64 | 66 | def last_weekday_of_month(year, month, weekday) do |
65 | 67 | weekday = weekday_string_to_number!(weekday) |
66 | 68 | 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)) |
68 | 70 | {:ok, day} = first_matching_weekday_in_month(year, month, weekday, day_list) |
69 | 71 | day |
70 | 72 | end |
@@ -96,7 +98,7 @@ defmodule Tzdata.Util do |
96 | 98 | # Can be in the previous month if no matching date and weekday is found in the specified month |
97 | 99 | defp first_weekday_of_month_at_most(year, month, weekday, maximum_date) do |
98 | 100 | 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)) |
100 | 102 |
|
101 | 103 | case first_matching_weekday_in_month(year, month, weekday, day_list) do |
102 | 104 | {:ok, day} when is_integer(day) -> |
@@ -565,4 +567,19 @@ defmodule Tzdata.Util do |
565 | 567 | _ -> false |
566 | 568 | end |
567 | 569 | 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 |
568 | 585 | end |
0 commit comments