计算世纪秒的算法(基于1970-1-1)

 /*为时间定义*/
#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 ;
}

 

改进点:

    该方法通过将年每次相加,可以提供统一数组来提供,这样只需要一次访问就可以,大大提高访问效率,减少内存访问次数。

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值