I wrote a function in C to calculate how many days it has been since January 1, 2000. However, it doesn't work correctly for some years (e.g. it works for May 5, 2020, but not for May 5, 2029). For some years it determines the number of days correctly, for others 1 day less, and for some two or three days less. Which part of the code could cause this problem?
bool isLeapYear(int y) {
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
return true;
else
return false;
}
int days_since(int y, int m, int d)
{ int m_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int leap_count = 0;
int years_index = y-2000;
for (int i=2001; i<=y; i++){
if(isLeapYear(i))
leap_count ++;
}
int add_years = 366 * (leap_count) + 365 * (years_index-leap_count);
int add_months = 0;
if (isLeapYear(y))
m_days[1] = 29;
for (int i=0; i<m-1; i++)
add_months += m_days[i];
int add_days = d-1;
int total_days = add_years + add_months + add_days;
return total_days;
}
Dealing with February 29th in the middle of the year is a pain and OP's code has troubles with that too.
I do not see a simple fix of OP's code.
Instead, move leap day to the end of the year by making March the first month and January/February the last months of the previous year. That's what the Romans did and then the 8th month is October and the 10th month is December. This simplifies much code by putting leap day concerns at the year's end.
Notice
days_since_March1[]
below is the same for leap years and non-leap years and the leap year calculation is simple.