Ensures an ‘at’ value is translated to a float (to be compared with the float coming from time.to_f)
# File lib/rufus/sc/rtime.rb, line 336
336: def Rufus.at_to_f(at)
337:
338: # TODO : use chronic if present
339:
340: at = Rufus::to_ruby_time(at) if at.is_a?(String)
341: at = Rufus::to_gm_time(at) if at.is_a?(DateTime)
342: #at = at.to_f if at.is_a?(Time)
343: at = at.to_f if at.respond_to?(:to_f)
344:
345: raise ArgumentError.new(
346: "cannot determine 'at' time from : #{at.inspect}"
347: ) unless at.is_a?(Float)
348:
349: at
350: end
Equivalent to java.lang.System.currentTimeMillis()
# File lib/rufus/sc/rtime.rb, line 94
94: def Rufus.current_time_millis
95:
96: (Time.new.to_f * 1000).to_i
97: end
Ensures that a duration is a expressed as a Float instance.
duration_to_f("10s")
will yield 10.0
# File lib/rufus/sc/rtime.rb, line 326
326: def Rufus.duration_to_f(s)
327:
328: return s if s.kind_of?(Float)
329: return parse_time_string(s) if s.kind_of?(String)
330: Float(s.to_s)
331: end
Returns the current time as an ISO date string
# File lib/rufus/sc/rtime.rb, line 50
50: def Rufus.now
51:
52: to_iso8601_date(Time.new)
53: end
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
# File lib/rufus/sc/rtime.rb, line 123
123: def Rufus.parse_time_string(string)
124:
125: return string.to_f if FLOAT_DURATION.match(string)
126:
127: string = string.strip
128:
129: index = 1
130: result = 0.0
131:
132: number = ''
133:
134: loop do
135:
136: index = index + 1
137:
138: if index >= string.length
139: result = result + (Float(number) / 1000.0) if number.length > 0
140: break
141: end
142:
143: c = string[index, 1]
144:
145: if (c >= '0' and c <= '9')
146: number = number + c
147: next
148: end
149:
150: value = Integer(number)
151: number = ''
152:
153: multiplier = DURATIONS[c]
154:
155: raise ArgumentError.new("unknown time char '#{c}'") unless multiplier
156:
157: result = result + (value * multiplier)
158: end
159:
160: result
161: end
the old method we used to generate our ISO datetime strings
# File lib/rufus/sc/rtime.rb, line 74
74: def Rufus.time_to_iso8601_date(time)
75:
76: s = time.getutc.strftime(TIME_FORMAT)
77: o = time.utc_offset / 3600
78: o = "#{o}00"
79: o = "0#{o}" if o.length < 4
80: o = "+#{o}" unless o[0..1] == '-'
81:
82: "#{s} #{o}"
83: end
Converts a Time instance to a DateTime one
# File lib/rufus/sc/rtime.rb, line 179
179: def Rufus.to_datetime(time)
180:
181: s = time.sec + Rational(time.usec, 10**6)
182: o = Rational(time.utc_offset, 3600 * 24)
183:
184: begin
185:
186: DateTime.new(time.year, time.month, time.day, time.hour, time.min, s, o)
187:
188: rescue Exception => e
189:
190: DateTime.new(
191: time.year,
192: time.month,
193: time.day,
194: time.hour,
195: time.min,
196: time.sec,
197: time.utc_offset)
198: end
199: end
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/sc/rtime.rb, line 293
293: def Rufus.to_duration_hash(seconds, options={})
294:
295: h = {}
296:
297: if seconds.is_a?(Float)
298: h[:ms] = (seconds % 1 * 1000).to_i
299: seconds = seconds.to_i
300: end
301:
302: if options[:drop_seconds]
303: h.delete :ms
304: seconds = (seconds - seconds % 60)
305: end
306:
307: durations = options[:months] ? DURATIONS2M : DURATIONS2
308:
309: durations.each do |key, duration|
310:
311: count = seconds / duration
312: seconds = seconds % duration
313:
314: h[key.to_sym] = count if count > 0
315: end
316:
317: h
318: end
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/sc/rtime.rb, line 251
251: def Rufus.to_duration_string(seconds, options={})
252:
253: return (options[:drop_seconds] ? '0m' : '0s') if seconds <= 0
254:
255: h = to_duration_hash seconds, options
256:
257: s = DU_KEYS.inject('') { |r, key|
258: count = h[key]
259: count = nil if count == 0
260: r << "#{count}#{key}" if count
261: r
262: }
263:
264: ms = h[:ms]
265: s << ms.to_s if ms
266:
267: s
268: end
# File lib/rufus/sc/rtime.rb, line 201
201: def Rufus.to_gm_time(dtime)
202:
203: to_ttime(dtime.new_offset, :gm)
204: end
As the name implies.
# File lib/rufus/sc/rtime.rb, line 57
57: def Rufus.to_iso8601_date(date)
58:
59: date = case date
60: when Date then date
61: when Float then to_datetime(Time.at(date))
62: when Time then to_datetime(date)
63: else DateTime.parse(date)
64: end
65:
66: s = date.to_s # this is costly
67: s[10] = ' '
68:
69: s
70: end
# File lib/rufus/sc/rtime.rb, line 206
206: def Rufus.to_local_time(dtime)
207:
208: to_ttime(dtime.new_offset(DateTime.now.offset-offset), :local)
209: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.