lib/rufus/sc/rtime.rb in rufus-scheduler-2.0.17 vs lib/rufus/sc/rtime.rb in rufus-scheduler-2.0.18
- old
+ new
@@ -1,7 +1,7 @@
#--
-# Copyright (c) 2005-2011, John Mettraux, jmettraux@gmail.com
+# Copyright (c) 2005-2013, John Mettraux, jmettraux@gmail.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@@ -94,12 +94,10 @@
def Rufus.current_time_millis
(Time.new.to_f * 1000).to_i
end
- FLOAT_DURATION = /^\d*\.\d*$/
-
# 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
@@ -118,28 +116,39 @@
# 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
#
- def Rufus.parse_time_string(string)
+ def self.parse_time_string(string)
- if m = string.match(/^(\d*)\.?(\d*)([A-Za-z])(.*)$/)
+ return 0.0 if string == ''
- number = "#{m[1]}.#{m[2]}".to_f
- multiplier = DURATIONS[m[3]]
+ m = string.match(/^(-?)([\d\.#{DURATION_LETTERS}]+)$/)
- raise ArgumentError.new("unknown time char '#{m[3]}'") unless multiplier
+ raise ArgumentError.new("cannot parse '#{string}'") unless m
- return number * multiplier + parse_time_string(m[4])
+ mod = m[1] == '-' ? -1.0 : 1.0
+ val = 0.0
- else
+ s = m[2]
- return string.to_i / 1000.0 if string.match(/^\d+$/)
- return string.to_f if string.match(/^\d*\.?\d*$/)
-
- raise ArgumentError.new("cannot parse '#{string}'")
+ while s.length > 0
+ m = nil
+ if m = s.match(/^(\d+|\d+\.\d*|\d*\.\d+)([#{DURATION_LETTERS}])(.*)$/)
+ val += m[1].to_f * DURATIONS[m[2]]
+ elsif s.match(/^\d+$/)
+ val += s.to_i / 1000.0
+ elsif s.match(/^\d*\.\d*$/)
+ val += s.to_f
+ else
+ raise ArgumentError.new("cannot parse '#{string}' (especially '#{s}')")
+ end
+ break unless m && m[3]
+ s = m[3]
end
+
+ mod * val
end
class << self
alias_method :parse_duration_string, :parse_time_string
end
@@ -183,11 +192,11 @@
to_ttime(dtime.new_offset, :gm)
end
def Rufus.to_local_time(dtime)
- to_ttime(dtime.new_offset(DateTime.now.offset-offset), :local)
+ to_ttime(dtime.new_offset(DateTime.now.offset - offset), :local)
end
def Rufus.to_ttime(d, method)
usec = (d.sec_fraction * 3600 * 24 * (10**6)).to_i
@@ -230,11 +239,11 @@
#
def Rufus.to_duration_string(seconds, options={})
return (options[:drop_seconds] ? '0m' : '0s') if seconds <= 0
- h = to_duration_hash seconds, options
+ h = to_duration_hash(seconds, options)
s = DU_KEYS.inject('') { |r, key|
count = h[key]
count = nil if count == 0
r << "#{count}#{key}" if count
@@ -278,11 +287,11 @@
h[:ms] = (seconds % 1 * 1000).to_i
seconds = seconds.to_i
end
if options[:drop_seconds]
- h.delete :ms
+ h.delete(:ms)
seconds = (seconds - seconds % 60)
end
durations = options[:months] ? DURATIONS2M : DURATIONS2
@@ -315,23 +324,23 @@
#
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 = to_ruby_time(at) if at.is_a?(String)
+ at = 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
- protected
+ protected # well, somehow
DURATIONS2M = [
[ 'y', 365 * 24 * 3600 ],
[ 'M', 30 * 24 * 3600 ],
[ 'w', 7 * 24 * 3600 ],
@@ -341,13 +350,11 @@
[ 's', 1 ]
]
DURATIONS2 = DURATIONS2M.dup
DURATIONS2.delete_at(1)
- DURATIONS = DURATIONS2M.inject({}) do |r, (k, v)|
- r[k] = v
- r
- end
+ DURATIONS = DURATIONS2M.inject({}) { |r, (k, v)| r[k] = v; r }
+ DURATION_LETTERS = DURATIONS.keys.join
DU_KEYS = DURATIONS2M.collect { |k, v| k.to_sym }
end