In Ruby, with ActiveSupport and TZInfo present, I’m trying to parse arbitrary time strings that can include a timezone identifier.
Handling the timezone when given as an offset (e.g., '-08:00') is no problem. And both styles of long names seem to work (e.g., 'America/Vancouver', 'Pacific Time (US & Canada)'), but the common abbreviations, and their daylight-savings alternates, mostly fail—with a few exceptions.
Looking at the List of time zone abbreviations on Wikipedia, there are 190 distinct abbreviations. When I test each one of them with TZInfo::Timezone.get(abbrev) (where abbrev is a variable holding a given abbreviation string, such as 'MST'), only 9 are recognized: CET, EET, EST, GMT, HST, MET, MST, UTC, and WET.
For example:
> TZInfo::Timezone.get('MST')
=> #<TZInfo::DataTimezone: MST>
> TZInfo::Timezone.get('PST')
TZInfo::InvalidTimezoneIdentifier
So: Is there a method that will convert most, or all, of the common 3-5 character timezone abbreviations to a TZInfo or ActiveSupport timezone?
Or will I have to write my own conversion helper, trying to keep the conversion table up to date as timezone policies change around the world?
(I do recognize that the abbreviations are not 100% reliable or authoritative—especially as there are a few cases where the same abbreviation refers to multiple timezones with different offsets—but I’m still needing to make a “best guess” rather than none.)
tzinfo describes where the data it uses comes from:
The timezone definition files are often located in
/usr/share/zoneinfo. On my machine there is nothing in this directory about, for example,PMDT.The tzinfo-data is here, doesn't have
PMDTeither.If you locate a suitable set of timezone info definition files that contains the zones you want to query,
tzinfoshould in theory be able to use them.