day of the week_en
Calculating the day of the week
To calculate the day of the week, we use Kim Larsen’s formula.
The formula for calculating the day of the week is based on determining the offset of a given date relative to January 1 AD (Sunday). This determines the offset for a given date (day of the week) of the first day of the year and the offset of the month. Then the sum of «year + month + date» is calculated, where «date» is the day of the month. The remainder of dividing the above amount by 7 will be the number of the day of the week.
«Date» — date, range 1-31
«month» — month, range 3-14, January and February of the current year should be treated as the 13th and 14th month of the previous year.
«year» is a year, while when the «month» is 1 or 2, the year «year» should be reduced by one.
Initial data:
int Set_year; // year
int Set_month; // month
int Set_day; // day
int ned; // day
of the week
The program code for calculating the day of the week on a given date:
byte a = f_div((14 — int(Set_month)), 12);
unsigned int y = (2000 + int(Set_year)) — a;
byte m = int(Set_month) + 12 * a — 2;
unsigned int y4 = f_div(y, 4);
byte y100 = f_div(y, 100);
byte y400 = f_div(y, 400);
byte x = f_div(31 * m, 12);
ned = (int(Set_day) + y + y 4y 100 + y400 + x) %7; //result in the «ned» variable
unsigned int f_div(unsigned int x, unsigned int y)
{
the result is unsigned int;
result = (x — (x % y)) / y;
the returned result;
}