Handling Daylight Saving Time in Rails

Posted by – April 28, 2009

I’ve just been banging my head against this for a few minutes so I thought I would blog this for other people, and also for myself for when I forget in the future.

All Rails apps have a default timezone which is set in config/environment.rb as config.time_zone. For me this is always set as ‘UTC’ which is fine, apart from when it’s the summer which means I have issues with daylight saving, meaning that Time.now returns 1 hour in the future

#Current time = Tue Apr 28 22:39:09 +0000
>> Time.now
=> Tue Apr 28 23:39:09 +0100 2009

So, in order to get round this, I need to ensure that I use Time.zone.now instead:

>> Time.now
=> Tue Apr 28 23:40:25 +0100 2009
>>; Time.zone.now
=> Tue, 28 Apr 2009 22:40:25 UTC +00:00

and Bingo.

Note that if you want to change the time zone your app is sat in you simply change the config.time settings. If you need a list of time zones available, just run

rake -D time

from the command line for more options.


0 Comments on Handling Daylight Saving Time in Rails

Closed