get NTP time

Discussion to talk about software related topics only.
Post Reply
cnth98
Posts: 14
Joined: Wed Dec 01, 2010 8:15 am

get NTP time

Post by cnth98 »

Hi all,
The GetNTPTime(NTP_serverIP) returns a DWORD. How can it be converted to a tm structure?
Thanks in advance,
greengene
Posts: 164
Joined: Wed May 14, 2008 11:20 am
Location: Lakeside, CA

Re: get NTP time

Post by greengene »

here's what i use:

#define NTP_TimeOffset ((DWORD)(2208988800U))
struct tm MyTime;
DWORD NTPtime = GetNTPTime( NTP_server_ip);

if (NTPtime != 0) {
NTPtime -= NTP_TimeOffset;
memcpy((void*)&MyTime, (void*)localtime((const time_t*)&NTPtime),
sizeof(struct tm));
}
User avatar
pbreed
Posts: 1087
Joined: Thu Apr 24, 2008 3:58 pm

Re: get NTP time

Post by pbreed »

You really should not bug the NTP server every time you want to know what time it is.

The better approach is to use the

result = SetNTPTime( AsciiToIp( NTP_SERVER_IP ) );
Once...

Then get the time...
DWORD tv = time( NULL );
struct tm tm_struct;
gmtime_r( (const time_t *) &tv, &tm_struct );

Or if you want a local time zone...

tzsetchar("EST5EDT4,M3.2.0/01:00:00,M11.1.0/02:00:00");
localtime_r((const time_t *) &tv, &tm_struct );


Take a look at nburn\examples\ntpclient it does all of the timezone converting the time to various forms of tm (GMT local etc...)

The only thing its missing is the periodic call to SetNTPTime to rezero the clock every few hours....

The Netburner crystal should keep time to 50ppm.
so you should be off one second ~ every 6 hrs or so.
So calling NTP every two hours will keep you well within a second....

Paul
Post Reply