473,394 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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: cl**@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.
Jul 10 '06 #1
6 19915
bg***@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
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
--
comp.lang.c.moderated - moderation address: cl**@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.
Jul 25 '06 #2
bg***@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.

--
Simon.
--
comp.lang.c.moderated - moderation address: cl**@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.
Jul 25 '06 #3
bg***@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= <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
--
comp.lang.c.moderated - moderation address: cl**@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.
Jul 25 '06 #4
On 10 Jul 2006 23:11:34 GMT in comp.lang.c.moderated, bg***@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

Br**********@CSi.com (Brian[dot]Inglis{at}SystematicSW[dot]ab[dot]ca)
fake address use address above to reply
--
comp.lang.c.moderated - moderation address: cl**@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.
Jul 25 '06 #5
In article <cl****************@plethora.net>, bg***@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: cl**@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.
Jul 25 '06 #6
rl*@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) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
--
comp.lang.c.moderated - moderation address: cl**@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.
Aug 9 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dave Smithz | last post by:
Help, I am sure this has been asked before but it is difficult to search for. Given a date, is there a function or a already written script out there that will allow my to get the previous...
5
by: Ron Adam | last post by:
Hi, I'm having fun learning Python and want to say thanks to everyone here for a great programming language. Below is my first Python program (not my first program) and I'd apreciate any...
2
by: Philip Townsend | last post by:
I need to add together some times that are stored in a database as seperate int values. Here is a code segment that is flawed (ts is declared as: TimeSpan ts=new TimeSpan(0,0,0). As is, the value...
4
by: Mark Harrison | last post by:
So, I'm recording the timestamp from some unix files. Any opinions as to whether it's best to use a "bigint" (which matches the file system data) or a "timestamp" (which might be easier to...
2
by: bufbec | last post by:
I have worked on this for hours and can't come up with a solution. Hope someone can help me. I have a table called TMBS_HMAUDIT_PARMS. this table contains data to tell me how often a person is...
4
by: iamonthisboat | last post by:
I have a data set like so: UTC_TIME Timestamp NodeID Message Flag Line Station 11/19/2005 10:45:07 1132397107.91 1 3 5 1028 1034...
16
by: Brian Tkatch | last post by:
I have a PROCEDURE that test other PROCEDUREs. Timings_Call ------------------ Id Group Name Text Timings_Log
6
by: sangith | last post by:
Hi, Is there any module which checks for the previous day's date. For eg I run the program which gives me the current date and I have to check another log file to see if it contains the timestamp...
4
by: =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= | last post by:
Say I have a class like, class Sample { public decimal first = 10; public decimal second = 20; } I have initialized it
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.