mon*_*kut 4 python binary datetime
我试图找出我认为应该是8byte/64位时间戳的内容.
import datetime
GPS_EPOCH = datetime.datetime(1980, 1, 6)
t1 = "\x00\x00\xBF\x13\xDB\x79\xC0\x00" # expected: 2012-10-04 01:00:51.759
t2 = "\x00\x00\xC0\x13\xDB\x79\xC0\x00" # expected: 2012-10-04 01:00:51.760
t3 = "\x00\x00\xC2\x13\xDB\x79\xC0\x00" # expected: 2012-10-04 01:00:51.763
t4 = "\x00\x00\x80\xE7\xFB\x79\xC0\x00" # expected: 2012-10-04 01:45:40.960
Run Code Online (Sandbox Code Playgroud)
我相信由GPS_EPOCH 产生t1并且t2应该从GPS_EPOCH偏移的值(s?).但是,我似乎无法得到与预期结果datetime匹配的结果.
我一直在阅读,这似乎是合乎逻辑的,它将被分成两部分,一部分可能是小数,另一部分是秒(每个4字节?).但是,我没有找到任何基于GPS时代的时间戳格式的参考.
任何想法如何将其转化为预期的结果?
我有它.你提供了足够的例子.
>>> t1 = "\x00\x00\xBF\x13\xDB\x79\xC0\x00" # expected: 2012-10-04 01:00:51.759
>>> import struct
>>> import datetime
>>> GPS_EPOCH = datetime.datetime(1980, 1, 6)
>>> t1_unpacked = struct.unpack('<q', t1)[0]
>>> t1_seconds = t1_unpacked / 52428800
>>> t1_us = int(round((t1_unpacked % 52428800) / 52.428800, 0))
>>> GPS_EPOCH + datetime.timedelta(seconds=t1_seconds, microseconds=t1_us)
datetime.datetime(2012, 10, 4, 1, 0, 51, 758750)
Run Code Online (Sandbox Code Playgroud)
把它们放在一起:
def gps_time(timestamp):
unpacked = struct.unpack('<q', timestamp)[0]
seconds = unpacked / 52428800
microseconds = int(round((unpacked % 52428800) / 52.428800, 0))
return GPS_EPOCH + datetime.timedelta(seconds=seconds, microseconds=microseconds)
>>> gps_time(t2)
datetime.datetime(2012, 10, 4, 1, 0, 51, 760000)
>>> gps_time(t3)
datetime.datetime(2012, 10, 4, 1, 0, 51, 762500)
>>> gps_time(t4)
datetime.datetime(2012, 10, 4, 1, 45, 40, 960000)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1981 次 |
| 最近记录: |