# Zune loop

According to David Magda in RISKS 25.50, the Zune crash on January 31, 2008 was caused by this code:
while (days > 365) {
if (IsLeapYear(year))    {
if (days > 366) {
days -= 366;
year += 1;
}
} else {
days -= 365;
year += 1;
}
}


Which doesn’t properly handle days=366. I’ll even bet they started using the % (mod) operator. But then how should they deal with the leap year? I know that a program like SmallCheck would have helped catch this problem. I know that commonly available static checkers look for >/>= problems with nearby loops and conditionals, though I don’t know any specific product well enough to know that it would catch this.

I’d very much like to say that the author should have written (dayOfYear, year) = (day % 365 - leapYearsBefore year, day div 365), but I’m having trouble finding a good example of how simple and elegant FP can make this problem. Existing efforts are less than helpful.

RISKS 25.50 was not on the web at the time of this post. It should be up shortly at the URL linked above.