I agree, us anon's example, or some other library function that does date/time calculations.
Code:
theyer1=int(365.25*(yer1))+int(30.6001*(mnth1+1))+fyear1+day1+1720995;
Have you tested this for all sorts of arbitrary calculations of short and long date intervals, gapping over leap years and non-leapyears, etc, etc.
By the way, there's a bunch of duplication in your code. The first thing I would do is to convert your common for first and second year calculation into one function:
Code:
int dayNo(int y, int m, int d)
{
int yoff = 0;
int moff = 1;
int fy, sy;
if (m == 1 || m == 2)
{
yoff = 1;
moff = 13;
}
fy = (y - 1) / 100;
sy = 2 - fy + (fy / 4);
return int(365.25*(y - yoff)+int(30.6001*(m+moff)) + sy + d = 1720995;
}
I removed a bunch of "int()" since the calculation itself is done completely in integer in the first place, it doesn't need to be "int()" around it - it's already done automagically by the compiler (or rather the processor - but by the compiler choosing integer math operations).
I also used a few local variables to avoid having an almost identical calculation twice. It makes it easier to read the code, and it's easier to change ONE long complicated calculation than to do two different calculations.
Using this function makes it much easier to produce the days funciton:
Code:
int days (int yer2,int yer1,int mnth2,int mnth1,int day1,int day2)
{
int theyer1 = dayNo(yer1, mnth1, day1);
int theyer2 = dayNo(yer2, mnth2, day2);
return abs(theyer1-theyer2);
}
I'm also dubious to the code in red below:
Code:
fyear1=int((yer1-1)/100);
syear1=2-fyear1+int(fyear1/4);
fyear2=int((yer2-1)/100);
syear2=2-fyear2+int(fyear2/4);
theyer1=int(365.25*(yer1-1))+int(30.6001*(mnth1+13))+fyear1+day1+1720995;
theyer2=int(365.25*(yer2-1))+int(30.6001*(mnth2+13))+syear2+day2+1720995;
--
Mats