Advertisement

Linux 各种BCD时间与Unix时间戳格式转换

阅读量:

在Linux环境下进行C或C++编程时(由于不同的应用场景需求),通常会遇到多种类型的时间表示问题(常见地遇到)。其中(有时会使用到)BCD形式的时间表示(有时会使用到),还有Unix系统中的文件访问时刻(也称为绝对时间和访问时刻)。具体讲解几种常见的BCD、Unix及绝对时间格式间的转换应用。

Unix时间戳:

Unix时间戳作为一个绝对数值表示自公历1970年1月1日午夜零时起的时间长度,在C/C++语言中使用数据类型time_t来表示时间戳,time_t变量实际上等同于一个long整数类型 获取当前时间的时间戳代码如下所示

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, const char * argv[])
{
time_t now;
time(&now);
printf("Now:%ld \n",now);
}
行结果为:

biao@ubuntu:~/test/test ./a.out Now:1515229831 biao@ubuntu:~/test/test
BCD时间:

所谓BCD时间就是以BCD码形式表示的时间。比如整型0x201801061647 表示2018年1月6日16时47分。

下面举例几种BCD时间的转换:

#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%100)/10)*16+(i_cHexData%10);
return i;
}

输入参数

/***************************************************************************
*函数名称:Get_CurBCDTime6
*函数介绍:转换为BCD码格式时间
*输入参数:-*time:
*输出参数:无
*返回值 :无
*修改历史:
**************************************************************************/
void Get_BCDTime6(time_t i_tTime,char
o_cpBcdTime)
{
struct tm *time;
time=localtime(&i_tTime);
o_cpBcdTime[0]=(unsigned char)((((time->tm_year-100)/10)<<4)|((time->tm_year-100)%10));
o_cpBcdTime[1]=(unsigned char)((((1+time->tm_mon)/10)<<4)|((1+time->tm_mon)%10));
o_cpBcdTime[2]=(unsigned char)((((time->tm_mday)/10)<<4)|((time->tm_mday)%10));
o_cpBcdTime[3]=(unsigned char)((((time->tm_hour)/10)<<4)|((time->tm_hour)%10));
o_cpBcdTime[4]=(unsigned char)((((time->tm_min)/10)<<4)|((time->tm_min)%10));
o_cpBcdTime[5]=(unsigned char)((((time->tm_sec)/10)<<4)|((time->tm_sec)%10));
}

/***************************************************************************
*函数名称:Get_CurBCDTime6
*函数介绍:获取当前BCD码格式时间
*输入参数:-*time:
*输出参数:无
*返回值 :无
修改历史:add by lianzihao 2016-09-13
/
void Get_CurBCDTime6(char
o_cpBcdTime)
{
time_t lt=0;
lt = time(NULL);
struct tm * pCurTime = localtime(<);
o_cpBcdTime[0]=(unsigned char)((((pCurTime->tm_year-100)/10)<<4)|((pCurTime->tm_year-100)%10));
o_cpBcdTime[1]=(unsigned char)((((1+pCurTime->tm_mon)/10)<<4)|((1+pCurTime->tm_mon)%10));
o_cpBcdTime[2]=(unsigned char)((((pCurTime->tm_mday)/10)<<4)|((pCurTime->tm_mday)%10));
o_cpBcdTime[3]=(unsigned char)((((pCurTime->tm_hour)/10)<<4)|((pCurTime->tm_hour)%10));
o_cpBcdTime[4]=(unsigned char)((((pCurTime->tm_min)/10)<<4)|((pCurTime->tm_min)%10));
o_cpBcdTime[5]=(unsigned char)((((pCurTime->tm_sec)/10)<<4)|((pCurTime->tm_sec)%10));
}
/

*函数名称:Get_CurBCDTime7
*函数介绍:获取当前BCD码格式时间
*输入参数:-*time:
*输出参数:无
*返回值 :无
*修改历史:
**************************************************************************/
void Get_CurBCDTime7(char
o_cpBcdTime)
{
time_t tt;
struct tm *curtime;
time(&tt);
curtime=localtime(&tt);
o_cpBcdTime[0]=Char2Bcd((curtime->tm_year+1900)/100);
o_cpBcdTime[1]=Char2Bcd((curtime->tm_year+1900)%100);
o_cpBcdTime[2]=Char2Bcd(curtime->tm_mon+1);
o_cpBcdTime[3]=Char2Bcd(curtime->tm_mday);
o_cpBcdTime[4]=Char2Bcd(curtime->tm_hour);
o_cpBcdTime[5]=Char2Bcd(curtime->tm_min);
o_cpBcdTime[6]=Char2Bcd(curtime->tm_sec);
}

