新闻  |   论坛  |   博客  |   在线研讨会
linux打印一个精确到毫秒级的时间
电子禅石 | 2021-08-11 11:54:02    阅读:2266   发布文章

linux打印一个精确到毫秒级的时间


#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>
 
 
int main(int argc ,char *argv[])
{
	
	char buf[32] = {0};
	struct timeval tv;
	struct tm      tm;
	size_t         len = 28;
	
	memset(&tv, 0, sizeof(tv));
	memset(&tm, 0, sizeof(tm));
	gettimeofday(&tv, NULL);
	localtime_r(&tv.tv_sec, &tm);
	strftime(buf, len, "%Y-%m-%d %H:%M:%S", &tm);
	len = strlen(buf);
	sprintf(buf + len, ".%-6.3d", (int)(tv.tv_usec/1000)); 
	printf("%s\n", buf);
	
	return 0;
}

(1条消息) linux打印一个精确到毫秒级的时间_u013206135的专栏-CSDN博客_linux打印时间毫秒

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
属于自己的技术积累分享,成为嵌入式系统研发高手。
推荐文章
最近访客