Why doesn't `config.time_zone` seem to do anything?

In application.rb, it says:

Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.

But setting config.time_zone = 'Central Time (US & Canada)' or config.time_zone = 'Eastern Time (US & Canada)' has no effect - the created_at field in a model is stil being saved in UTC.

According to this railsforum answer:

config.time_zone just lets rails know that your server is set to this timezone so when it writes dates to the database it can properly convert it to UTC.

If that is true, then why is it that when my system time is Pacific Time (US & Canada) and config.time_zone = 'Central Time (US & Canada)' or config.time_zone = 'Eastern Time (US & Canada)', that the created_at time is the correct UTC? Should it not be incorrect?!

Because, if the PST time is 8 PM, then EST is 11 PM and UTC is 4 AM. Presuming that Rails does Time.now, that would be 8 PM. And we told Rails that the server is in EST. So, 8 PM would be EST time as far as Rails is concerned and the UTC would then be 5 AM UTC, which would be incorrect (because the actual time is 8 PM PST/11 PM EST, which is 4 AM UTC)

What's going on here?


Here's a list of concepts and things that may help you:

config.time_zone doesn't set "Server Time", that is controlled by your operating system usually.

Rails always stores your dates in UTC in the database (unless you change a different setting).

Time.now returns the localtime for your computer in your timezone and it also includes the local timezone offset from your operating system, which means Ruby and therefore Rails knows how to convert localtime into UTC. You can try this by using irb directly, so no Rails libraries are loaded:

ctcherry$ irb
>> Time.now
=> Mon Feb 21 20:53:14 -0800 2011
>> 

If config.time_zone, or Time.zone is set, to lets say EST, Rails expects that if you set a datetime attribute that you mean for that time and date to be in specified timezone, in this case EST. This is why you set Time.zone equal to your end users timezone, so they can use their local time and dates, and you can pass them directly into your ActiveRecord models and Rails can convert it to UTC for storage in the database.

Does that help at all?


You need to use in_time_zone (i.e. Time.now.in_time_zone) to get some something other than UTC.