lib/et-orbi.rb in et-orbi-1.1.1 vs lib/et-orbi.rb in et-orbi-1.1.2

- old
+ new

@@ -2,14 +2,16 @@ require 'date' if RUBY_VERSION < '1.9.0' require 'time' require 'tzinfo' +require 'et-orbi/zone_aliases' + module EtOrbi - VERSION = '1.1.1' + VERSION = '1.1.2' # # module methods class << self @@ -101,11 +103,11 @@ z ||= t.zone # pass the abbreviation anyway, # it will be used in resulting the error message - EoTime.new(t.to_f, z) + EoTime.new(t, z) end def make_from_date(d, zone) make_from_time( @@ -149,19 +151,20 @@ o = to_offset(o) if o.is_a?(Numeric) return nil unless o.is_a?(String) - (@custom_tz_cache ||= {})[o] || - get_offset_tzone(o) || - (::TZInfo::Timezone.get(o) rescue nil) + s = unalias(o) + + get_offset_tzone(s) || + (::TZInfo::Timezone.get(s) rescue nil) end def render_nozone_time(seconds) t = - Time.utc(0) + seconds + Time.utc(1970) + seconds ts = t.strftime('%Y-%m-%d %H:%M:%S') + ".#{(seconds % 1).to_s.split('.').last}" tz = EtOrbi.determine_local_tzone @@ -173,26 +176,30 @@ def platform_info etos = Proc.new { |k, v| "#{k}:#{v.inspect}" } - '(' + - { - 'etz' => ENV['TZ'], - 'tnz' => Time.now.zone, - 'tzid' => defined?(TZInfo::Data), - 'rv' => RUBY_VERSION, - 'rp' => RUBY_PLATFORM, - 'rorv' => (Rails::VERSION::STRING rescue nil), - 'astz' => ([ Time.zone.class, Time.zone.tzinfo.name ] rescue nil), - 'eov' => EtOrbi::VERSION, - 'eotnz' => EtOrbi::EoTime.now.zone, - 'eotnfz' => EtOrbi::EoTime.now.strftime('%z'), - 'eotlzn' => EtOrbi::EoTime.local_tzone.name, - }.collect(&etos).join(',') + ',' + - gather_tzs.collect(&etos).join(',') + - ')' + h = { + 'etz' => ENV['TZ'], + 'tnz' => Time.now.zone, + 'tzid' => defined?(TZInfo::Data), + 'rv' => RUBY_VERSION, + 'rp' => RUBY_PLATFORM, + 'win' => Gem.win_platform?, + 'rorv' => (Rails::VERSION::STRING rescue nil), + 'astz' => ([ Time.zone.class, Time.zone.tzinfo.name ] rescue nil), + 'eov' => EtOrbi::VERSION, + 'eotnz' => '???', + 'eotnfz' => '???', + 'eotlzn' => '???' } + if ltz = EtOrbi::EoTime.local_tzone + h['eotnz'] = EtOrbi::EoTime.now.zone + h['eotnfz'] = EtOrbi::EoTime.now.strftime('%z') + h['eotlzn'] = ltz.name + end + + "(#{h.map(&etos).join(',')},#{gather_tzs.map(&etos).join(',')})" end alias make make_time # For `make info` @@ -299,11 +306,11 @@ @seconds = s.to_f @zone = self.class.get_tzone(zone || :local) fail ArgumentError.new( "Cannot determine timezone from #{zone.inspect}" + - "\n#{EtOrbi.render_nozone_time(s)}" + + "\n#{EtOrbi.render_nozone_time(@seconds)}" + "\n#{EtOrbi.platform_info.sub(',debian:', ",\ndebian:")}" + "\nTry setting `ENV['TZ'] = 'Continent/City'` in your script " + "(see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)" + (defined?(TZInfo::Data) ? '' : "\nand adding gem 'tzinfo-data'") ) unless @zone @@ -326,19 +333,20 @@ # Returns this ::EtOrbi::EoTime as a ::Time instance # in the current UTC timezone. # def utc - Time.utc(1970, 1, 1) + @seconds + Time.utc(1970) + @seconds end # Returns true if this ::EtOrbi::EoTime instance timezone is UTC. # Returns false else. # def utc? - %w[ zulu utc gmt ].include?(@zone.canonical_identifier.downcase) + %w[ gmt utc zulu etc/gmt etc/utc ].include?( + @zone.canonical_identifier.downcase) end alias getutc utc alias getgm utc alias to_utc_time utc @@ -614,11 +622,11 @@ def determine_local_tzone etz = ENV['TZ'] - tz = ::TZInfo::Timezone.get(etz) rescue nil + tz = etz && (::TZInfo::Timezone.get(etz) rescue nil) return tz if tz if Time.respond_to?(:zone) && Time.zone.respond_to?(:tzinfo) tz = Time.zone.tzinfo return tz if tz @@ -626,18 +634,48 @@ tz = ::TZInfo::Timezone.get(os_tz) rescue nil return tz if tz tzs = determine_local_tzones - (etz && tzs.find { |z| z.name == etz }) || tzs.first + tz = (etz && tzs.find { |z| z.name == etz }) || tzs.first + return tz if tz + + n = Time.now + + get_tzone(n.zone) || + get_tzone(n.strftime('%Z%z')) end + attr_accessor :_os_zone # test tool + def os_tz + return (@_os_zone == '' ? nil : @_os_zone) if @_os_zone + @os_tz ||= (debian_tz || centos_tz || osx_tz) end + def to_windows_tz(zone_name, time=Time.now) + + twin = Time.utc(time.year, 1, 1) # winter + tsum = Time.utc(time.year, 7, 1) # summer + + tz = ::TZInfo::Timezone.get(zone_name) + tzo = tz.period_for_local(time).utc_total_offset + tzop = tzo < 0 ? nil : '-'; tzo = tzo.abs + tzoh = tzo / 3600 + tzos = tzo % 3600 + tzos = tzos == 0 ? nil : ':%02d' % (tzos / 60) + + abbs = [ + tz.period_for_utc(twin).abbreviation.to_s, + tz.period_for_utc(tsum).abbreviation.to_s ] + .uniq + + [ abbs[0], tzop, tzoh, tzos, abbs[1] ].compact.join + end + # # protected module methods protected @@ -662,11 +700,12 @@ hr = nil if hr.abs > 11 hr = nil if mn > 59 mn = -mn if hr && hr < 0 return ( - @custom_tz_cache[str] = create_offset_tzone(hr * 3600 + mn * 60, str) + (@custom_tz_cache ||= {})[str] = + create_offset_tzone(hr * 3600 + mn * 60, str) ) if hr nil end @@ -692,30 +731,35 @@ end def determine_local_tzones tabbs = (-6..5) - .collect { |i| (Time.now + i * 30 * 24 * 3600).zone } + .collect { |i| + t = Time.now + i * 30 * 24 * 3600 + "#{t.zone}_#{t.utc_offset}" } .uniq .sort .join('|') t = Time.now #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 + twin = Time.local(t.year, 1, 1) # winter + tsum = Time.local(t.year, 7, 1) # summer @tz_all ||= ::TZInfo::Timezone.all @tz_winter_summer ||= {} @tz_winter_summer[tabbs] ||= @tz_all .select { |tz| + pw = tz.period_for_local(twin) + ps = tz.period_for_local(tsum) tabbs == - [ - tz.period_for_utc(twin).abbreviation.to_s, - tz.period_for_utc(tsum).abbreviation.to_s - ].uniq.sort.join('|') } + [ "#{pw.abbreviation}_#{pw.utc_total_offset}", + "#{ps.abbreviation}_#{ps.utc_total_offset}" ] + .uniq.sort.join('|') } + + @tz_winter_summer[tabbs] end # # system tz determination