Autodetect timezone in Rails given UTC offset and DST

Posted by Jose on Stack Overflow See other posts from Stack Overflow or by Jose
Published on 2010-05-23T10:46:16Z Indexed on 2010/05/23 10:50 UTC
Read the original article Hit count: 471

Filed under:
|
|

I basically want to autodetect a user's timezone using Rails. I can use this JS code at the user's browser (http://www.onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/) to send a form with the UTC offset and the fact that the time zone observes DST during summer or not, in the user's time zone.

Once I have that info in the server, I want to select the matching time zone. In Rails, I can get a list of time zones with ActiveSupport::TimeZone.all. Also, I can filter zones by utf offset thanks to the utc_offset method. However, I don't know how to filter the timezones that do/don't observe DST.

E.g. suppose a user lives in Amsterdam. Filtering by UTC offset will return Berlin, Belgrado, Madrid, etc timezones, as well as West Central Africa. All of them, but West Central Africa, would be appropriate timezones for a user in Amsterdam (as they provide the same time/date), but I need to filter West Central Africa, which does not perform DST in summer.

How can I do this in Rails? Also, are any of my assumptions wrong?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about timezone