[net.sources] Algorithm wanted

demillo@uwmacc.UUCP (Rob DeMillo) (12/03/84)

Gang ---

    Does someone "out there" know of an algorithm (or can direct me
to a source) which will calculate the Day of the Week (e.g. Monday,
Tuesday, etc) based on the month, the day and the year?
    Please mail your response to:
       ...seismo!uwvax!uwmacc!demillo
It will be greatly appreciated....
            - Rob DeMillo
              Madison Academic Computing Center

33500911@sdcc3.UUCP (James Hayes) (12/05/84)

>    Does someone "out there" know of an algorithm (or can direct me
>to a source) which will calculate the Day of the Week (e.g. Monday,
>Tuesday, etc) based on the month, the day and the year?
>It will be greatly appreciated....
>            - Rob DeMillo
>              Madison Academic Computing Center

Sorry Folks, it's in BASIC...

X=INT(.6+1/M) : Y1=Y-X : M1=M+12*X : X=Y1/100
N4=INT(X/4) : N3=INT(X) : N2=INT((5*Y1)/4) : N1=INT(13*(M1+1)/5)
X=N1+N2-N3+N4+D-1
FD=X-(7*INT(X/7))

On entry:  Y=year (must be after 1752)
	   D=day
	   M=month

On exit:   FD will contain an integer from 0 to 6. 0=Sunday and
						   6=Saturday.
                       (And as you might have guessed 1-5 fall
			in-between.)

			James Hayes
			{ucbvax,sdcsvax}!sdcc3!33500911


[SPEED ISN'T EVERYTHING. YOU HAVE TO BE CORRECT SOME OF THE TIME]

rcj@burl.UUCP (R. Curtis Jackson) (12/06/84)

Make sure that whatever algorithm you get takes the following
into account about leap years:

a) Any year divisible by 4 is a leap year, unless
b) it is divisible by 100 as well, in which case it is NOT a leap
   year, unless
c) it is divisible by 400 as well, in which case it is a leap year
   after all.

What stupid rules!

Thanks to Geoff and John here for this scintillating info on leap
year calculation,
-- 
The MAD Programmer -- 919-228-3313 (Cornet 291)
alias: Curtis Jackson	...![ ihnp4 ulysses cbosgd mgnetp ]!burl!rcj
			...![ ihnp4 cbosgd akgua masscomp ]!clyde!rcj

nazgul@apollo.uucp (Kee Hinckley) (12/07/84)

....
This is taken from a posting of parsedate a while back.  If I had more time
I would comment this excerpt with an explanation, but.... I'll leave it as
an exercise to the reader.

.............................................................................
		    DATE MANIPULATION PACKAGE
			Richard B. Wales
	  UCLA Center for Experimental Computer Science

		Copyright (c) 1984 by Richard B. Wales

The author hereby grants permission to use or redistribute this package
freely and without charge, subject to the following restrictions:

(1) The copyright notice must be retained in all copies of the source.

(2) Any changes made to the source must be clearly documented (such as
    by #ifdef's or by use of a source-code control system such as RCS or
    SCCS), so that the original version of the source as distributed by
    the author can be reconstructed if necessary and distinguished from
    modifications made by others.



struct parsedate
    {	long unixtime;	/* UNIX internal representation of time */
	char *error;	/* NULL = OK; non-NULL = error */
	int year;	/* year (1600 on) */
	int month;	/* month (1-12) */
	int day;	/* day of month (1-31) */
	int hour;	/* hour (0-23) */
	int minute;	/* minute (0-59) */
	int second;	/* second (0-59) */
	int zone;	/* time zone offset in minutes -- "+" or "-" */
	int dst;	/* daylight savings time (0 = no, 1 = yes) */
	int weekday;	/* real day of week (0-6; 0 = Sunday) */
	int c_weekday;	/* claimed day of week (0-6; 0 = Sunday) */
    };

struct parsedate *pd;

    /* Compute the day of the week.  The next several lines constitute a
     * perpetual-calendar formula.  Note, of course, that the "claimed"
     * day of the week (pd->c_weekday) is ignored here.
     */
    if (pd->year > 0 && pd->month > 0 && pd->day > 0)
    {	if (pd->month >= 3) n = pd->year / 100,
			    l = pd->year % 100;
	else                n = (pd->year-1) / 100,
			    l = (pd->year-1) % 100;
	a = (26 * ((pd->month+9)%12 + 1) - 2) / 10;
	weekday = (a+(l>>2)+(n>>2)+l-(n+n)+pd->day);
	while (weekday < 0) weekday += 7;
	pd->weekday = weekday % 7;
    }
.........................................................................
                                                     Kee Hinckley
                                               ...decvax!wivax!apollo!nazgul

roy@gitpyr.UUCP (Roy J. Mongiovi) (12/08/84)

> Make sure that whatever algorithm you get takes the following
> into account about leap years:
> 
> a) Any year divisible by 4 is a leap year, unless
> b) it is divisible by 100 as well, in which case it is NOT a leap
>    year, unless
> c) it is divisible by 400 as well, in which case it is a leap year
>    after all.
> 
> What stupid rules!

What do you mean "what stupid rules!" ?  It happens to be an approximation
of the ratio of the time it takes the earth to rotate around the sun to
the time it takes it to rotate on its axis.  Perhaps we should change one or
the other of those numbers to make the approximation more esthetic?  :-)

Also, you can carry the approximation one place further (if for any reason
you would need to) by taking into account that if the year is divisible by
4000, then it isn't a leap year after all.

On a slightly different track, you can determine the number of days preceeding
the beginning of a particular month by looking it up in a table, or by using
an interesting method known as Zeller's Congruence.  If you consider a year to
start on March 1 and continue through February 28 (or 29), it forms a pattern
which can be approximated to enough accuracy to ensure accurate results (all
the error is in the fraction).  The number of days preceeding the beginning of
month N is (153 * N + 2) / 5 when computed with integer operations.  The table
is probably the better idea, but Zeller's Congruence rather appeals to me
somehow....
-- 
Roy J. Mongiovi.	Office of Computing Services.		User Services.
Georgia Institute of Technology.	Atlanta GA  30332.	(404) 894-6163
 ...!{akgua, allegra, amd, hplabs, ihnp4, masscomp, ut-ngp}!gatech!gitpyr!roy

	  Who me?  I'm not even a REAL modo, I'm only a quasi-modo.

bees@drutx.UUCP (DavisRB) (12/12/84)

--
Don't forget the "leap second"!  Yes, these buggers really do exist,
but there is no algorithm for them.  Every so often, the National
Bureau of Standards declares another leap second.

I once had to write a date/time conversion routine for the National
Earthquake Information Service's Seismic Evaluation Data Analysis
System, that had to take leap seconds into account.

Ray Davis
AT&T Information Systems Laboratories, Denver
{ihnp4, houxe, stcvax!ihnp4}!drutx!bees, (303)538-3991