[ntp:questions] Re: Question about synchronizing client and server clock

Terje Mathisen terje.mathisen at hda.hydro.com
Mon Jan 5 22:09:00 UTC 2004

Danny Mayer wrote:
> If you are using Windows, I might suggest looking at two functions
> SystemTimeToFileTime() and FileTimeToSystemTime(). Do your calculations
> using FileTime which provides 64-bit integers though you have to copy
> it to a ULARGE_INTEGER structure to do that. If you want to use ntp code
> itself there is are two undocumented Win32 API functions in the IPHLPAPI DLL
> called NTPTimeToNTFileTime() and NTTimeToNTPTime(). Unfortunately I don't
> know the arguments to the calls.

Those two should be relatively trivial:

NT's _ftime counts 100 ns intervals, starting from 1600 (?), so a 
quick&dirty conversion can use fp operations to make life easy:

int64_t ntp2nt(uint32_t ntphi, uint32_t ntplo)
   int64_t t = ntphi;
   double tl = (double) ntplo;

   t = t * 10000000 + (int32_t) (tl * (1e7 / (65536.0*65536.0)));


   return t;

The opposite operation is similar, but easier to handle with integer ops 

void ntp2nt(int64_t nt, uint32_t *ntphi, uint32_t *ntplo)
   uint32_t h, l;

   nt -= EPOCH_OFFSET;

   h = (uint32_t) (nt / 10000000);  // Seconds, can truncate!
   nt -= h * (int64_t) 10000000;	// Fraction in NT units

   l = (uint32_t) ((nt << 32) / 10000000);

   *ntphi = h;
   *ntplo = l;

- <Terje.Mathisen at hda.hydro.com>
"almost all programming can be viewed as an exercise in caching"

More information about the questions mailing list