Quantcast
Channel: Embedded Software (Read Only)
Viewing all articles
Browse latest Browse all 25965

Forum Post: RE: Example for setting the system time

$
0
0

Karl,

Thank you very much for your quick reply. I've implemented your suggestion and had to make a few tweaks to the time() function for a few reasons.

1. The comments within the sntp.h file state that the first argument to SNTP_start() should be a function pointer that returns the number of seconds since 1/1/1970. I found this a bit strange considering that I believe NTP uses an epoch of 1/1/1900. Nonetheless, your suggestion to pass MYTIME_gettime and MYTIME_settime into SNTP_start doesn't seem to cause any issues.

2. Before the SNTP client has set the time via MYTIME_settime, MYTIME_gettime, I needed to call MYTIME_init (and MYTIME_exit where necessary to clean up any resources). If this isn't done, the MYTIME clock will not count forward and will be seemingly "stuck" on the set time.

3. It seems as though the SNTP client may have a bug related to requiring an IPv6 specific symbol definition even when not using IPv6. Line 246 of sntp.c is referencing IPV6_UNSPECIFIED_ADDRESS which isn't defined unless I enable IPv6 in my app.cfg. It may be that I'm missing an include somewhere, but I got around the issue by including the following definition in my code:

IP6N  IPV6_UNSPECIFIED_ADDRESS;

Anyway, my time() implementation is below for anyone who is interested. There are a few peculiarities, the biggest one is that both localtime() and gmtime() return GMT. This isn't a big deal for me, but may be an issue for someone else.

const unsigned long seventyYears = 2208988800UL;  uint32_t time(time_t *timer) {  time_t result = (time_t)MYTIME_gettime() + seventyYears;  if(timer) *timer = result;   return(result); }

Thank you very much for your help. It was very useful.

-Brian


Viewing all articles
Browse latest Browse all 25965

Trending Articles