从日期时间转换为GPS时间

2024-09-25 10:17:57 发布

您现在位置:Python中文网/ 问答频道 /正文

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

为了在LinuxBash中获取单个日期的GPS秒数,我只需输入date2sec datetimestring,它就会返回一个数字

我可以在python中的for循环中实现这一点。但是,由于它是一个外部脚本,我如何将其合并到python脚本中呢

或者,是否有其他方法可以将日期时间字符串数组(或合并到for循环中的单个日期时间字符串)转换为GPS时间而不使用date2sec


Tags: 字符串脚本for环境hh时间数组ss
2条回答

以下是我在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

更新答案:使用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对象不会考虑闰秒。

其他有用的链接如下: How to get current date and time from GPS unsegment time in python

相关问题 更多 >