int datediff(Date d1, Date d2) { int diff = 0; if (d2.yyyy != d1.yyyy) { diff += (d2.yyyy - d1.yyyy) * DAYS_IN_YEAR; int ystart; int yend; if (d2.yyyy > d1.yyyy) { ystart = d1.yyyy; yend = d2.yyyy; } else { ystart = d2.yyyy; yend = d1.yyyy; } for (; ystart < yend; ystart++) { if (isleap(ystart)) { diff++; } } } int day2 = daysinyear(d2); int day1 = daysinyear(d1); return day2 - day1 + diff; }
/** * Convert the given time in seconds elapsed since the beginning of the given * year. It is assumed that year <= t[0] */ long long int secondsfrom(int year, const int* val) { long long int res = 0; if (val[5] != -1) res += val[5]; if (val[4] != -1) res += val[4] * 60; if (val[3] != -1) res += val[3] * 3600; if (val[2] != -1) res += (val[2]-1) * 3600 * 24; if (val[1] != -1) for (int i = 1; i < val[1]; ++i) res += daysinmonth(val[0], i) * 3600 * 24; for (int i = year; i < val[0]; ++i) res += daysinyear(i) * 3600 * 24; return res; }