2

我想在 python 环境中将日期时间字符串数组 (YYYY-MM-DD hh:mm:ss) 转换为 GPS 秒数(2000-01-01 12:00:00 之后的秒数)。

为了在 Linux BASH 中获取单个日期的 GPS 秒数,我只需输入date2sec datetimestring并返回一个数字。

我可以在 python 的 for 循环中执行此操作。但是,我如何将它合并到 python 脚本中,因为它是一个外部脚本?

或者,是否有另一种方法可以在不使用 date2sec 的情况下将日期时间字符串数组(或合并到 for 循环中的单个日期时间字符串)转换为 GPS 时间?

4

2 回答 2

2

更新答案:使用 Astropy 库:

from astropy.time import Time

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

在这里,您以 UTC 设置日期t.gps并将日期时间转换为 GPS 秒。

进一步的研究表明,直接使用 datetime 对象不会考虑闰秒。

此处的其他有用链接: 如何从 Python 中的 GPS 取消分段时间获取当前日期和时间

于 2020-02-16T06:23:25.563 回答
0

这是我在 for 循环中用于整个日期时间数组的解决方案:

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

一个日期时间条目的简单转换是:

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

datetime不应要求导入。

于 2020-02-21T03:54:37.577 回答