Quote:
Originally Posted by programphases
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;
}
}
|
Wouldn't the if in your else statement be unecessary? If days > 365 and not > 366, it must == 366. I know it won't make much of a performance difference these days, but I cut my teeth on a 1 MHz 6502 and still cringe at wasted CPU cycles.
I haven't seen the rest of the code, but could setting days to 0 cause any problems? The code may be expecting days to be >= 1 or it might cause problems with DRM licensing.
I also found this code snippet from read.pudn.com/downloads88/sourcecode/embed/339383/EBOOT/timerxsc1.c__.htm that might be another solution:
Code:
year = ORIGINYEAR;
while (1)
{
leap = isleap(year);
if (day < 365+leap)
break;
day -= 365+leap;
year++;
}
lpst->wYear = year;
By the way, I plugged my Zune in just after midnight. It's working just fine so far, though it did boot, show the menu, then immediately rebooted. After the reboot, everything seems okay, though the time is an hour ahead.
Edit: Of course, this might be even simpler, assuming that days == 1 on January 1, 1980. I plugged the formulas into Excel, used a few dates (year end on leap and non-leap years as well as mid-year and 1/1 dates), and it seems to work. It would be safer, since there are no loops to get stuck in.
Code:
year = floor ((days - 1) / 365.25);
days -= ceil (year * 365.25);
year += ORIGINYEAR;