How to Convert TZInfo identifier to Rails TimeZone name/key

TheEricMiller picture TheEricMiller · Jan 8, 2013 · Viewed 11.1k times · Source

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 Time.zone Documentation
http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html#method-i-parse

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


Using js packaged gem 'temporal-rails' to detect users timezone:
https://github.com/jejacks0n/temporal

User Time_Zone implement as seen:
http://railscasts.com/episodes/106-time-zones-revised

*Using Devise & Devise-Inevitable


Sign-Up View Script

    <script>
    $(function() {
        var detected_zone = Temporal.detect();
        console.log(detected_zone);  // returns object
        detected_zone = detected_zone.timezone.name;
        console.log(detected_zone);  // returns "America/New_York"
        $('#user_time_zone').val(detected_zone);  // ! need to convert this to rails TimeZone name !
    });
    </script>

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 %>

Answer

jejacks0n picture jejacks0n · Jan 9, 2013

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