#include #include int datediff(const char *pfx, const char *s1, const char *s2) { struct tm t1, t2; char *fmt = "%Y-%m-%d"; time_t z1, z2, diff; memset(&t1, 0, sizeof(t1)); memset(&t2, 0, sizeof(t2)); strptime(s1, fmt, &t1); strptime(s2, fmt, &t2); z1 = mktime(&t1); z2 = mktime(&t2); diff = z2 - z1; diff /= (60 * 60 * 24); printf("%s : %d days\n", pfx, (int) diff); return diff; } void dateproject(const char *pfx, const char *s1, int days) { struct tm t1, *t2; char *fmt = "%Y-%m-%d"; char s2[16]; time_t z1, new; memset(&t1, 0, sizeof(t1)); strptime(s1, fmt, &t1); z1 = mktime(&t1); new = z1 + days * (60 * 60 * 24); t2 = localtime(&new); strftime(s2, 15, fmt, t2); printf("%s on %s\n", pfx, s2); } int main(int argc, char *argv[]) { float f = 0; f += datediff("2.6.20..2.6.21", "2007-02-04", "2007-04-27"); f += datediff("2.6.21..2.6.22", "2007-04-27", "2007-07-08"); f += datediff("2.6.22..2.6.23", "2007-07-08", "2007-10-09"); f += datediff("2.6.23..2.6.24", "2007-10-09", "2008-01-24"); f += datediff("2.6.24..2.6.25", "2008-01-24", "2008-04-16"); f += datediff("2.6.25..2.6.26", "2008-04-16", "2008-07-13"); f += datediff("2.6.26..2.6.27", "2008-07-13", "2008-10-09"); f += datediff("2.6.27..2.6.28", "2008-10-09", "2008-12-24"); f /= 8; printf("average %.2f days\n", f); dateproject("2.6.29", "2008-12-24", (int)f); dateproject("2.6.30", "2008-12-24", (int)f + (int)f); return 0; }