×
单片机 > 单片机程序设计 > 详情

RTC--根据年月日计算[星期]的函数

发布时间:2020-09-03 发布时间:
|

一、


u8 const table_week[12]={0,3,3,6,1,4,6,2,5,0,3,5}; //月修正数据表    


 u8 RTC_Get_Week(u16 year,u8 month,u8 day)

 u16 temp2;

 u8 yearH,yearL;

 

 yearH=year/100; yearL=year%100; 

 // 如果为21世纪,年份数加100  

 if (yearH>19)yearL+=100;

 // 所过闰年数只算1900年之后的  

 temp2=yearL+yearL/4;

 temp2=temp2%7; 

 temp2=temp2+day+table_week[month-1];

 if (yearL%4==0&&month<3)temp2--;

 return(temp2%7);

}


月修正数据表 table_week[12]={0,3,3,6,1,4,6,2,5,0,3,5};


如果


1月1日是星期一,


2月1日是星期四,4-1=3


3月1日是星期四,4-1=3


4月1日是星期日,7-1=6


依次类推。前提是这一年是平年。



 yearH=year/100; yearL=year%100; 

 // ------------------------如果为21世纪,年份数加100 ---------------------------

 if (yearH>19)yearL+=100;  


//-----------year/4是闰年的年数,平年一年365天,365%7=1;temp2算出自1900年以来积累的多出来的天数----------

temp2=yearL+yearL/4; 


//---------------------算比1900年1月1日多出来的天数----------------------------

temp2=temp2+day+table_week[month-1];


//---------------润年1月,2月要 -1,因为yearL/4已把当年多出来的一天计算了,三月份以后才能计算加的一天---------------------------

if (yearL%4==0&&month<3)temp2--;


//---------------------算出日期----------------------------

 return(temp2%7);



二、


          u8 RTC_Get_Week(u16 year,u8 month,u8 day)

          {

               static u8 no_leap_year[12]={6,2,2,5,0,3,5,1,4,6,2,4};//非闰年

               static u8 leap_year[12]={5,1,2,5,0,3,5,1,4,6,2,4};//闰年  

               u8 temp2;

               u8 yearL;

               yearL=year0;

               yearL=(yearL/4+yearL)%7;

               temp2=Is_Leap_Year(year)?leap_year[month-1]:no_leap_year[month-1]; 

               return ((day+temp2+yearL)%7); 

          }



三、


         u8 RTC_Get_Week(u16 year,u8 month,u8 day)

         {

             u16 W;

             u8 m;

             u8 yearH=year/100,yearL=year0;

             if(month<3)m=month+12;

             else m=month;

             W=((yearH/4)-(2*yearH)+yearL+(yearL/4)+(26*(m+1)/10)+day-1)%7; 

             return ((u8)W); 

         }




『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多