Hi all,
The GetNTPTime(NTP_serverIP) returns a DWORD. How can it be converted to a tm structure?
Thanks in advance,
get NTP time
Re: get NTP time
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));
}
#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));
}
Re: get NTP time
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
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