| Class | Dsadmin::AbsoluteTimeSpec |
| In: |
lib/dsadmin/time_spec.rb
|
| Parent: | TimeSpec |
Not to be used directly — see class TimeSpec instead
| MONTHS | = | (1..12) |
| MDAYS | = | (1..31) |
| HOURS | = | (0..23) |
| MINUTES | = | (0..59) |
| DEFAULTS | = | {'M' => MONTHS, 'd' => MDAYS, 'h' => HOURS, 'm' => MINUTES} |
| OVERFLOW_BIT | = | 0x10000 |
# File lib/dsadmin/time_spec.rb, line 332
332: def initialize(aSpecString)
333: super()
334: parse(aSpecString)
335: @spec_string = aSpecString
336: end
# File lib/dsadmin/time_spec.rb, line 235
235: def getNextTime(aNow = sys.now)
236: year = aNow.year
237: month = aNow.month
238: day = aNow.day
239: hour = aNow.hour
240:
241: # (1) Calculate a basic match
242:
243: min = findNextMatch(aNow.min, @minute)
244: if((min & OVERFLOW_BIT) != 0)
245: min ^= OVERFLOW_BIT
246: hour += 1
247: hc = true
248: end
249:
250: hour = findNextMatch(hour, @hour)
251: if((hour & OVERFLOW_BIT) != 0)
252: hour ^= OVERFLOW_BIT
253: day += 1
254: dc = true
255: end
256:
257: day = findNextMatch(day, @day)
258: if((day & OVERFLOW_BIT) != 0)
259: day ^= OVERFLOW_BIT
260: month += 1
261: mc = true
262: end
263:
264: month = findNextMatch(month, @month)
265: if((month & OVERFLOW_BIT) != 0)
266: month ^= OVERFLOW_BIT
267: year += 1
268: yc = true
269: end
270:
271: # (2) Optimize
272: yc = (year != aNow.year)
273: mc = (month != aNow.month)
274: dc = (day != aNow.day)
275: hc = (hour != aNow.hour)
276: month = findNextMatch(1, @month) if(yc)
277: day = findNextMatch(1, @day) if(yc || mc)
278: hour = findNextMatch(0, @hour) if(yc || mc || dc)
279: min = findNextMatch(0, @minute) if(yc || mc || dc || hc)
280:
281: return Time.local(year, month, day, hour, min, 0, 0)
282: end
# File lib/dsadmin/time_spec.rb, line 294
294: def parsePart(aMatchData, anIndex)
295: str = aMatchData[pre_indices[anIndex]]
296: defaults = DEFAULTS[anIndex]
297:
298: case(str)
299: when /^\*$/, '', nil # single wildcard or empty
300: return defaults
301: when /,/ # list of values
302: res = str.split(',')
303: res = res.collect { |val| val.to_i }
304: when /^%/ # modulo
305: val = str.gsub(/[^\d]+/, '').to_i
306: return defaults.to_a.delete_if { |i| ((i % val) != 0) }
307: else # single number
308: res = [str.to_i]
309: end
310:
311: # puts "res = [#{res.join(', ')}] | def = [#{defaults.join(', ')}]"
312: # check the values against the possible range where needed
313: res.each { |val|
314: unless(defaults.include?(val))
315: raise ArgumentError,
316: "Time value '#{val}' for '#{anIndex}' out of range"
317: end
318: }
319:
320: return res
321: end
# File lib/dsadmin/time_spec.rb, line 324
324: def regex_part_base
325: "(\\d+(?:,\\d+)*|\\*|%\\d+)"
326: end
# File lib/dsadmin/time_spec.rb, line 339
339: def findNextMatch(aNowVal, aMatchingVals)
340: max = aMatchingVals.max # Cheap for Ranges (the common case), kinda
341: # expensive for arrays
342:
343: if(aNowVal > max)
344: min = aMatchingVals.min
345: return (min | OVERFLOW_BIT)
346: end
347:
348: # puts "mv = [#{aMatchingVals.join(', ')}]"
349: aMatchingVals.each { |val|
350: return val if(val >= aNowVal)
351: }
352:
353: # no match found
354: assertNotReachable
355: end