Skip to content

Instantly share code, notes, and snippets.

@shurikk
Created May 12, 2015 00:43
Show Gist options
  • Save shurikk/3c7ddd925d4e0eb70054 to your computer and use it in GitHub Desktop.
Save shurikk/3c7ddd925d4e0eb70054 to your computer and use it in GitHub Desktop.
crontab string eval
class Foo
def schedule
# crontab like string, e.g. "5 * * * *"
end
def run?(time)
fields = schedule.split(/\s+/)
!%w(min hour day month wday).map.with_index do |name, i|
fields[i].gsub(/(?:(?![\d\/\*,]).)*/, '').split(',').select do |v|
now = time.send(name.to_sym).to_s
v.gsub(/\*/, now) == now
end.size > 0
end.include?(false)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment