LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-31-2009, 08:50 AM   #1
minimol
LQ Newbie
 
Registered: Aug 2006
Distribution: Fedora
Posts: 10

Rep: Reputation: 0
Get the number of leap seconds in time ( time_t)


time() API gives the number of seconds since 1970 Jan 1st 00:00:00 without considering leap seconds. How to get the number of leap seconds which needs to be considered in the value returned by time()..

(gmtime will convert time_t to struct tm* and considers leap seconds. I am trying to write an API which will do the same function as gmtime)
 
Old 03-31-2009, 09:30 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
The value returned by the time() functions is the number of seconds since 01 January 1970 UTC; i.e., including the leap seconds inserted periodically. When the friendly folks at the atomic clocks spread around the world add a leap second, it's one more tick and, if your systems are referencing NTP, a WWV clock or some other time referenced back to atomic clocks, a tick is added.

You can, if you really want to fiddle around, use Julian Day Numbers and see if you can get the difference. The Julian Day Number for 01 Jan 1970 is 2440588 and for today (31 March 2009) is 2454922 (14334 days). Given that there are about 86400 seconds per day...

There's a discussion at http://tycho.usno.navy.mil/leapsec.html that may be interesting.

Last edited by tronayne; 03-31-2009 at 09:42 AM.
 
Old 04-07-2009, 07:30 AM   #3
minimol
LQ Newbie
 
Registered: Aug 2006
Distribution: Fedora
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks for the information. But I am not able to find out how many leap seconds are present at a given time.. ( like there are
24 leap seconds at 01/01/2009 01:00:00.
23 leap seconds in 31/12/2008 01:00:00 ).

How will I get this information ? Functions like gmtime() are getting this info from /etc/localtime. But this is an encrypted file. How to parse it and get the information ?

( With Julian day numbers I could find out how many leap seconds are there at the current time. I can compare Julian seconds value and time() to get this. )
 
Old 04-07-2009, 08:57 AM   #4
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
If you are running NTP, your system clock is kept up-to-date Universal Coordinated Time (UTC). UTC includes all the leap seconds added since 1972 (when the first leap second was added). On the other hand, Global Positioning System (GPS) time is not adjusted for leap seconds (this is discussed in the USNO article referenced above) and, as of 1 Jan 2006, GPS was ahead of UTC by 14 seconds (now it's 25).

So, if I was really, really interested in keeping track, I'd synchronize my system clock with NTP (so it's UTC) and I'd buy a GPS receiver with a USB port that I could query periodically and calculate the difference between UTC and GPS time. It may be that there is a GPS time reference somewhere or other on the Internet (Google is your friend here) that could be used instead.

By the way, /etc/localtime is not encrypted, it's data (and, no, the number of leap seconds is not stored in that or any other file that I know of). Take a look at the manual page (man localtime).

