class Rufus::Scheduler::ZoTime
Zon{ing|ed}Time, whatever.
def self.envtzable?(s)
def self.envtzable?(s) TIMEZONES.include?(s) end
def self.is_timezone?(str)
def self.is_timezone?(str) return false if str == nil return false if str == '*' return false if str.index('#') # "sun#2", etc... On OSX would go all the way to true return true if Time.zone_offset(str) return !! (::TZInfo::Timezone.get(str) rescue nil) if defined?(::TZInfo) return true if TIMEZONES.include?(str) return true if TIMEZONEs.include?(str) t = ZoTime.new(0, str).time return false if t.zone == '' return false if t.zone == 'UTC' return false if t.utc_offset == 0 && str.start_with?(t.zone) # 3 common fallbacks... return false if RUBY_PLATFORM.include?('java') && ! envtzable?(str) true end
def self.parse(str, opts={})
def self.parse(str, opts={}) if defined?(::Chronic) && t = ::Chronic.parse(str, opts) return ZoTime.new(t, ENV['TZ']) end begin DateTime.parse(str) rescue raise ArgumentError, "no time information in #{o.inspect}" end if RUBY_VERSION < '1.9.0' zone = nil s = str.gsub(/\S+/) { |m| if envtzable?(m) zone ||= m '' else m end } return nil unless zone.nil? || is_timezone?(zone) zt = ZoTime.new(0, zone || ENV['TZ']) zt.in_zone { zt.seconds = Time.parse(s).to_f } zt.seconds == nil ? nil : zt end
def add(s)
def add(s) @seconds += s.to_f end
def in_zone(&block)
def in_zone(&block) current_timezone = ENV['TZ'] ENV['TZ'] = @zone block.call ensure ENV['TZ'] = current_timezone end
def initialize(s, zone)
def initialize(s, zone) @seconds = s.to_f @zone = zone end
def substract(s)
def substract(s) @seconds -= s.to_f end
def time
def time in_zone do t = Time.at(@seconds) if t.isdst t1 = Time.at(@seconds + 3600) t = t1 if t.zone != t1.zone && t.hour == t1.hour && t.min == t1.min # ambiguous TZ (getting out of DST) else t.hour # force t to compute itself end t end end
def to_f
def to_f @seconds end
def utc
def utc time.utc end