439,977 Members | 1,369 Online
Need help? Post your question and get tips & solutions from a community of 439,977 IT Pros & Developers. It's quick & easy.

# formula used in calculating diffrence between dates

 P: n/a Hi folks, Al Bowers wrote this program on comp.lang.c Date: 2001-07-09 13:41:58 PST #include int isleap (unsigned yr); static unsigned months_to_days (unsigned month); static long years_to_days (unsigned yr); long ymd_to_scalar (unsigned yr, unsigned mo, unsigned day); int main(void) { unsigned y1 = 2000, m1 = 2, d1 = 1, /* represent 01FEB2000 */ y2 = 2000, m2 = 3,d2 = 1; /* represent 01MAR2000 */ long date1,date2; date1 = ymd_to_scalar(y1,m1,d1); date2 = ymd_to_scalar(y2,m2,d2); printf("There are %ld days\n",date2-date1); printf("It %s a leap year\n",(isleap(y1))?"is":"is not"); return 0; } int isleap (unsigned yr) { return yr % 400 == 0 || (yr % 4 == 0 && yr % 100 != 0);} static unsigned months_to_days (unsigned month) { return (month * 3057 - 3007) / 100; } static long years_to_days (unsigned yr) {return yr * 365L + yr / 4 - yr / 100 + yr / 400;} long ymd_to_scalar (unsigned yr, unsigned mo, unsigned day) { long scalar; scalar = day + months_to_days(mo); if ( mo > 2 ) /* adjust if past February */ scalar -= isleap(yr) ? 1 : 2; yr--; scalar += years_to_days(yr); return scalar; } can anybody explain the logic or formula applied in functions:- static unsigned months_to_days (unsigned month); static long years_to_days (unsigned yr): if want to know about the formula which is used here -- comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must have an appropriate newsgroups line in your header for your mail to be seen, or the newsgroup name in square brackets in the subject line. Sorry. Nov 14 '05 #1