Вот (только форматирование подправить) (+)
(«Телесистемы»: Конференция «Микроконтроллеры и их применение»)

миниатюрный аудио-видеорекордер mAVR

Отправлено Delavar 21 октября 2003 г. 14:44
В ответ на: А кто встречал алгоритм расчета даты по известным месяцу, году, дню недели. Поделитесь плз отправлено allmaker 21 октября 2003 г. 14:26

/****************************************************************************/
/* This function calculate the day at current date */
/* output: the day (SUNDAY=1,MONDAY=2,..,SATURDAY=7) */
/****************************************************************************/
unsigned char calc_day(void) {
unsigned int elapsed;
char i;

elapsed=elapsed_days(date_day,date_month,date_year);
elapsed+=7; // elapsed days from 1/1/2000 which is saturday
i=(elapsed%7);
if (i==0) i=7;
return(i);
}


/****************************************************************************/
/* This function calculate the number of days elapsed from 1/1/2000 */
/* Input : date (day,month,year) */
/* output: the number of days elapsed */
/****************************************************************************/
unsigned int elapsed_days(unsigned char day, unsigned char month, unsigned char year) {
unsigned int elapsed;
char i;

elapsed=day-1;
for (i=month-1;i;i--)
elapsed+=days_in_month(i,year);
if (year)
for (i=year-1;i>=0;i--) {
elapsed+=365;
if ((i & 0x03) == 0) // leap year - February have one more day
elapsed++;
}
return(elapsed);
}

/****************************************************************************/
/* This function returns the number of days in a specific month */
/* input: month and year */
/* output: number of days in this month */
/****************************************************************************/
unsigned char days_in_month(unsigned char month, unsigned char year) {
// days in month table 1 2 3 4 5 6 7 8 9 10 11 12
code unsigned char MONTH_TABLE[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
unsigned char days;

if (month > 12) // not a valid month
return 0;
days = MONTH_TABLE[date_month];
days = MONTH_TABLE[month]; // get number of days for this month from table
if (month != 2) // all months have same numbr of days except for February
return (days);

// this is February - check if this is leap year
// if the year divides by 4 - this is a leap year. if it divide by 100 it is not leap year, except
// if it also divide by 400. therefore 2000 is a leap year, and the next year dividing by 4 which
// is not leap year will be 2100. this system include the years 2000 - 2099 (2 digits for the year)
// so we only need to check if it divides by 4.
if ((year & 0x03) == 0) // leap year - February have one more day
days++;
return (days);
}

Составить ответ  |||  Конференция  |||  Архив

Ответы



Перейти к списку ответов  |||  Конференция  |||  Архив  |||  Главная страница  |||  Содержание  |||  Без кадра

E-mail: info@telesys.ru