CNetworkTime::GetTime Method  
 
BOOL GetTime(
  LPCTSTR lpszHostName,  
  UINT nHostPort,  
  ULONGLONG* lpNetworkTime  
  UINT nTimeout  
);
BOOL GetTime(
  LPCTSTR lpszHostName,  
  UINT nHostPort,  
  LPSYSTEMTIME lpSystemTime  
  UINT nTimeout  
);
BOOL GetTime(
  LPCTSTR lpszHostName,  
  UINT nHostPort,  
  CTime& localTime  
  UINT nTimeout  
);

The GetTime method returns the network time from the specified host.

Parameters

lpszHostName
The name of the server. The host must be running a time server that complies with the specifications outlined in RFC 868.
nHostPort
The port the time server is running on. A value of zero indicates that the default port number for the service should be used. By default, port 123 is used to connect to the time server.
lpNetworkTime
A pointer to an unsigned 64-bit integer which will contain the current network time when the method returns. Note that this value is not the same as UNIX time and cannot be used with the standard C time functions.
lpSystemTime
A pointer to a SYSTEMTIME structure that will contain the system time when the method returns. This structure can be used with many of the standard Windows API time-related functions.
localTime
A CTime object that will contain the local time when the method returns.
nTimeout
The number of seconds that the method will wait for a response from the server.

Return Value

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

Remarks

The GetTime method 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