Time zones in Rails 2.1

Rails 2.1 has pretty good support of Time Zones. It’s possible to save user-specific time zones. So if your users are from different zones, you can display the output in the corresponding zone of the user.
To set a default time zone, add config.time_zone in environment.rb. For the output, set the current time zone in before filters via application controller.

def set_zoneTime.zone = @user.time_zone if @userend

If you display timestamps in ActionMailer, you must set the Time.zone option within the ActionMailer class. Otherwise, the default settings of the database are taken (Time zone: UTC)

Rails provides nice helper methods to work with time zones. A useful method is in_time_zone(). It presents a time in any time zone.
>> t = Time.now
>> t.in_time_zone('Bern')=> Mon, 01 Sep 2008 14:47:00 +0200

Several rake task helps you to find out the possible zones:
rake time:zones:all
rake time:zones:local
rake time:zones:us

back

Leave a Reply