Posted By

fortuity on 05/31/07


Tagged

textmate rails countries timezones


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

jarjar2k7
tkmr
jatkins
webstic
schroedinger


Time Zones By Country Using Rails TZInfo


 / Published in: Ruby
 

  1. countries = Country.find(:all)
  2. for country in countries
  3. begin
  4. tzc = TZInfo::Country.get(country.code.upcase)
  5. rescue
  6. puts "can't find TZInfo for #{country.code.upcase}"
  7. next
  8. end
  9. puts tzc.name
  10. for zone in tzc.zone_names
  11. puts country.code.upcase
  12. puts zone
  13. CountryTimeZone.create(:parent_id => country.id, :name => zone)
  14. end
  15. end

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: fortuity on May 31, 2007

The class TZInfo::Country requires the tzinfo gem and TZInfo plugin (http://tzinfo.rubyforge.org). Country and CountryTimeZone classes are my own (subclassed from my Geography class). This snippet gets the two-letter country code for each country and uses it to find all the time zones for the country and then creates a new CountryTimeZone instance for each, specifying the country as a parent_id so we can do a tree list of countries and their "children" time zones. This snippet is run from the console and populates a data table.

You need to login to post a comment.