#define LEN 7
int main(void)
{
char l_arrBcdtime[LEN] ={0};
Get_CurBCDTime7(l_arrBcdtime);
printf("Get_CurBCDTime7 :");
for(int i=0;i<LEN;i++)
{
printf("%02x",l_arrBcdtime[i]);
}

Get_CurBCDTime6(l_arrBcdtime);

为了将我们常用的时间格式转换回对应的 Unix 时间戳,
可以通过调用系统库函数 strptime 来实现。
请注意:strptime 函数与 strftime 函数具有相似的功能。

虽然这个说法不太准确,请稍等片刻后再进行详细讨论

该函数的作用是将一个字符串解析为指定的时间格式

该函数的简单应用如下:将时间转换为Unix时间戳

#define _XOPEN_SOURCE
Include< stdio.h>
Include< stdlib.h>
Include< time.h>
Include< sys/time.h>
Include< unistd.h>

long convertToUnixEpochTime()
{
struct tm local_tm;
Use the C standard library's strptime function to parse the given string into a time structure.
const char* dateString = "2018-01-6 15:10:01";
const char* dateFormat = "%Y-%m-%d %H:%M:%S";
strptime(dateString, dateFormat, &local_tm);
Convert the parsed time structure into a time_t value.
time_t unixTime = mktime(&local_tm);
Return the converted Unix epoch time value.
return unixTime;
}

strftime:

该函数以其强大的功能著称于编程界。它能够将时间格式化为多种不同的格式,并支持大量常用的日期和时间显示方式。其核心功能在于通过指定特定的格式指令将时间信息转换并存储到目标字符串中,在此过程中可选性地调整输出结果的最大容量设置为maxsize个字符单位。该函数返回值表示成功插入到目标字符串中的字符数量,并非强制性要求填充完整容量空间若超出最大容量则会触发溢出保护机制以防止数据损坏这种情况通常由调用者进行适当的内存分配管理程序设计人员应充分考虑潜在溢出风险并采取相应的安全防护措施以确保系统运行的安全性和稳定性

在实际应用中我们常常用到的是其操作机制与sprintf()类似的特性即通过识别特定模式化的指令序列来进行数据转换这一过程的关键在于正确理解每个指令所代表的具体含义以及如何将其与输入的时间结构参数相结合以便生成符合预期的结果

如果想显示现在是几点了,并以12小时制显示,就象下面这段程序:

#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>

The function main() begins execution.
{
A pointer ptr is allocated to a local struct tm variable.
A variable lt of type time_t is declared.
A character array str of length 80 is initialized.
The current calendar time is obtained by calling the standard library function time(). This value is assigned to the variable lt.
The local calendar time corresponding to the argument passed into the function call is assigned to the pointer ptr.
The formatted string of interest is constructed using the standard C library function strftime(). The format string used here reads '%I:%p' and copies this information into the character array str.
The formatted string stored in str is printed to standard output, followed by a newline character.
The program terminates normally with an exit status of zero.

biao@ubuntu:~/test/test ./a.out It is now 05 PM biao@ubuntu:~/test/test gcc timer.c
如果要显示全部的时间,则如下:
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
void main( void )
{
struct tm *newtime;
char tmpbuf[128];
time_t lt1;
time(<1);
newtime=localtime(<1);
strftime( tmpbuf, 128, "Today is %A, day %d of %B in the year %Y.\n", newtime);
printf("%s\n",tmpbuf);
}
结果为:
biao@ubuntu:~/test/test gcc timer.c biao@ubuntu:~/test/test ./a.out
Today is Saturday, day 06 of January in the year 2018.

biao@ubuntu:~/test/test$

---------------------
笔者:李文强(li_wen01)
来源:网站(.net)
原文链接为
版权声明:以上文章属本人原创作品,请转载时注明出处
发布日期为2023年10月26日 上午15时37分48秒

全部评论 (0)

还没有任何评论哟~