Skip to content

Fix parse #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 33 additions & 37 deletions lib/cron_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class InternalTime
attr_accessor :year, :month, :day, :hour, :min
attr_accessor :time_source

def initialize(time,time_source = Time)
def initialize(time, time_source = Time)
@year = time.year
@month = time.month
@day = time.day
Expand All @@ -29,29 +29,29 @@ def inspect
end

SYMBOLS = {
"jan" => "1",
"feb" => "2",
"mar" => "3",
"apr" => "4",
"may" => "5",
"jun" => "6",
"jul" => "7",
"aug" => "8",
"sep" => "9",
"oct" => "10",
"nov" => "11",
"dec" => "12",

"sun" => "0",
"mon" => "1",
"tue" => "2",
"wed" => "3",
"thu" => "4",
"fri" => "5",
"sat" => "6"
"jan" => "1",
"feb" => "2",
"mar" => "3",
"apr" => "4",
"may" => "5",
"jun" => "6",
"jul" => "7",
"aug" => "8",
"sep" => "9",
"oct" => "10",
"nov" => "11",
"dec" => "12",

"sun" => "0",
"mon" => "1",
"tue" => "2",
"wed" => "3",
"thu" => "4",
"fri" => "5",
"sat" => "6"
}

def initialize(source,time_source = Time)
def initialize(source, time_source = Time)
@source = interpret_vixieisms(source)
@time_source = time_source
validate_source
Expand All @@ -76,7 +76,6 @@ def interpret_vixieisms(spec)
end
end


# returns the next occurence after the given date
def next(now = @time_source.now, num = 1)
t = InternalTime.new(now, @time_source)
Expand All @@ -100,15 +99,15 @@ def next(now = @time_source.now, num = 1)
nudge_minute(t)
t = t.to_time
if num > 1
recursive_calculate(:next,t,num)
recursive_calculate(:next, t, num)
else
t
end
end

# returns the last occurence before the given date
def last(now = @time_source.now, num=1)
t = InternalTime.new(now,@time_source)
def last(now = @time_source.now, num = 1)
t = InternalTime.new(now, @time_source)

unless time_specs[:month][0].include?(t.month)
nudge_month(t, :last)
Expand All @@ -129,13 +128,12 @@ def last(now = @time_source.now, num=1)
nudge_minute(t, :last)
t = t.to_time
if num > 1
recursive_calculate(:last,t,num)
recursive_calculate(:last, t, num)
else
t
end
end


SUBELEMENT_REGEX = %r{^(\d+)(-(\d+)(/(\d+))?)?$}
def parse_element(elem, allowed_range)
values = elem.split(',').map do |subel|
Expand All @@ -160,10 +158,9 @@ def parse_element(elem, allowed_range)
[Set.new(values), values, elem]
end


protected

def recursive_calculate(meth,time,num)
def recursive_calculate(meth, time, num)
array = [time]
num.-(1).times do |num|
array << self.send(meth, array.last)
Expand Down Expand Up @@ -238,6 +235,7 @@ def nudge_hour(t, dir = :next)

def nudge_minute(t, dir = :next)
spec = time_specs[:minute][1]
t.min = t.min + 1 unless dir == :next
next_value = find_best_next(t.min, spec, dir)
t.min = next_value || (dir == :next ? spec.first : spec.last)

Expand All @@ -249,11 +247,11 @@ def time_specs
# tokens now contains the 5 fields
tokens = substitute_parse_symbols(@source).split(/\s+/)
{
:minute => parse_element(tokens[0], 0..59), #minute
:hour => parse_element(tokens[1], 0..23), #hour
:dom => parse_element(tokens[2], 1..31), #DOM
:month => parse_element(tokens[3], 1..12), #mon
:dow => parse_element(tokens[4], 0..6) #DOW
:minute => parse_element(tokens[0], 0..59), # minute
:hour => parse_element(tokens[1], 0..23), # hour
:dom => parse_element(tokens[2], 1..31), # DOM
:month => parse_element(tokens[3], 1..12), # mon
:dow => parse_element(tokens[4], 0..6) # DOW
}
end
end
Expand All @@ -264,7 +262,6 @@ def substitute_parse_symbols(str)
end
end


def stepped_range(rng, step = 1)
len = rng.last - rng.first

Expand All @@ -275,7 +272,6 @@ def stepped_range(rng, step = 1)
result
end


# returns the smallest element from allowed which is greater than current
# returns nil if no matching value was found
def find_best_next(current, allowed, dir)
Expand Down
2 changes: 1 addition & 1 deletion lib/parse-cron/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Parse
module Cron
VERSION = "0.1.4"
VERSION = "0.1.5"
end
end
Loading