#include <time.h>
#ifdef WIN32
# include <windows.h>
#else
# include <sys/time.h>
#endif
#ifdef WIN32
int gettimeofday(struct timeval *tp, void *tzp)
{
time_t clock;
struct tm tm;
SYSTEMTIME wtm;
GetLocalTime(&wtm);
tm.tm_year = wtm.wYear - 1900;
tm.tm_mon = wtm.wMonth - 1;
tm.tm_mday = wtm.wDay;
tm.tm_hour = wtm.wHour;
tm.tm_min = wtm.wMinute;
tm.tm_sec = wtm.wSecond;
tm. tm_isdst = -1;
clock = mktime(&tm);
tp->tv_sec = (long)clock;
tp->tv_usec = wtm.wMilliseconds;
return (0);
}
#endif
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//#include "gettimewin.h"
#ifdef WIN32
# include <windows.h>
#else
# include <sys/time.h>
#endif
int gettimeofday(struct timeval *tp, void *tzp);
#if defined(_MSC_VER) && !defined(snprintf)
#define snprintf _snprintf
#endif
int main(int argc, char *argv[])
{
struct timeval tv;
char buf[] = "1970-01-01 00:00:00.000";
struct tm *newtime;
char log_name[128];
char log_time[128];
time_t lt;
int start = 0;
int end = 0;
while(1)
{
time(<);
newtime = localtime(<);
strftime( log_time,128,"%Y-%m-%d %H:%M:%S",newtime);
Sleep(1000);
gettimeofday(&tv, 0);
printf("wMilliseconds = %d\n",(int)(tv.tv_usec));
printf("%s.%03d\n",log_time,(int)(tv.tv_usec));
/*
start = (int)tv.tv_usec + (int)tv.tv_sec * 1000;
Sleep(1000);
gettimeofday(&tv, 0);
end = (int)tv.tv_usec + (int)tv.tv_sec * 1000;
printf("%d\n", end - start);
*/
}
return (0);
}
打印系统当前时间(精确到毫秒)
最新推荐文章于 2024-05-22 10:12:05 发布