Is there a way to get the TZInfo style string for the local time zone?

I need to try to get a TZInfo a-la 'America / New_York' style string representing the local time zone of the system I'm on. I can’t figure out how to do this.

Time.zone

#<ActiveSupport::TimeZone:0x007ff2e4f89240 @name="UTC", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: Etc/UTC>, @current_period=#<TZInfo::TimezonePeriod: nil,nil,#<TZInfo::TimezoneOffsetInfo: 0,0,UTC>>>>

Here the TimezoneProxy # Etc / UTC field is the style I want, but UTC is not local time.

Time.now.zone

"EST"

Here, “EST” is not what I want, and I don’t see a way to pass Time.now or EST to TZInfo to get what I want?

Is there a way to get "America / New_York" or even a list of all equivalent timezone lines based on my current timezone?

+4
source share
1 answer

EST

current_tz = ActiveSupport::TimeZone['EST']
ActiveSupport::TimeZone.
  all.
  select{|tz| tz.utc_offset == current_tz.utc_offset }.
  map(&:tzinfo).
  map(&:name).
  uniq

["America/Bogota", "EST", "America/New_York", "America/Indiana/Indianapolis", "America/Lima"]

, , - . :

current_tz = ActiveSupport::TimeZone['EST']
ActiveSupport::TimeZone.
  all.
  select{|tz| 
    tz.utc_offset == current_tz.utc_offset && 
      tz.tzinfo.current_period.dst? == current_tz.tzinfo.current_period.dst? 
  }.
  map(&:tzinfo).
  map(&:name).
  uniq

["America/Bogota", "EST", "America/Lima"] 
0

All Articles