Checking if a date has a leap second
In order to check if a date has a leap second, you must first obtain the leap second data by using one of the methods described in Obtaining a list of leap seconds. Then, you can iterate over the fetched leap seconds to check for the date.
For example, in order to check if December 31st, 2016 has a leap second, you can use the following code:
from datetime import date, timedelta
from leapseconddata import LeapSecondData
my_date = date(2016, 12, 31)
data = LeapSecondData.from_standard_source()
for leap in data.leap_seconds:
time = leap.start - timedelta(seconds=1)
if my_date.year == time.year and my_date.month == time.month and my_date.day == time.day:
print(f"{my_date} has a leap second!")
break
else:
print(f"{my_date} does not have a leap second.")
The output of this program is the following:
2016-12-31 has a leap second!