1. 程式人生 > 其它 >蔡勒公式:計算星期

蔡勒公式:計算星期

/*蔡勒公式:w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1    計算的時間在1582年之後
c:世紀(年的高兩位數);                            輸入年月日,計算出周幾
y:年(年的低兩位數);                        
m:月(m大於等於3,小於等於14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月*/
u8 get_week(u8 *in_time_str,struct SD3078_TIMER *out_timer) //2022-03-09 03:26:49
{
    short century = 0,year = 0,month = 0
,day = 0,week = 0;//世紀 u8 tmp_data[20] = {0},len = 0; len = strlen(in_time_str); strncpy(tmp_data,in_time_str,len); out_timer->year = asc_int(tmp_data, 4); out_timer->month = asc_int(&tmp_data[5], 2); out_timer->day = asc_int(&tmp_data[8], 2); out_timer->hour = asc_int(&tmp_data[11
], 2); out_timer->minute = asc_int(&tmp_data[14], 2); out_timer->second = asc_int(&tmp_data[17], 2); century = asc_int(tmp_data,2); //20世紀 year = asc_int(&tmp_data[2],2); //22年 month = out_timer->month; if(month == 1 || month == 2) { if(month == 1
) month = 13; else if(month == 2) month = 14; if((year/10) || (year % 10)) year -= 1; //如果是2000年,那麼世紀減1:19 年份為:99 合計:1999 else { century -= 1; year = 99; } } week = year+(year/4)+(century/4)-2*century+(26*(month+1)/10)+out_timer->day-1; week = (week%7+7)%7; out_timer->week = week; printf("week:%d\n",week); return 0; }