summaryrefslogtreecommitdiff
path: root/ports/winnt/libntp/getclock.c
blob: 0fdcb69ba7e87ac837602a95c2fe33607310ea37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * getclock.c - Emulate Unix getclock(3) nanosecond interface for libntp/ntpd
 */
#include "config.h"

#include "ntp_unixtime.h"
#include "clockstuff.h"
#include "ntp_stdlib.h"

/*
 * getclock() is in libntp.  To use interpolation, 
 * ports/winnt/ntpd/nt_clockstuff.c overrides GetSystemTimeAsFileTime 
 * via the pointer get_sys_time_as_filetime.
 */
PGSTAFT get_sys_time_as_filetime;
PGSTAFT pGetSystemTimePreciseAsFileTime;


int
getclock(
	int		clktyp,
	struct timespec *ts
	)
{
	union {
		FILETIME ft;
		ULONGLONG ull;
	} uNow;

	if (clktyp != TIMEOFDAY) {
		TRACE(1, ("getclock() supports only TIMEOFDAY clktyp\n"));
		errno = EINVAL;
		return -1;
	}

	if (NULL == get_sys_time_as_filetime)
		init_win_precise_time();
	(*get_sys_time_as_filetime)(&uNow.ft);

	/* 
	 * Convert the hecto-nano second time to timespec format
	 */
	uNow.ull -= FILETIME_1970;
	ts->tv_sec = (time_t)( uNow.ull / HECTONANOSECONDS);
	ts->tv_nsec = (long)(( uNow.ull % HECTONANOSECONDS) * 100);

	return 0;
}


void
init_win_precise_time(void)
{
	HANDLE	hDll;
	FARPROC	pfn;

	hDll = LoadLibrary("kernel32");
	pfn = GetProcAddress(hDll, "GetSystemTimePreciseAsFileTime");
	if (NULL != pfn) {
		pGetSystemTimePreciseAsFileTime = (PGSTAFT)pfn;
		get_sys_time_as_filetime = pGetSystemTimePreciseAsFileTime;
	} else {
		get_sys_time_as_filetime = &GetSystemTimeAsFileTime;
	}
}