Skip to content

Instantly share code, notes, and snippets.

@jeremiahajohnson
Last active April 3, 2022 02:33
Show Gist options
  • Save jeremiahajohnson/eca97484db88bcf6b124 to your computer and use it in GitHub Desktop.
Save jeremiahajohnson/eca97484db88bcf6b124 to your computer and use it in GitHub Desktop.
Python function to take GPS week and GPS seconds and return a UTC datetime string in the format "YYYY-MM-DD HH:MM:SS"
def weeksecondstoutc(gpsweek,gpsseconds,leapseconds):
import datetime, calendar
datetimeformat = "%Y-%m-%d %H:%M:%S"
epoch = datetime.datetime.strptime("1980-01-06 00:00:00",datetimeformat)
elapsed = datetime.timedelta(days=(gpsweek*7),seconds=(gpsseconds+leapseconds))
return datetime.datetime.strftime(epoch + elapsed,datetimeformat)
weeksecondstoutc(1811,164196.732,16) ## --> '2014-09-22 21:36:52'
@LuisDLCP
Copy link

Great! I have a question, how do you get the leap seconds?

@zfb132
Copy link

zfb132 commented Aug 20, 2020

@LuisDLCP I get every leap second in Leap_Second.dat from the website International Earth rotation and Reference systems Service.
And GPS was zero at 1980-01-06 00:00:00, so we can count from the line which is the 20th leap second in UTC. In other words, the leap second of GPS is 35-19=16 on September 22, 2014.
Here is a website that displays the local time, UTC, and GPS time real-timely.

@LuisDLCP
Copy link

Thank you @zfb132!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment