打印系统当前时间(精确到毫秒)

#include 
#ifdef WIN32
#   include 
#else
#   include 
#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 
#include 
#include 
//#include "gettimewin.h"
#ifdef WIN32
#   include 
#else
#   include 
#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);
}


你可能感兴趣的:(c)