I have a college job in which I should build a C calendar from the Easter date of a given year. I can find the date of Easter and build the calendar correctly in ASCII art, but I can not find the day of the week of January 1.
This Wikipedia page is written:
The basic approach to all methods is to find an 'anchor date': a known pair (for example: January 1, 1800, a Wednesday), determine the number of days between the anchor date and the date which you want to find, and use module 7 (% 7 or mod 7) to find the code for the new day of the week.
I then decided to apply this concept in my program, the snippet looked like this:
if(mes_pascoa == 3)
dia_semana = (dia_pascoa + bissexto + 28 + 31) % 7;
else if(mes_pascoa == 4)
dia_semana = (dia_pascoa + bissexto + 31 + 28 + 31) % 7;
However, the value of the day of the week is incorrect, even with the correct implementation of the code. For example, for mes_pascoa = 3
and dia_pascoa = 31
that was easter of 2013 therefore bissexto = 0
, the dia_semana
value should be 2, since January 1, 2013 dropped on a Tuesday. However, my snippet is returning 6 for dia_semana
.
My question is, is there a mistake in my code or in this concept I used? I would also like to know if there is any other C code implementation that can help me.