class Rufus::CronLine
(man 5 crontab) file line.
A ‘cron line’ is a line in the sense of a crontab
def self.monthday(date)
def self.monthday(date) count = 1 date2 = date.dup loop do date2 = date2 - DAY_IN_SECONDS break if date2.month != date.month count = count + 1 end "#{WEEKDAYS[date.wday]}##{count}" end
def date_match?(date)
def date_match?(date) return false unless sub_match?(date.day, @days) return false unless sub_match?(date.month, @months) return false unless sub_match?(date.wday, @weekdays) return false unless sub_match?(CronLine.monthday(date), @monthdays) true end
def initialize(line)
def initialize(line) super() @original = line items = line.split @timezone = (TZInfo::Timezone.get(items.last) rescue nil) items.pop if @timezone raise ArgumentError.new( "not a valid cronline : '#{line}'" ) unless items.length == 5 or items.length == 6 offset = items.length - 5 @seconds = offset == 1 ? parse_item(items[0], 0, 59) : [ 0 ] @minutes = parse_item(items[0 + offset], 0, 59) @hours = parse_item(items[1 + offset], 0, 24) @days = parse_item(items[2 + offset], 1, 31) @months = parse_item(items[3 + offset], 1, 12) @weekdays, @monthdays = parse_weekdays(items[4 + offset]) end
def matches?(time)
Returns true if the given time matches this cron line.
def matches?(time) time = Time.at(time) unless time.kind_of?(Time) time = @timezone.utc_to_local(time.getutc) if @timezone return false unless sub_match?(time.sec, @seconds) return false unless sub_match?(time.min, @minutes) return false unless sub_match?(time.hour, @hours) return false unless date_match?(time) true end
def monthday_match(monthday, monthdays)
def monthday_match(monthday, monthdays) return true if monthdays == nil return true if monthdays.include?(monthday) end
def next_time(now=Time.now)
(Thanks to K Liu for the note and the examples)
#=> Fri Oct 24 02:30:00 -0500 2008
Time.utc(2008, 10, 24, 7, 29)).localtime
Rufus::CronLine.new('30 7 * * *').next_time(
#=> Fri Oct 24 07:30:00 UTC 2008
Time.utc(2008, 10, 24, 7, 29))
Rufus::CronLine.new('30 7 * * *').next_time(
#=> Fri Oct 24 07:30:00 -0500 2008
Time.mktime(2008, 10, 24, 7, 29))
Rufus::CronLine.new('30 7 * * *').next_time(
Time.now))
be passed if no start time is specified (search start time set to
the given start point Time (thus a result in the local time zone will
Note that the time instance returned will be in the same time zone that
for the 'search'. By default, it's Time.now
This method accepts an optional Time parameter. It's the starting point
(Well, I was wrong, takes 0.001 sec on 1.8.7 and 1.9.1)
This is raw, 3 secs to iterate over 1 year on my macbook :( brutal.
Returns the next time that this cron line is supposed to 'fire'
def next_time(now=Time.now) time = @timezone ? @timezone.utc_to_local(now.getutc) : now time = time - time.usec * 1e-6 + 1 # little adjustment before starting loop do unless date_match?(time) time += (24 - time.hour) * 3600 - time.min * 60 - time.sec next end unless sub_match?(time.hour, @hours) time += (60 - time.min) * 60 - time.sec next end unless sub_match?(time.min, @minutes) time += 60 - time.sec next end unless sub_match?(time.sec, @seconds) time += 1 next end break end if @timezone time = @timezone.local_to_utc(time) time = time.getlocal unless now.utc? end time end
def parse_item(item, min, max)
def parse_item(item, min, max) return nil if item == '*' return parse_list(item, min, max) if item.index(',') return parse_range(item, min, max) if item.index('*') or item.index('-') i = item.to_i i = min if i < min i = max if i > max [ i ] end
def parse_list(item, min, max)
def parse_list(item, min, max) item.split(',').inject([]) { |r, i| r.push(parse_range(i, min, max)) }.flatten end
def parse_range(item, min, max)
def parse_range(item, min, max) i = item.index('-') j = item.index('/') return item.to_i if (not i and not j) inc = j ? item[j + 1..-1].to_i : 1 istart = -1 iend = -1 if i istart = item[0..i - 1].to_i iend = if j item[i + 1..j - 1].to_i else item[i + 1..-1].to_i end else # case */x istart = min iend = max end istart = min if istart < min iend = max if iend > max result = [] value = istart loop do result << value value = value + inc break if value > iend end result end
def parse_weekdays(item)
def parse_weekdays(item) return nil if item == '*' items = item.downcase.split(',') weekdays = nil monthdays = nil items.each do |it| if it.match(/#[12345]$/) raise ArgumentError.new( "ranges are not supported for monthdays (#{it})" ) if it.index('-') (monthdays ||= []) << it else WEEKDAYS.each_with_index { |a, i| it.gsub!(/#{a}/, i.to_s) } its = it.index('-') ? parse_range(it, 0, 7) : [ Integer(it) ] its = its.collect { |i| i == 7 ? 0 : i } (weekdays ||= []).concat(its) end end weekdays = weekdays.uniq if weekdays [ weekdays, monthdays ] end
def sub_match?(value, values)
def sub_match?(value, values) values.nil? || values.include?(value) end
def to_array
This method is used by the cronline unit tests.
months, weekdays).
Returns an array of 6 arrays (seconds, minutes, hours, days,
def to_array [ @seconds, @minutes, @hours, @days, @months, @weekdays, @monthdays, @timezone ? @timezone.name : nil ] end