Handling leapseconds under unix

125 Views Asked by At

I have a library which needs to know the number of seconds since a previous epoch. This needs to work across leap second boundaries. One of the functions to make it easier for users is to specify the time in unix time - which of course does not take account for leap seconds. This is easily handled if I know when leap seconds have (or will be) inserted (or removed).

Is there any standard way of getting a list of leap second events, other than just keeping a hardcoded table (or similar), which of course needs to be updated every couple of years?

1

There are 1 best solutions below

4
On

Here are two partial answers to your problem:

  1. Because leap seconds are fundamentally unpredictable – they're caused by irregular variations in the rotation of the earth, due ultimately to the distribution of mass on the planet sloshing around – they're not defined more than a few months in advance. Therefore if you need to know leap seconds, you'll need an update every six months, even if the update is ‘no leap second this January/July’.

  2. If all you need is the number of seconds from a previous epoch, you don't have to use UTC. GPS time is essentially UTC without the leap seconds (that is, when a GPS device shows you civil time, it's essentially showing you GPS time plus a count of leap seconds). If you can get a hold of GPS time, therefore, you're sorted. You'd be able to get that from a GPS receiver. Alternatively, though I don't know, I'd be surprised if it wasn't possible to persuade an NTP server to give you GPS time.