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

来源:互联网 发布:ansitxt2mobi mac 编辑:程序博客网 时间:2024/05/29 18:20

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



0 0
原创粉丝点击