hello all. i have managed to make a NTP request and retrieve the server time from it's NTP response. i want to convert this number to a Human-readable time, writing in C++. can some one help me ? as example you can look at: http://www.4webhelp.net/us/timestamp.php?action=stamp&stamp=771554255&timezone=0 once you set the timestamp to 771554255 you'll get "29/7/2010 13:14:32". i wanna do the same in my code, any help ?
A:
For C++, you can use the gmtime()
function.
#include <stdio.h>
#include <time.h>
int main()
{
time_t now = time(0);
struct tm *tm = gmtime(&now);
printf("%04d-%02d-%02d %02d:%02d:%02d\n",
1900+tm->tm_year, 1+tm->tm_mon, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
}
Greg Hewgill
2010-07-29 13:21:31
The answer is un-related to the NTP timestamp.
pascal
2010-07-29 13:28:52
The OP linked to a page which converts Unix timestamps. Is the input really an NTP timestamp? Or is it just a Unix timestamp from a daytime service? We don't really know.
Greg Hewgill
2010-07-29 13:35:28
the input is the value of the ntp_msg.xmttime.int_partl taken from http://www.openbsd.org/cgi-bin/cvsweb/src/usr.sbin/ntpd/ntp.h?rev=1.13;content-type=text/plainit's the response from a ntp server.i can see that the ethreal know how to convert the talked timestamp to readable time. i am not sure if the ntp time stamp is the same as unix timestamp. but i am alos sure that it is not the timestamp returned by time(...) function
2010-07-29 13:45:25
At least, the question mentionned NTP server... Let's see.
pascal
2010-07-29 13:46:33
@user314536 OK, so the page you referenced was just misleading...
pascal
2010-07-29 13:47:32
So the NTP timestamp is evidently just a simple offset from the Unix timestamp. Subtract the magic value, and use `gmtime()`.
Greg Hewgill
2010-07-29 13:56:56
ok, please , let me re ask my question:my program does an ntp request and get an answer, according to the ethreal the response Transmit Time Stamp:"Transmit Time Stamp: Jul 29, 2010 13:59:40.6074 UTC"but that is the the eathreeal decoding the time stamp number sent to it. the actual number sent to is is a 64 bits number "cf fc 07 cc 9b 7b fe 24".if the ethreal can decode the number to a human readable date, i can do it as well. the only problem is, i dont know how
2010-07-29 14:02:35
A:
It's not C++, but here's a perl implementation. Converting this into C++ should be no big deal:
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);
NinjaCat
2010-07-29 13:28:13
I hesitated on posting this at first, because it's perl, not C++. But the method is correct, and can easily be ported to C++.
NinjaCat
2010-07-29 15:14:09