GetNetworkTime Function  
 
DWORD WINAPI GetNetworkTime(
  LPCTSTR lpszHostName,  
  UINT nHostPort,  
  UINT nTimeout  
);

The GetNetworkTime function returns the network time from the specified host. The network time is a 32-bit number, represented as the number of seconds since midnight, 1 January 1900 UTC.

Parameters

lpszHostName
The name of the server. The host must be running a time server that complies with the Network Time Protocol (NTP) specification outlined in RFC 5905.
nHostPort
The port the time server is running on. A value of zero indicates that the default port number for the NTP service should be used. If the application specifies port 37, the library will attempt to use the legacy TIME protocol defined in RFC 868. Support for this protocol is deprecated and is only provided for backwards compatibility.
nTimeout
The number of seconds that the function will wait for a response from the server.

Return Value

If the function succeeds, it returns the number of seconds since midnight, 1 January 1900 UTC. If the function was unable to obtain the time from the specified host, it returns zero.

Remarks

This function is included for backwards compatibility and should not be used with new applications. The 32-bit network time value can only represent a date and time up to the year 2036. Use the GetNetworkTime64 function which returns a 64-bit integer and avoids potential problems with the 32-bit value rolling over.

This function will cause the calling thread to block until the time is returned by the server, or the operation times out. If you do not want the current thread to block, you can create a worker thread and call this function from within that thread. The blocking network call in the worker thread will not interfere with any other threads in your application.

In most cases, it is preferable to use the NTP pool servers which distribute requests over a number of different servers:

  • pool.ntp.org is a global load-balanced pool of time servers
  • us.pool.ntp.org servers are located in the United States
  • eu.pool.ntp.org servers are located in Europe
  • asia.pool.ntp.org servers are located in Asia

There are also load balanced pools of time servers available in specific countries prefixed by their country code. For example, the de.pool.ntp.org servers are located in Germany and the uk.pool.ntp.org servers are located in the United Kingdom. Regardless of their location, the time values they return are always in Coordinated Universal Time (UTC) and are not localized based on the physical location of the server.

In the United States, the National Institute of Standards and Technology (NIST) hosts a number of public servers which can be used to obtain the current time. The following table lists the current host names and addresses:

Server Name IP Address Location
time-a-g.nist.gov 129.6.15.28 Gaithersburg, Maryland
time-b-g.nist.gov 129.6.15.29 Gaithersburg, Maryland
time-nw.nist.gov 131.107.13.100 Redmond, Washington
time-a-b.nist.gov 132.163.96.1 Boulder, Colorado
time-b-b.nist.gov 132.163.96.2 Boulder, Colorado
time-c-b.nist.gov 132.163.96.3 Boulder, Colorado

Time servers are also commonly maintained by government agencies and universities. If you are unable to obtain the time from a server, contact the system administrator to determine if they support the standard time service available on port 123.

If you are requesting the time from a public server, you should ensure that your application does not send too many requests over a short period of time. In most cases, you should not query a time server more than once per minute. Most public time servers will block the IP address of a client which sends too many requests.

It is important to note that the network time value is not the same as the UNIX time value used with the standard C runtime library. The time() function returns the number of seconds since January 1, 1970 which is commonly referred to as the UNIX epoch. NTP servers return the time as the number of seconds since January 1, 1900.

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: cstimv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also