From: ism@metagen.uucp (Ian Miller)
Subject: Re: Determining the Day of the Week
Message-ID: <184@babbage.metagen.uucp>
Date: 25 May 93 18:01:23 GMT
Organization: Meta_Generics Ltd, Cambridge, UK

The simplest technique is probably to convert to a Julian Day number and
then take the result mod7.  This has the advantage that you can
do a lot of other things with Julian Day number as well.  The best
algorithm I know of was published as a letter from Henry F Fliegel and
Thomas C van Flandern in the October 1968 (vol 11 #10) issue of the 
Communications of the ACM.

Julian Day Number = day - 32075 + 1461 * (year + 4800 - (14 - month)/12)/4 +
      367 * (month - 2 + (14 - month)/12*12)/12 -
      3 * ((year + 4900 - (14 - month)/12)/100)/4

This is valid for all dates in the Gregorian Calendar up to 28 Feb 4000.
(I have modified this algorithm slightly from the original to avoid dividing
negative numbers.  The original only worked in FORTRAN.  The modified version
should work in any language.)

They also quote the following reverse algorithm:-

temp1 = julian_date + 68569
temp2 = 4*temp1/146097
temp1 = temp1 - (146097 * temp2 + 3) / 4
year  = 4000 * (temp1 + 1) / 1461001
temp1 = temp1 - 1461 * year/4 + 31
month = 80 * temp1 / 2447
day   = temp1 - 247 * month / 80
temp1 = month / 11
month = month + 2 - 12 * temp1
year  = 100 * (temp2 - 49) + year + temp1

They do work.  Don't ask me how.


-- 
Ian Miller   Any replies to: ian_m@cix.compulink.co.uk

