Date functionality

Engbert Gerrit IJff engbert at cs.vu.nl
Thu Aug 9 04:54:52 AEST 1990


In article <357 at taumet.com>,
	steve at taumet.com (Stephen Clamage) writes:
) fpb at ittc.wec.com (Frank P. Bresz) writes:
) 
) >	I am looking for an algorithm to give me the day of the week when
) >handed the current date.  I can't use any builtin routines from any OS
) >(this is targeted for a real brain-damaged machine).
) 
) Here is an algorithm known as Zeller's congruence.
) 
) The day of the week (0 = Sunday) is given by
)   W = ((26*M - 2) / 10 + D + Y + Y/4 + C/4 - 2*C) % 7
)   where
) 	C is the century
) 	Y is the last two digits of the year
) 	D is the day of the month
) 	M is a special month number, where Jan and Feb are taken as
) 	  month 11 and 12 of the previous year
)   and each division is truncating division, and cannot be combined.
) -- 
) 
) Steve Clamage, TauMetric Corp, steve at taumet.com

Another point, I forgot to mention in my previous followup,
is that the number on which you do the % operation may be
negative, and so may be the result. Using the result as an index
to a table of day names then may cause a program crash.
Instead, use    W = ((...expression...) % 7 + 7) % 7


Bert IJff.



More information about the Comp.lang.c mailing list