windows/linux 获取本地时间(精确到微妙)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/listener51/article/details/53044290

参考网址: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



猜你喜欢

转载自blog.csdn.net/listener51/article/details/53044290