Parent

Rufus::CronLine

A 'cron line' is a line in the sense of a crontab (man 5 crontab) file line.

Constants

WDS

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

new(line) click to toggle source
# File lib/rufus/sc/cronline.rb, line 47
def initialize (line)

  super()

  @original = line

  items = line.split

  unless items.length == 5 or items.length == 6
    raise(
      "cron '#{line}' string should hold 5 or 6 items, not #{items.length}")
  end

  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 = parse_weekdays(items[4 + offset])
end

Public Instance Methods

matches?(time) click to toggle source

Returns true if the given time matches this cron line.

# File lib/rufus/sc/cronline.rb, line 73
def matches? (time)

  time = Time.at(time) unless time.kind_of?(Time)

  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 sub_match?(time.day, @days)
  return false unless sub_match?(time.month, @months)
  return false unless sub_match?(time.wday, @weekdays)
  true
end
next_time(time=Time.now) click to toggle source

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. (Well, I was wrong, takes 0.001 sec on 1.8.7 and 1.9.1)

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 121
def next_time (time=Time.now)

  time -= time.usec * 1e-6
  time += 1

  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

  time
end
to_array() click to toggle source

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
def to_array

  [ @seconds, @minutes, @hours, @days, @months, @weekdays ]
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.