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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/p1279030826/article/details/83901765

一、

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); 
         }

猜你喜欢

转载自blog.csdn.net/p1279030826/article/details/83901765
今日推荐