The Zune Leap Year Bug: Source Code
January 02, 2009
For those who have been watching the news, Microsoft’s Zune mp3 player froze for owners on December 31. Microsoft blamed the bug on the leap year, and it seems that things have resolved themselves. Looking at the Zune source code, one can see exactly where things went wrong:
while (days > 365) { if(IsLeapYear(year)) { if (days > 366) { days -= 366; year += 1; } } else { days -= 365; year += 1; } }
For those without a programming background, the problem is that there is no “if” for day 366, just for days greater than 366 (which would be January 1 of 2009). This means that the code gets caught in in infinite loop on the 366th day (December 31) of the year. On January 1 of 2009, however, the “days” variable is set to 367, which causes the day to be set back to 1 (January 1) and the year to be incremented to 2009. So the bug only lasts a day.
Programming 101 – be sure to test all of your cases, particularly those inside of loops.