I am playing with a timezones and I have a simple registration form. after user's registration, I would like to user display his current time, how to do that?
-
Do you know the users location? You could grab the local time from that information. – Devin M Apr 13 '12 at 14:53
2 Answers
You can not do it only with Ruby, because such information isn't available on the server side.
1) If you know the user's location, you can do geo locating and determine the timezone. But it could be a problem if ip range you got from geo database has a few timezones.
2) Another solution, you can use javascript to help. So, with javascript you can calculate timezone and set it as cookie, and then access with Ruby from the request object. (How to get timezone with Javascript)
-
hmmm, so as I read the possibilities, I think that the best solution will be let the user set up his timezone and with this timezone then work ... Thanks. – user984621 Apr 13 '12 at 17:02
-
always think before implement ... it's more UX problem not to be too boring for user (do not require too many unnecessary actions from user). If user doesn't need right time in the account and it's only you want it, then it's better not to do then ask user for an action (like select timezone). – gakhov Apr 14 '12 at 21:53
You could ask the user for their current timezone during registration and persist it with their user details.
ActiveSupport (included as part of the Rails API) has a great way to deal with timezones, you can convert the current time to a time in a different timezone using *in_time_zone*.
As an example
Time.now.in_time_zone("America/Guyana")
Time.now.in_time_zone(8)
Where the time_zone key can be found via this API document: http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html
See TimeWithZone for more info, http://api.rubyonrails.org/classes/ActiveSupport/TimeWithZone.html#method-i-in_time_zone.

- 595
- 4
- 15