Skip to content

Commit

Permalink
wip simplify gettime
Browse files Browse the repository at this point in the history
  • Loading branch information
aklomp committed Jan 8, 2024
1 parent 1485b21 commit de91d72
Showing 1 changed file with 2 additions and 35 deletions.
37 changes: 2 additions & 35 deletions test/gettime_win.c
@@ -1,42 +1,9 @@
#ifndef CLOCK_REALTIME
#define CLOCK_REALTIME 2
#endif

// Number of 100 ns intervals from January 1, 1601 till January 1, 1970.
#define UNIX_EPOCH 116444736000000000ULL

struct timespec {
time_t tv_sec;
long tv_nsec;
};

static ULARGE_INTEGER
xgetfiletime (void)
{
ULARGE_INTEGER current_time;
FILETIME current_time_ft;

// Return the current time in UTC as a 64-bit value representing the
// number of 100-nanosecond intervals since January 1, 1601.
GetSystemTimePreciseAsFileTime(&current_time_ft);
current_time.LowPart = current_time_ft.dwLowDateTime;
current_time.HighPart = current_time_ft.dwHighDateTime;

return current_time;
}


static int
clock_gettime (const int id, struct timespec* ts)
{
if (id != CLOCK_REALTIME) {
return -1;
}

ULARGE_INTEGER current_time = xgetfiletime();

// Time from Epoch to now.
ts->tv_sec = (current_time.QuadPart - UNIX_EPOCH) / 10000000;
ts->tv_nsec = ((current_time.QuadPart - UNIX_EPOCH) % 10000000) * 100;

return 0;
return timespec_get(ts, TIME_UTC) ? 0 : -1;
}

0 comments on commit de91d72

Please sign in to comment.