Basj
Basj

Reputation: 46193

How many seconds elapsed since 01/01/1970, leap-seconds included?

The Unix timestamp given by:

int(time.time())

gives the number of seconds elapsed since 01/01/1970, without leap-seconds.

Just out of curiosity, how to get the true number of seconds elapsed since this date, leap-seconds included? (i.e. the distance between these two events on a time axis)

Notes:

Upvotes: 3

Views: 1365

Answers (2)

Compholio
Compholio

Reputation: 1002

I ran into this same issue, and you can work around it by using "right" time:

import os; import time; import datetime;
tai_epoch=datetime.datetime(1970, 1, 1, 0, 0, 10)
timezone=time.tzname[0];os.environ['TZ']='right/UTC';time.tzset()
now=datetime.datetime.utcnow()
leap_seconds=int(now.timestamp())-int((now-tai_epoch).total_seconds())
right_time=int(now.timestamp())+leap_seconds
os.environ['TZ']=timezone;time.tzset()

You can compare this against the regular UTC time like so:

utc_time=int(time.time())
print(right_time-utc_time)

which, at the moment, will give you 37.

Upvotes: 0

Steve Allen
Steve Allen

Reputation: 326

The number of SI seconds between any two UTC timestamps since 1972-01-01 requires access to the list of leap seconds which have been introduced into UTC. This list is available as part of the IANA tzdata distribution and it can also be obtained from other sources.

Caution is required because the number of SI seconds between what was known as 1970-01-01 and 1972-01-01 is 2x365x24x60x60 + 1.999918 SI seconds because at 1970 the official time was determined not by cesium atoms but by actually measuring the rotation of the earth, so the official seconds were mean solar seconds not SI seconds.

Upvotes: 2

Related Questions