Add Date Tests for Daylight Savings

I wrote my iOS App, Habits, after I had been at a job with very strict daylight savings coding rules, so it has lots of unit tests that take the code across the daylight savings/standard time boundaries.

But now it’s been almost 20 years (!) since I worked there, and I’ve gotten bad habits, which bit me last week. On Nov 3rd, at 2am, and for the next week, I had a bug in my code for calculating the date that was “one week ago” because I used JavaScript’s setDate() instead of setUTCDate().

Luckily, my app is still being written, not in production, so it was only a problem in my unit tests. The tests use the current date, and so they inefficiently find daylight savings problems.

The right way is to also have tests that specifically set the date to known daylight savings boundaries and the dates around them.