2

I want to convert an array of date-time strings (YYYY-MM-DD hh:mm:ss) to GPS seconds (seconds after 2000-01-01 12:00:00) in the python environment.

In order to get the GPS seconds for a single date in Linux BASH, I simply input date2sec datetimestring and it returns a number.

I could do this within a for-loop within python. But, how would I incorporate this within the python script, as it is an external script?

Or, is there another way to convert an array of date-time strings (or single date-time strings incorporated into a for-loop) to GPS time without using date2sec?

2 Answers2

2

Updated Answer: uses Astropy library:

from astropy.time import Time

t = Time('2019-12-03 23:55:32', format='iso', scale='utc')
print(t.gps)

Here you are setting the date in UTC and t.gps converts the datetime to GPS seconds.

Further research showed that directly using datetime objects doesn't take leap seconds into account.

other helpful links here: How to get current date and time from GPS unsegment time in python

hiranyajaya
  • 579
  • 4
  • 13
  • 1
    I'm not sure it is the correct answer. The question was about GPS time, and `total_seconds()` docs say nothing. But `timestamp` doc gives us hints (using `total_seconds()`: it uses the POSIX convention and not the GPS convention. I never saw `date2sec` command, so I'm not sure which time are really meant. – Giacomo Catenazzi Feb 21 '20 at 10:36
  • I did further research based on your feedback and found out using Astropy library might be the least complicated approach to the problem. Thanks for your input :) – hiranyajaya Feb 26 '20 at 06:43
0

Here is the solution that I used for an entire array of date-times in a for-loop:

import numpy as _np
J2000 = _np.datetime64('2000-01-01 12:00:00')                    # Time origin
dateTime = [...]                                                 # an array of date-times in 'YYYY-MM-DD hh:mm:ss' format
GPSarray_secs = []                                               # Create new empty array
for i in range(0,len(dateTime)) :                                # For-loop conversion
     GPSseconds = (_np.datetime64(dateTime) - J2000).astype(int) # Calculate GPS seconds
     GPSarray_secs = _np.append(GPSarray_secs , GPSseconds)      # Append array

The simple conversion for one date-time entry is:

import numpy as _np
J2000 = _np.datetime64('2000-01-01 12:00:00')                    # Time origin
GPSseconds = (_np.datetime64(dateTime) - J2000).astype(int)      # Conversion where dateTime is in 'YYYY-MM-DD hh:mm:ss' format

Importing datetime should not be required.

  • Also your answer is not correct: you may miss some seconds. It uses unix time, so leap seconds will be ignored in the count of seconds. GPS time includes the leap second in the total seconds. – Giacomo Catenazzi Feb 21 '20 at 10:41