2

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?

user984621
  • 46,344
  • 73
  • 224
  • 412

2 Answers2

3

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)

Community
  • 1
  • 1
gakhov
  • 1,925
  • 4
  • 27
  • 39
  • 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
1

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.

Callum Jones
  • 595
  • 4
  • 15