我最终找到了这个页面:
gettimeofday()
函数适用于Windows(现在通过Wayback Machine),其中有一个方便的、漂亮的
gettimeofday()
在Windows上的实现。它使用
GetSystemTimeAsFileTime()
方法来获取准确的时钟。
更新:这里有一个替代的活动链接,来自“Unix到HPC Windows移植词典”
gettimeofday()
(现在通过Wayback Machine),指向OP所提到的实现。请注意,链接的实现中还有一个拼写错误。
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 // WRONG
#else
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL // WRONG
#endif
显示的值末尾缺少一个额外的
0
(它们假定是微秒,而不是100纳秒间隔的数量)。通过
这条评论在Google代码项目页面上发现了此错误。下面显示了正确使用的值:
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
#define DELTA_EPOCH_IN_MICROSECS 116444736000000000Ui64 // CORRECT
#else
#define DELTA_EPOCH_IN_MICROSECS 116444736000000000ULL // CORRECT
#endif
PostgreSQL在Windows上实现的gettimeofday函数:
#include "c.h"
#include <sys/time.h>
static const unsigned __int64 epoch = ((unsigned __int64) 116444736000000000ULL);
int
gettimeofday(struct timeval * tp, struct timezone * tzp)
{
FILETIME file_time;
SYSTEMTIME system_time;
ULARGE_INTEGER ularge;
GetSystemTime(&system_time);
SystemTimeToFileTime(&system_time, &file_time);
ularge.LowPart = file_time.dwLowDateTime;
ularge.HighPart = file_time.dwHighDateTime;
tp->tv_sec = (long) ((ularge.QuadPart - epoch) / 10000000L);
tp->tv_usec = (long) (system_time.wMilliseconds * 1000);
return 0;
}