Path: utzoo!mnetor!uunet!steinmetz!davidsen From: davidsen@steinmetz.steinmetz.UUCP (William E. Davidsen Jr) Newsgroups: comp.sys.ibm.pc Subject: Re: day of week Message-ID: <9404@steinmetz.steinmetz.UUCP> Date: 3 Feb 88 15:16:02 GMT References: <141900013@occrsh.ATT.COM> Reply-To: davidsen@crdos1.UUCP (bill davidsen) Organization: General Electric CRD, Schenectady, NY Lines: 94 There has been so much commentary on the topic of "what day of the week," that I am posting a short program to provide the information. It returns a pseudo-julian date which may be used to uniquely identify any date from Jan 1 1980 to Feb 29, 2100. One procedure takes the month, day, year, and returns a unique number, the other does the inverse. The number is such that num%7 = day_of_week, and 0 means Sunday. Before I get a million postings, yes I have a real julian date routine. This one works and returns values which will fit in a short as long as I live. /* * JULI - pseudo julian date package * * Convert any post-1980 date to an integer day number and back to * day, month, year. This will work up to feb 29, 2100. */ static months[12][2] = { {0, 0}, {31, 31}, {59, 60}, {90, 91}, {120, 121}, {151, 152}, {181, 182}, {212, 213}, {243, 244}, {273, 274}, {304, 305}, {334, 335}}; int jdate (month, day, year) int month, day, year; { register int iy, ly, lyf; iy = year - 1980; ly = (iy+3) / 4; /* leap year count */ lyf = (iy%4 == 0); return (365*iy + ly + months[month-1][lyf] + day); } /* * Convert day number to date */ unjuli (jd, month, day, year) int jd; /* julian day */ int *month, *day, *year; { register int lyf, /* leap year (this year) flag */ dd, mm, /* calculated day and month */ yy, ly; /* incremental years, leapyears */ /* first guess as to year */ yy = jd / 365; ly = (yy+3) / 4; while ((dd = jd - 365*yy - ly) <= 0) { /* correct for dates near the end of a year */ yy--; ly = (yy+3) / 4; } /* find the month */ lyf = (yy%4 == 0); mm = 0; while (months[mm][lyf] < dd && ++mm < 12); /* return values */ *year = yy + 1980; *month = mm; *day = dd - months[mm-1][lyf]; } /* * Test routine for julian package */ #ifdef TEST #include main () { int m, d, y, jd; for ( ; ; ) { printf ("Enter month, day, year: "); scanf ("%d%d%d", &m, &d, &y); if (y < 1900) y += 1900; jd = jdate (m, d, y); printf ("Juliam for %d/%d/%d = %d\n", m, d, y, jd); unjuli (jd, &m, &d, &y); printf ("Converted back = %d/%d/%d\n", m, d, y); } } #endif -- bill davidsen (wedu@ge-crd.arpa) {uunet | philabs | seismo}!steinmetz!crdos1!davidsen "Stupidity, like virtue, is its own reward" -me