Skip to content

Instantly share code, notes, and snippets.

@kayws426
Created April 17, 2016 18:06
Show Gist options
  • Save kayws426/c39def40b7318a4ceae57b8bca8ce8d9 to your computer and use it in GitHub Desktop.
Save kayws426/c39def40b7318a4ceae57b8bca8ce8d9 to your computer and use it in GitHub Desktop.
int clock_gettime(int, struct timespec *tv)
{
LARGE_INTEGER curCount;
static int initialized = 0;
static LARGE_INTEGER freq, startCount;
static struct timespec tv_start;
time_t sec_part;
long nsec_part;
if (!initialized) {
FILETIME filetime;
__int64 temp;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&startCount);
GetSystemTimeAsFileTime(&filetime);
temp = filetime.dwHighDateTime;
temp <<= 32;
temp |= filetime.dwLowDateTime;
temp -= 116444736000000000ULL;
tv_start.tv_sec = temp / 10000000UL;
tv_start.tv_nsec = (temp % 10000000UL) * 100;
initialized = 1;
}
QueryPerformanceCounter(&curCount);
curCount.QuadPart -= startCount.QuadPart;
sec_part = curCount.QuadPart / freq.QuadPart;
nsec_part = (long)((curCount.QuadPart - (sec_part * freq.QuadPart))
* 1000000000UL / freq.QuadPart);
tv->tv_sec = tv_start.tv_sec + sec_part;
tv->tv_nsec = tv_start.tv_nsec + nsec_part;
if(tv->tv_nsec >= 1000000000UL) {
tv->tv_sec += 1;
tv->tv_nsec -= 1000000000UL;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment