Skip to content

Commit

Permalink
Use minute / hour / day units for DateTime shift (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
maennchen authored Feb 9, 2024
1 parent 6f6b619 commit 9122b11
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
30 changes: 15 additions & 15 deletions lib/crontab/date_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ defmodule Crontab.DateHelper do
@spec next_weekday_to(NaiveDateTime.t()) :: Calendar.day()
def next_weekday_to(date = %NaiveDateTime{year: year, month: month, day: day}) do
weekday = :calendar.day_of_the_week(year, month, day)
next_day = NaiveDateTime.add(date, 86_400, :second)
previous_day = NaiveDateTime.add(date, -86_400, :second)
next_day = NaiveDateTime.add(date, 1, :day)
previous_day = NaiveDateTime.add(date, -1, :day)

cond do
weekday == 7 && next_day.month == date.month -> next_day.day
weekday == 7 -> NaiveDateTime.add(date, -86_400 * 2, :second).day
weekday == 7 -> NaiveDateTime.add(date, -2, :day).day
weekday == 6 && previous_day.month == date.month -> previous_day.day
weekday == 6 -> NaiveDateTime.add(date, 86_400 * 2, :second).day
weekday == 6 -> NaiveDateTime.add(date, 2, :day).day
true -> date.day
end
end
Expand All @@ -95,9 +95,9 @@ defmodule Crontab.DateHelper do
|> Date.leap_year?()

if leap_year? do
NaiveDateTime.add(date, 366 * 86_400, :second)
NaiveDateTime.add(date, 366, :day)
else
NaiveDateTime.add(date, 365 * 86_400, :second)
NaiveDateTime.add(date, 365, :day)
end
end

Expand All @@ -109,9 +109,9 @@ defmodule Crontab.DateHelper do
|> Date.leap_year?()

if leap_year? do
NaiveDateTime.add(date, -366 * 86_400, :second)
NaiveDateTime.add(date, -366, :day)
else
NaiveDateTime.add(date, -365 * 86_400, :second)
NaiveDateTime.add(date, -365, :day)
end
end

Expand All @@ -122,7 +122,7 @@ defmodule Crontab.DateHelper do
|> NaiveDateTime.to_date()
|> Date.days_in_month()

NaiveDateTime.add(date, (days + 1 - day) * 86_400, :second)
NaiveDateTime.add(date, days + 1 - day, :day)
end

@spec dec_month(NaiveDateTime.t()) :: NaiveDateTime.t()
Expand All @@ -132,7 +132,7 @@ defmodule Crontab.DateHelper do
|> NaiveDateTime.to_date()
|> Date.days_in_month()

NaiveDateTime.add(date, days * -86_400, :second)
NaiveDateTime.add(date, -days, :day)
end

@spec _beginning_of(NaiveDateTime.t(), [{unit, {any, any}}]) :: NaiveDateTime.t()
Expand Down Expand Up @@ -180,13 +180,13 @@ defmodule Crontab.DateHelper do

@spec nth_weekday(NaiveDateTime.t(), Calendar.day_of_week(), :start) :: boolean
defp nth_weekday(date = %NaiveDateTime{}, _, 0, :start),
do: NaiveDateTime.add(date, -86_400, :second).day
do: NaiveDateTime.add(date, -1, :day).day

defp nth_weekday(date = %NaiveDateTime{year: year, month: month, day: day}, weekday, n, :start) do
if :calendar.day_of_the_week(year, month, day) == weekday do
nth_weekday(NaiveDateTime.add(date, 86_400, :second), weekday, n - 1, :start)
nth_weekday(NaiveDateTime.add(date, 1, :day), weekday, n - 1, :start)
else
nth_weekday(NaiveDateTime.add(date, 86_400, :second), weekday, n, :start)
nth_weekday(NaiveDateTime.add(date, 1, :day), weekday, n, :start)
end
end

Expand All @@ -195,7 +195,7 @@ defmodule Crontab.DateHelper do
weekday = :calendar.day_of_the_week(year, month, day)

if weekday > 5 do
last_weekday_of_month(NaiveDateTime.add(date, -86_400, :second), :end)
last_weekday_of_month(NaiveDateTime.add(date, -1, :day), :end)
else
day
end
Expand All @@ -206,7 +206,7 @@ defmodule Crontab.DateHelper do
if :calendar.day_of_the_week(year, month, day) == weekday do
day
else
last_weekday(NaiveDateTime.add(date, -86_400, :second), weekday, :end)
last_weekday(NaiveDateTime.add(date, -1, :day), weekday, :end)
end
end
end
22 changes: 11 additions & 11 deletions lib/crontab/scheduler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ defmodule Crontab.Scheduler do
def get_next_run_dates(cron_expression, date \\ DateTime.to_naive(DateTime.utc_now()))

def get_next_run_dates(cron_expression = %CronExpression{extended: false}, date) do
_get_next_run_dates(cron_expression, date, fn date -> NaiveDateTime.add(date, 60, :second) end)
_get_next_run_dates(cron_expression, date, fn date -> NaiveDateTime.add(date, 1, :minute) end)
end

def get_next_run_dates(cron_expression = %CronExpression{extended: true}, date) do
Expand Down Expand Up @@ -237,7 +237,7 @@ defmodule Crontab.Scheduler do

def get_previous_run_dates(cron_expression = %CronExpression{extended: false}, date) do
_get_previous_run_dates(cron_expression, date, fn date ->
NaiveDateTime.add(date, -60, :second)
NaiveDateTime.add(date, -1, :minute)
end)
end

Expand Down Expand Up @@ -338,19 +338,19 @@ defmodule Crontab.Scheduler do
defp correct_date(:second, date, :increment), do: {:ok, date |> NaiveDateTime.add(1, :second)}

defp correct_date(:minute, date, :increment),
do: {:ok, date |> NaiveDateTime.add(60, :second) |> DateHelper.beginning_of(:minute)}
do: {:ok, date |> NaiveDateTime.add(1, :minute) |> DateHelper.beginning_of(:minute)}

defp correct_date(:hour, date, :increment),
do: {:ok, date |> NaiveDateTime.add(3_600, :second) |> DateHelper.beginning_of(:hour)}
do: {:ok, date |> NaiveDateTime.add(1, :hour) |> DateHelper.beginning_of(:hour)}

defp correct_date(:day, date, :increment),
do: {:ok, date |> NaiveDateTime.add(86_400, :second) |> DateHelper.beginning_of(:day)}
do: {:ok, date |> NaiveDateTime.add(1, :day) |> DateHelper.beginning_of(:day)}

defp correct_date(:month, date, :increment),
do: {:ok, date |> DateHelper.inc_month() |> DateHelper.beginning_of(:month)}

defp correct_date(:weekday, date, :increment),
do: {:ok, date |> NaiveDateTime.add(86_400, :second) |> DateHelper.beginning_of(:day)}
do: {:ok, date |> NaiveDateTime.add(1, :day) |> DateHelper.beginning_of(:day)}

defp correct_date(:year, %NaiveDateTime{year: 9_999}, :increment), do: {:error, :upper_bound}

Expand All @@ -364,23 +364,23 @@ defmodule Crontab.Scheduler do
do:
{:ok,
date
|> NaiveDateTime.add(-60, :second)
|> NaiveDateTime.add(-1, :minute)
|> DateHelper.end_of(:minute)
|> DateHelper.beginning_of(:second)}

defp correct_date(:hour, date, :decrement),
do:
{:ok,
date
|> NaiveDateTime.add(-3_600, :second)
|> NaiveDateTime.add(-1, :hour)
|> DateHelper.end_of(:hour)
|> DateHelper.beginning_of(:second)}

defp correct_date(:day, date, :decrement),
do:
{:ok,
date
|> NaiveDateTime.add(-86_400, :second)
|> NaiveDateTime.add(-1, :day)
|> DateHelper.end_of(:day)
|> DateHelper.beginning_of(:second)}

Expand All @@ -396,7 +396,7 @@ defmodule Crontab.Scheduler do
do:
{:ok,
date
|> NaiveDateTime.add(-86_400, :second)
|> NaiveDateTime.add(-1, :day)
|> DateHelper.end_of(:day)
|> DateHelper.beginning_of(:second)}

Expand Down Expand Up @@ -424,7 +424,7 @@ defmodule Crontab.Scheduler do

case clean_microseconds do
%NaiveDateTime{second: 0} -> clean_microseconds
_ -> NaiveDateTime.add(clean_microseconds, 60, :second)
_ -> NaiveDateTime.add(clean_microseconds, 1, :minute)
end
end
end

0 comments on commit 9122b11

Please sign in to comment.