# Calculating NTP timestamp in C program - NTP

This is a discussion on Calculating NTP timestamp in C program - NTP ; 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 ...

# Thread: Calculating NTP timestamp in C program

1. ## Calculating NTP timestamp in C program

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;

How might I calculate the fraction part?

Thanks very much for your help,

Barry.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
or the newsgroup name in square brackets in the subject line. Sorry.

2. ## Re: Calculating NTP timestamp in C program

bg_ie@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

3. ## Re: Calculating NTP timestamp in C program

bg_ie@yahoo.com writes:
> 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;
>
> How might I calculate the fraction part?
>
> Thanks very much for your help,

First, "ntp.seconds" is a syntax error; identifiers can't contain '.'
characters.

I'm not familiar with NTP, but I think you're trying to get something
like the number of seconds since the year 1900. You're assuming that
time() returns the number of seconds since 1970 -- which is commonly
true (and required by POSIX, I think), but not guaranteed by the C
standard.

The C standard itself guarantees only that time_t is an arithmetic
type capable of representing times. It doesn't specify what the epoch
is, or even that there is an epoch, and it doesn't specify the
granularity. There is no standard way to get a time precise to
fractions of a second.

Furthermore, even assuming a 1-second granularity and an epoch of

time(NULL) + 2208988800

will overflow a 32-bit signed long int. For that matter, so will
2208988800 by itself (it exceeds 2**31-1).

If you can assume POSIX compliance, there's probably more you can do
-- but then you should probably be posting to comp.unix.programmer,
not comp.lang.c or comp.lang.c.moderated.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org
San Diego Supercomputer Center <*>
We must do something. This is something. Therefore, we must do this.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
or the newsgroup name in square brackets in the subject line. Sorry.

4. ## Re: Calculating NTP timestamp in C program

bg_ie@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.

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.

--
Simon.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
or the newsgroup name in square brackets in the subject line. Sorry.

5. ## Re: Calculating NTP timestamp in C program

bg_ie@yahoo.com wrote:

> 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;

Not at all. It _may_ be correct for some (perhaps quite common)
implementations, but it's not reliably so. time() returns _a_
representation of the system time. Which representation is used is
implementation-dependent. (Oh, and of course you can't declare a single
struct member like that. Assign to it, yes; but you have to declare the
struct as a whole.)
If you want the number of seconds since a particular moment, the best
you can do is this:

struct tm then_struct;
time_t then;
long seconds;

then_struct.tm_year= ;
/* ...and so on until... */
then_struct.tm_sec = ;
then=mktime(&then_struct);

seconds=difftime(time(), then);

It may be, btw, that your first time cannot fit in a time_t at all; for
example, on many common systems the NTP start time will be before the
earliest representable time, albeit not much before. Not much is, alas,
enough in this case. (To be less circumlocutory, the epoch under POSIX
is 00:00:00 on 1970-01-01; time_t, on such systems, is a signed long
int; and if that system is 32-bits and uses 32-bit longs, as many do,
2**15 seconds is just over 68 years, making the most negative time_t
represent a time somewhere in 1901... just over a year after NTP's
1900-01-01 :-/ ).
In such cases, the easy solution is to use a later start time, compute
the difference in seconds between that time and now, and then add the
(fixed, and known in advance) number of seconds between the helper start
time and the real start time. And then add a comment explaining why that
number of seconds was chosen, please...

> How might I calculate the fraction part?

You can't, in ISO C. On some systems, a time_t _might_ be a long int
representing hundreths of a second since foo. On others, it might be a
double representing seconds, with fractional part, since bar. On many,
it has no fractional part at all, and whole seconds are all you have.
You'll have to resort to system-specific functions. Most systems have
some high resolution time function, for varying meanings of "high".

Richard
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
or the newsgroup name in square brackets in the subject line. Sorry.

6. ## Re: Calculating NTP timestamp in C program

On 10 Jul 2006 23:11:34 GMT in comp.lang.c.moderated, bg_ie@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;

Yep.

>How might I calculate the fraction part?

The fraction part is an unsigned binary fraction of a second i.e. MSB
== 0.5s so depending on where you get your high resolution time from:

unsigned long ntp.fraction = (double)units/UNITS_PER_S*NTP_PER_S;
where:
#define UNITS_PER_S 1E9 /* or impl defined symbol */
#define NTP_PER_S 4294967296.

but for your time to be meaningful, you need to get your seconds and
fraction counts at the same instant, through a non-standard function,
such as POSIX clock_gettime(), not through a separate call to time().

Note that ntp.seconds should also be an unsigned long; the NTP date is
a signed 128 bit quantity with a 64 bit fraction, but the current NTP
timestamp is truncated to 32 bits each for seconds and fraction.

See:
http://www.cis.udel.edu/~mills/y2k.html#ntp
http://en.wikipedia.org/wiki/Unix_time

--
Thanks. Take care, Brian Inglis Calgary, Alberta, Canada

Brian.Inglis@CSi.com (Brian[dot]Inglis{at}SystematicSW[dot]ab[dot]ca)
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
or the newsgroup name in square brackets in the subject line. Sorry.

7. ## Re: Calculating NTP timestamp in C program

In article , bg_ie@yahoo.com wrote:

> 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;
>
> How might I calculate the fraction part?

Perhaps
http://en.wikipedia.org/wiki/Network_Time_Protocol
can be of some help.

--
Hans Aberg
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
or the newsgroup name in square brackets in the subject line. Sorry.

8. ## Re: Calculating NTP timestamp in C program

rlb@hoekstra-uitgeverij.nl (Richard Bos) writes:
[...]
> It may be, btw, that your first time cannot fit in a time_t at all; for
> example, on many common systems the NTP start time will be before the
> earliest representable time, albeit not much before. Not much is, alas,
> enough in this case. (To be less circumlocutory, the epoch under POSIX
> is 00:00:00 on 1970-01-01; time_t, on such systems, is a signed long
> int; and if that system is 32-bits and uses 32-bit longs, as many do,
> 2**15 seconds is just over 68 years, making the most negative time_t
> represent a time somewhere in 1901... just over a year after NTP's
> 1900-01-01 :-/ ).

You mean 2**31 seconds, not 2**15 seconds.

(This being comp.lang.moderated, I'll probably be one of at least half
a dozen people pointing this out.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org
San Diego Supercomputer Center <*>
We must do something. This is something. Therefore, we must do this.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must