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
No support for timezone abbreviations #38
Some systems (android, some browsers, etc) report timezone using its abbreviation. The list of all abbreviations is at http://en.wikipedia.org/wiki/List_of_time_zone_abbreviations
Is it possible to support them? I've noticed there is no support for them when trying to do
Please note that
As the link you've posted shows, time zone abbreviations are not unique (for example, IST), so they cannot be used on their own to identify a time zone. There are also time zones in the list that have multiple different abbreviations (for example, AEST and EST for Australia's Eastern Standard Time).
TZInfo doesn't have any way of mapping an abbreviation to a time zone. Due to the problems with abbreviations outlined above, I don't have any plans to add such a feature to TZInfo.
You can use TZInfo to retrieve the abbreviation in use for a time zone at a given time, for example:
require 'tzinfo' tz = TZInfo::Timezone.get('America/Sao_Paulo') tz.period_for_utc(Time.utc(2015,7,1)).abbreviation.to_s # returns 'BRT' tz.period_for_utc(Time.utc(2015,1,1)).abbreviation.to_s # returns 'BRST'
This will just give the single abbreviation specified by the IANA Time Zone Database rather than all the abbreviations in common (so will give just AEST/AEDT for Australia/Sydney from the current version of the database).
I agree the main problem is non uniqueness. And it is good to know TZInfo is already storing abbreviations.
But maybe in non unique cases, the abbreviation could return an a array of possible timezones? For example:
TZInfo can only determine the abbreviations used by a time zone by reading the file for that time zone. Locating a time zone by abbreviation would require opening all the time zone files and scanning through each of the abbreviations in each of the files.
Abbreviations used by time zones vary over time (for example, Europe/London has used BDST in the past, but now only uses GMT and BST). Some applications may only be interested in 'current' abbreviations (however that is defined), others may be interested in historical or future abbreviations.
There is also no guarantee that the abbreviations available in the IANA Time Zone Database match those in use elsewhere.
I would suggest that you build your own mapping. You can use TZInfo as a basis for constructing this mapping (see above). You can use
FYI, you might be interested checking out my gem TimezoneParser
you can get timezones or even offsets for abbreviations.
=> abbr = TimezoneParser::Abbreviation.new('AMT') => puts abbr.getTimezones # America/Boa_Vista # America/Campo_Grande # America/Cuiaba # America/Manaus # America/Porto_Velho # Asia/Yerevan => puts abbr.getOffsets # [-14400, 14400]