Authors
Publication
Pub Details
Date
Pages
While playing around with the function on paper there appears to be a slight error. I am sure that I will not be the first to see this but I will attempt to explain anyway.
The year part of the function Y + Y/4 – Y/100 + Y/400 is correct taking into account leap years, centuries and leap centuries. The month part however, 2m + 3(m+1)/5, is fine in most cases except for the way rounding occurs.
Consider 20 June 1995. We get:
30+2*6+3*7/5+1995+1995/4-1995/100+1995/400
30+12+4.2+1995+498.75-19.95+4.9875
2524.9875
The Modulus 7 of this is 4.9875. Rounded up to give 5, which equals Friday.
Now consider 2 July 1995. We get:
2+2*7+3*8/5+1995+1995/4-1995/100+1995/400
2+14+4.8+1995+498.75+4.9875
2499.5875
The Modulus 7 of this is 0.5875. Rounded up this give us 1, which equals Monday. This should be 0 for Sunday.
To correct this could I suggest the following function instead:
DEFine FuNction day_of_week$(d,m,y)
LOCal a
IF m=1 OR m=2 THEN
m=m+12
d=d-1
END IF
a = INT((d+2.6*m+y+y/4-y/100+y/400+0.8125) MOD 7)
IF a = 0 THEN RETurn "Sunday"
IF a = 1 THEN RETurn "Monday"
IF a = 2 THEN RETurn "Tuesday"
IF a = 3 THEN RETurn "Wednesday"
IF a = 4 THEN RETurn "Thursday"
IF a = 5 THEN RETurn "Friday"
IF a = 6 THEN RETurn "Saturday"
END DEFine
The 2.6*m is for the offset each month required due to months not being made up of an exact number of weeks. The 0.8125 is the initial adjustment to set Sundays correctly.