Skip to content

Commit

Permalink
fix issue with 1.month and :at. closes javan#99
Browse files Browse the repository at this point in the history
  • Loading branch information
javan committed Dec 20, 2011
1 parent edac3ab commit 7f148ca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
19 changes: 14 additions & 5 deletions lib/whenever/cron.rb
Expand Up @@ -8,14 +8,15 @@ class Cron
attr_accessor :time, :task

def initialize(time = nil, task = nil, at = nil)
@at_given = at
@time = time
@task = task
@at = at.is_a?(String) ? (Chronic.parse(at) || 0) : (at || 0)
end

def self.enumerate(item, detect_cron = true)
if item and item.is_a?(String)
items =
items =
if detect_cron && item =~ REGEX
[item]
else
Expand All @@ -35,7 +36,7 @@ def self.output(times, job)
end
end
end

def output
[time_in_cron_syntax, task].compact.join(' ').strip
end
Expand All @@ -50,12 +51,16 @@ def time_in_cron_syntax
end

protected
def day_given?
months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]
@at_given.is_a?(String) && months.any? { |m| @at_given.downcase.index(m) }
end

def parse_symbol
shortcut = case @time
when :reboot then '@reboot'
when :year then 12.months
when :yearly,
when :yearly,
:annually then '@annually'
when :day then 1.day
when :daily then '@daily'
Expand All @@ -67,7 +72,7 @@ def parse_symbol
when :hour then 1.hour
when :hourly then '@hourly'
end

if shortcut.is_a?(Numeric)
@time = shortcut
parse_time
Expand Down Expand Up @@ -103,7 +108,11 @@ def parse_time
month_frequency = (@time / 30 / 24 / 60 / 60).round
timing[0] = @at.is_a?(Time) ? @at.min : 0
timing[1] = @at.is_a?(Time) ? @at.hour : 0
timing[2] = @at.is_a?(Time) ? @at.day : (@at.zero? ? 1 : @at)
timing[2] = if @at.is_a?(Time)
day_given? ? @at.day : 1
else
@at.zero? ? 1 : @at
end
timing[3] = comma_separated_timing(month_frequency, 12, 1)
else
return parse_as_string
Expand Down
21 changes: 15 additions & 6 deletions test/unit/cron_test.rb
Expand Up @@ -117,6 +117,15 @@ class CronTest < Test::Unit::TestCase
assert_equal '0 0 1 12 *', parse_time(12.months)
end

should "parse months with a date and/or time" do
# should set the day to 1 if no date is given
assert_equal '0 17 1 * *', parse_time(1.month, nil, "5pm")
# should use the date if one is given
assert_equal '0 2 23 * *', parse_time(1.month, nil, "February 23rd at 2am")
# should use an iteger as the day
assert_equal '0 0 5 * *', parse_time(1.month, nil, 5)
end

should "parse correctly when given an 'at' with days, hours, minutes as a Time" do
# first param is an array with [days, hours, minutes]
assert_days_and_hours_and_minutes_equals %w(1 3 45), 'January 1st 3:45am'
Expand Down Expand Up @@ -170,7 +179,7 @@ class CronTest < Test::Unit::TestCase
assert_equal '2 18 * * 6,0', parse_time('Weekends', nil, "6:02PM")
end
end

context "When parsing time using the cron shortcuts" do
should "parse a :symbol into the correct shortcut" do
assert_equal '@reboot', parse_time(:reboot)
Expand All @@ -182,30 +191,30 @@ class CronTest < Test::Unit::TestCase
assert_equal '@weekly', parse_time(:weekly)
assert_equal '@hourly', parse_time(:hourly)
end

should "convert time-based shortcuts to times" do
assert_equal '0 0 1 * *', parse_time(:month)
assert_equal '0 0 * * *', parse_time(:day)
assert_equal '0 * * * *', parse_time(:hour)
assert_equal '0 0 1 12 *', parse_time(:year)
assert_equal '0 0 1,8,15,22 * *', parse_time(:week)
end

should "raise an exception if a valid shortcut is given but also an :at" do
assert_raises ArgumentError do
parse_time(:hourly, nil, "1:00 am")
end

assert_raises ArgumentError do
parse_time(:reboot, nil, 5)
end

assert_raises ArgumentError do
parse_time(:daily, nil, '4:20pm')
end
end
end

context "When given raw cron sytax" do
should "return the same cron sytax" do
crons = ['0 0 27-31 * *', '* * * * *', '2/3 1,9,22 11-26 1-6 *']
Expand Down

0 comments on commit 7f148ca

Please sign in to comment.