Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Understanding US/Pacific #6
How do I get rails time zone from "US/Pacific"?
I get obsolete time zones in an api I have exposed to iOS and I have no way to convert "US/Pacific" to rails ActiveSupport::TimeZone unlike "America/Los_Angeles" which I can understand through mapping it provides.
You should be able to access the access the US/Pacific zone using
Any zone identifier that isn't included in the ActiveSupport mapping will be looked up in TZInfo (it calls
Are you using an old Rails release? Rails version 2.3 and earlier included a cut-down version of TZInfo with just the zones required for the ActiveSupport mapping (therefore omitting US/Pacific). To use zones that aren't defined in ActiveSupport, you'll need to install the tzinfo gem. Version 0.3.38 is the latest version of tzinfo that is compatible with Rails 2.3.
I don't think there would be a straight-forward way to do this in the general case.
If you're only using a limited number of zones, you may be best off manually working out what the best equivalent zone in Rails is for each of them.
If you check the IANA time zone database, you'll see that US/Pacific is just an alias for America/Los_Angeles (see https://github.com/eggert/tz/blob/master/backward). In other cases though, you may be using time zones that have no equivalent in Rails (either because the current rules are different or because historical rules are different).
The following function will return the target of a linked zone:
require 'tzinfo' def linked_to(identifier) zone = TZInfo::Timezone.get(identifier) if zone.kind_of?(TZInfo::LinkedTimezone) link = zone.send(:info).link_to_identifier linked_to(link) || link else nil end end
> linked_to('US/Pacific') # a linked zone, returns the target of the link => "America/Los_Angeles" > linked_to('America/Los_Angeles') # a fully defined zone, returns nil => nil
The code will work with either TZInfo version 0.3.x, or with TZinfo >= 1.0.0 when TZInfo::Data (the tzinfo-data gem) is also installed.
Please note that this uses undocumented and unsupported interfaces in TZInfo, so I would only recommend using it as a one off (i.e. please don't rely on it continuing to work with future TZInfo releases).