How to Convert TZInfo identifier to Rails TimeZone name/key

How do you convert js values received as TZInfo identifiers to Rails TimeZone name/key?

FROM: "America/New_York" returned from JavaScript TZinfo detection TO: "Eastern Time (US & Canada)" convention used in Rails TimeZone

or another example: "Pacific/Honolulu" => converted to => "Hawaii"

Both are available in ActiveSupport::TimeZone < Object mapping but rails uses the key [i.g. "Eastern Time (US & Canada)"] in drop-downs, validation & storing to Time.use_zone().

Based on what I understand of ActiveSupport::TimeZone.us_zones this seems to be important especially incases of DayLights savings time (which rails sounds to handle well) and matching just the offset would not accomplish. If it is not stored to DB with the rails TimeZone name then validation fails and does not match up properly in the user's profile settings page with the dropdown list of ActiveSupport::TimeZone.zones_map

Goal of this is that the user does not have to select their timezone on signup or are required to change it in their settings after signup. The browser detects it and passes it to hidden_field on signup. In the rare occasion they sign up in a place different than their home/work. they can manually override in their account settings later.

Seems to be a common gap when trying to ingest js timezone detection. This might even become a secondary question of how to pass the returned info from js to rails for conversion and then back to js to store back in the hidden_field of the form? Hopefully I framed the question properly & admittedly a bit green with rails so there may be a simple solution to this...

Thanks so much for all the help! -E

ActiveSupport Documentation

MAPPING = {"Eastern Time (US & Canada)" => "America/New_York"

Using js packaged gem 'temporal-rails' to detect users timezone:

User Time_Zone implement as seen:

*Using Devise & Devise-Inevitable

Sign-Up View Script
    $(function() {
        var detected_zone = Temporal.detect();
        console.log(detected_zone);  // returns object
        detected_zone =;
        console.log(detected_zone);  // returns "America/New_York"
        $('#user_time_zone').val(detected_zone);  // ! need to convert this to rails TimeZone name !
User Model
    validates_inclusion_of :time_zone, in: ActiveSupport::TimeZone.zones_map(&:name)
User Account Settings Form
    <%= f.label :time_zone, label: "Time Zone" %><br />
    <%= f.time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones %>


Temporal includes the needed logic, but to answer your question: ="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. {|k, v| v == "America/New_York" }.keys.first

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(]
 => #<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

Need Your Help

Using JavaScript to display Laravel's Variable

javascript php laravel laravel-5

In my code I am using typeahead.js. I use Laravel 5 and I need to replace the var states with my {{ $jobs }} variable. I need to list all Job Titles as an array.

Remove row class from table -Jquery

jquery html

I have a table .Table id is resultTable. Some of the rows in the table have a class.