1

Following function represents Sakamoto's Algorithm from Wikipedia page- http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week

int dow(int y, int m, int d) {
    /* 1 <= m <= 12,  y > 1752 (in the U.K.) */
    static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
    y -= m < 3;  
    return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}

i do not understand how does the algorithm work? especially, the t[] array.

2 Answers2

2

The explanation is quite straightforward:

http://en.wikipedia.org/wiki/Leap_year

if (year is not divisible by 4) then (it is a common year)
else
if (year is not divisible by 100) then (it is a leap year)
else
if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)

t[] is a table with "#/days" offset for each month.

FoggyDay
  • 11,962
  • 4
  • 34
  • 48
2

The result of a comparison is an int containing 0 or 1. So this line

y -= m < 3;  

means "if the month is January or February, the leap day for this year has not yet occurred".

luser droog
  • 18,988
  • 3
  • 53
  • 105