Skip to content

Commit e029ecb

Browse files
committed
Improve #132
1 parent c91046b commit e029ecb

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

lib/crontab/cron_expression.ex

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ defmodule Crontab.CronExpression do
4343
| min_max(time_unit), pos_integer}
4444
| min_max(time_unit)
4545
| {:W, time_unit | :L}
46+
| {:"#", time_unit, pos_integer}
4647

4748
@typedoc deprecated: "Use Calendar.second/0 instead"
4849
@type second :: Calendar.second()

lib/crontab/date_helper.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ defmodule Crontab.DateHelper do
5656
"""
5757
@spec nth_weekday(NaiveDateTime.t(), Calendar.day_of_week(), pos_integer()) ::
5858
Calendar.day() | nil
59-
def nth_weekday(%{month: month} = date, weekday, n),
59+
def nth_weekday(date = %{month: month}, weekday, n),
6060
do: find_nth_weekday(%{date | day: 1}, month, weekday, n)
6161

6262
@doc """
@@ -182,7 +182,7 @@ defmodule Crontab.DateHelper do
182182
weekday :: Calendar.day_of_week(),
183183
n :: non_neg_integer()
184184
) :: Calendar.day() | nil
185-
defp find_nth_weekday(%{month: month} = date, month, weekday, n) do
185+
defp find_nth_weekday(date = %{month: month}, month, weekday, n) do
186186
modifier =
187187
if Date.day_of_week(date) == weekday,
188188
do: n - 1,

test/crontab/functional_test.exs

+15-5
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ defmodule Crontab.FunctionalTest do
130130
{"* * * * 5#1", "* * * * 5#1 *", ~N[2011-07-01 00:00:00], ~N[2011-07-01 00:00:00],
131131
~N[2011-07-01 00:00:00], true},
132132
{"* * * * 3#4", "* * * * 3#4 *", ~N[2011-07-01 00:00:00], ~N[2011-07-27 00:00:00],
133-
~N[2011-06-22 23:59:00], false}
133+
~N[2011-06-22 23:59:00], false},
134+
{"0 9 * * mon#5", "0 9 * * 1#5 *", ~N[2024-10-22 09:00:00], ~N[2024-12-30 09:00:00],
135+
~N[2024-09-30 09:00:00], false}
134136
]
135137

136138
for {cron_expression, written_cron_expression, start_date, next_search_date,
@@ -149,17 +151,25 @@ defmodule Crontab.FunctionalTest do
149151
{:ok, cron_expression} = Parser.parse(@cron_expression)
150152
assert Composer.compose(cron_expression) == @written_cron_expression
151153

152-
assert Crontab.Scheduler.get_next_run_date(cron_expression, @start_date) ==
153-
{:ok, @next_search_date}
154+
assert {:ok, next_search_date} =
155+
Crontab.Scheduler.get_next_run_date(cron_expression, @start_date)
156+
157+
assert Crontab.DateChecker.matches_date?(cron_expression, next_search_date)
158+
159+
assert next_search_date == @next_search_date
154160

155161
case @previous_search_date do
156162
:none ->
157163
assert Crontab.Scheduler.get_previous_run_date(cron_expression, @start_date) ==
158164
{:error, "No compliant date was found for your interval."}
159165

160166
_ ->
161-
assert Crontab.Scheduler.get_previous_run_date(cron_expression, @start_date) ==
162-
{:ok, @previous_search_date}
167+
assert {:ok, previous_search_date} =
168+
Crontab.Scheduler.get_previous_run_date(cron_expression, @start_date)
169+
170+
assert Crontab.DateChecker.matches_date?(cron_expression, previous_search_date)
171+
172+
assert previous_search_date == @previous_search_date
163173
end
164174

165175
assert Crontab.DateChecker.matches_date?(cron_expression, @start_date) == @matches_now

0 commit comments

Comments
 (0)