1

I receive this data from the GPS Integrated in my Vehicle:

INS_Time::INS_Time_Millisec[ms]   # example of the value: 295584830.0
INS_Time::INS_Time_Week[Week]  # example of the value: 2077.0
INS_Time::Leap_seconds[s]  # example of the value: 18.0

what I need is a UTC timestamp so I assume that I must combine all those different times from GPS to get a UTC timestamp but I don't know how this should be done. I would be thankful if someone can guide me through this.

example of the result I want to have: 1572430625230

I'm using python 3.7, if there is a library for this it would be very helpful otherwise I'm searching also for an Algorithm to do this

basilisk
  • 1,156
  • 1
  • 14
  • 34
  • This might be helpful: https://python-forum.io/Thread-GPS-UTC-time-conversion – Yuvraj Jaiswal Nov 04 '19 at 14:06
  • @YuvrajJaiswal thanks but I made a google search before asking this. what confused me is those week and leap seconds features that I received. the link you send me is for converting only those milliseconds to UTC but in my case I have weeks and leap seconds that I must also somehow combine it with the millisec and then I can convert to UTC – basilisk Nov 04 '19 at 14:14

1 Answers1

4

My guess :

According to https://en.wikipedia.org/wiki/Epoch_(computing)#Notable_epoch_dates_in_computing , GPS epoch is 6 January 1980, GPS counts weeks (a week is defined to start on Sunday) and 6 January is the first Sunday of 1980

And according to http://leapsecond.com/java/gpsclock.htm , GPS time was zero at 0h 6-Jan-1980 and since it is not perturbed by leap seconds GPS is now ahead of UTC by 18 seconds.

So we have to define a gps_epoch and substract the given leap seconds in order to have utc datetime

from datetime import datetime, timedelta
import pytz

def gps_datetime(time_week, time_ms, leap_seconds):
    gps_epoch = datetime(1980, 1, 6, tzinfo=pytz.utc)
    # gps_time - utc_time = leap_seconds
    return gps_epoch + timedelta(weeks=time_week, milliseconds=time_ms, seconds=-leap_seconds)

With your example

>>>gps_datetime(2077, 295584830.0,18.0)
datetime.datetime(2019, 10, 30, 10, 6, 6, 830000, tzinfo=<UTC>)
>>>gps_datetime(2077, 295584830.0,18.0).timestamp()
1572429966.83  

But I am quite far from your expected result (1572430625230 even if it is expressed in ms)

Do not forget to pip install pytz

jbernardes
  • 106
  • 5
  • @jbernades thanks for your answer, but can you please explain the code? I mean I don't get why you used the 1,6,1980 and why you substract the leap seconds instead of adding it – basilisk Nov 04 '19 at 17:23
  • @basilisk i have edited my answer with some explanations. – jbernardes Nov 04 '19 at 17:41
  • @jbernades thanks again, I ll try and see if it will work for me. do you think that I also need to add the offset in case my timezone is not GMT ? – basilisk Nov 04 '19 at 18:53
  • @basilisk actually i forgot to express gps_epoch as a UTC time. I just updated the code above. If you just need timestamp the last edited code should work. – jbernardes Nov 05 '19 at 10:12
  • 2
    Just for enduring value of this answer, worth noting that the 18 seconds mentioned in the text is current as of the date of the answer, but it changes over time as leap seconds are added or subtracted. (Never subtracted one yet, but it's allowed by the standards.) – Brick Jun 13 '22 at 13:59