Class: Rufus::CronLine

Inherits:
Object
  • Object
show all
Defined in:
lib/rufus/sc/cronline.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line) ⇒ CronLine

Returns a new instance of CronLine.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rufus/sc/cronline.rb', line 49

def initialize (line)

  super()

  @original = line

  times, zone = line.split(' : ')
  items = times.split
  
  @zone = parse_zone(zone.to_s)

  if zone && !@zone
    raise "zone '#{zone}' is an invalid timezone"
  end

  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

Instance Attribute Details

#daysObject (readonly)

Returns the value of attribute days.



40
41
42
# File 'lib/rufus/sc/cronline.rb', line 40

def days
  @days
end

#hoursObject (readonly)

Returns the value of attribute hours.



40
41
42
# File 'lib/rufus/sc/cronline.rb', line 40

def hours
  @hours
end

#minutesObject (readonly)

Returns the value of attribute minutes.



40
41
42
# File 'lib/rufus/sc/cronline.rb', line 40

def minutes
  @minutes
end

#monthsObject (readonly)

Returns the value of attribute months.



40
41
42
# File 'lib/rufus/sc/cronline.rb', line 40

def months
  @months
end

#originalObject (readonly)

The string used for creating this cronline instance.



38
39
40
# File 'lib/rufus/sc/cronline.rb', line 38

def original
  @original
end

#secondsObject (readonly)

Returns the value of attribute seconds.



40
41
42
# File 'lib/rufus/sc/cronline.rb', line 40

def seconds
  @seconds
end

#weekdaysObject (readonly)

Returns the value of attribute weekdays.



40
41
42
# File 'lib/rufus/sc/cronline.rb', line 40

def weekdays
  @weekdays
end

#zoneObject (readonly)

Returns the value of attribute zone.



40
41
42
# File 'lib/rufus/sc/cronline.rb', line 40

def zone
  @zone
end

Instance Method Details

#matches?(time) ⇒ Boolean

Returns true if the given time matches this cron line.

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rufus/sc/cronline.rb', line 83

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

  if @zone
    utc = time.utc?
    time = time.getutc unless utc
    time = @zone.utc_to_local time
  end

  [
    sub_match?(time.sec, @seconds),
    sub_match?(time.min, @minutes),
    sub_match?(time.hour, @hours),
    sub_match?(time.day, @days),
    sub_match?(time.month, @months),
    sub_match?(time.wday, @weekdays)
  ].all?
end

#next_time(time = Time.now) ⇒ Object

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)



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/rufus/sc/cronline.rb', line 137

def next_time (time=Time.now)

  if @zone
    utc = time.utc?
    time = time.getutc unless utc
    time = @zone.utc_to_local time
  end

  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

  if @zone
    time = @zone.local_to_utc time
    time = time.getlocal unless utc
  end

  time
end

#to_arrayObject

Returns an array of 6 arrays (seconds, minutes, hours, days, months, weekdays). This method is used by the cronline unit tests.



107
108
109
110
# File 'lib/rufus/sc/cronline.rb', line 107

def to_array

  [ @seconds, @minutes, @hours, @days, @months, @weekdays, (@zone.name if @zone) ]
end