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

Simon Biber news at ralmin.cc
Tue Jul 25 10:53:12 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?

It would have been helpful if you had describes to us what "ntp time" 
is. It's not defined by the C standard, and so it's not considered 
common knowledge on comp.lang.c.

 From reading http://www.eecis.udel.edu/%7emills/leap.html
I get the idea that ntp time is given as
the number of seconds since 0h 1 January 1900

> long int ntp.seconds = time(NULL) + 2208988800;

Several problems with the above:
  - ntp.seconds is not a valid variable name, as it contains a period.
  - time(NULL) may be the number of weeks since the fall of Rome
    that is, it is not necessarily a count of seconds
    and it is not necessarily based on the Unix epoch.
  - You may overflow the long int.

> How might I calculate the fraction part?

The difftime function returns a floating point value in seconds, that 
may or may not include a fraction of a second.

C doesn't guarantee that you will get a time stamp accurate to the 
second, let alone the fraction of a second.

C also doesn't guarantee that leap seconds will be properly implemented, 
so you can't be sure whether or not your NTP time will be accurate.

On the whole, you are better off receiving advice in a forum that can 
tell you about the intricacies of time handling on your particular platform.

comp.lang.c.moderated - moderation address: clcm at plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line.  Sorry.

More information about the questions mailing list