class Rufus::CronLine

def next_time (now = Time.now)


(Thanks to K Liu for the note and the examples)

=> Fri Oct 24 02:30:00 -0500 2008
>> Rufus::CronLine.new('30 7 * * *').next_time( Time.utc(2008,10,24,7,29) ).localtime

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

=> Fri Oct 24 07:30:00 -0500 2008
>> Rufus::CronLine.new('30 7 * * *').next_time( Time.mktime(2008,10,24,7,29) )

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

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)
  #
  # position now to the next cron second
  if @seconds
    next_sec = @seconds.find { |s| s > now.sec } || 60 + @seconds.first
    now += next_sec - now.sec
  else
    now += 1
  end
  #
  # prepare sec jump array
  sjarray = nil
  if @seconds
    sjarray = []
    i = @seconds.index(now.sec)
    ii = i
    loop do
      cur = @seconds[ii]
      ii += 1
      ii = 0 if ii == @seconds.size
      nxt = @seconds[ii]
      nxt += 60 if ii == 0
      sjarray << (nxt - cur)
      break if ii == i
    end
  else
    sjarray = [ 1 ]
  end
  #
  # ok, seek...
  i = 0
  loop do
    return now if matches?(now)
    now += sjarray[i]
    i += 1
    i = 0 if i == sjarray.size
    # danger... potentially no exit...
  end
  nil
end