Montag
Sep012008
Time zones in Rails 2.1
Montag, September 1, 2008 at 3:23PM
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.
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.
Several rake task helps you to find out the possible zones:
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 @userendIf 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 +0200Several rake task helps you to find out the possible zones:
rake time:zones:allrake time:zones:localrake time:zones:us
tagged
rails time zones
rails time zones 

Reader Comments