Calculating NTP timestamp in C program

B

bg_ie

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.
 
K

Keith Thompson

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
1970-01-01 00:00:00 GMT, your calculation:

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.
 
S

Simon Biber

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/~mills/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.
 
R

Richard Bos

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= <the year of your base date>;
/* ...and so on until... */
then_struct.tm_sec = <the seconds of your base time>;
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
 
B

Brian Inglis

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
 
K

Keith Thompson

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top