函数使用了long变量,比较占RAM,单片机要是空间紧张就别用了,会把mcu算糊涂的。
/******* timestamp时间戳函数 开始**********/#define SECOND_OF_DAY 86400 //一天多少秒idata uchar DayOfMon[]={31,28,31,30,31,30,31,31,30,31,30,31};
/********************************************************************************************************
* FunctionName : GetSecondTime()
* Description : 日期格式转化为unix time
* EntryParameter :
* ReturnValue :
********************************************************************************************************/
unsigned long GetSecondTime(void)
{
idata uint rYear,i,Cyear=0;
idata uchar rMon,rDay,rHour,rMin,rSec;
idata unsigned long CountDay = 0;
rSec =BCD_to_DEC(RTC[6]);
rMin =BCD_to_DEC(RTC[5]);
rHour =BCD_to_DEC(RTC[4]);
rDay =BCD_to_DEC(RTC[2]);
rMon =BCD_to_DEC(RTC[1]);
rYear= 2000+BCD_to_DEC(RTC[0]);
for(i = 1970; i < rYear; i++)
{
if(((i%4==0) && (i%100!=0)) || (i%400==0)) Cyear++;
}
CountDay = Cyear * 366 + (rYear-1970-Cyear) * 365;
for(i=1; i {
if((i==2) && (((rYear%4==0)&&(rYear%100!=0)) || (rYear%400==0)))
CountDay += 29;
else
CountDay += DayOfMon[i-1];
}
CountDay += (rDay-1);
CountDay = CountDay*SECOND_OF_DAY + (unsigned long)rHour*3600 + (unsigned long)rMin*60 + rSec;
CountDay = CountDay - 8*3600;//北京时间转化为世界时间
return CountDay;
}
/********************************************************************************************************
* FunctionName : GetDateTimeFromSecond()
* Description : unix time转化为常用格式
* EntryParameter :
* ReturnValue :
********************************************************************************************************/
void GetDateTimeFromSecond(unsigned long lSec)
{
uint i,j,iDay;
idata unsigned long lDay;
lSec = lSec + 8*3600;//转换为北京时间
lDay = lSec / SECOND_OF_DAY;
lSec = lSec % SECOND_OF_DAY;
i = 1970;
while(lDay > 365)
{
if(((i%4==0) && (i%100!=0)) || (i%400==0))//闰年
{
lDay -= 366;
}else
{
lDay -= 365;
}
i++;
}
if((lDay == 365) && !(((i%4==0)&&(i%100!=0)) || (i%400==0)))
{
lDay -= 365;
i++;
}
RTC[0] = i-2000;//年份
for(j=0;j<12;j++)
{
if((j==1) && (((i%4==0)&&(i%100!=0)) || (i%400==0)))
{
iDay = 29;
}else
{
iDay = DayOfMon[j];
}
if(lDay >= iDay)
{
lDay -= iDay;
}
else
{
break;
}
}
RTC[1] = j+1; //月
RTC[2] = lDay+1;//日
RTC[4] = ((lSec / 3600))%24; //时
RTC[5] = (lSec % 3600) / 60; //分
RTC[6] = (lSec % 3600) % 60; //秒
}
/********时间戳函数 结束***********/