/*为时间定义*/
#define FEBRUARY 2
#define STARTOFTIME 1970
#define SECDAY 86400L
#define SECYR (SECDAY * 365)
static int month_days[12] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
#define LEAPYEAR(year) ((year) % 4 == 0 && \
((year) % 100 != 0 || (year) % 400 == 0))
#define DAYS_IN_YEAR(a) (LEAPYEAR(a) ? 366 : 365)
#define DAYS_IN_MONTH(a) (month_days[(a) - 1])
/*日期数据结构*/
typedef struct
{
uint16_t unMicroSec; /* 0-999 */
uint16_t unMSEL; /* 0-999 */
uint8_t ucSec; /* 0-59 */
uint8_t ucMinute; /* 0-59 */
uint8_t ucHour; /* 0-23 */
uint8_t ucDate; /* 1-31 */
uint8_t ucWeekDay;
uint8_t ucMonth;
uint16_t unYear;
} DATE_TIME;
/*计算SOC*/
void TM_Time_To_Soc( DATE_TIME* pdttmRslt, UINT32 *pu32Soc)
{
int i;
unsigned long ulRslt=0;
for(i=STARTOFTIME; i<pdttmRslt->unYear; i++)
{
ulRslt += DAYS_IN_YEAR(i);
}
if (LEAPYEAR(pdttmRslt->unYear))
DAYS_IN_MONTH(FEBRUARY) = 29;
for (i = 1; i<pdttmRslt->ucMonth; i++)
ulRslt += DAYS_IN_MONTH(i);
DAYS_IN_MONTH(FEBRUARY) = 28;
ulRslt += pdttmRslt->ucDate- 1;
ulRslt = ulRslt * SECDAY;
ulRslt += pdttmRslt->ucHour*3600+pdttmRslt->ucMinute*60+pdttmRslt->ucSec;
*pu32Soc = ulRslt;
return ;
}
改进点:
该方法通过将年每次相加,可以提供统一数组来提供,这样只需要一次访问就可以,大大提高访问效率,减少内存访问次数。