Module Rufus

  1. lib/rufus/sc/jobqueues.rb
  2. lib/rufus/sc/cronline.rb
  3. lib/rufus/sc/jobs.rb
  4. lib/rufus/sc/rtime.rb
  5. show all
---
---

require ‘parsedate’

Classes and Modules

Module Rufus::Schedulable
Module Rufus::Scheduler
Class Rufus::CronLine

Constants

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

External Aliases

parse_time_string -> parse_duration_string
to_duration_string -> to_time_string

Public class methods

at_to_f (at)

Ensures an ‘at’ value is translated to a float (to be compared with the float coming from time.to_f)

[show source]
     # File lib/rufus/sc/rtime.rb, line 322
322:   def Rufus.at_to_f (at)
323: 
324:     # TODO : use chronic if present
325: 
326:     at = Rufus::to_ruby_time(at) if at.is_a?(String)
327:     at = Rufus::to_gm_time(at) if at.is_a?(DateTime)
328:     #at = at.to_f if at.is_a?(Time)
329:     at = at.to_f if at.respond_to?(:to_f)
330: 
331:     raise ArgumentError.new(
332:       "cannot determine 'at' time from : #{at.inspect}"
333:     ) unless at.is_a?(Float)
334: 
335:     at
336:   end
current_time_millis ()

Equivalent to java.lang.System.currentTimeMillis()

[show source]
    # File lib/rufus/sc/rtime.rb, line 78
78:   def Rufus.current_time_millis
79: 
80:     (Time.new.to_f * 1000).to_i
81:   end
duration_to_f (s)

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

duration_to_f("10s")

will yield 10.0

[show source]
     # File lib/rufus/sc/rtime.rb, line 311
311:   def Rufus.duration_to_f (s)
312: 
313:     return s if s.kind_of?(Float)
314:     return parse_time_string(s) if s.kind_of?(String)
315:     Float(s.to_s)
316:   end
now ()

Returns the current time as an ISO date string

[show source]
    # File lib/rufus/sc/rtime.rb, line 34
34:   def Rufus.now
35: 
36:     to_iso8601_date(Time.new())
37:   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
[show source]
     # File lib/rufus/sc/rtime.rb, line 104
104:   def Rufus.parse_time_string (string)
105: 
106:     string = string.strip
107: 
108:     index = -1
109:     result = 0.0
110: 
111:     number = ''
112: 
113:     loop do
114: 
115:       index = index + 1
116: 
117:       if index >= string.length
118:         result = result + (Float(number) / 1000.0) if number.length > 0
119:         break
120:       end
121: 
122:       c = string[index, 1]
123: 
124:       if (c >= '0' and c <= '9')
125:         number = number + c
126:         next
127:       end
128: 
129:       value = Integer(number)
130:       number = ''
131: 
132:       multiplier = DURATIONS[c]
133: 
134:       raise "unknown time char '#{c}'" unless multiplier
135: 
136:       result = result + (value * multiplier)
137:     end
138: 
139:     result
140:   end
time_to_iso8601_date (time)

the old method we used to generate our ISO datetime strings

[show source]
    # File lib/rufus/sc/rtime.rb, line 58
58:   def Rufus.time_to_iso8601_date (time)
59: 
60:     s = time.getutc().strftime(TIME_FORMAT)
61:     o = time.utc_offset / 3600
62:     o = "#{o}00"
63:     o = "0#{o}" if o.length < 4
64:     o = "+#{o}" unless o[0..1] == '-'
65: 
66:     "#{s} #{o}"
67:   end
to_datetime (time)

Converts a Time instance to a DateTime one

[show source]
     # File lib/rufus/sc/rtime.rb, line 157
157:   def Rufus.to_datetime (time)
158: 
159:     s = time.sec + Rational(time.usec, 10**6)
160:     o = Rational(time.utc_offset, 3600 * 24)
161: 
162:     begin
163: 
164:       DateTime.new(
165:         time.year,
166:         time.month,
167:         time.day,
168:         time.hour,
169:         time.min,
170:         s,
171:         o)
172: 
173:     rescue Exception => e
174: 
175:       DateTime.new(
176:         time.year,
177:         time.month,
178:         time.day,
179:         time.hour,
180:         time.min,
181:         time.sec,
182:         time.utc_offset)
183:     end
184:   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
[show source]
     # File lib/rufus/sc/rtime.rb, line 278
278:   def Rufus.to_duration_hash (seconds, options={})
279: 
280:     h = {}
281: 
282:     if seconds.is_a?(Float)
283:       h[:ms] = (seconds % 1 * 1000).to_i
284:       seconds = seconds.to_i
285:     end
286: 
287:     if options[:drop_seconds]
288:       h.delete :ms
289:       seconds = (seconds - seconds % 60)
290:     end
291: 
292:     durations = options[:months] ? DURATIONS2M : DURATIONS2
293: 
294:     durations.each do |key, duration|
295: 
296:       count = seconds / duration
297:       seconds = seconds % duration
298: 
299:       h[key.to_sym] = count if count > 0
300:     end
301: 
302:     h
303:   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
[show source]
     # File lib/rufus/sc/rtime.rb, line 236
236:   def Rufus.to_duration_string (seconds, options={})
237: 
238:     return (options[:drop_seconds] ? '0m' : '0s') if seconds <= 0
239: 
240:     h = to_duration_hash seconds, options
241: 
242:     s = DU_KEYS.inject('') do |r, key|
243:       count = h[key]
244:       count = nil if count == 0
245:       r << "#{count}#{key}" if count
246:       r
247:     end
248: 
249:     ms = h[:ms]
250:     s << ms.to_s if ms
251: 
252:     s
253:   end
to_gm_time (dtime)
[show source]
     # File lib/rufus/sc/rtime.rb, line 186
186:   def Rufus.to_gm_time (dtime)
187: 
188:     to_ttime(dtime.new_offset, :gm)
189:   end
to_iso8601_date (date)

As the name implies.

[show source]
    # File lib/rufus/sc/rtime.rb, line 41
41:   def Rufus.to_iso8601_date (date)
42: 
43:     date = case date
44:       when Date then date
45:       when Float then to_datetime(Time.at(date))
46:       when Time then to_datetime(date)
47:       else DateTime.parse(date)
48:     end
49: 
50:     s = date.to_s # this is costly
51:     s[10] = ' '
52: 
53:     s
54:   end
to_local_time (dtime)
[show source]
     # File lib/rufus/sc/rtime.rb, line 191
191:   def Rufus.to_local_time (dtime)
192: 
193:     to_ttime(dtime.new_offset(DateTime.now.offset-offset), :local)
194:   end
to_ruby_time (sdate)

Returns a Ruby time

[show source]
    # File lib/rufus/sc/rtime.rb, line 71
71:   def Rufus.to_ruby_time (sdate)
72: 
73:     DateTime.parse(sdate)
74:   end
to_ttime (d, method)
[show source]
     # File lib/rufus/sc/rtime.rb, line 196
196:   def Rufus.to_ttime (d, method)
197: 
198:     usec = (d.sec_fraction * 3600 * 24 * (10**6)).to_i
199:     Time.send(method, d.year, d.month, d.day, d.hour, d.min, d.sec, usec)
200:   end