Method: Allora::CronLine#next_time

Defined in:
lib/allora/cron_line.rb

#next_time(time) ⇒ Time

Returns the next time that this cron line is supposed to ‘fire’

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))

Examples:


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

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

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

Parameters:

  • time (Time)

    the time from which to compute the next time

Returns:

  • (Time)

    the next time after the given Time



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/allora/cron_line.rb', line 81

def next_time(time)
  # little adjustment before starting
  time = 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