class Rufus::Scheduler::CronLine

def next_time(from=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::Scheduler::CronLine.new('30 7 * * *').next_time(

#=> Fri Oct 24 07:30:00 UTC 2008
Time.utc(2008, 10, 24, 7, 29))
Rufus::Scheduler::CronLine.new('30 7 * * *').next_time(

#=> Fri Oct 24 07:30:00 -0500 2008
Time.mktime(2008, 10, 24, 7, 29))
Rufus::Scheduler::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(from=Time.now)
  time = nil
  zotime = ZoTime.new(from.to_i + 1, @timezone || ENV['TZ'])
  loop do
    time = zotime.time
    unless date_match?(time)
      zotime.add((24 - time.hour) * 3600 - time.min * 60 - time.sec)
      next
    end
    unless sub_match?(time, :hour, @hours)
      zotime.add((60 - time.min) * 60 - time.sec)
      next
    end
    unless sub_match?(time, :min, @minutes)
      zotime.add(60 - time.sec)
      next
    end
    unless sub_match?(time, :sec, @seconds)
      zotime.add(next_second(time))
      next
    end
    break
  end
  time
end