取得系统时间并以BCD形式保存到字符串中

  
    
#include < stdio.h >
#include
< time.h >

#define HEX2BCD(x) (((x) % 10) + ((((x) / 10) % 10) << 4)) /*20 -> 20H*/
#define UINT8 unsigned char

/*
功能:获取BCD时间
out: [out] 保存获取到的BCD时间,格式为:秒分时日月年(各占一字节,BCD格式)
*/
void getBCDTime(UINT8 * out )
{
time_t t;
int i = 0 ;
struct tm * tp = NULL;

t
= time(NULL);
tp
= localtime( & t);
out [i ++ ] = HEX2BCD(tp -> tm_sec);
out [i ++ ] = HEX2BCD(tp -> tm_min);
out [i ++ ] = HEX2BCD(tp -> tm_hour);
out [i ++ ] = HEX2BCD(tp -> tm_mday);
out [i ++ ] = HEX2BCD(tp -> tm_mon + 1 );
out [i ++ ] = HEX2BCD(tp -> tm_year - 100 );
}

int main()
{
UINT8
out [ 6 ] = { 0 };
int i = 0 ;
getBCDTime(
out );

while (i < 6 )
{
printf(
" %.2X " , out [i ++ ]);
}
printf(
" \n " );

return 0 ;
}

你可能感兴趣的:(系统时间)