Thursday, January 1, 2009

Zune Date Problems

So today the Urban word of the day was: Y2K9 - The simultaneous worldwide crash of every 30GB Zune worldwide during the early hours of the morning on Dec 31, 2008. Seems it is related to the following stub of C-code.
year = ORIGINYEAR; /* = 1980 */

while (days > 365)
{
    if (IsLeapYear(year))
    {
        if (days > 366)
        {
            days -= 366;
            year += 1;
        }
    }
    else
    {
        days -= 365;
        year += 1;
    }
}
This site has some good discussion of the problem. When in a leap-year and it reaches the last day of the year and can't break out of the outer loop. Hmmm, perhaps a little bit of TESTING! Trying this function with perhaps just a few samples would have revealed this. First off - this is a precise case where this code is needlessy complex, and quite honestly for no reason whatever. There are probably somewhere in the neighborhood of a few thousand routines for dates. Why did they need to invent something new. By the way - the fix is simply to change the days>366 to days>=366.

1 comment:

Frank P. Bresz }*{ said...

Just realized that this kind of code is why things are so slow. I haven't looked at it tightly, but there has to be a faster way to do this than run a loop 365 times to count up days in a year.