++ .. . api , . , , . "CSNTPClient" "sntp.h" URL ".htm" > http://read.pudn.com/downloads160/sourcecode/windows/comm/720007/SntpTest/Sntp.h_.htm ". PJ Naughters.. ... !
int main()
{
//Initialise the winsock stack
WSADATA wsaData;
BYTE wsMajorVersion = 1;
BYTE wsMinorVersion = 1;
WORD wVersionRequested = MAKEWORD(wsMinorVersion, wsMajorVersion);
if (WSAStartup(wVersionRequested, &wsaData) != 0)
{
_tprintf(_T("Failed to load winsock stack\n"));
return 1;
}
if (LOBYTE(wsaData.wVersion) != wsMajorVersion || HIBYTE(wsaData.wVersion) != wsMinorVersion)
{
_tprintf(_T("Winsock stack does not support version which this program requires\n"));
WSACleanup();
return 1;
}
//Do the actual NTP Query
CSNTPClient sntp;
NtpServerResponse response;
if (sntp.GetServerTime(specify ntp server url or ip, response))
{
_tprintf(_T("Time was successfully retreived from NTP server\n"));
SYSTEMTIME st1 = response.m_OriginateTime;
SYSTEMTIME st2 = response.m_ReceiveTime;
SYSTEMTIME st3 = response.m_TransmitTime;
SYSTEMTIME st4 = response.m_DestinationTime;
cout << response.m_DestinationTime << endl;
TIME_ZONE_INFORMATION lpTimeZoneInfo;
GetTimeZoneInformation(&lpTimeZoneInfo); //Get the local TIME ZONE
SYSTEMTIME stLocal;
//To Get Local Time from the fetched GMT/UTC Time from the server, use SystemTimeToTzSpecificLocalTime()
//To get GMT/UTC Time from Local Time, use the API TzSpecificLocalTimeToSystemTime()
SystemTimeToTzSpecificLocalTime(&lpTimeZoneInfo, &st3, &stLocal);
_tprintf(_T("\n"));
_tprintf(_T(" DD/MM/YYYY HH:MM:SS.MS\n"));
_tprintf(_T("\n\n\nObtaining Time thru API SystemTimeToTzSpecificLocalTime :\n\n"));
_tprintf(_T("Server Transmit Date was %02d/%02d/%04d, %02d:%02d:%02d.%03d\n"), st3.wDay, st3.wMonth, st3.wYear, st3.wHour, st3.wMinute, st3.wSecond, st3.wMilliseconds);
_tprintf(_T("Client Destination Date was %02d/%02d/%04d, %02d:%02d:%02d.%03d\n"), stLocal.wDay, stLocal.wMonth, stLocal.wYear, stLocal.wHour, stLocal.wMinute, stLocal.wSecond, stLocal.wMilliseconds);
}
}