Skip to content

Commit

Permalink
more robust implementation and fix the @SPEC
Browse files Browse the repository at this point in the history
  • Loading branch information
tarzan committed Oct 22, 2024
1 parent 36f1af9 commit 0ab2bfb
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions lib/crontab/date_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,9 @@ defmodule Crontab.DateHelper do
"""
@spec nth_weekday(date :: date, weekday :: Calendar.day_of_week(), n :: pos_integer) ::
Calendar.day()
def nth_weekday(date, weekday, n) do
date
|> beginning_of(:month)
|> nth_weekday(weekday, n, :start)
end
Calendar.day() | nil
def nth_weekday(%{month: month} = date, weekday, n),
do: find_nth_weekday(%{date | day: 1}, month, weekday, n)

@doc """
Find last occurrence of weekday in month
Expand Down Expand Up @@ -258,22 +255,25 @@ defmodule Crontab.DateHelper do
units
end

@spec nth_weekday(date :: date, weekday :: Calendar.day_of_week(), position :: :start) ::
boolean
defp nth_weekday(date, _, 0, :start), do: add(date, -1, :day).day

defp nth_weekday(date, weekday, n, :start) do
@spec find_nth_weekday(
date :: date,
month :: Calendar.month(),
weekday :: Calendar.day_of_week(),
n :: non_neg_integer()
) :: Calendar.day() | nil
defp find_nth_weekday(%{month: month} = date, month, weekday, n) do
modifier =
if Date.day_of_week(date) == weekday,
do: n - 1,
else: n

increment_day = add(date, 1, :day)

if increment_day.month == date.month,
do: nth_weekday(increment_day, weekday, modifier, :start)
if modifier == 0,
do: date.day,
else: find_nth_weekday(add(date, 1, :day), month, weekday, modifier)
end

defp find_nth_weekday(_, _, _, _), do: nil

@spec last_weekday_of_month(date :: date(), position :: :end) :: Calendar.day()
defp last_weekday_of_month(date = %{day: day}, :end) do
if Date.day_of_week(date) > 5 do
Expand Down

0 comments on commit 0ab2bfb

Please sign in to comment.