【時間統計】windows/linux 獲取本地時間(精確到微妙)

參考網址:http://blog.sina.com.cn/s/blog_538dd0670100ilqm.html

參考網址:http://bbs.csdn.net/topics/330029922

 

1、windows下獲取本地時間的方法:

 

#if defined(WIN32) || defined(UNDER_CE) || defined(WIN64)

#include<time.h>
#include<stdio.h>
#include<string.h>
#include<windows.h>

void getstimeval()
{
        long time;
        SYSTEMTIME sys;
        unsigned int us;
        LARGE_INTEGER tick;
        LARGE_INTEGER timestamp;
        QueryPerformanceFrequency(&tick);
        QueryPerformanceCounter(×tamp);
        us=(timestamp.QuadPart % tick.QuadPart)*1E6/tick.QuadPart;
        GetLocalTime(&sys);
    
        printf("%04d-%02d-%02d %02d:%02d:%02d.%06d\n",sys.wYear, sys.wMonth, sys.wDay, \
        sys.wHour, sys.wMinute, sys.wSecond, us);
}
#endif

 

 

 

 

 

 

 

 

 

 

2、linux下獲取本地時間的方法:

 

#if defined(__GUNC__)

#include<sys/time.h>
#include<time.h>
#include<stdio.h>
#include<string.h>

void getstimeval()
{
        char buf[28];
        struct timeval tv;
        struct tm      tm;
        size_t         len = 28;
        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, ".%06.6d", (int)(tv.tv_usec));
        printf("%s\n",buf);
}
#endif

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章