Time zone support in rails is now easier than ever. You can set your time zone dynamically based on users information.

Here are the steps:

In the configuration file, config/environment.rb add the following line

config.time_zone = "UTC"

You can prompt from users for their time zone and save it in the database for future reference. For the population of time zones in the dropdown list add the following line in you *.rhtml file:


Save this data in the database field.

When the user will be logged in fetch the time_zone from a database and set it at Time.zone.

def set_timezone(time_zone)
Time.zone = time_zone
end

To persist it everywhere make sure to call it:

set_timezone(@logged_in_user.time_zone)

That’s it!

Contributor: Fuad Bin Omar, COO, Nascenia

Published On: January 10th, 2009 / Categories: Blog, Ruby on Rails, Ruby on Rails / Tags: , , /