View Single Post
Old 12-31-2008, 08:52 PM   #18 (permalink)
programphases
Squirt
 
Join Date: Dec 2008
Posts: 10
programphases is on a distinguished road
Default

Quote:
Originally Posted by twhoffman View Post
That would prevent the infinite loop, but would introduce a new bug where it advances the year on the last day of a leap year. It would have thought today was December 31, 2009. That's better than locking up, but it also would mean that all time-limited DRM media would stop working. So, if you have a Zune Pass, none of your songs would play.

What's needed is a break within an else like this (I hope, my C's a little rusty, and was never that good to begin with):

Code:
year = ORIGINYEAR; /* = 1980 */

while (days > 365)
{
    if (IsLeapYear(year))
    {
        if (days > 366)
        {
            days -= 366;
            year += 1;
        }
        else
        {
            break;
        }
    }
    else
    {
        days -= 365;
        year += 1;
    }
}
Will the break release from the outer if statement? (edit: yes...see below)

This should work too:

Code:
year = ORIGINYEAR; /* = 1980 */

while (days > 365)
{
    if (IsLeapYear(year))
    {
        if (days > 366)
        {
            days -= 366;
            year += 1;
        }
        else if (days == 366)
           days -= 366;
    }
    else
    {
        days -= 365;
        year += 1;
    }
}

Last edited by programphases; 12-31-2008 at 09:10 PM.



programphases is offline   Reply With Quote