Skip to content

Instantly share code, notes, and snippets.

@jeremiahajohnson
Created September 24, 2014 15:42
Show Gist options
  • Save jeremiahajohnson/c3500d7404c4dcad1cb3 to your computer and use it in GitHub Desktop.
Save jeremiahajohnson/c3500d7404c4dcad1cb3 to your computer and use it in GitHub Desktop.
Python functions to transform GPS second to time
from decimal import Decimal
def timeconvert24(GPSweekSec):
DayOfWeek = GPSweekSec / 86400
Hour = Decimal(str(DayOfWeek))%1 * 86400 / 3600
Minute = Decimal(str(Hour))%1 * 3600 / 60
Second = Decimal(str(Minute))%1 * 60
return "%02d" % (Hour,) + ":" + "%02d" % (Minute,) + ":" + "%02d" % (Second,)
def timeconvert12(GPSweekSec):
DayOfWeek = GPSweekSec / 86400
if (Decimal(str(DayOfWeek))%1 * 86400 / 3600) > 12:
Hour = (Decimal(str(DayOfWeek))%1 * 86400 / 3600) - 12
AMPM = "PM"
else:
Hour = Decimal(str(DayOfWeek))%1 * 86400 / 3600
AMPM = "AM"
Minute = Decimal(str(Hour))%1 * 3600 / 60
Second = Decimal(str(Minute))%1 * 60
return "%02d" % (Hour,) + ":" + "%02d" % (Minute,) + ":" + "%02d" % (Second,) + " " + AMPM
#Function tests
testtime = 137760.9116
print "GPS week seconds: " + str(testtime)
print "24hr time: " + timeconvert24(testtime)
print "12hr time: " + timeconvert12(testtime)
raw_input("Press enter to close")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment