Class: Allora::CronLine

Inherits:
Object
  • Object
show all
Defined in:
lib/allora/cron_line.rb

Overview

Parses a crontab string to determine the times it represents.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line) ⇒ CronLine

Returns a new instance of CronLine.

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/allora/cron_line.rb', line 38

def initialize(line)
  items = line.split

  raise ArgumentError.new("not a valid cronline : '#{line}'") \
    unless items.length == 5 or items.length == 6

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

Instance Attribute Details

#daysObject (readonly)

Returns the value of attribute days.



32
33
34
# File 'lib/allora/cron_line.rb', line 32

def days
  @days
end

#hoursObject (readonly)

Returns the value of attribute hours.



31
32
33
# File 'lib/allora/cron_line.rb', line 31

def hours
  @hours
end

#minutesObject (readonly)

Returns the value of attribute minutes.



30
31
32
# File 'lib/allora/cron_line.rb', line 30

def minutes
  @minutes
end

#monthdaysObject (readonly)

Returns the value of attribute monthdays.



35
36
37
# File 'lib/allora/cron_line.rb', line 35

def monthdays
  @monthdays
end

#monthsObject (readonly)

Returns the value of attribute months.



33
34
35
# File 'lib/allora/cron_line.rb', line 33

def months
  @months
end

#secondsObject (readonly)

Returns the value of attribute seconds.



29
30
31
# File 'lib/allora/cron_line.rb', line 29

def seconds
  @seconds
end

#timezoneObject (readonly)

Returns the value of attribute timezone.



36
37
38
# File 'lib/allora/cron_line.rb', line 36

def timezone
  @timezone
end

#weekdaysObject (readonly)

Returns the value of attribute weekdays.



34
35
36
# File 'lib/allora/cron_line.rb', line 34

def weekdays
  @weekdays
end

Instance Method Details

#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

#to_arrayObject



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/allora/cron_line.rb', line 109

def to_array
  [
    @seconds,
    @minutes,
    @hours,
    @days,
    @months,
    @weekdays,
    @monthdays
  ]
end