Reputation: 351
In my rails 4.0 application I have "on the go journeys" module, where user can save his journey while checking in or checking out from anywhere in the world. now, if suppose my friend has checked-in in a hotel in a London and his checked-in time according to London timezone is 8:00 Am and at the same time according to Indian timezone the time in India is 10:00 Am. Now, suppose I am logging into my application from India at 10:00 Am, then I should get message saying "your friend just checked_in in London, hotel xyz at 10:00 Am" i.e. timezone should be according to Indian timezone. currently I am able to get the user's country name with the help of IP address , and then I want to convert the timezone according to that country name. How can I do this ?
I have used this link for getting country name Getting a user country name from originating IP address with Ruby on Rails
Upvotes: 1
Views: 1457
Reputation: 11421
You'll need country code to use with Country class from TZInfo library that should be available in rails by default, I am wondering what you'll do with countries that has more than one time-zone, like Russia, US, Canada. Anyway, for a small country:
2.2.1 :001 > c = TZInfo::Country.get('MD')
=> #<TZInfo::Country: MD>
2.2.1 :002 > c.zone_identifiers
=> ["Europe/Chisinau"]
2.2.1 :003 > c.zones
=> [#<TZInfo::TimezoneProxy: Europe/Chisinau>]
2.2.1 :004 > c = TZInfo::Country.get('US')
for a bigger one:
2.2.1 :004 > c = TZInfo::Country.get('US')
=> #<TZInfo::Country: US>
2.2.1 :005 > c.zone_identifiers
=> ["America/New_York", "America/Detroit", "America/Kentucky/Louisville", "America/Kentucky/Monticello", "America/Indiana/Indianapolis", "America/Indiana/Vincennes", "America/Indiana/Winamac", "America/Indiana/Marengo", "America/Indiana/Petersburg", "America/Indiana/Vevay", "America/Chicago", "America/Indiana/Tell_City", "America/Indiana/Knox", "America/Menominee", "America/North_Dakota/Center", "America/North_Dakota/New_Salem", "America/North_Dakota/Beulah", "America/Denver", "America/Boise", "America/Phoenix", "America/Los_Angeles", "America/Metlakatla", "America/Anchorage", "America/Juneau", "America/Sitka", "America/Yakutat", "America/Nome", "America/Adak", "Pacific/Honolulu"]
2.2.1 :006 > c.zones
=> [#<TZInfo::TimezoneProxy: America/New_York>, #<TZInfo::TimezoneProxy: America/Detroit>, #<TZInfo::TimezoneProxy: America/Kentucky/Louisville>, #<TZInfo::TimezoneProxy: America/Kentucky/Monticello>, #<TZInfo::TimezoneProxy: America/Indiana/Indianapolis>, #<TZInfo::TimezoneProxy: America/Indiana/Vincennes>, #<TZInfo::TimezoneProxy: America/Indiana/Winamac>, #<TZInfo::TimezoneProxy: America/Indiana/Marengo>, #<TZInfo::TimezoneProxy: America/Indiana/Petersburg>, #<TZInfo::TimezoneProxy: America/Indiana/Vevay>, #<TZInfo::TimezoneProxy: America/Chicago>, #<TZInfo::TimezoneProxy: America/Indiana/Tell_City>, #<TZInfo::TimezoneProxy: America/Indiana/Knox>, #<TZInfo::TimezoneProxy: America/Menominee>, #<TZInfo::TimezoneProxy: America/North_Dakota/Center>, #<TZInfo::TimezoneProxy: America/North_Dakota/New_Salem>, #<TZInfo::TimezoneProxy: America/North_Dakota/Beulah>, #<TZInfo::TimezoneProxy: America/Denver>, #<TZInfo::TimezoneProxy: America/Boise>, #<TZInfo::TimezoneProxy: America/Phoenix>, #<TZInfo::TimezoneProxy: America/Los_Angeles>, #<TZInfo::TimezoneProxy: America/Metlakatla>, #<TZInfo::TimezoneProxy: America/Anchorage>, #<TZInfo::TimezoneProxy: America/Juneau>, #<TZInfo::TimezoneProxy: America/Sitka>, #<TZInfo::TimezoneProxy: America/Yakutat>, #<TZInfo::TimezoneProxy: America/Nome>, #<TZInfo::TimezoneProxy: America/Adak>, #<TZInfo::TimezoneProxy: Pacific/Honolulu>]
Upvotes: 3