【问题标题】:Converting from Date-Time to GPS time从日期时间转换为 GPS 时间
【发布时间】:2020-02-16 05:54:17
【问题描述】:

我想在 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 时间?

【问题讨论】:

    标签: python bash time gps gps-time


    【解决方案1】:

    更新答案:使用 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

    【讨论】:

    • 我不确定这是不是正确的答案。问题是关于 GPS 时间的,total_seconds() docs 什么也没说。但是timestamp doc 给了我们提示(使用total_seconds():它使用POSIX 约定而不是GPS 约定。我从未见过date2sec 命令,所以我不确定真正指的是哪个时间。
    • 我根据您的反馈做了进一步的研究,发现使用 Astropy 库可能是解决问题最简单的方法。感谢您的意见:)
    【解决方案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

    【讨论】:

    • 您的答案也不正确:您可能会错过几秒钟。它使用 unix 时间,因此闰秒将在秒数中被忽略。 GPS 时间包括总秒数中的闰秒。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多