Nejc Galof
Nejc Galof

Reputation: 2614

NTP server return always same - wrong date (windows)

I want to get time from NTP server. I found lot of examples, but in my case I get from server always same date. I don't know what is wrong. I want get currect date and hour, but I don't.

unsigned char buf[1024];
...
tmit=ntohl((time_t)buf[4]); // get transmit time
//tmit=ntohl((time_t)buf[10]); //try this too
time_t tempa = tmit;
printf("Recieve time: %s",ctime(&tempa));
...

Here is my output:

Recieve time: Thu Jan 01 01:00:00 1970

I don't know why recieve this date or why recalculate to wrong date. Where is problem? I working in c++ on windows.

Upvotes: 0

Views: 350

Answers (1)

David Schwartz
David Schwartz

Reputation: 182769

tmit=ntohl((time_t)buf[4]); // get transmit time

You are casting a single character (buf[4]) to a time_t. Since an unsigned char can only have 256 possible values, a cast to a time_t can only have 256 possible values. That can't be right.

Upvotes: 2

Related Questions