No support for timezone abbreviations #38

Closed
brauliobo opened this Issue Jun 10, 2015 · 6 comments

Comments

Projects
None yet
3 participants
@brauliobo

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 Time.zone = 'BRT' (brazilian time), in Rails.

@philr

This comment has been minimized.

Show comment
Hide comment
@philr

philr Jun 10, 2015

Member

Please note that Time.zone is a feature of Rails' ActiveSupport - it is not provided by TZInfo. ActiveSupport uses TZInfo internally to implement support for time zones.

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).

Member

philr commented Jun 10, 2015

Please note that Time.zone is a feature of Rails' ActiveSupport - it is not provided by TZInfo. ActiveSupport uses TZInfo internally to implement support for time zones.

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).

@philr philr closed this Jun 10, 2015

@brauliobo

This comment has been minimized.

Show comment
Hide comment
@brauliobo

brauliobo Jun 10, 2015

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:
tz = TZInfo::Timezone.get('AMT') would return an array of the two possible values from the wikipedia link.

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:
tz = TZInfo::Timezone.get('AMT') would return an array of the two possible values from the wikipedia link.

@philr

This comment has been minimized.

Show comment
Hide comment
@philr

philr Jun 10, 2015

Member

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 Timezone.all or Timezone.all_country_zones to return lists of time zones.

Member

philr commented Jun 10, 2015

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 Timezone.all or Timezone.all_country_zones to return lists of time zones.

@brauliobo

This comment has been minimized.

Show comment
Hide comment

Thanks @philr, agreed.

As a reference, I send the related issue to this: http://stackoverflow.com/questions/30756405/cant-set-timezone-using-abbreviation

@davispuh

This comment has been minimized.

Show comment
Hide comment
@davispuh

davispuh Jun 10, 2015

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]

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]
@brauliobo

This comment has been minimized.

Show comment
Hide comment

nice @davispuh !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment