C语言打印时间精确到毫秒
- 源码
源码
Windows Qt下面验证
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <stdarg.h>
#include <windows.h>int getCurrentTime()
{struct timeval tv;gettimeofday(&tv, NULL);struct tm *timeinfo;timeinfo = localtime(&tv.tv_sec);long milliseconds = tv.tv_usec / 1000; // 微秒转毫秒printf(" %d-%02d-%02d %02d:%02d:%02d.%03ld ",timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday,timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec,milliseconds);return 0;
}void printf_time(char *format, ...)
{va_list args;va_start(args, format);getCurrentTime();vprintf(format, args);va_end(args);
}int main()
{while(1){printf_time("aa:%d bb:%d\n", 33, 44);Sleep(1000);}return 0;
}