Today fowlduck and I were talking in the #rubyonrails channel and we both wondered about timezones and why they were so (apparently) screwy. It turns out all I (and he possibly also) forgot to do was to put config.time_zone in the config/environment.rb. So what does this mysterious method do? Well:
In Rails 2.2...
REXML could not parse this XML/HTML: <ul> <li>1. <b>The <span class='term'>Configuration</span> Class</b><br /> This class begins all the way down on <a href='http://github.com/rails/rails/blob/2-2-stable/railties/lib/initializer.rb#L578'>line #578 of <i>railties/lib/initializer.rb</i></a> in the Rails source. This just simply defines a class in the <i>Rails</i> module called <i>Configuration</i>. What we can get really excited about is on <a href='http://github.com/rails/rails/blob/2-2-stable/railties/lib/initializer.rb#L748'>line #748</a> it defines an <span class='term'><a href='http://www.ruby-doc.org/core/classes/Module.html#M001704'>attr_accessor</a></span> for <span class='term'>:time_zone</span>. This, as you probably already know defines two methods a setter (<span class='term'>time_zone=</span>) and a getter (<span class='term'>time_zone</span>) in which we can store values. </li>
By default this time_zone method will be set to nil. It's up to you to set it in your config/environment.rb file which you do by doing something along these lines:
Rails::Initializer.run do |config|
config.time_zone = "Adelaide"
end
This will set the time_zone value to be the Adelaide Time zone, something like: #<ActiveSupport::TimeZone:0x30f4f8 @tzinfo=nil, @name="Adelaide", @utc_offset=34200>. You don't have to set it to Adelaide, just try your nearest major city and it Should Just Work ™. If you don't set this in your config/environment.rb date and time values returned from the database will not be set to whatever time zone you specify.
REXML could not parse this XML/HTML: </ul>
Changelog
Updated on April 23rd, 2009- Fixed line number linkings, linking directly to 2-2-stable branch which, ideally, should now never change.