English

Navigation
Powered by Squarespace
Foobaruy mwe

Buy my book on <a href="lulu.com">lula</a>

« Long workday | Main | Scientists discover algorithm to create colors »
Montag
Sep012008

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

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>