|
BOOL GetTime( |
|
LPCTSTR lpszRemoteHost, |
|
|
UINT nRemotePort, |
|
|
LPDWORD lpdwNetworkTime |
|
|
UINT nTimeout |
|
); |
BOOL GetTime( |
|
LPCTSTR lpszRemoteHost, |
|
|
UINT nRemotePort, |
|
|
LPSYSTEMTIME lpSystemTime |
|
|
UINT nTimeout |
|
); |
BOOL GetTime( |
|
LPCTSTR lpszRemoteHost, |
|
|
UINT nRemotePort, |
|
|
CTime& localTime |
|
|
UINT nTimeout |
|
); |
The GetTime method returns the network time from the
specified host.
Parameters
- lpszRemoteHost
- A pointer to the name of the server. The host must be
running a time server that complies with the specifications
outlined in RFC 868.
- nRemotePort
- 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.nist.gov |
129.6.15.28 |
Gaithersburg, Maryland |
time-b.nist.gov |
129.6.15.29 |
Gaithersburg, Maryland |
time-nw.nist.gov |
131.107.13.100 |
Redmond, Washington |
time-a.timefreq.bldrdoc.gov |
132.163.4.101 |
Boulder, Colorado |
time-b.timefreq.bldrdoc.gov |
132.163.4.102 |
Boulder, Colorado |
time-c.timefreq.bldrdoc.gov |
132.163.4.103 |
Boulder, Colorado |
Time servers are also commonly maintained by Internet service
providers and universities. If you are unable to obtain the time from
a server, contact the system administrator to determine if they have
the standard time service available on port 37.
Example
CNetworkTime netTime;
DWORD dwNetworkTime;
// Get the current time from a NIST time server
if (netTime.GetTime(_T("time-nw.nist.gov"), &dwNetworkTime))
{
// Convert the network time value to a CTime object
CTime localTime;
netTime.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 (netTime.SetTime(dwNetworkTime) == FALSE)
netTime.ShowError();
}
}
else
{
netTime.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,
SetTime
|
|