When a day is not a day

It may sound weird, but days are not all made the same.  No, I’m not talking qualitatively, but in quantitative terms.  Whenever we have a daylight savings time switch, days can be either 23 or 25 hours long.

A quick example of this in Rails using Timecop:

Timecop.freeze Time.local 2014,3,9,9 # Go to 9AM 3/9/14 just after DST
24.hours.ago # => 8AM the previous day
1.day.ago # => 9AM the previous day

When you ask Rails for 1.day.ago, it is smart enough to know that you really mean the same time yesterday.  If you try to be smart yourself, Rails won’t be there to help you.

If you are experiencing weird results in your application around DST, try doing a code search for 24.hours and see what turns up.  You may be making the false assumption that a day is always 24 hours long.

Comments are closed.