时间戳和日期格式的转换

//将tick数转换成协议时间格式
void tick_to_tm(uint32_t tick,uint8_t *p_time)
{
    struct tm *p_tm = gmtime(&tick);//24小时制
    
    p_time[0] = p_tm->tm_year - 100;
    p_time[1] = p_tm->tm_mon + 1;
    p_time[2] = p_tm->tm_mday ;
    p_time[3] = p_tm->tm_hour ;
    p_time[4] = p_tm->tm_min ;
    p_time[5] = p_tm->tm_sec ;
//    NRF_LOG_DEBUG("%s",asctime(p_tm));
}
//将协议时间格式转换成tick数
void tm_to_tick(uint32_t *p_tick,uint8_t *p_time)
{
   int ret = 0;
   struct tm info;
   
   info.tm_year  = p_time[0] + 100;
   info.tm_mon   = p_time[1] - 1;
   info.tm_mday  = p_time[2];
   info.tm_hour  = p_time[3];
   info.tm_min   = p_time[4];
   info.tm_sec   = p_time[5];
   info.tm_isdst = -1;

   ret = mktime(&info);
   if( ret == -1 )
   {
       NRF_LOG_DEBUG("-----ERR:mktime----");
   }
   else
   {
      *p_tick = ret;
   }
}
//测试local time
void test_time_fun()
{
    uint8_t buff[6] = {0};
    uint32_t tick = 0;
    
    NRF_LOG_DEBUG("tick_to_tm............!");
    tick_to_tm(1566278055,buff);
    NRF_LOG_DEBUG("Year :%04d",2000+buff[0]);
    NRF_LOG_DEBUG("Month:%02d",buff[1]);
    NRF_LOG_DEBUG("Day  :%02d",buff[2]);
    NRF_LOG_DEBUG("Hour :%02d",buff[3]);
    NRF_LOG_DEBUG("Minu :%02d",buff[4]);
    NRF_LOG_DEBUG("Sec  :%02d",buff[5]);
    
    NRF_LOG_DEBUG("tm_to_tick............!");
    memset(buff,0,6);
    buff[0] = 19;
    buff[1] = 8;
    buff[2] = 20;
    buff[3] = 13;
    buff[4] = 14;
    buff[5] = 15;
    tm_to_tick(&tick,buff);
    NRF_LOG_DEBUG("tick:%d, differ:%d",tick,tick - 1566278055);
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_28851611/article/details/99843959