一、把long型时间戳,转为6位BCD时间
源码:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
int timestamp2bcd(long timestamp, unsigned char bcd_buf[])
{
//时区不同.手动加八个小时
timestamp = timestamp + 28800; //北京时间
struct tm *info;
char buffer[20];
info = gmtime(×tamp);
strftime(buffer,20,"%y%m%d%H%M%S", info);
bcd_buf[0] = (unsigned char)((buffer[0] - '0')<<4) + (buffer[1] - '0');
bcd_buf[1] = (unsigned char)((buffer[2] - '0')<<4) + (buffer[3] - '0');
bcd_buf[2] = (unsigned char)((buffer[4] - '0')<<4) + (buffer[5] - '0');
bcd_buf[3] = (unsigned char)((buffer[6] - '0')<<4) + (buffer[7] - '0');
bcd_buf[4] = (unsigned char)((buffer[8] - '0')<<4) + (buffer[9] - '0');
bcd_buf[5] = (unsigned char)((buffer[10] - '0')<<4) + (buffer[11] - '0');
#if 1
printf("buffer :%s \n", buffer);
int i;
for(i=0; i<6; i++)
{
printf("bcd_buf[%d] :%02x \n", i, bcd_buf[i]);
}
printf("\n");
#endif
return 0;
}
int main()
{
unsigned char bcd_buf[6]= {0};
timestamp2bcd(1561105427, bcd_buf);
return 0;
}
编译测试:
root@ubuntu:/# gcc -o test test.c
root@ubuntu:/# ./test
buffer :190621162347
bcd_buf[0] :19
bcd_buf[1] :06
bcd_buf[2] :21
bcd_buf[3] :16
bcd_buf[4] :23
bcd_buf[5] :47
root@ubuntu:/#
BCD码时间格式为:yymmddHHMMSS
二、获取当前时间戳
源码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main ()
{
time_t tmp;
long timestamp;
timestamp = time(&tmp);
printf("timestamp :%ld\n", timestamp);
return(0);
}
编译测试:
root@ubuntu:/# gcc -o test test.c
root@ubuntu:/# ./test
timestamp :1561116583
root@ubuntu:/# ./test
timestamp :1561116584
root@ubuntu:/# ./test
timestamp :1561116585
root@ubuntu:/# ./test
timestamp :1561116585
root@ubuntu:/# ./test
timestamp :1561116586
root@ubuntu:/#
三、获取当前时间,并转为BCD码
源码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int timestamp2bcd(long timestamp, unsigned char bcd_buf[])
{
//时区不同.手动加八个小时
timestamp = timestamp + 28800; //北京时间
struct tm *time;
time = gmtime(×tamp);
char buffer[20];
strftime(buffer,20,"%y%m%d%H%M%S", time);
bcd_buf[0] = (unsigned char)((buffer[0] - '0')<<4) + (buffer[1] - '0');
bcd_buf[1] = (unsigned char)((buffer[2] - '0')<<4) + (buffer[3] - '0');
bcd_buf[2] = (unsigned char)((buffer[4] - '0')<<4) + (buffer[5] - '0');
bcd_buf[3] = (unsigned char)((buffer[6] - '0')<<4) + (buffer[7] - '0');
bcd_buf[4] = (unsigned char)((buffer[8] - '0')<<4) + (buffer[9] - '0');
bcd_buf[5] = (unsigned char)((buffer[10] - '0')<<4) + (buffer[11] - '0');
return 0;
}
int main ()
{
time_t tmp;
long timestamp = time(&tmp);
printf("timestamp :%ld\n", timestamp);
unsigned char BCD_Time[6] = {0};
timestamp2bcd(timestamp, BCD_Time);
int i;
printf("BCD_Time :");
for(i=0; i<6; i++)
{
printf("%02x", BCD_Time[i]);
}
printf("\n");
return(0);
}
编译测试:
root@ubuntu:/# gcc -o test test.c
root@ubuntu:/# ./test
timestamp :1561117002
BCD_Time :190621193642
root@ubuntu:/# ./test
timestamp :1561117009
BCD_Time :190621193649
root@ubuntu:/# ./test
timestamp :1561117010
BCD_Time :190621193650
root@ubuntu:/# ./test
timestamp :1561117011
BCD_Time :190621193651
root@ubuntu:/# ./test
timestamp :1561117012
BCD_Time :190621193652
root@ubuntu:/# ./test
timestamp :1561117013
BCD_Time :190621193653
root@ubuntu:/#
参考链接:
https://wiki.jikexueyuan.com/project/c/strftime.html