class Rufus::Scheduler::ZoTime

Zon{ing|ed}Time, whatever.

Attributes

seconds[RW]
zone[RW]

Public Class Methods

envtzable?(s) click to toggle source

DELTA_TZ_REX = /A[0-1]:?[0-5]z/

# File lib/rufus/scheduler/zotime.rb, line 90
def self.envtzable?(s)

  TIMEZONES.include?(s)
end
is_timezone?(str) click to toggle source
# File lib/rufus/scheduler/zotime.rb, line 127
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
new(s, zone) click to toggle source
# File lib/rufus/scheduler/zotime.rb, line 38
def initialize(s, zone)

  @seconds = s.to_f
  @zone = zone
end
parse(str, opts={}) click to toggle source
# File lib/rufus/scheduler/zotime.rb, line 95
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
    fail 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

Public Instance Methods

add(s) click to toggle source
# File lib/rufus/scheduler/zotime.rb, line 73
def add(s)

  @seconds += s.to_f
end
in_zone(&block) click to toggle source
# File lib/rufus/scheduler/zotime.rb, line 154
def in_zone(&block)

  current_timezone = ENV['TZ']
  ENV['TZ'] = @zone

  block.call

ensure

  ENV['TZ'] = current_timezone
end
substract(s) click to toggle source
# File lib/rufus/scheduler/zotime.rb, line 78
def substract(s)

  @seconds -= s.to_f
end
time() click to toggle source
# File lib/rufus/scheduler/zotime.rb, line 44
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
      #
      # jump out of DST as soon as possible, jumps 1h as seen from UTC

    t.hour # force t to compute itself
      #
      # stay in DST as long as possible, no jump seen from UTC

    t
  end
end
to_f() click to toggle source
# File lib/rufus/scheduler/zotime.rb, line 83
def to_f

  @seconds
end
utc() click to toggle source
# File lib/rufus/scheduler/zotime.rb, line 68
def utc

  time.utc
end