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;

}

day of the week_ru

Вычисление дня недели

Для вычисления дня недели используем формулу Кима Ларсона.

Формула вычисления дня недели основана на определении смещения заданной даты относительно 1 января нашей эры (воскресенья). Таким образом определяется смещение для заданной даты (день недели) первого дня года и смещение месяца. Затем вычисляется сумма «год + месяц + дата», где «дата» – день месяца. Остаток от деления вышеприведенной суммы на 7 и будет номером дня недели.

«Дата» — дата, диапазон 1-31

«месяц» — месяц, диапазон 3-14, январь и февраль текущего года должны обрабатываться как 13-й и 14-й месяц предыдущего года.

«год» — год, при этом когда в «месяце» — 1 или 2, год «год» нужно уменьшить на единицу.

Исходные данные:

int Set_year; // год

int Set_month; // месяц

int Set_day; // день

int ned; // день
недели

Код программы для вычисления дня недели по заданной дате:

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 + y4 — y100 + y400 + x) % 7; //результат в переменной «ned»

unsigned int f_div(unsigned int x, unsigned int y)

{

unsigned int result;

result = (x — (x % y)) / y;

return result;

}

Яндекс.Метрика