Re: [LEAPSECS] Mechanism to provide tai-utc.dat locally

From: Keith Winstein <keithw_at_MIT.EDU>
Date: Mon, 25 Dec 2006 02:55:26 -0500 (EST)

On Sun, 24 Dec 2006, Ashley Yakeley wrote:

> This has led me to consider run-time methods of obtaining leap-second
> information, and how that might be standardised for use by software
> authors. For instance, I imagine a software package that established
> a well-known place in the directory hierarchy to find tai-utc.dat and
> perhaps other "earth data" files, and was responsible for keeping
> them up to date. Other software could then make use of the package
> without each having to implement their own mechanism.
>
> Does this strike people as worthwhile? Does anyone know of an
> existing effort along these lines?

Hi Ashley,

The GNU C library includes a leap-second table in its timezone/leapseconds
file in the source package. This gets compiled in to the "right" (that is,
non-POSIX) zoneinfo files in, e.g., /usr/share/zoneinfo/right/... .

If your users have an up-to-date GNU libc, they can get the value of
TAI-UTC at any point since 1970 using C library functions. Here's a sample
C program that shows how to do it.

Merry Christmas!

-Keith

===================
Example output:

$ ./taiutc 915148799
At Thu Dec 31 23:59:59 1998 UTC, TAI-UTC was 31.0 seconds.
$ ./taiutc 915148800
At Fri Jan 1 00:00:00 1999 UTC, TAI-UTC was 32.0 seconds.
$ ./taiutc 1136073599
At Sat Dec 31 23:59:59 2005 UTC, TAI-UTC was 32.0 seconds.
$ ./taiutc 1136073600
At Sun Jan 1 00:00:00 2006 UTC, TAI-UTC was 33.0 seconds.

===================

The program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main( int argc, char *argv[] )
{
   time_t posix_epoch_time;
   time_t leap_second_time;
   struct tm my_tm;
   double difference;
   char *when;
   char newline = '\n';

   if ( argc != 2 ) {
     fprintf( stderr, "USAGE: %s POSIX_EPOCH_TIME\n", argv[ 0 ] );
     exit( 1 );
   }

   posix_epoch_time = atoi( argv[ 1 ] );

   /* Interpret argument as POSIX epoch time */
   setenv( "TZ", "UTC", 1 ); localtime_r( &posix_epoch_time, &my_tm );

   /* Get calendar description and strip trailing newline */
   strtok( when = asctime( &my_tm ), &newline );

   /* Find actual number of elapsed seconds between 1970 and the time */
   setenv( "TZ", "right/UTC", 1 ); leap_second_time = mktime( &my_tm );

   /* Subtract POSIX epoch time from actual number of elapsed secs, */
   /* adding 10 to reflect TAI-UTC difference at 1970. */
   difference = difftime( leap_second_time, posix_epoch_time ) + 10;

   printf( "At %s UTC, TAI-UTC was %.1f seconds.\n", when, difference );

   return 0;
}

===================

Compile with:

$ gcc -O2 -g -Wall -o taiutc taiutc.c
Received on Sun Dec 24 2006 - 23:56:07 PST

This archive was generated by hypermail 2.3.0 : Sat Sep 04 2010 - 09:44:55 PDT