当前位置:网站首页>51 SCM time stamp correlation function

51 SCM time stamp correlation function

2022-06-25 09:34:00 silno

The function uses long Variable , Comparative proportion RAM, If the space of single-chip microcomputer is tight, don't use it , Will be able to mcu It's silly .


/******* timestamp Time stamp function Start **********/

#define  SECOND_OF_DAY  86400   // How many seconds a day

idata uchar DayOfMon[]={31,28,31,30,31,30,31,31,30,31,30,31};


/********************************************************************************************************


* FunctionName   : GetSecondTime()
* Description    : Date format is converted to 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<rMon; 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;// Beijing time is transformed into world time
  return CountDay;

}




/********************************************************************************************************
* FunctionName   : GetDateTimeFromSecond()
* Description    : unix time Convert to common format
* EntryParameter : 
* ReturnValue    : 
********************************************************************************************************/
void GetDateTimeFromSecond(unsigned long lSec)
{
uint i,j,iDay;
idata unsigned long lDay;




lSec = lSec + 8*3600;// Convert to Beijing time

  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))// Leap year
{
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;// year
  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;  // month
     RTC[2] = lDay+1;// Japan
     RTC[4] = ((lSec / 3600))%24;  // when
     RTC[5] = (lSec % 3600) / 60;  // branch
     RTC[6] = (lSec % 3600) % 60;  // second



}






/******** Time stamp function end ***********/
原网站

版权声明
本文为[silno]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200551257148.html

随机推荐