diff --git a/lib/crontab/scheduler.ex b/lib/crontab/scheduler.ex index 3e0a3f0..aaf5db5 100644 --- a/lib/crontab/scheduler.ex +++ b/lib/crontab/scheduler.ex @@ -280,16 +280,39 @@ defmodule Crontab.Scheduler do end defp get_run_date(conditions, date, max_runs, direction) do - {status, corrected_date} = search_and_correct_date(conditions, date, direction) + case search_and_correct_date(conditions, date, direction) do + {:found, corrected_date} -> + {:ok, corrected_date} - case status do - :found -> {:ok, corrected_date} - _ -> get_run_date(conditions, corrected_date, max_runs - 1, direction) + {:error, :impossible} -> + {:error, "No compliant date was found for your interval."} + + {:not_found, corrected_date} -> + get_run_date(conditions, corrected_date, max_runs - 1, direction) end end @spec search_and_correct_date(CronExpression.condition_list(), NaiveDateTime.t(), direction) :: NaiveDateTime.t() | {:not_found, NaiveDateTime.t()} + + defp search_and_correct_date( + [{:year, [target_year]} | _], + %NaiveDateTime{year: from_year}, + :increment + ) + when is_integer(target_year) and target_year < from_year do + {:error, :impossible} + end + + defp search_and_correct_date( + [{:year, [target_year]} | _], + %NaiveDateTime{year: from_year}, + :decrement + ) + when is_integer(target_year) and target_year > from_year do + {:error, :impossible} + end + defp search_and_correct_date([{interval, conditions} | tail], date, direction) do if matches_date?(interval, conditions, date) do search_and_correct_date(tail, date, direction)