[comp.sys.mac] Week DAYS

hallett@shoreland.uucp (Jeff Hallett x4-6328) (09/07/89)

In article <8909061947.AA00416@vs04csc.UMD.EDU> russotto@wam.UMD.EDU writes:
>The easy answer is that the Mac OS does it from 1904.  The deeper reason
>for it, though, is that there is an easier leap year rule for dates
>starting in 1904 and going to a date < 2100.  That is, for those
>dates, a leap year is any year divisible by 4.  If you include 1900,
>a leap year is any year divisible by four that is either not divisible
>by 100, or is divisible by 400.

Interesting.  One thing I've always wondered about, though, is, given
a date in the Gregorian calendar, how does one determine the day of
the week?  There is obviously a brute force approach, but does someone
know (and will relate) an elegant solution?

Thanks

--
                Jeffrey A. Hallett, PET Software Engineering
                    GE Medical Systems, W641, PO Box 414
                            Milwaukee, WI  53201
          (414) 548-5163 : EMAIL -  hallett@positron.gemed.ge.com

barmar@think.COM (Barry Margolin) (09/07/89)

In article <972@mrsvr.UUCP> hallett@shoreland.UUCP (Jeff Hallett x4-6328) writes:
>Interesting.  One thing I've always wondered about, though, is, given
>a date in the Gregorian calendar, how does one determine the day of
>the week?

I don't know the formula offhand, but I believe it's called "Zeller's
Congruence".  It's a weird-looking, one-line formula that will
calculate the day of the week.

However, it's probably not much better than the brute force approach.
It's less clear and it requires floating point or decimal arithmetic,
whereas the brute force approach only needs integer arithmetic.

Barry Margolin
Thinking Machines Corp.

barmar@think.com
{uunet,harvard}!think!barmar

jeremyr@cs.qmc.ac.uk (Jeremy Roussak) (09/08/89)

In article <972@mrsvr.UUCP> hallett@shoreland.UUCP (Jeff Hallett x4-6328) writes:
>Interesting.  One thing I've always wondered about, though, is, given
>a date in the Gregorian calendar, how does one determine the day of
>the week?  There is obviously a brute force approach, but does someone
>know (and will relate) an elegant solution?
>
>Thanks


The formulat I've always used is

dofw = ( |(2.6m-0.2)| + d + y + |y/4| + c + |c/4| ) mod 7

where d is the date
      y is the year in the century (ie this year is 89)
      c is the century (ie 19)
  and m is the month, but Jan and Feb are months 11 and 12 of the preceding
        year (so  Jan 89 is 11/88 and April 89 is 2/89)

I always forget whether 0 is Saturday or Sunday and have to calibrate
the formular using a date I know (today always seems a good idea).

Hope this helps

Jeremy Roussak

jeremyr@cs.qmc.ac.uk (Jeremy Roussak) (09/08/89)

Whoops! I should have said in my last message that |x| means the integer
part of x (not abs(x), which I think is another possible meaning)

Jeremy Roussak

mclow@telesoft.telesoft.com (Marshall Clow @telesoft) (09/09/89)

In article <972@mrsvr.UUCP>, hallett@shoreland.uucp (Jeff Hallett x4-6328) writes:
> Interesting.  One thing I've always wondered about, though, is, given
> a date in the Gregorian calendar, how does one determine the day of
> the week?  There is obviously a brute force approach, but does someone
> know (and will relate) an elegant solution?

A trick I used in writing some routines was to notice that in any 400
year period there are 97 leap years and 303 non-leap years for a total
of  146097 days, which is divisible by 7! This means that Jan 01, 1 is
the same day of the week as Jan 01, 2001. ( Tuesday, if I remember ).
From there, you can grunt the answer out.

	Marshall Clow
	mclow@telesoft.com

ts@cup.portal.com (Tim W Smith) (09/10/89)

The formula is not too hard to work out.  If you want to, however, you
will find it much easier if you pretend that March 1 is the start of
the year.

If you want to find day-of-week from a given date in this century, there
is a simple method Martin Gardner gave once that is easy to do in your
head.  You need to memorize the following 12 numbers:

	1 4 4 0 2 5 0 3 6 1 4 6

Let's say you want the day of the week for M/D/19YY.  You first divide
YY by 12, given, say, A with a remainder of B.  Divide B by 4.
Add these numbers together.  In other words,

	YY/12 + YY%12 + (YY%12)/4

Let's call this N.

Now use the month, M, to look up a number in that table given above.
Add this to N.  Then add D.  Reduce this mod 7.  This gives you the
day of the week, with 0 == Saturday, etc.

For example, today is 9/10/1989.

	89/12 = 7
	89%12 = 5
 	 5/4  = 1
       ----------
	   N  = 13, which we can reduce mod 7 to 6.  You might as well
		remember this number, since it will be valid all year.

	Number for September ( 9th number in 1 4 4 0 2 5 0 3 6 1 4 6 )
	is 6.

	Thus, today is 6  + 6 + 10 = 22.

	Mod 7, this is 1, or Sunday.  Hey, it worked!

Another example, 12/7/1941.  41/12 = 3, 41%12 = 5, 5/4 = 1.
Number for December = 6.  Day of week is 9+6+7 = 1 mod 7
which is Sunday.

If the year is a leap year, modify the month table as follows for
that year:
	0 3 4 0 2 5 0 3 6 1 4 6

						Tim Smith