Last active
April 3, 2022 02:33
-
-
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"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
Great! I have a question, how do you get the leap seconds?
@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.
Thank you @zfb132!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, there may be a small mistake in the 5th line because GPS is ahead of UTC. Additionally, the
calendar
module is never used.Optimized code: