在lpc1778/1788上实现
/********************************************************************************************************
* FunctionName : GetSecondTime()
* Description : 日期格式转化为unix time
* EntryParameter :
* ReturnValue :
********************************************************************************************************/
unsigned long GetSecondTime(void)
{
uint16_t rYear,i,Cyear=0;
uint8_t rMon,rDay,rHour,rMin,rSec;
unsigned long CountDay = 0;
rSec =(uint8_t)((LPC_RTC->SEC)&0x7f);
rMin =(uint8_t)((LPC_RTC->MIN)&0x7f);
rHour = ((LPC_RTC->HOUR) & 0x3f);
rDay = ((LPC_RTC->DOM) & 0x3f);
rMon = ((LPC_RTC->MONTH) & 0x1f);
rYear = ((LPC_RTC->YEAR));
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, TIME_T *tTime)
{
uint16 i,j,iDay;
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++;
}
tTime->year = i;//年份
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;
}
tTime->month = j+1;
tTime->day = lDay+1;
tTime->hour = ((lSec / 3600))%24;
tTime->min = (lSec % 3600) / 60;
tTime->sec = (lSec % 3600) % 60;
}