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

来源:互联网 发布:金盛集团网络登录平台 编辑:程序博客网 时间:2024/04/29 09:18

刚开始搞安卓的时候,并不知道logcat可以直接打印一个时间,然后自己傻傻的写了一个时间的函数 ,这里记录一下。



#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;}


0 0
原创粉丝点击