Monday, October 26, 2009

Specs failing with Daylight Saving Time change?

So I've been banging my head for the past hour or so, trying to work out why some of our specs have suddenly started failing without the code having been touched, and it comes down to an inconsistency in how Rails is handling UTC offsets when Time objects are manipulated:


>> Time.now
=> Mon Oct 26 15:55:20 0000 2009
>> Time.now.utc
=> Mon Oct 26 15:55:26 UTC 2009


OK, that's fine. Now let's try a calculation:


>> (Time.now - 1.day).utc
[Sun Oct 25 14:55:41 UTC 2009


What? Where did that one hour offset come from??

It turns out THAT is the source of all the specs which suddenly failed for no apparent reason this morning.

The good news is, it's easy to fix:

>> Time.now.utc - 1.day
=> Sun Oct 25 15:56:08 UTC 2009

MUCH better!

No comments: