lib/rufus/scheduler/cronline.rb in rufus-scheduler-3.1.3 vs lib/rufus/scheduler/cronline.rb in rufus-scheduler-3.1.4

- old
+ new

@@ -184,23 +184,10 @@ end time end - if RUBY_VERSION >= '1.9' - def toa(item) - item == nil ? nil : item.to_a - end - else - def toi(item); item.is_a?(String) ? item.hash.abs : item.to_i; end - protected :toi - def toa(item) - item.is_a?(Set) ? item.to_a.sort_by { |e| toi(e) } : item - end - end - protected :toa - # Returns an array of 6 arrays (seconds, minutes, hours, days, # months, weekdays). # This method is used by the cronline unit tests. # def to_array @@ -226,20 +213,16 @@ # def frequency return brute_frequency unless @seconds && @seconds.length > 1 - delta = 60 secs = toa(@seconds) - prev = secs[0] - secs[1..-1].each do |sec| + secs[1..-1].inject([ secs[0], 60 ]) { |(prev, delta), sec| d = sec - prev - delta = d if d < delta - end - - delta + [ sec, d < delta ? d : delta ] + }[1] end # Returns the shortest delta between two potential occurences of the # schedule described by this cronline. # @@ -286,32 +269,47 @@ end delta end - protected - def next_second(time) - secs = @seconds.sort + secs = toa(@seconds) - return secs.last + 60 - time.sec if time.sec > secs.last + return secs.first + 60 - time.sec if time.sec > secs.last secs.shift while secs.first < time.sec secs.first - time.sec end def prev_second(time) - secs = @seconds.sort + secs = toa(@seconds) secs.pop while time.sec < secs.last time.sec - secs.last end + protected + + def sc_sort(a) + + a.sort_by { |e| e.is_a?(String) ? 61 : e.to_i } + end + + if RUBY_VERSION >= '1.9' + def toa(item) + item == nil ? nil : item.to_a + end + else + def toa(item) + item.is_a?(Set) ? sc_sort(item.to_a) : item + end + end + WEEKDAYS = %w[ sun mon tue wed thu fri sat ] DAY_S = 24 * 3600 WEEK_S = 7 * DAY_S def parse_weekdays(item) @@ -349,11 +347,11 @@ (weekdays ||= []).concat(its) end end - weekdays = weekdays.uniq if weekdays + weekdays = weekdays.uniq.sort if weekdays [ weekdays, monthdays ] end def parse_item(item, min, max) @@ -363,9 +361,11 @@ r = item.split(',').map { |i| parse_range(i.strip, min, max) }.flatten raise ArgumentError.new( "found duplicates in #{item.inspect}" ) if r.uniq.size < r.size + + r = sc_sort(r) Set.new(r) end RANGE_REGEX = /^(\*|\d{1,2})(?:-(\d{1,2}))?(?:\/(\d{1,2}))?$/