#include <stdio.h>
#include <stdlib.h>
int isLeap(int);
int countInterval(int, int, int, int, int, int);
int main()
{
int s_yy=1988, s_mm=2, s_dd=1,
e_yy=1990, e_mm=2, e_dd=1;
int days;
printf("%d / %d / %d\n", s_yy, s_mm, s_dd);
printf("%d / %d / %d\n", e_yy, e_mm, e_dd);
printf("---------------------------------\n");
days = countInterval(s_yy, s_mm, s_dd, e_yy, e_mm, e_dd);
printf("through: %d days\n", days);
system("pause");
return 0;
}
/////////////////////////////
// check Leap year
/////////////////////////////
int isLeap(int yy)
{
int result=0;
if (yy%4 != 0)
result = 0;
else if (yy%400 ==0)
result = 0;
else
result =1;
return result; //1 is leap, 0 is not leap
}
/////////////////////////////////////////////
// count the interval
// input: Date1(yy, mm, dd), Date2(YY, MM, DD)
// output: interval by days
/////////////////////////////////////////////
int countInterval(int s_yy, int s_mm, int s_dd, int e_yy, int e_mm, int e_dd)
{
// month 1 2 3 4 5 6 7 8 9 10 11 12
int Leap[]={31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
nLeap[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// index [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
///////////////////////////////////////
// 1. in the same year
// 2. two succesive years
// 3. through >= 1year
//////////////////////////////////////
int interval, s_isleap, e_isleap, t_isleap;
int i, part_1, part_2, part_3, temp;
////////////////////////
// 1. in the same year
if (s_yy == e_yy)
{
s_isleap = isLeap(s_yy);
interval = 0;
temp = 0;
// in Leap Year
if (s_isleap == 1)
{
if (s_mm==e_mm && s_dd==e_dd)
interval = 0;
else if (s_mm == e_mm)
interval = e_dd - s_dd;
else
{
for(i=s_mm ; i < e_mm-1 ; i++)
{
temp += Leap[i];
}
interval = Leap[s_mm-1] - s_dd + temp + e_dd;
}
}
// not in Leap year
else
{
if (s_mm==e_mm && s_dd==e_dd)
interval = 0;
else if (s_mm == e_mm)
interval = e_dd - s_dd;
else
{
for(i=s_mm ; i < e_mm-1 ; i++)
{
temp += nLeap[i];
}
interval = nLeap[s_mm-1] - s_dd + temp + e_dd;
}
}
}
///////////////////////////////
// 2. two succesive years
else if (e_yy - s_yy == 1)
{
s_isleap = isLeap(s_yy);
e_isleap = isLeap(e_yy);
interval = 0;
// part_1: from (yy/mm/dd) to (yy/12/31)
temp = 0;
part_1 = 0;
if (s_isleap ==1)
{
for(i=s_mm ; i <=12 ; i++)
temp += Leap[i];
part_1 = Leap[s_mm-1] - s_dd + temp;
}
else
{
for(i=s_mm ; i < 12 ; i++)
temp += nLeap[i];
part_1 = nLeap[s_mm-1] - s_dd + temp;
printf("%d\n", part_1);
}
// part_2: from (yy/12/31) to (yy+1/MM/DD)
temp = 0;
part_2 = 0;
if (e_isleap ==1)
{
for(i=0 ; i < e_mm-1 ; i++)
temp += Leap[i]; // temp contain (12/31 to 1/1)
part_2 = temp + e_dd;
}
else
{
for(i=0 ; i < e_mm-1 ; i++)
temp += nLeap[i]; // temp contain (12/31 to 1/1)
part_2 = temp + e_dd;
printf("%d\n", part_2);
}
// interval
interval = part_1 + part_2;
printf("%d\n", interval);
}
///////////////////////////////
// 3. through >= 1year
else
{
s_isleap = isLeap(s_yy);
e_isleap = isLeap(e_yy);
interval = 0;
// part_1: from (yy/mm/dd) to (yy/12/31)
temp = 0;
part_1 = 0;
if (s_isleap ==1)
{
for(i=s_mm ; i <=12 ; i++)
temp += Leap[i];
part_1 = Leap[s_mm-1] - s_dd + temp;
}
else
{
for(i=s_mm ; i < 12 ; i++)
temp += nLeap[i];
part_1 = nLeap[s_mm-1] - s_dd + temp;
}
// part_2: from (yy+1/1/1) to (yy+1/MM/DD)
temp = 0;
part_2 = 0;
if (e_isleap ==1)
{
for(i=0 ; i < e_mm-1 ; i++)
temp += Leap[i];
part_2 = temp + e_dd;
}
else
{
for(i=0 ; i < e_mm-1 ; i++)
temp += nLeap[i];
part_2 = temp + e_dd;
}
// part_3: from (s_yy+1) to (e_YY-1)
temp = 0;
part_3 = 0;
for (i=s_yy+1 ; i < e_yy ; i++)
{
t_isleap = isLeap(i);
if (t_isleap == 1)
temp += 366;
else
temp += 365;
}
part_3 = temp;
interval = part_1 + part_2 + part_3;
}
return interval;
}