[ntp:questions] Re: Calculating NTP timestamp in C program

davids at webmaster.com davids at webmaster.com
Mon Jul 17 21:11:22 UTC 2006


bg_ie at yahoo.com wrote:
> Hi,
>
> I wish to write a C program which obtains the system time and hence
> uses this time to print out its ntp equivalent.
>
> Am I right in saying that the following is correct for the seconds part
> of the ntp time?
>
> long int ntp.seconds = time(NULL) + 2208988800;

The add will probably overflow. So you should write this as:

unsigned int seconds=time(NULL);
seconds+=2208988800u;

> How might I calculate the fraction part?

You can't without using some function that gives you the system time
with an accuracy to more than a second. Look at functions like
'gettimeofday'.

DS




More information about the questions mailing list