Module: Rufus
- Defined in:
- lib/rufus/sc/jobs.rb,
lib/rufus/sc/rtime.rb,
lib/rufus/sc/version.rb,
lib/rufus/sc/cronline.rb,
lib/rufus/sc/jobqueues.rb
Defined Under Namespace
Modules: Schedulable, Scheduler Classes: CronLine
Constant Summary collapse
- FLOAT_DURATION =
/^\d*\.\d*$/
Class Method Summary collapse
-
.at_to_f(at) ⇒ Object
Ensures an ‘at’ value is translated to a float (to be compared with the float coming from time.to_f).
-
.current_time_millis ⇒ Object
Equivalent to java.lang.System.currentTimeMillis().
-
.duration_to_f(s) ⇒ Object
Ensures that a duration is a expressed as a Float instance.
-
.now ⇒ Object
Returns the current time as an ISO date string.
-
.parse_time_string(string) ⇒ Object
(also: parse_duration_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).
-
.time_to_iso8601_date(time) ⇒ Object
the old method we used to generate our ISO datetime strings.
-
.to_datetime(time) ⇒ Object
Converts a Time instance to a DateTime one.
-
.to_duration_hash(seconds, options = {}) ⇒ Object
Turns a number of seconds (integer or Float) into a hash like in :.
-
.to_duration_string(seconds, options = {}) ⇒ Object
(also: to_time_string)
Turns a number of seconds into a a time string.
- .to_gm_time(dtime) ⇒ Object
-
.to_iso8601_date(date) ⇒ Object
As the name implies.
- .to_local_time(dtime) ⇒ Object
-
.to_ruby_time(sdate) ⇒ Object
Returns a Ruby time.
- .to_ttime(d, method) ⇒ Object
Class Method Details
.at_to_f(at) ⇒ Object
Ensures an ‘at’ value is translated to a float (to be compared with the float coming from time.to_f)
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/rufus/sc/rtime.rb', line 327 def Rufus.at_to_f (at) # TODO : use chronic if present at = Rufus::to_ruby_time(at) if at.is_a?(String) at = Rufus::to_gm_time(at) if at.is_a?(DateTime) #at = at.to_f if at.is_a?(Time) at = at.to_f if at.respond_to?(:to_f) raise ArgumentError.new( "cannot determine 'at' time from : #{at.inspect}" ) unless at.is_a?(Float) at end |
.current_time_millis ⇒ Object
Equivalent to java.lang.System.currentTimeMillis()
78 79 80 81 |
# File 'lib/rufus/sc/rtime.rb', line 78 def Rufus.current_time_millis (Time.new.to_f * 1000).to_i end |
.duration_to_f(s) ⇒ Object
Ensures that a duration is a expressed as a Float instance.
duration_to_f("10s")
will yield 10.0
316 317 318 319 320 321 |
# File 'lib/rufus/sc/rtime.rb', line 316 def Rufus.duration_to_f (s) return s if s.kind_of?(Float) return parse_time_string(s) if s.kind_of?(String) Float(s.to_s) end |
.now ⇒ Object
Returns the current time as an ISO date string
34 35 36 37 |
# File 'lib/rufus/sc/rtime.rb', line 34 def Rufus.now to_iso8601_date(Time.new()) end |
.parse_time_string(string) ⇒ Object Also known as: parse_duration_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 "0.5" # => 0.5
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
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/rufus/sc/rtime.rb', line 107 def Rufus.parse_time_string (string) return string.to_f if FLOAT_DURATION.match(string) string = string.strip index = -1 result = 0.0 number = '' loop do index = index + 1 if index >= string.length result = result + (Float(number) / 1000.0) if number.length > 0 break end c = string[index, 1] if (c >= '0' and c <= '9') number = number + c next end value = Integer(number) number = '' multiplier = DURATIONS[c] raise "unknown time char '#{c}'" unless multiplier result = result + (value * multiplier) end result end |
.time_to_iso8601_date(time) ⇒ Object
the old method we used to generate our ISO datetime strings
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rufus/sc/rtime.rb', line 58 def Rufus.time_to_iso8601_date (time) s = time.getutc().strftime(TIME_FORMAT) o = time.utc_offset / 3600 o = "#{o}00" o = "0#{o}" if o.length < 4 o = "+#{o}" unless o[0..1] == '-' "#{s} #{o}" end |
.to_datetime(time) ⇒ Object
Converts a Time instance to a DateTime one
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/rufus/sc/rtime.rb', line 162 def Rufus.to_datetime (time) s = time.sec + Rational(time.usec, 10**6) o = Rational(time.utc_offset, 3600 * 24) begin DateTime.new( time.year, time.month, time.day, time.hour, time.min, s, o) rescue Exception => e DateTime.new( time.year, time.month, time.day, time.hour, time.min, time.sec, time.utc_offset) end end |
.to_duration_hash(seconds, options = {}) ⇒ Object
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
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/rufus/sc/rtime.rb', line 283 def Rufus.to_duration_hash (seconds, ={}) h = {} if seconds.is_a?(Float) h[:ms] = (seconds % 1 * 1000).to_i seconds = seconds.to_i end if [:drop_seconds] h.delete :ms seconds = (seconds - seconds % 60) end durations = [:months] ? DURATIONS2M : DURATIONS2 durations.each do |key, duration| count = seconds / duration seconds = seconds % duration h[key.to_sym] = count if count > 0 end h end |
.to_duration_string(seconds, options = {}) ⇒ Object Also known as: to_time_string
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
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/rufus/sc/rtime.rb', line 241 def Rufus.to_duration_string (seconds, ={}) return ([:drop_seconds] ? '0m' : '0s') if seconds <= 0 h = to_duration_hash seconds, s = DU_KEYS.inject('') do |r, key| count = h[key] count = nil if count == 0 r << "#{count}#{key}" if count r end ms = h[:ms] s << ms.to_s if ms s end |
.to_gm_time(dtime) ⇒ Object
191 192 193 194 |
# File 'lib/rufus/sc/rtime.rb', line 191 def Rufus.to_gm_time (dtime) to_ttime(dtime.new_offset, :gm) end |
.to_iso8601_date(date) ⇒ Object
As the name implies.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rufus/sc/rtime.rb', line 41 def Rufus.to_iso8601_date (date) date = case date when Date then date when Float then to_datetime(Time.at(date)) when Time then to_datetime(date) else DateTime.parse(date) end s = date.to_s # this is costly s[10] = ' ' s end |
.to_local_time(dtime) ⇒ Object
196 197 198 199 |
# File 'lib/rufus/sc/rtime.rb', line 196 def Rufus.to_local_time (dtime) to_ttime(dtime.new_offset(DateTime.now.offset-offset), :local) end |
.to_ruby_time(sdate) ⇒ Object
Returns a Ruby time
71 72 73 74 |
# File 'lib/rufus/sc/rtime.rb', line 71 def Rufus.to_ruby_time (sdate) DateTime.parse(sdate) end |
.to_ttime(d, method) ⇒ Object
201 202 203 204 205 |
# File 'lib/rufus/sc/rtime.rb', line 201 def Rufus.to_ttime (d, method) usec = (d.sec_fraction * 3600 * 24 * (10**6)).to_i Time.send(method, d.year, d.month, d.day, d.hour, d.min, d.sec, usec) end |