There is already a question which answers how to convert "America/Los_Angeles" to "Pacific Time (US & Canada)". However I want to convert "US/Pacific" and other obsolete time zones to Rails TimeZone. I am unable to find anything in the library which helps me accomplish this.
3 Answers
From the Rails ActiveSupport::TimeZone docs:
The version of TZInfo bundled with Active Support only includes the definitions necessary to support the zones defined by the TimeZone class. If you need to use zones that aren’t defined by TimeZone, you’ll need to install the TZInfo gem (if a recent version of the gem is installed locally, this will be used instead of the bundled version.)
Personally, I think Rails time zones are silly. They are arbitrarily limited to what Rails describes as "a meaningful subset of 146 zones". But they do nothing to explain how they determine which zones are "meaningful" and which are discarded.
Identifiers such as "US/Pacific" are known as "links" or "aliases" in the TZDB data. I could see that perhaps they might be omitted from Rails, but there are many other real-world time zones that are missing entirely.
The only place I've every seen Rails time zones identifiers in the real world is in the Twitter API. On the other hand, IANA/TZDB identifiers are ubiquitous.
The best advice I can offer is to forget about Rails time zones, and just use the Ruby TZInfo Gem directly. It has the full TZDB implementation with all zones, including aliases.
If you really must continue to use Rails zones, you could first use TZInfo to resolve the alias to the actual zone, and then see if that that zone is in the Rails MAPPING
dictionary. For example:
"US/Pacific" => "America/Los_Angeles" (via TZInfo)
"America/Los_Angeles" => "Pacific Time (US & Canada)" (via Rails MAPPING)
I believe when you load "US/Pacific" via TZInfo, it will be subclassed as a LinkedTimezoneInfo
object, so you could use the link_to_identifier
attribute from there.

- 230,703
- 74
- 448
- 575
-
````TZInfo::Timezone.get('US/Pacific').link_to_identifier````NoMethodError. – ajbraus Aug 06 '14 at 14:50
-
@ajbraus - `America/Vancouver` is a distinct zone, not a link. So it's not going to have a `link_to_identifier` attribute. But alas, it's not a zone that's in Rails's list of "meaningful" zones. So there's not much you'd be able to do here, except use the TZInfo gem exclusively and *not* try to map them back to Rails zones. – Matt Johnson-Pint Aug 06 '14 at 14:55
-
@ajbraus - saw you updated your comment to show `US/Pacific` - that one is indeed a link. – Matt Johnson-Pint Aug 06 '14 at 14:57
-
@ajbraus - I'm not fluent with Ruby, but perhaps that's just not the right syntax for retrieving an attribute. – Matt Johnson-Pint Aug 06 '14 at 14:58
-
2Note that [rails 5.0.1 ships with tzinfo and tzinfo-data](https://github.com/rails/rails/commit/9d664b18df7a81b75e45bb99858f920de040e9fb). – Dennis Feb 11 '17 at 19:43
You can do it by this:
arr = TZInfo::Timezone.all_country_zone_identifiers.collect { |x| x + " " + ActiveSupport::TimeZone[x].formatted_offset }
arr.sort

- 49
- 2
In this command will display the 144 timezone names.
ActiveSupport::TimeZone.zones_map

- 242
- 2
- 8