[comp.unix.wizards] Date conversion question

paul@devon.UUCP (02/17/87)

I seem to recall that there is a simple formula for converting a
date in MMDDYY format to YYMMDD; i.e. something like:

	YYMMDD_date = MMDDYY_date * some_number

(maybe not quite that simple).  Did I just dream this up, or is
there really such a formula.

And, (the obvious question), if there is one, can someone e-mail
it to me?

- paul

-- 
Paul Sutcliffe, Jr.	    paul@devon.UUCP	(or, if you prefer:)
Devon Computer Services	    {seismo,ihnp4,allegra,rutgers}!cbmvax!devon!paul
Allentown, PA
		"I love work.  I could sit and watch people do it all day!"

decot@hpisod2.UUCP (02/21/87)

> I seem to recall that there is a simple formula for converting a
> date in MMDDYY format to YYMMDD; i.e. something like:
> 
> 	YYMMDD_date = MMDDYY_date * some_number

YYMMDD_date = MMDDYY_date * 1000001L / 100 % 1000000L;

Dave Decot
Hewlett-Packard Company
hpda!decot

mkhaw@teknowledge-vaxc.UUCP (02/24/87)

Is there a clever formula that will give you the day of the week (as a number
between 1 and 7 or 0 and 6) given year, month, and date?

I've only been able determine day of week from date by using a reference date
like: Jan. 1, 1972, was a Saturday -- and computing the offset from it (and
keeping track of leap days).

Mike Khaw


-- 
internet:	mkhaw@teknowledge-vaxc.arpa
usenet:		{hplabs|sun|ucbvax|decwrl|sri-unix}!mkhaw%teknowledge-vaxc.arpa
voice:		415/424-0500
USnail:		Teknowledge, Inc., 1850 Embarcadero Rd., POB 10119, Palo Alto, CA 94303

garyp@cognos.UUCP (Gary Puckering) (03/02/87)

In article <9945@teknowledge-vaxc.ARPA> mkhaw@teknowledge-vaxc.UUCP (Michael Khaw) writes:
>Is there a clever formula that will give you the day of the week (as a number
>between 1 and 7 or 0 and 6) given year, month, and date?

This question came up in September of last year.  Here is the response I
submitted at that time:

There is an formula known as Zeller's Congruence which can be used to
calculate the day of the week given any date.  I found this somewhere,
years ago when I was a teenager, memorized it and never forgot it.  I
know it works for any year after 1700, maybe even earlier.  I can't
remember where it came from (I did well to remember the algorithm!).
Since it uses only integer addition and division, and one comparison
operation, its fairly cheap to implement.

Here it is:

	Let k be the day of the month
	    m be the month (March=1, April=2, ... December=10, January
			    and February are months 11 and 12 of the
			    previous year)
	    C be the century
	    D be the year of the century (adjusted according to m)
	    Z be the day of the week (0=Sunday, 6=Saturday)

	Then:

	  Z = { (26m - 2)//10 + k + D + D//4 + C//4 - 2C } mod 7

	  where // represents integer division with truncation


	Example:  for February 28, 1986

		k = 28
		y = 1986
		m = 2 - 2 = 0
		if m<1 then m=m+12, y=y-1
		C = 19
		D = 85

	Therefore:

		Z = { (26*12)//10 + 28 + 85 + 85//4 + 19//4 - 2*19} mod 7

		  = {      31     + 28 + 85 +  21   +   4   -  38 } mod 7
		  = { 131 } mod 7

		  = 5 (Friday)

-- 
Gary Puckering        3755 Riverside Dr.
Cognos Incorporated   Ottawa, Ontario       decvax!utzoo!dciem!
(613) 738-1440        CANADA  K1G 3N3       nrcaer!cognos!garyp

escott%deis.uci.edu@ROME.UCI.EDU (Scott Menter) (03/06/87)

Gary Puckering <garyp@cognos.UUCP> writes:

> There is an formula known as Zeller's Congruence which can be used to
> calculate the day of the week given any date.  I found this somewhere,
>
> Gary Puckering        3755 Riverside Dr.
> Cognos Incorporated   Ottawa, Ontario       decvax!utzoo!dciem!
> (613) 738-1440        CANADA  K1G 3N3       nrcaer!cognos!garyp

I'm just the opposite -- I couldn't remember the formula, but I do remember
(more or less) where it came from.  I saw it in "The Best of Creative
Computing", either Volume I or Volume II.  The book was an anthology of
articles in Creative Computing magazine, a favorite of mine at the time
(okay, so I was in junior high school).  I wonder if it still exists...

+-------------------------------------------------------------------------+
 Scott Menter  UCI ICS Computing Support Group   Univ. of Calif. at Irvine
                     (714) 856 7552              Irvine, California  92717

 Internet:  escott@ics.uci.edu             UUCP:  ...!ucbvax!ucivax!escott
 Bitnet:    escott@uci               CSNet: escott%ics.uci.edu@csnet-relay
 Internet (with Name Server):  TBA
+-------------------------------------------------------------------------+

authorplaceholder@tiger.UUCP.UUCP (03/10/87)

	I pulled this off of the net a few years ago so here it is
	again. No flames about this belonging in net.sources.

	
/* 
 * test as 
 * zeller month day year
 * year in 1987 form
 */
main(argc,argv)
int	argc;
char 	*argv[];
{
	char *zeller();
	char *day;

	day = zeller(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]));
	printf("Day = %s\n",day);
	exit(0);
}
 
/****************************************************************
Compute day of week given any date. Year passed as e.g. 1984.
Algorithm by Zeller.
****************************************************************/
char *
zeller (m,d,y)
int m,d,y; {
 
static char *day_wk[] =
 { "Sun","Mon","Tue","Wed","Thu","Fri","Sat" };
int y0, y1, y2, m1;
int dbuf;
 
m1 = ((m+9) % 12) + 1;
y0 = (m <= 2)?(y-1):y;
y1 = y0 / 100;
y2 = y0 % 100;
 
dbuf = ( (26*m1-2)/10 + d + y2 + y2/4 + y1/4 - 2*y1 ) % 7;
if (dbuf < 0) dbuf += 7;
return day_wk[dbuf];
}