我想从Unix时间转换为GPS时间,即计算自Python中的GPS时代(1980年1月6日)开始以来的周数。我不是要一年中的几个星期,而是1980年以后的几个星期。
首先,我的尝试是通过使用time.time()来返回自1970年称为Unix的时代以来的时间,并从该纪元与全球定位系统开始日期之间经过的秒数中减去秒数。
这将返回一个正确的值,以秒为单位,自1980年以来的时间,但我想要GPS周数。有一个标准的Python函数返回这个函数吗?
注意到
GPS日期表示为自时代以来的周数和每周数的秒数.全球定位系统时代是不同的-1980年1月6日00:00:00。此外,“全球定位系统计数周”,因为时代-全球定位系统周被定义为周日开始。注:1月6日是1980年的第一个星期日。1
Unix时间系统的时代是1970年1月1日00:00:00,ISO将一年中的第一周定义为“包含1月第四天的一周,这相当于说它是将新年至少重叠四天的第一周”。
还有其他的时间系统,最显著的是J2000。从一个时间系统到另一个时间系统的转换是不平凡的。
为了处理GPS时间,Perl提供了日期时间::精确库,它使用额外的GPS操作执行公共时间和日期操作。同样的问题是,Python是否提供了类似的库?
发布于 2017-07-31 18:57:49
计算日差然后除以7
要做到这一点,一种方法是使用python的datetime模块。其中一个有用的功能是,它允许您使用date.days获取日期并将其转换为多天。使用这些天,我们可以减去它们并除以7 (一周中的天数:p)来得到之间的周数。
但是,在将每一天转换为天之前,您需要首先从该date中减去一周中的一天。这将给你那个星期的Monday的日期,这将消除逐个错误.
要做到这一点,您可以这样做:
from datetime import date, timedelta
epoch = date(1980, 1, 6)
today = date.today()
epochMonday = epoch - timedelta(epoch.weekday())
todayMonday = today - timedelta(today.weekday())现在您有了他们周的Monday的日期,您需要减去它们来找出差异,然后除以7来得到周数。
这给出了最后的输出:
noWeeks = (todayMonday - epochMonday).days / 7发布于 2019-02-04 16:40:17
@Dave的答案def utctoweekseconds(utc,leapseconds):是好的,但是您需要在tdiff = utc -epoch -datetime.timedelta(seconds=leapseconds)中添加时间增量,而不是减去(GPS时间领先于UTC)。如果添加日期时间,而不是减去日期时间,函数就会很好地工作。
当我通过这个函数运行我的计算机的datetime.utcnow()时,我注意到了这一点,并将其与我连接到该设备的GPS接收器的时间进行了比较,它们最初被关闭了36秒(是闰秒的两倍)。
2014-09-22 21:36:52是164212周的全球定位系统时间,而不是164196 (根据https://www.labsat.co.uk/index.php/en/gps-time-calculator)。
(对不起,我不能留下评论,我没有足够的声誉)
正确的功能:
def utctoweekseconds(utc,leapseconds):
""" Returns the GPS week, the GPS day, and the seconds
and microseconds since the beginning of the GPS week """
import datetime, calendar
datetimeformat = "%Y-%m-%d %H:%M:%S"
epoch = datetime.datetime.strptime("1980-01-06 00:00:00",datetimeformat)
tdiff = utc -epoch + datetime.timedelta(seconds=leapseconds)
gpsweek = tdiff.days // 7
gpsdays = tdiff.days - 7*gpsweek
gpsseconds = tdiff.seconds + 86400* (tdiff.days -7*gpsweek)
return gpsweek,gpsdays,gpsseconds,tdiff.microseconds发布于 2017-12-10 20:00:57
gist:https://gist.github.com/jeremiahajohnson/eca97484db88bcf6b124上有一个反函数
def weeksecondstoutc(gpsweek,gpsseconds,leapseconds):
import datetime, calendar
datetimeformat = "%Y-%m-%d %H:%M:%S"
epoch = datetime.datetime.strptime("1980-01-06 00:00:00",datetimeformat)
elapsed = datetime.timedelta(days=(gpsweek*7),seconds=(gpsseconds+leapseconds))
return datetime.datetime.strftime(epoch + elapsed,datetimeformat)
weeksecondstoutc(1811,164196.732,16) ## --> '2014-09-22 21:36:52'请注意,此解决方案手动计算了POSIX和TAI之间的闰秒差异。(请参阅第二中的闰秒)Leap秒和https://stackoverflow.com/a/33445945/1653571一样可用,但这段代码不使用它。
因此,从unix时间转换回GPS还需要关注闰秒:
def utctoweekseconds(utc,leapseconds):
""" Returns the GPS week, the GPS day, and the seconds
and microseconds since the beginning of the GPS week """
import datetime, calendar
datetimeformat = "%Y-%m-%d %H:%M:%S"
epoch = datetime.datetime.strptime("1980-01-06 00:00:00",datetimeformat)
tdiff = utc -epoch -datetime.timedelta(seconds=leapseconds)
gpsweek = tdiff.days // 7
gpsdays = tdiff.days - 7*gpsweek
gpsseconds = tdiff.seconds + 86400* (tdiff.days -7*gpsweek)
return gpsweek,gpsdays,gpsseconds,tdiff.microseconds
utctoweekseconds(datetime.datetime.strptime('2014-09-22 21:36:52',"%Y-%m-%d %H:%M:%S"),16)
## gives: (1811, 1, 164196,0)我也需要一天的全球定位系统周,因为我在计算文件名的全球定位系统时钟和轨道文件从products.html &c。
https://stackoverflow.com/questions/45422739
复制相似问题