lib/rufus/sc/rtime.rb in rufus-scheduler-2.0.6 vs lib/rufus/sc/rtime.rb in rufus-scheduler-2.0.7

- old
+ new

@@ -22,25 +22,41 @@ # Hecho en Costa Rica #++ require 'date' -#require 'parsedate' module Rufus + #-- + # + # keeping that as a note. + # + #require 'tzinfo' + #def time_zone(time) + # offset = time.utc_offset / 3600 + # offset = offset < 0 ? offset.to_s : "+#{offset}" + # TZInfo::Timezone.get("Etc/GMT#{offset}") + #end + #def timeshift(time, tz) + # tz = TZInfo::Timezone.get(tz) unless tz.is_a?(TZInfo::Timezone) + # t = tz.utc_to_local(time.getutc) + # Time.parse(t.to_s[0..-5]) + #end + #++ + # Returns the current time as an ISO date string # def Rufus.now - to_iso8601_date(Time.new()) + to_iso8601_date(Time.new) end # As the name implies. # - def Rufus.to_iso8601_date (date) + 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) @@ -53,24 +69,24 @@ s end # the old method we used to generate our ISO datetime strings # - def Rufus.time_to_iso8601_date (time) + def Rufus.time_to_iso8601_date(time) - s = time.getutc().strftime(TIME_FORMAT) + 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 # Returns a Ruby time # - def Rufus.to_ruby_time (sdate) + def Rufus.to_ruby_time(sdate) DateTime.parse(sdate) end # Equivalent to java.lang.System.currentTimeMillis() @@ -97,16 +113,16 @@ # # 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 "1000" # => 1.0 # Rufus.parse_time_string "1h" # => 3600.0 - # Rufus.parse_time_string "1h10s" # => 3610.0 - # Rufus.parse_time_string "1w2d" # => 777600.0 + # Rufus.parse_time_string "1h10s" # => 3610.0 + # Rufus.parse_time_string "1w2d" # => 777600.0 # - def Rufus.parse_time_string (string) + def Rufus.parse_time_string(string) return string.to_f if FLOAT_DURATION.match(string) string = string.strip @@ -134,11 +150,11 @@ value = Integer(number) number = '' multiplier = DURATIONS[c] - raise "unknown time char '#{c}'" unless multiplier + raise ArgumentError.new("unknown time char '#{c}'") unless multiplier result = result + (value * multiplier) end result @@ -146,36 +162,30 @@ class << self alias_method :parse_duration_string, :parse_time_string end - # + #-- # conversion methods between Date[Time] and Time + #++ #-- # Ruby Cookbook 1st edition p.111 # http://www.oreilly.com/catalog/rubyckbk/ # a must #++ # Converts a Time instance to a DateTime one # - def Rufus.to_datetime (time) + 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) + DateTime.new(time.year, time.month, time.day, time.hour, time.min, s, o) rescue Exception => e DateTime.new( time.year, @@ -186,32 +196,32 @@ time.sec, time.utc_offset) end end - def Rufus.to_gm_time (dtime) + def Rufus.to_gm_time(dtime) to_ttime(dtime.new_offset, :gm) end - def Rufus.to_local_time (dtime) + def Rufus.to_local_time(dtime) to_ttime(dtime.new_offset(DateTime.now.offset-offset), :local) end - def Rufus.to_ttime (d, method) + 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 # 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 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. # @@ -223,12 +233,12 @@ # (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.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 : @@ -236,22 +246,22 @@ # * :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={}) + def Rufus.to_duration_string(seconds, options={}) return (options[:drop_seconds] ? '0m' : '0s') if seconds <= 0 h = to_duration_hash seconds, options - s = DU_KEYS.inject('') do |r, key| + s = DU_KEYS.inject('') { |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 @@ -278,11 +288,11 @@ # * :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={}) + def Rufus.to_duration_hash(seconds, options={}) h = {} if seconds.is_a?(Float) h[:ms] = (seconds % 1 * 1000).to_i @@ -311,22 +321,21 @@ # # duration_to_f("10s") # # will yield 10.0 # - def Rufus.duration_to_f (s) + 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 - # # Ensures an 'at' value is translated to a float # (to be compared with the float coming from time.to_f) # - def Rufus.at_to_f (at) + 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) @@ -358,8 +367,7 @@ r[k] = v r end DU_KEYS = DURATIONS2M.collect { |k, v| k.to_sym } - end