私はなんとかNTP要求を行い、そのNTP応答からサーバー時間を取得することができました。この数値を人間が読める時間に変換し、C++で記述したいと思います。誰かが私を助けることができますか?例として、次のように表示できます 。http : //www.4webhelp.net/us/timestamp.php?action=stamp&stamp=771554255&timezone=0タイムスタンプを771554255に設定すると、「29/7/201013:14」が表示されます。 :32"。私は私のコードで同じことをしたいのですが、何か助けはありますか?
12099 次
3 に答える
3
これはC++ではありませんが、ここにperlの実装があります。これをC++に変換することは大したことではありません:
http://www.ntp.org/ntpfaq/NTP-s-related.htm#AEN6780
# usage: perl n2u.pl timestamp
# timestamp is either decimal: [0-9]+.?[0-9]*
# or hex: (0x)?[0-9]+.?(0x)?[0-9]*
# Seconds between 1900-01-01 and 1970-01-01
my $NTP2UNIX = (70 * 365 + 17) * 86400;
my $timestamp = shift;
die "Usage perl n2u.pl timestamp (with or without decimals)\n"
unless ($timestamp ne "");
my ($i, $f) = split(/\./, $timestamp, 2);
$f ||= 0;
if ($i =~ /^0x/) {
$i = oct($i);
$f = ($f =~ /^0x/) ? oct($f) / 2 ** 32 : "0.$f";
} else {
$i = int($i);
$f = $timestamp - $i;
}
my $t = $i - $NTP2UNIX;
while ($t < 0) {
$t += 65536.0 * 65536.0;
}
my ($year, $mon, $day, $h, $m, $s) = (gmtime($t))[5, 4, 3, 2, 1, 0];
$s += $f;
printf("%d-%02d-%02d %02d:%02d:%06.3f\n",
$year + 1900, $mon+1, $day, $h, $m, $s);
于 2010-07-29T13:28:13.747 に答える
-1
ここにC++コードがあります..しかし、これはそうではありません..ここで使用されるWindows APIは必要なことを行いますが、その背後には優れた数学的計算があります..理解するのは非常に面倒なので、ここには入れませんでした. クラス「CSNTPClient」の場合、URL「.htm">http://read.pudn.com/downloads160/sourcecode/windows/comm/720007/SntpTest/Sntp からヘッダー「sntp.h」コンテンツをコピー アンド ペーストする必要があります。 .h_.htm". これは PJ Naughters のコード例の修正版です。
#include "stdafx.h"
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);
}
}
于 2013-06-01T13:00:01.543 に答える