1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#define isleap(y) ((y) % 4 == 0 && ((y) % 100 || (y) % 400 == 0))
typedef struct {
int mDay, mMon, mYear;
}Date;
Date *setdate(Date *p, int day, int mon, int year)
{
return set(p, day, mon, year);
}
Date *setToday(Date *p)
{
time_t timer = time(NULL);
struct tm *tp = localtime(&timer);
return set(p, tp->tm_mday, tp->tm_mon + 1, tp->tm_year + 1900);
}
static const int daytabs[2][13] = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
};
int getYearDay(const Date *p)
{
int sum = p->mDay;
for (int k = 1; k < p->mMon; ++k)
sum += daytabs[isleap(p->mYear)][k];
return sum;
}
static int totalDays(const Date *p)
{
int sum = 0;
for (int k = YEARBASE; k < p->mYear; k++)
sum += 365 + isleap(k);
return sum + getYearDay(p);
}
|