Linux下用C获取当前系统时间
发布日期:2021-06-30 19:03:23 浏览次数:3 分类:技术文章

本文共 543 字,大约阅读时间需要 1 分钟。

#include   <time.h>
time_t   time(time_t   calptr);
返回的是日历时间,即国际标准时间公元1970年1月1日00   :   00   :   00以来经过的秒数。然后再调用
char   *ctime(const   time_t   calptr)   ;
转化为字符串表示  

 

#include   
#include
int main () { time_t timep; time (&timep); printf( "%s ",ctime(&timep)); }

 

 

用localtime可直接分解出年月日时分秒:
struct   tm     *ptm;
long       ts;
int         y,m,d,h,n,s;
ts   =   time(NULL);
ptm   =   localtime(&ts);
y   =   ptm-> tm_year+1900;     //年
m   =   ptm-> tm_mon+1;             //月
d   =   ptm-> tm_mday;               //日
h   =   ptm-> tm_hour;               //时
n   =   ptm-> tm_min;                 //分
s   =   ptm-> tm_sec;                 //秒

转载地址:https://linuxstyle.blog.csdn.net/article/details/1534092 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Linux wait函数解析
下一篇:Tcpdump命令的使用与示例——linux下的网络分析

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年05月04日 06时30分14秒