Damien
Damien

Reputation: 795

Testing for leap seconds for a given time_t in Linux

Is there a simple way of determining how many (if any) leap seconds are applied for a given implementation of:

time_t unix_seconds = mktime(&my_tm);

For example, is there a field that is populated in my_tm?

Otherwise, I suppose my option is test for known time_t values for given times bordering leap second transitions, but it would be nice if there was something more convenient.

Upvotes: 3

Views: 520

Answers (2)

Ant6n
Ant6n

Reputation: 2007

You could probably reduce the problem to finding out how many leap seconds there are between two time_t times.

To find that, you could calculate the tm struct for both times ta and tb, and calculate the number of seconds that have passed since the last hour by doing aTmStruct->tm_min*60 + aTmStruct->tm_sec. Store that as secA and secB.

Then calculate the difftime of ta and tb. Now (diff + secB - secA) % 3600 should give you the number of leap seconds, as long as they were fewer than 3600 leapseconds between ta and tb. Basically, if there was a leap second inserted than the difftime should be one larger than the difference between secA and secB.

Upvotes: 1

psykoblast
psykoblast

Reputation: 116

you can do a loop of your implementation and calculate an average of time it takes.

int i_loop = 0;
float diff = 0 ;
struct timeval tv1,tv2;
struct timezone tz;
int k = 0;
N = 128;
time_t unix_seconds ;

while(true)
{
  i_loop = (k & N-1);
  if(i_loop= == 0)
   {

     gettimeofday(&tv2,&tz);
    //here put your code
     unix_seconds = mktime(&my_tm);
    //end of your code
     diff =  (tv2.tv_sec-tv1.tv_sec) * 1000L ;
     diff = diff/N;
     gettimeofday(&tv1,&tz);
}

  k++;
}

"diff" is the time it takes in ms

Upvotes: 0

Related Questions