Skip to content

Instantly share code, notes, and snippets.

@jeremiahajohnson
Created January 6, 2015 17:13
Show Gist options
  • Save jeremiahajohnson/7299e099a6cbc88fac0a to your computer and use it in GitHub Desktop.
Save jeremiahajohnson/7299e099a6cbc88fac0a to your computer and use it in GitHub Desktop.
Local time to GPS Week and GPS Seconds
def localtogps(timestring,timeformat,leapseconds,localoffset=0):
import datetime
epoch = datetime.datetime.strptime("1980-01-07 00:00:00","%Y-%m-%d %H:%M:%S")
local = datetime.datetime.strptime(timestring,timeformat)
utc = local - datetime.timedelta(hours=localoffset)
diff = utc-epoch
gpsWeek = diff.days/7
secondsThroughDay = (utc.hour * 3600) + (utc.minute * 60) + utc.second
if utc.isoweekday()== 7:
weekday = 0
else:
weekday = utc.isoweekday()
gpsSeconds = (weekday * 86400) + secondsThroughDay - leapseconds
return (gpsWeek,gpsSeconds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment