A ‘cron line’ is a line in the sense of a crontab (man 5 crontab) file line.
Constants
| WDS | = | %w[ sun mon tue wed thu fri sat ] |
Attributes
| days | [R] | |
| hours | [R] | |
| minutes | [R] | |
| months | [R] | |
| original | [R] | The string used for creating this cronline instance. |
| seconds | [R] | |
| weekdays | [R] |
Public class methods
# File lib/rufus/sc/cronline.rb, line 47 47: def initialize (line) 48: 49: super() 50: 51: @original = line 52: 53: items = line.split 54: 55: unless items.length == 5 or items.length == 6 56: raise( 57: "cron '#{line}' string should hold 5 or 6 items, not #{items.length}") 58: end 59: 60: offset = items.length - 5 61: 62: @seconds = offset == 1 ? parse_item(items[0], 0, 59) : [ 0 ] 63: @minutes = parse_item(items[0 + offset], 0, 59) 64: @hours = parse_item(items[1 + offset], 0, 24) 65: @days = parse_item(items[2 + offset], 1, 31) 66: @months = parse_item(items[3 + offset], 1, 12) 67: @weekdays = parse_weekdays(items[4 + offset]) 68: end
Public instance methods
Returns true if the given time matches this cron line.
# File lib/rufus/sc/cronline.rb, line 73 73: def matches? (time) 74: 75: time = Time.at(time) unless time.kind_of?(Time) 76: 77: return false unless sub_match?(time.sec, @seconds) 78: return false unless sub_match?(time.min, @minutes) 79: return false unless sub_match?(time.hour, @hours) 80: return false unless sub_match?(time.day, @days) 81: return false unless sub_match?(time.month, @months) 82: return false unless sub_match?(time.wday, @weekdays) 83: true 84: end
Returns the next time that this cron line is supposed to ‘fire’
This is raw, 3 secs to iterate over 1 year on my macbook :( brutal.
This method accepts an optional Time parameter. It’s the starting point for the ‘search’. By default, it’s Time.now
Note that the time instance returned will be in the same time zone that the given start point Time (thus a result in the local time zone will be passed if no start time is specified (search start time set to Time.now))
>> Rufus::CronLine.new('30 7 * * *').next_time( Time.mktime(2008,10,24,7,29) )
=> Fri Oct 24 07:30:00 -0500 2008
>> Rufus::CronLine.new('30 7 * * *').next_time( Time.utc(2008,10,24,7,29) )
=> Fri Oct 24 07:30:00 UTC 2008
>> Rufus::CronLine.new('30 7 * * *').next_time( Time.utc(2008,10,24,7,29) ).localtime
=> Fri Oct 24 02:30:00 -0500 2008
(Thanks to K Liu for the note and the examples)
# File lib/rufus/sc/cronline.rb, line 120 120: def next_time (time=Time.now) 121: 122: time -= time.usec * 1e-6 123: time += 1 124: 125: loop do 126: 127: unless date_match?(time) 128: time += (24 - time.hour) * 3600 - time.min * 60 - time.sec 129: next 130: end 131: 132: unless sub_match?(time.hour, @hours) 133: time += (60 - time.min) * 60 - time.sec 134: next 135: end 136: 137: unless sub_match?(time.min, @minutes) 138: time += 60 - time.sec 139: next 140: end 141: 142: unless sub_match?(time.sec, @seconds) 143: time += 1 144: next 145: end 146: 147: break 148: end 149: 150: time 151: end
Returns an array of 6 arrays (seconds, minutes, hours, days, months, weekdays). This method is used by the cronline unit tests.
# File lib/rufus/sc/cronline.rb, line 91 91: def to_array 92: 93: [ @seconds, @minutes, @hours, @days, @months, @weekdays ] 94: end