In almost every environment.rb
, there's a line for config.time_zone = 'UTC'
.
What exactly does this line do, and under what circumstances would I want to change it (to, e.g., config.time_zone = 'EST'
)?
In almost every environment.rb
, there's a line for config.time_zone = 'UTC'
.
What exactly does this line do, and under what circumstances would I want to change it (to, e.g., config.time_zone = 'EST'
)?
Setting config.time_zone
changes the default time zone for your Rails application. This is the time zone all times will be displayed in to your users. It is also the time zone it assumes when setting attributes.
However, Rails will always store the times in UTC in the database. The translation happens behind the scenes so (most of the time) you don't have to worry about it.
It's common to change this time zone to one that most of your users will be in. You can run this rake task to see all time zones you can choose from.
rake time:zones:all
It is also very easy to change the current time zone on a per-request basis allowing each user to configure which time zone they are in. here's a before filter example you might add to the application controller.
before_filter :set_user_time_zone
private
def set_user_time_zone
Time.zone = current_user.time_zone if logged_in?
end
See this Railscasts episode for more information.
Just to add one point to Ryan's excellent answer. If you wanted to set it to Eastern Time, it wouldn't be
config.time_zone = 'EST'
it would be
config.time_zone = 'Eastern Time (US & Canada)'
Use one of the following time zones to get the list of available options:
rake time:zones:all
rake time:zones:local
rake time:zones:us