+++
  CNetworkTime::GetTime Method  
 
BOOL GetTime(
  LPCTSTR lpszHostName,  
  UINT nHostPort,  
  LPDWORD lpdwNetworkTime  
  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 37 is used to connect to the time server.
lpdwNetworkTime
A pointer to an unsigned 32-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 network time is a 32-bit number, represented as the number of seconds since midnight, 1 January 1900 UTC. It can represent a date and time up to the year 2036. It is important to note that the network time value is not the same as the UNIX time value that is used the standard C library time functions. To convert between network time and other time values, use the ConvertTime method.

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 37 or 123.

Example

CNetworkTime timeClient;
DWORD dwNetworkTime = 0;

// Get the current time from a NIST time server
if (timeClient.GetTime(_T("time.nist.gov"), &dwNetworkTime))
{
    // Convert the network time value to a CTime object
    CTime localTime;
    timeClient.ConvertTime(dwNetworkTime, localTime);

    // Format the time string and create a message to display
    // to the user, asking if they want to update the time
    CString strTime;
    strTime = localTime.Format(_T("%B %d, %Y %H:%M:%S"));

    CString strMessage;
    strMessage.Format(_T("Update system time to %s?"), (LPCTSTR)strTime);

    INT nResult;
    nResult = AfxMessageBox(strMessage, MB_YESNO|MB_ICONQUESTION);

    if (nResult == IDYES)
    {
        // Update the local system time with the network time
        if (timeClient.SetTime(dwNetworkTime) == FALSE)
            timeClient.ShowError();
    }
}
else
{
    timeClient.ShowError();
}

Requirements

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

See Also

ConvertTime, FormatTime, SetTime