初级会员

- 积分
- 74
- 金钱
- 74
- 注册时间
- 2015-11-17
- 在线时间
- 5 小时
|
在RTC实时时钟库函数版本里的u8 RTC_Get(void)函数里有个小bug,会导致一个问题:当时间设定值为2016/12/31时,会转换出错,变成2017/13/1
出错的源码如下:
//得到当前的时间
//返回值:0,成功;其他:错误代码.
u8 RTC_Get(void)
{
static u16 daycnt=0;
u32 timecount=0;
u32 temp=0;
u16 temp1=0;
timecount=RTC_GetCounter();
temp=timecount/86400; //得到天数(秒钟数对应的)
if(daycnt!=temp)//超过一天了(若处于同一天则无需进入此循环)
{
daycnt=temp;
temp1=1970; //从1970年开始
while(temp>=365)
{
if(Is_Leap_Year(temp1))//是闰年
{
if(temp>=366)temp-=366;//闰年的秒钟数
else {temp1++;break;}
}
else temp-=365; //平年
temp1++;
}
calendar.w_year=temp1;//得到年份
temp1=0;
while(temp>=28)//超过了一个月
{
if(Is_Leap_Year(calendar.w_year)&&temp1==1)//当年是不是闰年/2月份
{
if(temp>=29)temp-=29;//闰年的秒钟数
else break;
}
else
{
if(temp>=mon_table[temp1])temp-=mon_table[temp1];//平年
else break;
}
temp1++;
}
calendar.w_month=temp1+1; //得到月份
calendar.w_date=temp+1; //得到日期
}
temp=timecount%86400; //得到秒钟数
calendar.hour=temp/3600; //小时
calendar.min=(temp%3600)/60; //分钟
calendar.sec=(temp%3600)%60; //秒钟
calendar.week=RTC_Get_Week(calendar.w_year,calendar.w_month,calendar.w_date);//获取星期
return 0;
}
出错的地方就在:
while(temp>=365)
{
if(Is_Leap_Year(temp1))//是闰年
{
if(temp>=366)temp-=366;//闰年的秒钟数
else {temp1++;break;}
}
}
这句 else {temp1++;break;}中多了temp1++;语句,导致设定时间为闰年的最后一天(如:2016/12/31)时,年份会多加一年,
最后,还会导致接下来月份计算是变成平年而多出一天,变成13月1号
说的不是很清楚,望大家自己琢磨琢磨咯。。。。。
|
|