3

I'm trying to determine the timezone of a country (from 2-letter ISO3166 country code) using Ruby. I have tried the Ruby gem tzinfo

I can get the timezones of Brazil this way:

TZInfo::Country.get("BR").zone_identifiers
=> ["America/Noronha", "America/Belem", "America/Fortaleza", "America/Recife", "America/Araguaina", "America/Maceio", "America/Bahia", "America/Sao_Paulo", "America/Campo_Grande", "America/Cuiaba", "America/Santarem", "America/Porto_Velho", "America/Boa_Vista", "America/Manaus", "America/Eirunepe", "America/Rio_Branco"] 

Brazil has several timezones. The list above is sorted based on proximity to UTC. In this case, east to west. We want to get the main timezone, by which I mean the timezone where the capital city of the country is located. "America/Noronha" is the timezone of an island in the Atlantic. We want to get the timezone of Brasilia (the capital city of Brazil) in this case.

Based on tzinfo documentation, I can get a list of timezones "ordered by importance according to the DataSource" using class TZInfo::CountryInfo (not sure what DataSource is)

But this doesn't seem to work:

>> x=TZInfo::CountryInfo.new('BR', 'Brazil')
=> #<TZInfo::CountryInfo: BR>
>> x.zones
=> []
>> x.zone_identifiers
=> []

So how do you easily obtain the time zone where a country's capital is located? Are there any gems that support capital city's timezone?

Thank you.

Zack Xu
  • 11,505
  • 9
  • 70
  • 78

2 Answers2

1

You have to set country before searching for zones. This should solve your problem:

x=TZInfo::CountryInfo.new('BR', 'Brazil')
=> #<TZInfo::CountryInfo: BR>
y=TZInfo::Country.get(x.code)
=> #<TZInfo::Country: BR>
y.zones
=> [#<TZInfo::TimezoneProxy: America/Noronha>, #<TZInfo::TimezoneProxy: America/Belem>, #<TZInfo::TimezoneProxy: America/Fortaleza>, #<TZInfo::TimezoneProxy: America/Recife>, #<TZInfo::TimezoneProxy: America/Araguaina>, #<TZInfo::TimezoneProxy: America/Maceio>, #<TZInfo::TimezoneProxy: America/Bahia>, #<TZInfo::TimezoneProxy: America/Sao_Paulo>, #<TZInfo::TimezoneProxy: America/Campo_Grande>, #<TZInfo::TimezoneProxy: America/Cuiaba>, #<TZInfo::TimezoneProxy: America/Santarem>, #<TZInfo::TimezoneProxy: America/Porto_Velho>, #<TZInfo::TimezoneProxy: America/Boa_Vista>, #<TZInfo::TimezoneProxy: America/Manaus>, #<TZInfo::TimezoneProxy: America/Eirunepe>, #<TZInfo::TimezoneProxy: America/Rio_Branco>]
y.zone_identifiers
=> ["America/Noronha", "America/Belem", "America/Fortaleza", "America/Recife", "America/Araguaina", "America/Maceio", "America/Bahia", "America/Sao_Paulo", "America/Campo_Grande", "America/Cuiaba", "America/Santarem", "America/Porto_Velho", "America/Boa_Vista", "America/Manaus", "America/Eirunepe", "America/Rio_Branco"]
Yaro
  • 570
  • 3
  • 20
0

We want to get the main timezone, which can be defined as the timezone of the capital city.

Uhhh.. no. Is the "main" time zone of the United States "Eastern Time"? Folks in California or Hawaii would certainly disagree.

Likewise for Russia, Australia, Brazil, and many other places in the world.

You're working from a false assumption. There is no such thing as the "main" time zone for a country. A country either has one time zone, or they have multiple time zones, but there's nothing that correlates the capital city to the "main" time zone.


EDIT

Based on your changes and our discussion, I now understand you want specifically to know the time zone for the capital city of a country. Since there's nothing in the underlying time zone data to distinguish which time zone the capital city falls into, then you will need additional information to solve this problem. In general, the steps would be as follows:

  1. Determine the capital city for a country. You'll need a table of country codes to their capitals to do this.

  2. Determine the latitude and longitude of the capital city. You might find this in the same source as the country-to-capital data, or you may have to use another data source.

  3. Use one of the methods listed here to obtain the IANA time zone identifier from the latitude and longitude coordinates.

  4. Use the IANA time zone identifier with the TZInfo gem.

So: Brazil -> Brasília -> Latitude: -15.7797200, Longitude: -47.9297200 -> America/Sao_Paulo

It's possible some of this info is already in Ruby or in Rails. I'm not a Rails developer, and I haven't looked. You might also find it via a web service or a publicly available data table or csv file, etc.

You might also just decide to let a web service do all this work for you. Some of the APIs in that answer in step 3 can accept an arbitrary location instead of lat/lon.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • I qualified "main timezone" by an unambiguous, non-debatable definition. That is, the timezone of the capital city. In practice, this works: EST is more dominant in the US, as is Moscow time in Russia, and Canberra time in Australia. I'm interested in a technical solution, not debate about preponderance of certain timezones in certain countries. Thank you. – Zack Xu Feb 26 '14 at 15:59
  • What is the basis for your assumption that EST is more dominant in the US than the other time zones? That's just not true. – Matt Johnson-Pint Feb 26 '14 at 16:02
  • If you'd like to avoid this debate, then don't start by making an assumption. It would be valid to ask for how to obtain the time zone for the capital city. But it's not valid to assume that the capital city is the "main" time zone. I can tell you now though, that there's nothing in the underlying time zone data that distinguishes a capital city from the others. In fact, the cities are typically chosen based on the most populated locations, rather than the capitals. It's likely that the capital city won't even be a valid time zone. For example, there is no `America/Washington_DC` zone. – Matt Johnson-Pint Feb 26 '14 at 16:03
  • That is precisely my question: how to obtain the timezone where a country's capital city is located. – Zack Xu Feb 26 '14 at 16:11
  • Thank you for your answer. But I'm looking for an answer in specific Ruby code. For example, which ruby gem allows converting from country code to capital city name. – Zack Xu Feb 27 '14 at 10:36