1
I have a college assignment in which I must build a calendar on C from the date of Easter of a given year. I can find the date of Easter and correctly build the calendar in ASCII art, but I can’t find the day of the week of January 1st.
On this Wikipedia page(in English) is written:
The basic approximation of all methods consists of finding an 'anchor date': a known pair (e.g.: 1 January 1800, a Wednesday), determining the amount of days between the anchor date and the date you want to find, and using module 7 (%7 or mod 7) to find the code for the new day of the week.
I decided to apply this concept in my program, the snippet was 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;
But 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
which was Easter in the year 2013 so bissexto = 0
, the value of dia_semana
should be 2, since January 1, 2013 fell on a Tuesday. However, my snippet is returning 6 to dia_semana
.
My question is, is there an error in my code or in this concept that I used? I would also love to know if there is any other C code implementation that can help me.
I was doubtful in this, even to calculate the 'distance' between two dates, can the concept of progress and regression be applied? That wouldn’t give me a negative day count in case of setback?
– g.carvalho97
Yeah, it’ll be a negative date. I have no problem with negative date ... 0 April 2015 corresponds to 31 March ... -4 May 2015 corresponds to 26 April
– pmg
Still January 1 is not correct, for the year 2013 it falls on a Sunday. I’m beginning to think that the problem is in the concept...
– g.carvalho97
31 - 0 - 28 - 31
is -28, and700 - 28
gives 672. 672 % 7 is 0. E31 % 7
is 3– g.carvalho97
You have to take the days of the current month. For April 20 (2014), the account is
20 - 20 - 0 - 31 - 28 - 31 + 1
– pmg