Skip to content

Commit

Permalink
Cleanup Helper Functions (maennchen#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
maennchen authored Jan 19, 2017
1 parent 97a2345 commit 6f23637
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 167 deletions.
197 changes: 36 additions & 161 deletions lib/crontab.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,284 +11,159 @@ defmodule Crontab do
alias Crontab.Scheduler
alias Crontab.DateChecker

@doc """
Find the next execution date relative to now for a string of an eventually
extended cron expression. (Extended = including seconds)
### Examples
iex> Crontab.get_next_run_date("* * * * *")
{:ok, ~N[2016-12-23 16:00:00.348751]}
iex> Crontab.get_next_run_date("* * * * *", true)
{:ok, ~N[2016-12-23 16:00:00.348751]}
iex> Crontab.get_next_run_date("* * * * *", false)
{:ok, ~N[2016-12-23 16:00:00.348751]}
"""
@spec get_next_run_date(binary, boolean) :: Scheduler.result
def get_next_run_date(cron_expression, extended \\ false) do
date = DateTime.to_naive(DateTime.utc_now)
get_next_run_date_relative_to(cron_expression, date, extended)
end

@doc """
Find the next execution date relative to a given date for a string of an
eventually extended cron expression. (Extended = including seconds)
### Examples
iex> Crontab.get_next_run_date_relative_to("* * * * *", ~N[2016-12-17 00:00:00])
iex> Crontab.get_next_run_date("* * * * *", ~N[2016-12-17 00:00:00])
{:ok, ~N[2016-12-17 00:00:00]}
iex> Crontab.get_next_run_date_relative_to("* * * * *", ~N[2016-12-17 00:00:00], false)
iex> Crontab.get_next_run_date("* * * * *", ~N[2016-12-17 00:00:00], false)
{:ok, ~N[2016-12-17 00:00:00]}
iex> Crontab.get_next_run_date_relative_to("* * * * *", ~N[2016-12-17 00:00:01], true)
iex> Crontab.get_next_run_date("* * * * *", ~N[2016-12-17 00:00:01], true)
{:ok, ~N[2016-12-17 00:00:01]}
"""
@spec get_next_run_date_relative_to(binary, NaiveDateTime.t, boolean) :: Scheduler.result
def get_next_run_date_relative_to(cron_expression, date, extended \\ false) do
@spec get_next_run_date(binary, NaiveDateTime.t, boolean) :: Scheduler.result
def get_next_run_date(cron_expression, date \\ DateTime.to_naive(DateTime.utc_now), extended \\ false) do
case Parser.parse(cron_expression, extended) do
{:ok, cron_format} -> Scheduler.get_next_run_date(cron_format, date)
error = {:error, _} -> error
end
end

@doc """
Find the next n execution dates relative to now for a string of an eventually
extended cron expression. (Extended = including seconds)
### Examples
iex> Crontab.get_next_run_dates(3, "* * * * *")
[{:ok, ~N[2016-12-23 16:00:00]},
{:ok, ~N[2016-12-23 16:01:00]},
{:ok, ~N[2016-12-23 16:02:00]}]
iex> Crontab.get_next_run_dates(3, "* * * * *", true)
[{:ok, ~N[2016-12-23 16:00:00]},
{:ok, ~N[2016-12-23 16:00:01]},
{:ok, ~N[2016-12-23 16:00:02]}]
iex> Crontab.get_next_run_dates(3, "* * * * *", false)
[{:ok, ~N[2016-12-23 16:00:00]},
{:ok, ~N[2016-12-23 16:01:00]},
{:ok, ~N[2016-12-23 16:02:00]}]
"""
@spec get_next_run_dates(pos_integer, binary, boolean) :: [Scheduler.result]
def get_next_run_dates(n, cron_expression, extended \\ false) do
date = DateTime.to_naive(DateTime.utc_now)
get_next_run_dates_relative_to(n, cron_expression, date, extended)
end

@doc """
Find the next n execution dates relative to a given date for a string of an
eventually extended cron expression. (Extended = including seconds)
### Examples
iex> Crontab.get_next_run_dates_relative_to(3, "* * * * *", ~N[2016-12-17 00:00:00])
iex> Crontab.get_next_run_dates(3, "* * * * *", ~N[2016-12-17 00:00:00])
[{:ok, ~N[2016-12-17 00:00:00]},
{:ok, ~N[2016-12-17 00:01:00]},
{:ok, ~N[2016-12-17 00:02:00]}]
iex> Crontab.get_next_run_dates_relative_to(3, "* * * * *", ~N[2016-12-17 00:00:00], true)
iex> Crontab.get_next_run_dates(3, "* * * * *", ~N[2016-12-17 00:00:00], true)
[{:ok, ~N[2016-12-17 00:00:00]},
{:ok, ~N[2016-12-17 00:00:01]},
{:ok, ~N[2016-12-17 00:00:02]}]
iex> Crontab.get_next_run_dates_relative_to(3, "* * * * *", ~N[2016-12-17 00:00:00], false)
iex> Crontab.get_next_run_dates(3, "* * * * *", ~N[2016-12-17 00:00:00], false)
[{:ok, ~N[2016-12-17 00:00:00]},
{:ok, ~N[2016-12-17 00:01:00]},
{:ok, ~N[2016-12-17 00:02:00]}]
"""
@spec get_next_run_dates_relative_to(pos_integer, binary, NaiveDateTime.t, boolean) :: [Scheduler.result]
def get_next_run_dates_relative_to(n, cron_expression, date, extended \\ false)
def get_next_run_dates_relative_to(0, _, _, _), do: []
def get_next_run_dates_relative_to(n, cron_expression, date, false) do
@spec get_next_run_dates(pos_integer, binary, NaiveDateTime.t, boolean) :: [Scheduler.result]
def get_next_run_dates(n, cron_expression, date \\ DateTime.to_naive(DateTime.utc_now), extended \\ false)
def get_next_run_dates(0, _, _, _), do: []
def get_next_run_dates(n, cron_expression, date, false) do
case Parser.parse(cron_expression, false) do
{:ok, cron_format} ->
result = {:ok, run_date} = Scheduler.get_next_run_date(cron_format, date)
[result | get_next_run_dates_relative_to(n - 1, cron_expression, Timex.shift(run_date, minutes: 1), false)]
[result | get_next_run_dates(n - 1, cron_expression, Timex.shift(run_date, minutes: 1), false)]
error = {:error, _} -> error
end
end
def get_next_run_dates_relative_to(n, cron_expression, date, true) do
def get_next_run_dates(n, cron_expression, date, true) do
case Parser.parse(cron_expression, true) do
{:ok, cron_format} ->
result = {:ok, run_date} = Scheduler.get_next_run_date(cron_format, date)
[result | get_next_run_dates_relative_to(n - 1, cron_expression, Timex.shift(run_date, seconds: 1), true)]
[result | get_next_run_dates(n - 1, cron_expression, Timex.shift(run_date, seconds: 1), true)]
error = {:error, _} -> error
end
end

@doc """
Find the previous execution date relative to now for a string of an
eventually extended cron expression. (Extended = including seconds)
### Examples
iex> Crontab.get_previous_run_date("* * * * *")
{:ok, ~N[2016-12-23 16:00:00.348751]}
iex> Crontab.get_previous_run_date("* * * * *", true)
{:ok, ~N[2016-12-23 16:00:00.348751]}
iex> Crontab.get_previous_run_date("* * * * *", false)
{:ok, ~N[2016-12-23 16:00:00.348751]}
"""
@spec get_previous_run_date(binary, boolean) :: Scheduler.result
def get_previous_run_date(cron_expression, extended \\ false) do
date = DateTime.to_naive(DateTime.utc_now)
get_previous_run_date_relative_to(cron_expression, date, extended)
end

@doc """
Find the previous execution date relative to a given date for a string of an
eventually extended cron expression.
### Examples
iex> Crontab.get_previous_run_date_relative_to("* * * * *", ~N[2016-12-17 00:00:00])
iex> Crontab.get_previous_run_date("* * * * *", ~N[2016-12-17 00:00:00])
{:ok, ~N[2016-12-17 00:00:00]}
iex> Crontab.get_previous_run_date_relative_to("* * * * *", ~N[2016-12-17 00:00:00], true)
iex> Crontab.get_previous_run_date("* * * * *", ~N[2016-12-17 00:00:00], true)
{:ok, ~N[2016-12-17 00:00:00]}
iex> Crontab.get_previous_run_date_relative_to("* * * * *", ~N[2016-12-17 00:00:00], false)
iex> Crontab.get_previous_run_date("* * * * *", ~N[2016-12-17 00:00:00], false)
{:ok, ~N[2016-12-17 00:00:00]}
"""
@spec get_previous_run_date_relative_to(binary, NaiveDateTime.t, boolean) :: Scheduler.result
def get_previous_run_date_relative_to(cron_expression, date, extended \\ false) do
@spec get_previous_run_date(binary, NaiveDateTime.t, boolean) :: Scheduler.result
def get_previous_run_date(cron_expression, date \\ DateTime.to_naive(DateTime.utc_now), extended \\ false) do
case Parser.parse(cron_expression, extended) do
{:ok, cron_format} -> Scheduler.get_previous_run_date(cron_format, date)
error = {:error, _} -> error
end
end

@doc """
Find the previous n execution dates relative to now for a string of an
eventually extended cron expression. (Extended = including seconds)
### Examples
iex> Crontab.get_previous_run_dates(3, "* * * * *")
[{:ok, ~N[2016-12-23 16:00:00]},
{:ok, ~N[2016-12-23 15:59:00]},
{:ok, ~N[2016-12-23 15:58:00]}]
iex> Crontab.get_previous_run_dates(3, "* * * * *", true)
[{:ok, ~N[2016-12-23 16:00:00]},
{:ok, ~N[2016-12-23 15:59:59]},
{:ok, ~N[2016-12-23 15:59:58]}]
iex> Crontab.get_previous_run_dates(3, "* * * * *", false)
[{:ok, ~N[2016-12-23 16:00:00]},
{:ok, ~N[2016-12-23 15:59:00]},
{:ok, ~N[2016-12-23 15:58:00]}]
"""
@spec get_previous_run_dates(pos_integer, binary, boolean) :: [Scheduler.result]
def get_previous_run_dates(n, cron_expression, extended \\ false) do
date = DateTime.to_naive(DateTime.utc_now)
get_previous_run_dates_relative_to(n, cron_expression, date, extended)
end

@doc """
Find the previous n execution dates relative to a given date for a string of
an eventually extended cron expression. (Extended = including seconds)
### Examples
iex> Crontab.get_previous_run_dates_relative_to(3, "* * * * *", ~N[2016-12-17 00:00:00])
iex> Crontab.get_previous_run_dates(3, "* * * * *", ~N[2016-12-17 00:00:00])
[{:ok, ~N[2016-12-17 00:00:00]},
{:ok, ~N[2016-12-16 23:59:00]},
{:ok, ~N[2016-12-16 23:58:00]}]
iex> Crontab.get_previous_run_dates_relative_to(3, "* * * * *", ~N[2016-12-17 00:00:00], true)
iex> Crontab.get_previous_run_dates(3, "* * * * *", ~N[2016-12-17 00:00:00], true)
[{:ok, ~N[2016-12-17 00:00:00]},
{:ok, ~N[2016-12-16 23:59:59]},
{:ok, ~N[2016-12-16 23:59:58]}]
iex> Crontab.get_previous_run_dates_relative_to(3, "* * * * *", ~N[2016-12-17 00:00:00], false)
iex> Crontab.get_previous_run_dates(3, "* * * * *", ~N[2016-12-17 00:00:00], false)
[{:ok, ~N[2016-12-17 00:00:00]},
{:ok, ~N[2016-12-16 23:59:00]},
{:ok, ~N[2016-12-16 23:58:00]}]
"""
@spec get_previous_run_dates_relative_to(pos_integer, binary, NaiveDateTime.t, boolean) :: [Scheduler.result]
def get_previous_run_dates_relative_to(n, cron_expression, date, extended \\ false)
def get_previous_run_dates_relative_to(0, _, _, _), do: []
def get_previous_run_dates_relative_to(n, cron_expression, date, false) do
@spec get_previous_run_dates(pos_integer, binary, NaiveDateTime.t, boolean) :: [Scheduler.result]
def get_previous_run_dates(n, cron_expression, date \\ DateTime.to_naive(DateTime.utc_now), extended \\ false)
def get_previous_run_dates(0, _, _, _), do: []
def get_previous_run_dates(n, cron_expression, date, false) do
case Parser.parse(cron_expression, false) do
{:ok, cron_format} ->
result = {:ok, run_date} = Scheduler.get_previous_run_date(cron_format, date)
[result | get_previous_run_dates_relative_to(n - 1, cron_expression, Timex.shift(run_date, minutes: -1), false)]
[result | get_previous_run_dates(n - 1, cron_expression, Timex.shift(run_date, minutes: -1), false)]
error = {:error, _} -> error
end
end
def get_previous_run_dates_relative_to(n, cron_expression, date, true) do
def get_previous_run_dates(n, cron_expression, date, true) do
case Parser.parse(cron_expression, true) do
{:ok, cron_format} ->
result = {:ok, run_date} = Scheduler.get_previous_run_date(cron_format, date)
[result | get_previous_run_dates_relative_to(n - 1, cron_expression, Timex.shift(run_date, seconds: -1), true)]
[result | get_previous_run_dates(n - 1, cron_expression, Timex.shift(run_date, seconds: -1), true)]
error = {:error, _} -> error
end
end


@doc """
Check if now matches a given string of an eventually extended cron expression.
### Examples
iex> Crontab.matches_date("*/2 * * * *")
{:ok, true}
iex> Crontab.matches_date("*/7 * * * *")
{:ok, false}
iex> Crontab.matches_date("*/2 * * * *", true)
{:ok, true}
iex> Crontab.matches_date("*/7 * * * *", false)
{:ok, false}
"""
@spec matches_date(binary, boolean) :: {:ok, boolean} | CronExpression.Parser.result
def matches_date(cron_expression, extended \\ false) do
date = DateTime.to_naive(DateTime.utc_now)
matches_date_relative_to(cron_expression, date, extended)
end

@doc """
Check if given date matches a given string of an eventually extended cron
expression.
### Examples
iex> Crontab.matches_date_relative_to("*/2 * * * *", ~N[2016-12-17 00:02:00])
iex> Crontab.matches_date("*/2 * * * *", ~N[2016-12-17 00:02:00])
{:ok, true}
iex> Crontab.matches_date_relative_to("*/7 * * * *", ~N[2016-12-17 00:06:00])
iex> Crontab.matches_date("*/7 * * * *", ~N[2016-12-17 00:06:00])
{:ok, false}
iex> Crontab.matches_date_relative_to("*/2 * * * *", ~N[2016-12-17 00:02:00], true)
iex> Crontab.matches_date("*/2 * * * *", ~N[2016-12-17 00:02:00], true)
{:ok, true}
iex> Crontab.matches_date_relative_to("*/7 * * * *", ~N[2016-12-17 00:06:00], false)
iex> Crontab.matches_date("*/7 * * * *", ~N[2016-12-17 00:06:00], false)
{:ok, false}
"""
@spec matches_date_relative_to(binary, NaiveDateTime.t, boolean) :: {:ok, boolean} | CronExpression.Parser.result
def matches_date_relative_to(cron_expression, date, extended \\ false) do
@spec matches_date(binary, NaiveDateTime.t, boolean) :: {:ok, boolean} | CronExpression.Parser.result
def matches_date(cron_expression, date \\ DateTime.to_naive(DateTime.utc_now), extended \\ false) do
case Parser.parse(cron_expression, extended) do
{:ok, cron_format} -> {:ok, DateChecker.matches_date(cron_format, date)}
error = {:error, _} -> error
Expand Down
7 changes: 1 addition & 6 deletions test/crontab_test.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
defmodule CrontabTest do
use ExUnit.Case
doctest Crontab, except: [
get_next_run_date: 2,
get_next_run_dates: 3,
get_previous_run_date: 2,
get_previous_run_dates: 3,
matches_date: 2]
doctest Crontab
end

0 comments on commit 6f23637

Please sign in to comment.