#include <stdio.h>
#include <stdlib.h>
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
uint8_t ZellerFunction2Week(uint16_t year_local, uint8_t month_local, uint8_t day_local)
{
uint8_t week_output = 0;
uint8_t y, m, c;
if (month_local >= 3)
{
m = month_local;
y = year_local % 100;
c = year_local / 100;
}
else
{
m = month_local + 12;
y = (year_local - 1) % 100;
c = (year_local - 1) / 100;
}
int week = y + (y/4) + (c/4) - 2*c + ((26*(m+1))/10) + day_local - 1;
printf ("Before: week is %d\n\r", week);
if (week < 0)
{
week = 7 - (-week) % 7;
}
else
{
week = week % 7;
}
printf ("After: week is %d\n\r", week);
week_output = (uint8_t)week;
return week_output;
}
bool CheckDate(uint16_t year1, uint8_t month1, uint8_t day1)
{
bool check_result = false;
printf("CheckDate: the input date is %d-%d-%d\n\r\n", year1, month1, day1);
if(month1>12)
{
check_result = false;
return check_result;
}
else if(day1>31)
{
check_result = false;
return check_result;
}
else if((month1==2||month1==4||month1==6||month1==9||month1==11)&&day1==31)
{
check_result = false;
return check_result;
}
else if(month1==2&&day1==30)
{
check_result = false;
return check_result;
}
else if(month1==2&&day1==29)
{
if( ((year1 % 4 == 0) && (year1 % 100 != 0)) || (year1 % 400 == 0) )
{
check_result = true;
}
else
{
check_result = false;
return check_result;
}
}
else
{
check_result = true;
}
printf("CheckDate: %c\n\r", check_result?'y':'n');
return check_result;
}
#define WEEK_TEST_MUNUAL 1
int main()
{
#if WEEK_TEST_MUNUAL
unsigned int year_input = 0 ;
unsigned int month_input = 0;
unsigned int day_input = 0;
#else
uint8_t year_input = 0 ;
uint8_t month_input = 1;
uint8_t day_input = 1;
#endif
uint8_t week_get;
bool flag = 0;
#if WEEK_TEST_MUNUAL
do
{
printf ("please input year(1byte: 0 - 127):\n\r");
scanf("%u" , &year_input);
printf ("please input month(1byte: 1 - 12):\n\r");
scanf("%u" , &month_input);
printf ("please input day(1byte: 1 - 31):\n\r");
scanf("%u" , &day_input);
flag = CheckDate(year_input + 2000, month_input, day_input);
if(flag == 0)
{
printf("you input a wrong date! Please inpuu again!\n\r");
printf("\n\r");
}
}while(flag == 0);
#endif
printf ("You have inputed date is %u-%u-%u\n\r", year_input, month_input, day_input);
week_get = ZellerFunction2Week(year_input + 2000, month_input, day_input);
printf ("Get: week_get is %d\n\r", week_get);
system("pause");
return 0;
}
执行结果:
