[net.unix] Dates and Times

lcc.jbrown@LOCUS.UCLA.EDU (Jordan Brown) (09/03/86)

> Is there also a simple routine that will allow me to calculate
> what day of the week an arbitrary date falls on?

Once you have Julian days (or any other days-since-some-base-date scheme)
you just add some appropriate small constant (0-6) and take it modulo 7.
The small constant depends on what day of the week your base date is;
I usually just fiddle with it until I get today right.  (I can NEVER
remember what the right value is.  I THINK that for Julian the right
value is 6, but am not at all sure.)

crocker@ihwpt.UUCP (ron crocker) (09/05/86)

> Is there also a simple routine that will allow me to calculate
> what day of the week an arbitrary date falls on?

This is from the back of my calendar, and it still sounds like
magic to me, but it works...

(From "The Ready Reference (r) Weekly Planner 1986 ((c) 1986 Ready
Reference)
 
"To Find Day of Week of Any Date

This formula is correct for any date after September 14, 1752

EXAMPLE - WHAT DAY OF WEEK WAS JANUARY 10th, 1946?

(1) Take the last two digits of the year			[46]

(2) Add a quarter of this number, neglecting any remainder	[11]

(3) Add the date in the month					[10]

(4) Add according to the month -				[ 1]
	Jan - 1 (Leap: 0)	May  - 2	Sept - 6
	Feb - 4 (Leap: 3)	June - 5	Oct  - 1
	Mar - 4			July - 0	Nov  - 4
	Apr - 0			Aug  - 3	Dec  - 6
	
(5) Add for the Century -					[ 0]
	18th - 4	19th - 2
	20th - 0	21st - 6
	
							[Total:  68]
(6) Divide total by 7 - Remainder gives the day		[68mod7 = 5]
	1 - Sunday	2 - Monday	3 - Tuesday
	4 - Wednesday	5 - Thursday	6 - Friday
	7 - Saturday
	
ANSWER - THURSDAY"

Ok, so it's magic.  It seems to work.  For later than 21st century,
I would suggest trial/error method - there are only 7 choices...

-- 
Ron Crocker
AT&T Bell Laboratories
Room IH 6D535
Naperville-Wheaton Road
Naperville, IL  60566

(312) 979-4051

lcc.jbrown@LOCUS.UCLA.EDU (Jordan Brown) (09/09/86)

> > Is there also a simple routine that will allow me to calculate
> > what day of the week an arbitrary date falls on?

> [ magical method for finding day of week ]

ACM 199 is somewhat more suitable for computer use (though MUCH less
suitable for human use) because it does not use any tables.  It's not
suitable for human use because it uses several large apparently random
integers.

I've typed in and tested a probably portable C version of ACM 199
(calendar to julian and julian to calendar conversions), if anybody
wants a copy.

ps:  ACM 199 is magic too...  probably more so.

garyp@cognos.UUCP (Gary Puckering) (09/09/86)

> > Is there also a simple routine that will allow me to calculate
> > what day of the week an arbitrary date falls on?
> 
> This is from the back of my calendar, and it still sounds like
> magic to me, but it works...
> 
> ...

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
COGNOS Incorporated
3755 Riverside Dr.
Ottawa, Ontario
Canada  K1G 3N3

Telephone: (613) 738-1440
Telex: 053-3341