Sometimes, you just have to break down and count the number of times something has happened -- in this case, there are 25 leap seconds inserted beginning with 1 Jan 1972, continuing through 1 Jan 2009 as shown in this chart (from the USNO) trimmed to show only the leap seconds and ignoring time adjustments prior to 1 Jan 1972.
Code:
 1972 JAN  1 =JD 2441317.5  TAI-UTC=  10.0       S + (MJD - 41317.) X 0.0      S
 1972 JUL  1 =JD 2441499.5  TAI-UTC=  11.0       S + (MJD - 41317.) X 0.0      S
 1973 JAN  1 =JD 2441683.5  TAI-UTC=  12.0       S + (MJD - 41317.) X 0.0      S
 1974 JAN  1 =JD 2442048.5  TAI-UTC=  13.0       S + (MJD - 41317.) X 0.0      S
 1975 JAN  1 =JD 2442413.5  TAI-UTC=  14.0       S + (MJD - 41317.) X 0.0      S
 1976 JAN  1 =JD 2442778.5  TAI-UTC=  15.0       S + (MJD - 41317.) X 0.0      S
 1977 JAN  1 =JD 2443144.5  TAI-UTC=  16.0       S + (MJD - 41317.) X 0.0      S
 1978 JAN  1 =JD 2443509.5  TAI-UTC=  17.0       S + (MJD - 41317.) X 0.0      S
 1979 JAN  1 =JD 2443874.5  TAI-UTC=  18.0       S + (MJD - 41317.) X 0.0      S
 1980 JAN  1 =JD 2444239.5  TAI-UTC=  19.0       S + (MJD - 41317.) X 0.0      S
 1981 JUL  1 =JD 2444786.5  TAI-UTC=  20.0       S + (MJD - 41317.) X 0.0      S
 1982 JUL  1 =JD 2445151.5  TAI-UTC=  21.0       S + (MJD - 41317.) X 0.0      S
 1983 JUL  1 =JD 2445516.5  TAI-UTC=  22.0       S + (MJD - 41317.) X 0.0      S
 1985 JUL  1 =JD 2446247.5  TAI-UTC=  23.0       S + (MJD - 41317.) X 0.0      S
 1988 JAN  1 =JD 2447161.5  TAI-UTC=  24.0       S + (MJD - 41317.) X 0.0      S
 1990 JAN  1 =JD 2447892.5  TAI-UTC=  25.0       S + (MJD - 41317.) X 0.0      S
 1991 JAN  1 =JD 2448257.5  TAI-UTC=  26.0       S + (MJD - 41317.) X 0.0      S
 1992 JUL  1 =JD 2448804.5  TAI-UTC=  27.0       S + (MJD - 41317.) X 0.0      S
 1993 JUL  1 =JD 2449169.5  TAI-UTC=  28.0       S + (MJD - 41317.) X 0.0      S
 1994 JUL  1 =JD 2449534.5  TAI-UTC=  29.0       S + (MJD - 41317.) X 0.0      S
 1996 JAN  1 =JD 2450083.5  TAI-UTC=  30.0       S + (MJD - 41317.) X 0.0      S
 1997 JUL  1 =JD 2450630.5  TAI-UTC=  31.0       S + (MJD - 41317.) X 0.0      S
 1999 JAN  1 =JD 2451179.5  TAI-UTC=  32.0       S + (MJD - 41317.) X 0.0      S
 2006 JAN  1 =JD 2453736.5  TAI-UTC=  33.0       S + (MJD - 41317.) X 0.0      S
 2009 JAN  1 =JD 2454832.5  TAI-UTC=  34.0       S + (MJD - 41317.) X 0.0      S
The values in the chart are explained in detail at the USNO web site.

Here's the bottom line: your system clock is kept synchronized with UTC (if you're using NTP). UTC includes leap seconds (if and when they are added); if you are using NTP, your system clock is synchronized when that happens (and it doesn't know and doesn't care that there was a leap second added). If you want to know how many leap seconds are added over time, you'll need access to a time source that is not adjusted for leap seconds -- GPS is one of those. By simply comparing UTC to GPS (or some other non-leap-second-adjusted source), you've got the number of leap seconds.

GPS is not adjusted because it's used for navigation (degrees-minutes-seconds) and, if it were adjusted, you would not be able to determine your position on the earth with any hope of accuracy (25 seconds of latitude or longitude is, well, quite a distance on the surface of the earth -- you might run into things).
 
Old 07-07-2009, 06:19 AM   #5
minimol
LQ Newbie
 
Registered: Aug 2006
Distribution: Fedora
Posts: 10

Original Poster
Rep: Reputation: 0
thanks again for the information..

/etc/localtime is not encrypted, it's data ... That info helped.

I parsed /etc/localtime and got the information (man tzfile gives the format of /etc/localtime)
 
Old 07-07-2009, 12:52 PM   #6
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,898
Blog Entries: 27

Rep: Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185
Hi,

Please mark your post as solved. You can use the 'Thread tools' as the OP to mark as solved.

I was going to assist but since it has been 'solved'. Get the point? Please mark it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to get the current time with nano seconds on a linux box using c++ fardad Linux - Software 3 01-09-2009 10:29 PM
Bash: how to get time (in seconds) elapsed since creation of a file? J_Szucs Linux - General 6 12-28-2008 12:28 AM
VB, 30 seconds of your time, please? rhoyerboat Programming 2 02-27-2007 02:26 PM
Trying to get ps to display time a process has been running in seconds kinetik Programming 1 02-26-2007 08:21 PM
get system time in mili-seconds how to onnyloh Programming 2 09-22-2004 02:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration