How to Convert TZInfo identifier to Rails TimeZone name/key

The magic I believe everyone is really after, and solves the problem that @ajbraus raised, is a one-liner that could be one of the strangely hardest things to find discussed anywhere:

timezone = ActiveSupport::TimeZone[TZInfo::Timezone.get('America/Vancouver').period_for_utc(Time.now.utc).utc_offset]
 => #<ActiveSupport::TimeZone:0x00007fc2baa6d900 @name="Pacific Time (US & Canada)", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: America/Los_Angeles>> 

Otherwise, if you try searching through ActiveSupport's entire DB of zones, you get nada:

ActiveSupport::TimeZone.all.find{ |tz| tz.tzinfo == ActiveSupport::TimeZone.find_tzinfo('America/Vancouver') }
 => nil

The complexity boils down to the following:

  • TZInfo gem is rigorously adhering to the IANA specification of timezones
  • Rails' ActiveSupport decided to try and simplify display/usage of zones but isn't an exhaustive DB of zones
  • ActiveSupport can look up an appropriate zone if you give it an offset
  • TZInfo doesn't give you an offset unless you create a TZInfo::TimezonePeriod in the zone of interest
  • Ruby 2.6 is apparently revisiting timezone support to clean this business up

Temporal includes the needed logic, but to answer your question:

Time.zone = ActiveSupport::TimeZone.new("America/New_York")

Edit, I guess my answer is incomplete. You want to get it from "America/New_York" to "Eastern Time (US & Canada)", correct? If that's the case this is the best solution I have -- though someone may be able to provide a better one.

ActiveSupport::TimeZone::MAPPING.select {|k, v| v == "America/New_York" }.keys.first