Skip to content

Commit

Permalink
#45 Microsecond Precision Scheduler Fix (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
maennchen authored Sep 27, 2017
1 parent d7e5a46 commit bf7aa33
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/crontab/scheduler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ defmodule Crontab.Scheduler do
defp correct_date(:year, date, :decrement), do: date |> DateHelper.dec_year |> DateHelper.end_of(:year) |> DateHelper.beginning_of(:second)

@spec clean_date(NaiveDateTime.t, :seconds | :microseconds) :: NaiveDateTime.t
defp clean_date(date = %NaiveDateTime{microsecond: {0,0}}, :microseconds), do: date
defp clean_date(date = %NaiveDateTime{microsecond: {0, _}}, :microseconds), do: date
defp clean_date(date = %NaiveDateTime{}, :microseconds) do
date
|> Map.put(:microsecond, {0,0})
Expand Down
8 changes: 8 additions & 0 deletions test/crontab/scheduler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ defmodule Crontab.SchedulerTest do
assert get_next_run_date(%Crontab.CronExpression{minute: [16]}, ~N[2002-01-13 23:00:07]) == {:ok, ~N[2002-01-13 23:16:00]}
end

test "check cron expression for second" do
assert get_next_run_date(%Crontab.CronExpression{extended: true, second: [{:/, :*, 3}]}, ~N[2002-01-13 23:00:06]) == {:ok, ~N[2002-01-13 23:00:06]}
end

test "check cron expression given microseconds" do
assert get_next_run_date(%Crontab.CronExpression{extended: true, second: [{:/, :*, 3}]}, ~N[2002-01-13 23:00:06.00]) == {:ok, ~N[2002-01-13 23:00:06]}
end

test "check combined" do
assert get_next_run_date(%Crontab.CronExpression{minute: [3], hour: [7], day: [27], month: [2]}, ~N[2002-01-13 23:00:07]) == {:ok, ~N[2002-02-27 07:03:00]}
end
Expand Down

0 comments on commit bf7aa33

Please sign in to comment.