概述
方案一:
#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 = clock;
tp->tv_usec = wtm.wMilliseconds * 1000;
return (0);
}
#endif
方案二:
gettimeofday的使用
//copy from muduo Timestamp Timestamp::now() { struct timeval tv; gettimeofday(&tv, NULL);//返回1970年至今的秒+微秒 int64_t seconds = tv.tv_sec; return Timestamp(seconds * kMicroSecondsPerSecond + tv.tv_usec); }
gettimeofday要求传入一个timeval和一个时区。因为存在微秒数,显然它比 time_t now = ::time(NULL)更精确。
但是这个函数是linux下的。所以我们需要一个跨平台的实现。
以下是一个实现,使用c++的chrono库。
#include <chrono> int gettimeofday(struct timeval *__restrict __tv, __timezone_ptr_t __tz) { auto now = std::chrono::system_clock::now(); auto now_ticks = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch());// __tv->tv_sec = (long)now_ticks.count() / 1000000; __tv->tv_usec = (long)now_ticks.count() % 1000000; return 0; }
now.time_since_epoch()返回的duration比较奇怪,需要转化成微秒。 其实主要是windows没有这个函数,那么我们实现它。
//copy from evpp #ifdef WIN32 int gettimeofday(struct timeval* tp, void* tzp) { uint64_t intervals; FILETIME ft; GetSystemTimeAsFileTime(&ft); /* * A file time is a 64-bit value that represents the number * of 100-nanosecond intervals that have elapsed since * January 1, 1601 12:00 A.M. UTC. * * Between January 1, 1970 (Epoch) and January 1, 1601 there were * 134744 days, * 11644473600 seconds or * 11644473600,000,000,0 100-nanosecond intervals. * * See also MSKB Q167296. */ intervals = ((uint64_t)ft.dwHighDateTime << 32) | ft.dwLowDateTime; intervals -= 116444736000000000; tp->tv_sec = (long)(intervals / 10,000,000); tp->tv_usec = (long)((intervals % 10000000) / 10); return (0); }
最后
以上就是任性火车为你收集整理的linux 下的gettimeofday 函数在windows上的替换方案的全部内容,希望文章能够帮你解决linux 下的gettimeofday 函数在windows上的替换方案所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复