#include <stdio.h>
static inline int is_leap_year(unsigned int year)
{
return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
}
typedef struct
{
int year;
int month;
int day;
} rtc_date_t;
const static int month_days[2][13] = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31},
};
int dday(rtc_date_t date1, rtc_date_t date2)
{
rtc_date_t *dmax = date1.year > date2.year ? &date1 : &date2;
rtc_date_t *dmin = date1.year > date2.year ? &date2 : &date1;
int days = 0;
for (int y = dmin->year; y < dmax->year; y++)
days += is_leap_year(y) ? 366 : 365;
int leap = is_leap_year(dmin->year);
for (int m = 1; m < dmin->month; m++)
days -= month_days[leap][m - 1];
leap = is_leap_year(dmax->year);
for (int m = 1; m < dmax->month; m++)
days += month_days[leap][m - 1];
days += dmax->day - dmin->day;
return days < 0 ? -days : days;
}
int main(int argc, char const *argv[])
{
rtc_date_t d1;
rtc_date_t d2;
scanf("%d %d %d %d %d %d", &d1.year, &d1.month, &d1.day, &d2.year, &d2.month, &d2.day);
int days = dday(d1, d2);
printf("%04d/%02d/%02d - %04d/%02d/%02d --> %d days\n", d1.year, d1.month, d1.day, d2.year, d2.month, d2.day, days);
return 0;
}
|