Path: blob/master/compat/gettimeofday.c
1295 views
#include < time.h >1#include <windows.h> //I've ommited this line.2#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)3#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui644#else5#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL6#endif78struct timezone9{10int tz_minuteswest; /* minutes W of Greenwich */11int tz_dsttime; /* type of dst correction */12};1314int gettimeofday(struct timeval *tv, struct timezone *tz)15{16FILETIME ft;17unsigned __int64 tmpres = 0;18static int tzflag;1920if (NULL != tv)21{22GetSystemTimeAsFileTime(&ft);2324tmpres |= ft.dwHighDateTime;25tmpres <<= 32;26tmpres |= ft.dwLowDateTime;2728/*converting file time to unix epoch*/29tmpres /= 10; /*convert into microseconds*/30tmpres -= DELTA_EPOCH_IN_MICROSECS;31tv->tv_sec = (long)(tmpres / 1000000UL);32tv->tv_usec = (long)(tmpres % 1000000UL);33}3435if (NULL != tz)36{37if (!tzflag)38{39_tzset();40tzflag++;41}42tz->tz_minuteswest = _timezone / 60;43tz->tz_dsttime = _daylight;44}4546return 0;47}4849void usleep(__int64 waitTime)50{51if (waitTime > 0)52{53if (waitTime > 100)54{55// use a waitable timer for larger intervals > 0.1ms5657HANDLE timer;58LARGE_INTEGER ft;5960ft.QuadPart = -(10*waitTime); // Convert to 100 nanosecond interval, negative value indicates relative time6162timer = CreateWaitableTimer(NULL, TRUE, NULL);63SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0);64WaitForSingleObject(timer, INFINITE);65CloseHandle(timer);66}67else68{69// use a polling loop for short intervals <= 100ms7071LARGE_INTEGER perfCnt, start, now;72__int64 elapsed;7374QueryPerformanceFrequency(&perfCnt);75QueryPerformanceCounter(&start);76do {77QueryPerformanceCounter((LARGE_INTEGER*) &now);78elapsed = (__int64)((now.QuadPart - start.QuadPart) / (float)perfCnt.QuadPart * 1000 * 1000);79} while ( elapsed < waitTime );80}81}82}838485