Linux时间戳转换成BCD码(转载)

时间:2020-11-30 06:18:29
 #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
/***************************************************************************
*函数名称:Char2Bcd
*函数介绍:字符串转BCD
*输入参数:-buf:指向字符串
*输出参数:无
*返回值 :转换后的双精度值
*修改历史:
***************************************************************************/
char Char2Bcd(char i_cHexData)
{
char i;
i=((i_cHexData%)/)*+(i_cHexData%);
return i;
} /***************************************************************************
*函数名称:Get_BCDTime7
*函数介绍:转换为BCD码格式时间
*输入参数:-*time:
*输出参数:无
*返回值 :无
*修改历史:
***************************************************************************/
void Get_BCDTime7(time_t i_tTime,char* o_cpBcdTime)
{
struct tm *time;
time=localtime(&i_tTime);
o_cpBcdTime[]=Char2Bcd((time->tm_year+)/);
o_cpBcdTime[]=Char2Bcd((time->tm_year+)%);
o_cpBcdTime[]=Char2Bcd(time->tm_mon+);
o_cpBcdTime[]=Char2Bcd(time->tm_mday);
o_cpBcdTime[]=Char2Bcd(time->tm_hour);
o_cpBcdTime[]=Char2Bcd(time->tm_min);
o_cpBcdTime[]=Char2Bcd(time->tm_sec);
} /***************************************************************************
*函数名称:Get_CurBCDTime6
*函数介绍:转换为BCD码格式时间
*输入参数:-*time:
*输出参数:无
*返回值 :无
*修改历史:
***************************************************************************/
void Get_BCDTime6(time_t i_tTime,char* o_cpBcdTime)
{
struct tm *time;
time=localtime(&i_tTime);
o_cpBcdTime[]=(unsigned char)((((time->tm_year-)/)<<)|((time->tm_year-)%));
o_cpBcdTime[]=(unsigned char)((((+time->tm_mon)/)<<)|((+time->tm_mon)%));
o_cpBcdTime[]=(unsigned char)((((time->tm_mday)/)<<)|((time->tm_mday)%));
o_cpBcdTime[]=(unsigned char)((((time->tm_hour)/)<<)|((time->tm_hour)%));
o_cpBcdTime[]=(unsigned char)((((time->tm_min)/)<<)|((time->tm_min)%));
o_cpBcdTime[]=(unsigned char)((((time->tm_sec)/)<<)|((time->tm_sec)%));
} /***************************************************************************
*函数名称:Get_CurBCDTime6
*函数介绍:获取当前BCD码格式时间
*输入参数:-*time:
*输出参数:无
*返回值 :无
*修改历史:add by lianzihao 2016-09-13
***************************************************************************/
void Get_CurBCDTime6(char* o_cpBcdTime)
{
time_t lt=;
lt = time(NULL);
struct tm * pCurTime = localtime(&lt);
o_cpBcdTime[]=(unsigned char)((((pCurTime->tm_year-)/)<<)|((pCurTime->tm_year-)%));
o_cpBcdTime[]=(unsigned char)((((+pCurTime->tm_mon)/)<<)|((+pCurTime->tm_mon)%));
o_cpBcdTime[]=(unsigned char)((((pCurTime->tm_mday)/)<<)|((pCurTime->tm_mday)%));
o_cpBcdTime[]=(unsigned char)((((pCurTime->tm_hour)/)<<)|((pCurTime->tm_hour)%));
o_cpBcdTime[]=(unsigned char)((((pCurTime->tm_min)/)<<)|((pCurTime->tm_min)%));
o_cpBcdTime[]=(unsigned char)((((pCurTime->tm_sec)/)<<)|((pCurTime->tm_sec)%));
}
/***************************************************************************
*函数名称:Get_CurBCDTime7
*函数介绍:获取当前BCD码格式时间
*输入参数:-*time:
*输出参数:无
*返回值 :无
*修改历史:
***************************************************************************/
void Get_CurBCDTime7(char* o_cpBcdTime)
{
time_t tt;
struct tm *curtime;
time(&tt);
curtime=localtime(&tt);
o_cpBcdTime[]=Char2Bcd((curtime->tm_year+)/);
o_cpBcdTime[]=Char2Bcd((curtime->tm_year+)%);
o_cpBcdTime[]=Char2Bcd(curtime->tm_mon+);
o_cpBcdTime[]=Char2Bcd(curtime->tm_mday);
o_cpBcdTime[]=Char2Bcd(curtime->tm_hour);
o_cpBcdTime[]=Char2Bcd(curtime->tm_min);
o_cpBcdTime[]=Char2Bcd(curtime->tm_sec);
} #define LEN 7
int main(void)
{
int i = ;
char l_arrBcdtime[LEN] ={};
Get_CurBCDTime7(l_arrBcdtime);
printf("Get_CurBCDTime7 :");
for(i=;i<LEN;i++)
{
printf("%02x",l_arrBcdtime[i]);
} Get_CurBCDTime6(l_arrBcdtime); printf("\nGet_CurBCDTime6 :");
for(i=;i<LEN-;i++)
{
printf("%02x",l_arrBcdtime[i]);
}
printf("\n");
return ;
} 运行结果:

Linux时间戳转换成BCD码(转载)

注:原文链接:https://blog.csdn.net/li_wen01/article/details/78990124