Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GPS & UTC time conversion?
#1
Hello,

I have 2 questions as below. Have you ever met them or had any experience for my questions?

1. How can we convert from GPS timestamp to UTC time (human can read)?
Example:
GPS timestamp: 1180344041
Converted: UTC time (Human readable): Jun 01, 2017 09:20:23

2. How can we convert from UTC timestamp to GPS timestamp?
UTC timestamp: 1496308823
Converted: GPS timestamp: 1180344041
(Both are Jun 01, 2017 09:20:23)

I appreciate for any help.
Reply
#2
you need to import datetime
import datetime
import time
gps = 1180344041
print(time.strftime("%b %d %Y %H:%M:%S", time.gmtime(gps)))
strftime reference here: http://strftime.org/
Reply
#3
The "UTC time stamp" is the number of seconds since the "Epoch" (Jan 1st, 1970, 00:00:00 UTC). All the times you mentioned are UTC. This one is known as "POSIX time" (this is how time is kept on (Unix-based) computers).

The GPS timestamp is the same thing but the reference is different,and seems to be December 31st, 1979, 23:59:42 (technically it is December 1st 1980, 00:00:00 UTC, but the leap seconds are not taken in account the same way). So do go from GPS to POSIX and vice-versa you add/subtract 315964782 seconds.

POSIX time is supported in all computer libratries. In python:

Output:
>>> import datetime >>> datetime.date.fromtimestamp(0) datetime.date(1970, 1, 1) >>> datetime.date.fromtimestamp(1496308823) datetime.date(2017, 6, 1)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#4
(Jun-01-2017, 10:27 AM)Larz60+ Wrote: you need to import datetime
import datetime
import time
gps = 1180344041
print(time.strftime("%b %d %Y %H:%M:%S", time.gmtime(gps)))
strftime reference here: http://strftime.org/

Thank you for your answer, but this can not solve my question.
In your Python code, the result is : May 28 2007 09:20:41
This result may be for UTC time (started from January 1, 1970), not GPS time (started from January 6, 1980)
Reply
#5
(Jun-01-2017, 11:37 AM)Ofnuts Wrote: So do go from GPS to POSIX and vice-versa you add/subtract  315964782 seconds.

POSIX time is supported in all computer libratries. In python:

Thank you for your detail explanation. This answer is what I am finding.
But there is a problem if we continue using value 315964782 for next year (in 2018, this value may be obsolete!).
So far, I have read from some related information, the method of GPS time is as follows.

The different values from 1970 to 1980 offset with leap seconds is 315964800 seconds.
The leap seconds is 37 seconds at 2017. At 1980, the leap seconds was 19.
GPS time has to care about leap seconds. So, number 315964782 is the result of:
315964800 - (37-19) = 315964782

Next year, the leap seconds may be increased (38?), so we can not use value 315964782 anymore.
In Python, do we have any method to know the leap seconds in the future? If we can know this, we do not need to update our code manually if leap seconds changed. It is not a good method if we must change our code with new leap seconds for each next year!
Reply
#6
According to Wikipedia you can't tell more that 6 months in the future because the necessity of the adjustment is not known in advance.

If you want to keep accurate time, you cannot use Posix time anyway, since in Posix time, all days are 86400 seconds, and the system does unmentionable things to insert the leap seconds.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#7
(Jun-05-2017, 11:47 AM)Ofnuts Wrote: According to Wikipedia you can't tell more that 6 months in the future because the necessity of the adjustment is not known in advance.

Thank you so much for your answer. I wonder whether there is any way to know the leap seconds in far future (ex. next 2 years), but your answer make me clear now.

So, just a confirmation for your answer. It is important for me because I am confusing as your answer in below.

(Jun-05-2017, 11:47 AM)Ofnuts Wrote: If you want to keep accurate time, you cannot use Posix time anyway, since in Posix time, all days are 86400 seconds, and the system does unmentionable things to insert the leap seconds.

cannot must be can?

As your explanation, Posix time (Unix time, epoch time) has all days are 86400 seconds. Posix time can handle leap seconds itself, so I can use Posix time without worrying about leap seconds anymore.

So please give me a confirmation: "you can use Posix time anyway"? Is this right?
Reply
#8
No, you cannot, at least if you need accuracy to the second. See explanation here.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Time conversion error tester_V 1 2,017 Oct-28-2020, 10:48 PM
Last Post: tester_V
  pyad time conversion error (plus solution) PapaZod 2 3,497 Nov-25-2018, 02:42 PM
Last Post: PapaZod
  Conversion of Time Duration in seconds in python jdevansh99 0 2,855 Jun-05-2018, 05:12 PM
Last Post: jdevansh99

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020