lib/et-orbi.rb in et-orbi-1.0.8 vs lib/et-orbi.rb in et-orbi-1.0.9

- old
+ new

@@ -5,11 +5,11 @@ require 'tzinfo' module EtOrbi - VERSION = '1.0.8' + VERSION = '1.0.9' # # module methods class << self @@ -60,11 +60,11 @@ secs = if str_zone local.to_f else - zone.period_for_local(local).to_utc(local).to_f + zone.local_to_utc(local).to_f end #p [ :parse, :secs, secs ] EoTime.new(secs, zone) end @@ -123,11 +123,10 @@ make_from_string(s, zone) end def make_from_string(s, zone) -#p [ :mfs, s, zone ] parse(s, zone: zone) end def make_from_numeric(f, zone) @@ -157,10 +156,13 @@ (::TZInfo::Timezone.get(o) rescue nil) end def local_tzone + @local_tzone_tz ||= nil + @local_tzone_loaded_at ||= nil + @local_tzone = nil \ if @local_tzone_loaded_at && (Time.now > @local_tzone_loaded_at + 1800) @local_tzone = nil \ if @local_tzone_tz != ENV['TZ'] @@ -508,15 +510,16 @@ protected # Returns a Ruby Time instance. # - # Warning: the timezone of that Time instance will be UTC. + # Warning: the timezone of that Time instance will be UTC when used with + # TZInfo < 2.0.0. # def to_time - @time ||= begin; u = utc; @zone.period_for_utc(u).to_local(u); end + @time ||= begin; u = utc; @zone.utc_to_local(u); end end def count_weeks(dir) c = 0 @@ -542,20 +545,17 @@ sn = off < 0 ? '-' : '+'; off = off.abs hr = off / 3600 mn = (off % 3600) / 60 sc = 0 - fmt = - if code == '%z' - "%s%02d%02d" - elsif code == '%:z' - "%s%02d:%02d" - else - "%s%02d:%02d:%02d" - end - - fmt % [ sn, hr, mn, sc ] + if code == '%z' + '%s%02d%02d' % [ sn, hr, mn ] + elsif code == '%:z' + '%s%02d:%02d' % [ sn, hr, mn ] + else + '%s%02d:%02d:%02d' % [ sn, hr, mn, sc ] + end end def _to_f(o) fail ArgumentError( @@ -658,29 +658,46 @@ hr = nil if hr.abs > 11 hr = nil if mn > 59 mn = -mn if hr && hr < 0 return ( - @custom_tz_cache[str] = - begin - tzi = TZInfo::TransitionDataTimezoneInfo.new(str) - tzi.offset(str, hr * 3600 + mn * 60, 0, str) - tzi.create_timezone - end + @custom_tz_cache[str] = create_offset_tzone(hr * 3600 + mn * 60, str) ) if hr nil end + if defined? TZInfo::DataSources::ConstantOffsetDataTimezoneInfo + # TZInfo >= 2.0.0 + + def create_offset_tzone(utc_off, id) + + off = TZInfo::TimezoneOffset.new(utc_off, 0, id) + tzi = TZInfo::DataSources::ConstantOffsetDataTimezoneInfo.new(id, off) + tzi.create_timezone + end + + else + # TZInfo < 2.0.0 + + def create_offset_tzone(utc_off, id) + + tzi = TZInfo::TransitionDataTimezoneInfo.new(id) + tzi.offset(id, utc_off, 0, id) + tzi.create_timezone + end + + end + def determine_local_tzones tabbs = (-6..5) .collect { |i| (Time.now + i * 30 * 24 * 3600).zone } .uniq .sort t = Time.now - tu = t.dup.utc # /!\ dup is necessary, #utc modifies its target + #tu = t.dup.utc # /!\ dup is necessary, #utc modifies its target twin = Time.utc(t.year, 1, 1) # winter tsum = Time.utc(t.year, 7, 1) # summer ::TZInfo::Timezone.all.select do |tz|