Module: Rufus

require ‘parsedate‘

Child modules and classes

Module Rufus::Schedulable
Class Rufus::AtJob
Class Rufus::CronJob
Class Rufus::CronLine
Class Rufus::EveryJob
Class Rufus::Job
Class Rufus::Scheduler

Constants

NameValue
DURATIONS2M [ [ "y", 365 * 24 * 3600 ], [ "M", 30 * 24 * 3600 ], [ "w", 7 * 24 * 3600 ], [ "d", 24 * 3600 ], [ "h", 3600 ], [ "m", 60 ], [ "s", 1 ]
DURATIONS2 DURATIONS2M.dup
DURATIONS DURATIONS2M.inject({}) do |r, (k, v)| r[k] = v
JOB_ID_LOCK Monitor.new

Aliases

Old nameNew name
parse_time_string parse_duration_string
to_duration_string to_time_string

Public Class Methods


current_time_millis ()

equivalent to java.lang.System.currentTimeMillis()

    # File lib/rufus/otime.rb, line 94
94:   def Rufus.current_time_millis
95: 
96:     (Time.new.to_f * 1000).to_i
97:   end

duration_to_f (s)

Ensures that a duration is a expressed as a Float instance.

  duration_to_f("10s")

will yield 10.0

     # File lib/rufus/otime.rb, line 346
346:   def Rufus.duration_to_f (s)
347: 
348:     return s if s.kind_of?(Float)
349:     return parse_time_string(s) if s.kind_of?(String)
350:     Float(s.to_s)
351:   end

is_digit? (c)

Returns true if the character c is a digit

(probably better served by a regex)

     # File lib/rufus/otime.rb, line 170
170:   def Rufus.is_digit? (c)
171: 
172:     return false if not c.kind_of?(String)
173:     return false if c.length > 1
174:     (c >= "0" and c <= "9")
175:   end

now ()

Returns the current time as an ISO date string

    # File lib/rufus/otime.rb, line 42
42:   def Rufus.now
43: 
44:     to_iso8601_date(Time.new())
45:   end

parse_time_string (string)

Turns a string like ‘1m10s’ into a float like ‘70.0’, more formally, turns a time duration expressed as a string into a Float instance (millisecond count).

w -> week d -> day h -> hour m -> minute s -> second M -> month y -> year ‘nada’ -> millisecond

Some examples :

  Rufus.parse_time_string "500"    # => 0.5
  Rufus.parse_time_string "1000"     # => 1.0
  Rufus.parse_time_string "1h"     # => 3600.0
  Rufus.parse_time_string "1h10s"    # => 3610.0
  Rufus.parse_time_string "1w2d"     # => 777600.0
     # File lib/rufus/otime.rb, line 121
121:   def Rufus.parse_time_string (string)
122: 
123:     string = string.strip
124: 
125:     index = -1
126:     result = 0.0
127: 
128:     number = ""
129: 
130:     loop do
131: 
132:       index = index + 1
133: 
134:       if index >= string.length
135:         result = result + (Float(number) / 1000.0) if number.length > 0
136:         break
137:       end
138: 
139:       c = string[index, 1]
140: 
141:       #if is_digit?(c)
142:       if (c >= "0" and c <= "9")
143:         number = number + c
144:         next
145:       end
146: 
147:       value = Integer(number)
148:       number = ""
149: 
150:       multiplier = DURATIONS[c]
151: 
152:       raise "unknown time char '#{c}'" \
153:         if not multiplier
154: 
155:       result = result + (value * multiplier)
156:     end
157: 
158:     result
159:   end

time_to_iso8601_date (time)

the old method we used to generate our ISO datetime strings

    # File lib/rufus/otime.rb, line 69
69:   def Rufus.time_to_iso8601_date (time)
70: 
71:     s = time.getutc().strftime(TIME_FORMAT)
72:     o = time.utc_offset / 3600
73:     o = o.to_s + "00"
74:     o = "0" + o if o.length < 4
75:     o = "+" + o unless o[0..1] == '-'
76: 
77:     s + " " + o.to_s
78:   end

to_datetime (time)

converts a Time instance to a DateTime one

     # File lib/rufus/otime.rb, line 189
189:   def Rufus.to_datetime (time)
190: 
191:     s = time.sec + Rational(time.usec, 10**6)
192:     o = Rational(time.utc_offset, 3600 * 24)
193: 
194:     begin
195: 
196:       DateTime.new(
197:         time.year,
198:         time.month,
199:         time.day,
200:         time.hour,
201:         time.min,
202:         s,
203:         o)
204: 
205:     rescue Exception => e
206: 
207:       DateTime.new(
208:         time.year,
209:         time.month,
210:         time.day,
211:         time.hour,
212:         time.min,
213:         time.sec,
214:         time.utc_offset)
215:     end
216:   end

to_duration_hash (seconds, options={})

Turns a number of seconds (integer or Float) into a hash like in :

  Rufus.to_duration_hash 0.051
    # => { :ms => "51" }
  Rufus.to_duration_hash 7.051
    # => { :s => 7, :ms => "51" }
  Rufus.to_duration_hash 0.120 + 30 * 24 * 3600 + 1
    # => { :w => 4, :d => 2, :s => 1, :ms => "120" }

This method is used by to_duration_string (to_time_string) behind the scene.

Options are :

  • :months, if set to true, months (M) of 30 days will be taken into account when building up the result
  • :drop_seconds, if set to true, seconds and milliseconds will be trimmed from the result
     # File lib/rufus/otime.rb, line 312
312:   def Rufus.to_duration_hash (seconds, options={})
313: 
314:     h = {}
315: 
316:     if seconds.is_a?(Float)
317:       h[:ms] = (seconds % 1 * 1000).to_i
318:       seconds = seconds.to_i
319:     end
320: 
321:     if options[:drop_seconds]
322:       h.delete :ms
323:       seconds = (seconds - seconds % 60)
324:     end
325: 
326:     durations = options[:months] ? DURATIONS2M : DURATIONS2
327: 
328:     durations.each do |key, duration|
329: 
330:       count = seconds / duration
331:       seconds = seconds % duration
332: 
333:       h[key.to_sym] = count if count > 0
334:     end
335: 
336:     h
337:   end

to_duration_string (seconds, options={})

Turns a number of seconds into a a time string

  Rufus.to_duration_string 0          # => '0s'
  Rufus.to_duration_string 60           # => '1m'
  Rufus.to_duration_string 3661         # => '1h1m1s'
  Rufus.to_duration_string 7 * 24 * 3600    # => '1w'
  Rufus.to_duration_string 30 * 24 * 3600 + 1   # => "4w2d1s"

It goes from seconds to the year. Months are not counted (as they are of variable length). Weeks are counted.

For 30 days months to be counted, the second parameter of this method can be set to true.

  Rufus.to_time_string 30 * 24 * 3600 + 1, true   # => "1M1s"

(to_time_string is an alias for to_duration_string)

If a Float value is passed, milliseconds will be displayed without ‘marker‘

  Rufus.to_duration_string 0.051             # =>"51"
  Rufus.to_duration_string 7.051             # =>"7s51"
  Rufus.to_duration_string 0.120 + 30 * 24 * 3600 + 1  # =>"4w2d1s120"

(this behaviour mirrors the one found for parse_time_string()).

Options are :

  • :months, if set to true, months (M) of 30 days will be taken into account when building up the result
  • :drop_seconds, if set to true, seconds and milliseconds will be trimmed from the result
     # File lib/rufus/otime.rb, line 269
269:   def Rufus.to_duration_string (seconds, options={})
270: 
271:     return (options[:drop_seconds] ? '0m' : '0s') if seconds <= 0
272: 
273:     h = to_duration_hash seconds, options
274: 
275:     s = DU_KEYS.inject("") do |r, key|
276:       count = h[key]
277:       count = nil if count == 0
278:       r << "#{count}#{key}" if count
279:       r
280:     end
281: 
282:     ms = h[:ms]
283:     s << ms.to_s if ms
284: 
285:     s
286:   end

to_gm_time (dtime)

     # File lib/rufus/otime.rb, line 218
218:   def Rufus.to_gm_time (dtime)
219: 
220:     to_ttime(dtime.new_offset, :gm)
221:   end

to_iso8601_date (date)

As the name implies.

    # File lib/rufus/otime.rb, line 50
50:   def Rufus.to_iso8601_date (date)
51: 
52:     if date.kind_of? Float
53:       date = to_datetime(Time.at(date))
54:     elsif date.kind_of? Time
55:       date = to_datetime(date)
56:     elsif not date.kind_of? Date
57:       date = DateTime.parse(date)
58:     end
59: 
60:     s = date.to_s # this is costly
61:     s[10] = " "
62: 
63:     s
64:   end

to_local_time (dtime)

     # File lib/rufus/otime.rb, line 223
223:   def Rufus.to_local_time (dtime)
224: 
225:     to_ttime(dtime.new_offset(DateTime.now.offset-offset), :local)
226:   end

to_ruby_time (iso_date)

Returns a Ruby time

    # File lib/rufus/otime.rb, line 83
83:   def Rufus.to_ruby_time (iso_date)
84: 
85:     DateTime.parse(iso_date)
86:   end

to_ttime (d, method)

     # File lib/rufus/otime.rb, line 228
228:   def Rufus.to_ttime (d, method)
229: 
230:     usec = (d.sec_fraction * 3600 * 24 * (10**6)).to_i
231:     Time.send(method, d.year, d.month, d.day, d.hour, d.min, d.sec, usec)
232:   end