I understand the basics of timezone support:
Essentially, I just have to do this:
controllers/application.rb
before_filter :set_time_zone
def set_time_zone
Time.zone = @current_user.time_zone if @current_user
end
That will set the timezone to the particular user’s stored timezone and
all will be well in the world.
The part I am trying to figure out is how to “guess” the user’s timezone
in the first place. The application I am working on calls for the user
registration to be dead simple and not include a huge timezone selection
box.
I can think of two methods for guessing timezone:
- Based on IP Address of client
- Based on a cookie stored on their computer with their current offset
I would probably go with #2 (i.e store a cookie with their current
offset from utc and then use that to fetch the first timezone which
matches the offset)
Yes this would be inaccurate in that the wrong timezone may be selected
but I would always have a select box in their settings area that would
allow them to correct it and it would “get the job done”.
Thoughts? Is there a better way?