lib/rufus/otime.rb in rufus-scheduler-1.0.3 vs lib/rufus/otime.rb in rufus-scheduler-1.0.4
- old
+ new
@@ -130,21 +130,18 @@
loop do
index = index + 1
if index >= string.length
- if number.length > 0
- result = result + (Float(number) / 1000.0)
- end
+ result = result + (Float(number) / 1000.0) if number.length > 0
break
end
c = string[index, 1]
- # TODO : investigate something better than this is_digit?
-
- if is_digit?(c)
+ #if is_digit?(c)
+ if (c >= "0" and c <= "9")
number = number + c
next
end
value = Integer(number)
@@ -159,13 +156,19 @@
end
result
end
+ class << self
+ alias_method :parse_duration_string, :parse_time_string
+ end
+
#
- # returns true if the character c is a digit
+ # Returns true if the character c is a digit
#
+ # (probably better served by a regex)
+ #
def Rufus.is_digit? (c)
return false if not c.kind_of?(String)
return false if c.length > 1
(c >= "0" and c <= "9")
@@ -199,18 +202,10 @@
s,
o)
rescue Exception => e
- #puts
- #puts OpenWFE::exception_to_s(e)
- #puts
- #puts \
- # "\n Date.new() problem. Params :"+
- # "\n....y:#{time.year} M:#{time.month} d:#{time.day} "+
- # "h:#{time.hour} m:#{time.min} s:#{s} o:#{o}"
-
DateTime.new(
time.year,
time.month,
time.day,
time.hour,
@@ -237,86 +232,130 @@
end
#
# Turns a number of seconds into a a time string
#
- # Rufus.to_time_string 0 # => '0s'
- # Rufus.to_time_string 60 # => '1m'
- # Rufus.to_time_string 3661 # => '1h1m1s'
- # Rufus.to_time_string 7 * 24 * 3600 # => '1w'
- # Rufus.to_time_string 30 * 24 * 3600 + 1 # => "4w2d1s"
+ # 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_time_string 0.051 #=>"51"
- # Rufus.to_time_string 7.051 #=>"7s51"
- # Rufus.to_time_string 0.120 + 30 * 24 * 3600 + 1 #=>"4w2d1s120"
+ # 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()).
#
- def Rufus.to_time_string (seconds, months=false)
+ # 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
+ #
+ def Rufus.to_duration_string (seconds, options={})
return '0s' if seconds <= 0
- ms = nil
+ h = to_duration_hash seconds, options
- if seconds.is_a?(Float)
- ms = (seconds % 1 * 1000).to_i
- seconds = seconds.to_i
+ s = DU_KEYS.inject("") do |r, key|
+ count = h[key]
+ count = nil if count == 0
+ r << "#{count}#{key}" if count
+ r
end
- durations = months ? DURATIONS2w : DURATIONS2
+ ms = h[:ms]
+ s << ms.to_s if ms
- s = durations.inject([ seconds, "" ]) { |r, d|
+ s
+ end
- #puts "result : " + r.inspect
- #puts "duration : " + d.inspect
+ class << self
+ alias_method :to_time_string, :to_duration_string
+ end
- sec, str = r
- s, d = d
+ #
+ # 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
+ #
+ def Rufus.to_duration_hash (seconds, options={})
- count = sec / d
- rest = sec % d
+ h = {}
- str << "#{count}#{s}" if count > 0
+ if seconds.is_a?(Float)
+ h[:ms] = (seconds % 1 * 1000).to_i
+ seconds = seconds.to_i
+ end
- [ rest, str ]
- }[1]
+ if options[:drop_seconds]
+ h.delete :ms
+ seconds = (seconds - seconds % 60)
+ end
- "#{s}#{ms}"
+ durations = options[: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
protected
- DURATIONS = {
- "y" => 365 * 24 * 3600,
- "M" => 30 * 24 * 3600,
- "w" => 7 * 24 * 3600,
- "d" => 24 * 3600,
- "h" => 3600,
- "m" => 60,
- "s" => 1
- }
-
- DURATIONS2w = [
- [ "y", DURATIONS["y"] ],
- [ "M", DURATIONS["M"] ],
- [ "w", DURATIONS["w"] ],
- [ "d", DURATIONS["d"] ],
- [ "h", DURATIONS["h"] ],
- [ "m", DURATIONS["m"] ],
- [ "s", DURATIONS["s"] ]
+ DURATIONS2M = [
+ [ "y", 365 * 24 * 3600 ],
+ [ "M", 30 * 24 * 3600 ],
+ [ "w", 7 * 24 * 3600 ],
+ [ "d", 24 * 3600 ],
+ [ "h", 3600 ],
+ [ "m", 60 ],
+ [ "s", 1 ]
]
- DURATIONS2 = DURATIONS2w.dup
+ DURATIONS2 = DURATIONS2M.dup
DURATIONS2.delete_at 1
+
+ DURATIONS = DURATIONS2M.inject({}) do |r, (k, v)|
+ r[k] = v
+ r
+ end
+
+ DU_KEYS = DURATIONS2M.collect { |k, v| k.to_sym }
end