NTPTime Client

NTPTime is a Network Time Protocol client program used to accurately set your computer's time from the internet. It runs on Windows NT or Windows 95. It can operate with a dial-up network connection or a direct internet link. Both the executable and the source code are freely available.

This document and the NTPTime program and source code are all Copyright © Tom Horsley, 1997, 1998. All rights reserved. The right to unlimited distribution and use of this program is granted to anyone who agrees to keep the copyright notice intact and not alter the program.

Translation: You want changes in the program, make 'em yourself, but don't go distributing that changed version with my name on it (or with your name on it either, since you didn't write most of it :-). If there are changes you would like to see distributed, by all means send 'em in to me, and if I like 'em I'll incorporate them in the next version.


NTPTime Features

NTPTime is an NTP client program. This means that it uses the internet to set the time on your computer by talking to a system somewhere out in the world which is running an NTP time server. There are many such servers available on the internet, and the installation section will describe how to find one.

NTPTime runs as a service on Windows NT which must be installed by a user with administrator privileges (I have only tested it on 4.0, but it may run fine under any version of NT). On Windows 95 it runs as hidden window, and you will probably want to add it to your start menu.

If you connect to the internet via dial up networking, NTPTime can be configured to lurk in the background until it notices you have an active internet connection, then talk to the server to set the time. This lurking (after much blood sweat and tears) will never trigger an autodial on your computer (I hope - Microsoft seems to keep "helpfully" making more and more events trigger autodialing). Instead, it waits for you to connect for some other reason, then asks for the time. If you want it to autodial, I recommend calling rasdial from your startup menu to initiate a connection as soon as you log in. If you have a direct internet connection, all this can be turned off and it will just assume it can get to the NTP server anytime it wants to.

If you don't like the idea of a background process cluttering up your system all the time, then you can configure it to set the time only once, then exit. This way it only clutters up your system until the first time you connect to the internet.

NTPTime can keep a log file showing when it adjusted the clock and how much it adjusted it by. This log will also record any errors which make the program exit, so the log is valuable if you are having trouble getting it to work.

Installing NTPTime

I haven't written a fancy installer for NTPTime, so you're going to have to do the install manually. This will involve copying the program where it should live and using the registry editor to install a new key and the values under that key which configure how NTPTime operates. On Windows NT you will also need to run the program once with a special option to register it as a service and you will need to be the administrator or at least have administrator privileges.

In anticipation of possible future versions, I should mention that the first thing you should do is uninstall any existing version before installing a new one (the uninstall directions are below).

Now copy the ntptime.exe program to the directory where it should live. A good place might be the something like C:\WINNT\System32 on Windows NT or C:\WINDOWS\SYSTEM on Windows 95, but it can really live wherever you want to put it.

If you are running on Windows NT, you now need to cd to the directory where you copied ntptime.exe and run the command .\ntptime --install. This should pop up a message about successfully installing the service. If it doesn't, then you forgot to log in as administrator or you forgot to uninstall an old version.

You now need to decide which NTP server you are going to talk to and what configuration options you want to use with NTPTime. The most interesting decision may be the time server. I don't have any defaults built in, because I don't want to cause any grief for particular well known time servers by listing them here. You should go through a two step process to find the best server to use. First contact your ISP or the administrators of any corporate network you may be attached to and find out if they are already running an NTP server. If they are, then that's the one you'll want to use, because it is sure to be closer to your machine than the internet at large.

If there is no local NTP server available to use, then you'll have to consult the NTP Home Page for pointers to publicly available NTP servers. There is no reason, given the accuracy of this program and PC clocks in general to talk to the most accurate "stratum 1" servers. Use one of the "stratum 2" or below servers. Pick one which has low ping times or which tracert shows has a small number of hops (or both). Or just pick one at random (if probably won't matter much). Be sure and read the notes about the server you pick and obey any restrictions about requesting permission, etc. Also be sure to complain to your ISP if they don't run an NTP server :-).

For both Windows 95 and Windows NT, you now need to run the registry editor and install the new key and values under that key which determine how NTPTime will behave on your system. In case you aren't familiar with the registry editor, it is called regedt32 on Windows NT and regedit on Windows 95. Don't go changing arbitrary stuff in your registry, your whole system configuration depends on it, but if you just fool with the key for NTPTime, you shouldn't have a problem.

The key you need to create is:

HKEY_LOCAL_MACHINE\Software\ntcli\NTPTime\2.3

The 2.3 is the version. If you have a newer version of the program, you'd better substitute the right version number. You can run ntptime --version to have it print the version information.

The ntcli part is where Microsoft recommends putting your company name. I'm not a company, but I do have a bunch of Windows NT command line interface tools I wrote, so I generally use ntcli in that subkey (I figured you might die of curiosity if I didn't explain that :-).

Now you need to create the following mandatory values under that key (if you don't define all of these values, NTPTime will probably barf as soon as it tries to run).

  • NTPServerName This must be a REG_SZ string giving the name of the NTP server system you will be getting the time from (probably something like ntp.some.system). This may also be a standard IP address string of dot separated digits (for example: 123.45.6.78)
  • WriteLogEntries This must be a REG_DWORD with the value 1 to turn on generation of log file entries and 0 to turn off logging. The only place any information about what caused an error will show up is in a log file, so it is a good idea to turn this on.
  • LogDirectory This is only really required if WriteLogEntries is set to 1, but it will never hurt to define it. It is a REG_SZ format string naming the full path name to a directory where the log files will be written. Be sure to include the leading device letter as part of the path name. You must create this directory, NTPTime won't do it for you, and logging won't work if it does not exist. Note that NTPTime creates one log file per day and doesn't currently clean up after itself, so you might want to periodically delete old log files.
  • WaitForRAS This must be a REG_DWORD that should be 1 if you want NTPTime to wait for a dial up connection to be active before it tries to set the time. If you have a direct internet connection (or you just want to pretend you do), then set it to 0.
  • RASPollMilliseconds This must be set if WaitForRAS is 1. It is a REG_DWORD value that is the number of milliseconds to wait in between polls to check and see if a dial up connection is active. The smaller you make this, the faster NTPTime will notice a connection and the more time it will waste doing the polling. I have it set to 5 seconds (5000) and it seems to work OK.
  • RunOnce This must be a REG_DWORD set to 0 if you want NTPTime to continue to run and periodically set the time. Set it to 1 if you want NTPTime to shut itself down after it sets the time once.
  • NTPPollMilliseconds This must be a REG_DWORD set to the number of milliseconds to wait after the time is set before it tries to set the time again. This can be a pretty large value. Something like a half hour to even several hours (you'll need to use the calculator to compute this in milliseconds). You can examine the log entries to see how much each clock adjustment was, and as long as the adjustment are tiny, you can increase the length of time to wait (perhaps someday this will be automagic, but not in version 2.3).

There are also some optional values you can set:

  • MaxAdjustSeconds This is a REG_DWORD that gives the largest number of seconds adjustment NTPTime will be allowed to apply. This is mainly a sanity check. After you have got the time set accurately, you can examine the log files for the largest adjustment and set this value to somewhat greater than that. If the server you talk to goes funny in the head and starts generating bad time values, NTPTime will then ignore them.
  • NTPServerPort This is a REG_DWORD giving a non-standard port number to talk NTP protocol on. Normally you will never set this and the standard port number 123 will be used, but if you are talking to an odd server or doing some strange testing of some kind you might have a reason to configure this.

If you are on Windows 95, you are now done. You can add an entry to your startup menu to run NTPTime when you login, or you can just run it manually when you feel the need to set the time. Note that no window or icon will show up for NTPTime when it is running, but it will appear in the task list you get when you Ctrl-Alt-Delete, and you can end the task from there.

If you are on Windows NT, there is one more step to take. You need to go to the control panel and double click on "Services" to bring up the service manager. Click on the NTPTime service to select it, and press the Start button. This should start the service running, and it will automatically start from now on when you boot the system. Because it will be running as administrator, it can set the time even if a non-administrator user is the one who logs on to the internet.

If you decide to change any registry entries, you will need to stop, then restart the service before it notices any of the changes.

UnInstalling NTPTime

To remove NTPTime, you basically just reverse the install process.

On Windows NT, you need to go to the services manager (from the control panel) and stop the service. Then you can run ntptime --uninstall to remove it as an NT service. You will need administrator privileges to do this.

If you don't want your registry cluttered up, you can remove the HKEY_LOCAL_MACHINE\Software\ntcli\NTPTime\2.3 key and all the values under it.

Then just delete the program, and it is gone forever.

If you had logging turned on, you might also want to remove the log directory.

And if you installed this html file somewhere you could remove it too.

Download NTPTime

The current version of NTPTime is 2.3.

NTPTime was built with Microsoft Visual C++ 5.0 on Windows NT 4.0. It has been tested under Windows 95. All of this has only been tested on the x86 architecture (the only flavor machine I have).

  • ntptime.zip The C++ source code (along with a copy of this html document). The zip file contains files with long file names, so you will need an unzip which supports that.
  • ntptime.exe The x86 executable (this is the executable itself, not a self extracting archive or anything like that).
  • This file is the only documentation for the program, so you might want to save a copy of it somewhere.

If anyone wants to build and host executables for other architectures, I will gladly provide pointers from here, but I don't have enough space that I'd want to host them myself.

Build NTPTime

The .zip file containing the source code must be unpacked with a newer zip that supports long file names. The file contains only the source, no project files, etc.

To build with Visual C++, you can just type nmake if you have all your C++ related environment variables setup correctly, or you should be able to create a new project workspace for a win32 application, then simply add all the source files to the project (I haven't tried any other compilers, so I don't know exactly what to do with them, but it should be similar). Note that it is probably not a good idea to turn on the optimizer. At least when I try to build with the optimizer on, I get a program that aborts instead of one that works perfectly.

The default library list that VC++ generates is not correct, to link successfully, you will need to change the project settings to use the following set of libraries:

kernel32.lib user32.lib advapi32.lib wsock32.lib

Some of the default libraries were removed from this list because they are not used.

Warnings

NOTE: This program has not been extensively tested (especially under Windows 95). It basically has one user (me) and has only talked to one server. I have gotten some feedback from previous versions, and at least some people seem to be happy with it (which was nice to hear about :-).

I do run this on my Windows NT 4.0 system at home, and it hasn't caused any problems since the one where it would hang the service control manager :-). But remember, this is free software, it has only as much support as I feel like providing, and it is relatively new (or was in May of 1997 when version 2.1 was publicly released for the first time). User beware. On the other hand if you really want something fixed, the source is available too, so you can always fix it yourself.

Yes, there is a program named ntptime out there already, but it appears to be something to read magical time parameters from some special precision timekeeping kernels. I didn't think it could be generally confused with this, and I like the way Windows NT and the NTP protocol both lend themselves to the NTPTime name :-).

To set the time, NTPTime needs to talk to an NTP server using the TCP/IP UDP protocol. Many firewalls won't relay UDP packets, so if you are behind a firewall, your best bet is to convince the powers that be to run an NTP server on the firewall, then you can talk to that.

NTPTime uses the win32 SetSystemTime() call to set the time. This interface wants the time expressed in UTC (universal time coordinated, or just a fancy name for GMT). This means that if you don't have your machine configured properly to use the right timezone and daylight time settings, the time won't appear to be correct, even if NTPTime is functioning perfectly. You can use the date and time control from the control panel to setup the timezone information. On a related note, the log files also long entries with UTC timestamps, and the new log file generated each day uses UTC to determine when a new day begins.

Some governments seem to be constantly fiddling with the daylight savings time standards for their country. As a result, windows may not be able to set the correct timezone for your computer. Microsoft has a utility program available on their web site for editing timezone and daylight time parameters. You can look for http://www.microsoft.com/windows/download/tze.exe if it is still in the last place I found it, or you might simply go to http://www.microsoft.com/ and use their search feature to look for tze.exe or other, perhaps new and improved timezone editing programs.

If you are running this, and also running some kind of high overhead game that hogs every resource it can get, I make no promises about any interference this might cause with games. Best bet is not to run it at the same time you are playing a game.

On Windows NT, the --install option which installs NTPTime as a service also makes NTPTime dependant on the RasMan service, if that service is already installed at the time you install NTPTime. This dependency is required for the service to start correctly at boot time if it is going to be polling for a dial-up connection. If you modify your NT configuration and install or uninstall the RAS services, you probably want to uninstall and reinstall NTPTime as well.

On Windows 95 and 98, NTPTime runs as a completely hidden windows program. If you don't see anything happen when you start it up, that's perfectly normal. The only sign that it is running will be the log file entries it creates (if you configured the registry to write log files), and the entry that will show up for it in the task list you get when you type Ctrl-Alt-Delete. You can use the task list to select NTPTime and do an "End Task" if you need to shut it down (which is slightly less drastic than rebooting the computer to shut it down :-).

References

Some useful resources for NTP information are:

  • NTP Home Page This is the source of all knowledge about the NTP protocol, available programs, etc. It has pointers to everything you might want to know about NTP.
  • rfc 1305 NTP protocol This is the internet standard document which defines the NTP protocol.
  • rfc 2030 SNTP spec This is an internet standard document clarifying issues involved in using NTP for simple time setting clients such as NTPTime.
  • Tom's Home Page This is the obligatory pointer to the author's home page.

Feedback

You can reach me with feedback at Tom.Horsley@worldnet.att.net. Bug reports should mention stuff like which operating system you are running on, which version of NTPTime you are using (you might want to check this web page to see if there is a newer version which might already fix your problem before reporting it). If the bug is unexpected termination of the program, then including the log file showing any messages it might have recorded will be a help (if you have logging turned off, I doubt I'll be able to guess what went wrong from zero data, try turning logging on and wait and see if it happens again).

Enhancement requests are also welcome, most especially ones which come with source code patches to implement the enhancement (which is one of the reasons I make the source available).

Just remember, I do this for learning and fun, not to make a profit, so don't expect lightning quick response. I only have a limited amount of free time to kill fooling with this program (but at least I can keep track of it very accurately now).

Any fury or flames about this program should be directed at the fool who decided to run an unsupported free program on his or her computer, not at me :-).

Deep Thoughts

Why does the world need another NTP client? It doesn't really, I just wanted to learn about writing Windows NT services, and I wanted the service I wrote to be something "real" rather than one of the dummy samples you often see in Microsoft's documentation. I also thought NTP was interesting, so I chose that as my project.

In addition to learning about services, I also took this opportunity to use exceptions and some of the other new C++ features. That turned out to be fun as well, and I learned a lot about how to make using exceptions a safe thing to do when you also need to worry about releasing critical sections, freeing up sockets, etc.

So, if yet another NTP client offends you, I apologize, but I found it a useful project, and as long as I had it, it seemed a shame not to clean it up a bit and make it available for anyone else who wants it.

If I ever decide to learn all about control panel apps or writing install programs, then this tool may become easier to administer or install :-).

Finally, the real subversive motive behind this program is using the free software I write as bait to lure unsuspecting people to my home page, where they can read about the political ideas that will revolutionize governing for generations to come! They will then write their congresscritters about it. The congresscritters will steal the ideas so they can take the credit, and pretty soon the whole country will be running with my ideas :-).

Changes

The initial release of NTPTime was version 2.1.

Version 2.2 was an internal version I never fully released. It didn't do much other than some improved (or different, anyway) error logging and making the NT service dependencies be a little smarter about RAS.

Version 2.3 is the new public version. It doesn't yet have major improvements (i.e. no setup program :-), but at least (I hope), you no longer need to have RAS installed on your machine in order to run NTPTime. It now only loads the rasapi32 library when it needs to reference it to decide if a dial-up connection is active. If you don't ask it to wait for a connection, it should never need RAS. When installing the service, it will only make itself depend on RAS if RAS is installed (if you install or uninstall RAS, you might want to uninstall and reinstall NTPTime as well). Version 2.3 has also changed from a console program to a windows program. This makes it work better under Windows 95 or 98 (if you leave it running, it won't interefere with a shutdown). This also means that if you run it with options like --help, it pops up a message box rather than trying to print to the console. Finally, if I did it right, you should be able to specify an IP address string or a DNS name for the time server.