23 DD 78 34 = 2013-01-28 21:52:XX //second not sure
92 e3 78 34 = 2013-01-28 22:14:XX
d4 e3 78 34 = 2013-01-28 22:15:XX
16 e4 78 34 = 2013-01-28 22:16:XX
如何将十六进制转换为日期时间?它不是 UNIX 日期
十六进制值看起来像是小端编码的,但它并没有接缝使用秒作为刻度值:
>>> values = [
... (0x3478dd23, datetime(2013, 1, 28, 21, 52)),
... (0x3478e392, datetime(2013, 1, 28, 22, 14)),
... (0x3478e3d4, datetime(2013, 1, 28, 22, 15)),
... (0x3478e416, datetime(2013, 1, 28, 22, 16))
... ]
...
>>> for s, dt in values:
... print dt - datetime.fromtimestamp(s)
...
5544 days, 19:02:37
5544 days, 18:57:10
5544 days, 18:57:04
5544 days, 18:56:58
由于偏移量随着时间的推移而缩小,我计算了一个校正因子:
>>> ts_delta = values[1][0] - values[0][0]
>>> ts_delta
1647
>>> dt_delta = values[1][1] - values[0][1]
>>> dt_delta
datetime.timedelta(0, 1320)
>>> dt_delta = dt_delta.days * 60*60*24 + dt_delta.seconds
>>> dt_delta
1320
>>> factor = float(dt_delta) / float(ts_delta)
>>> factor
0.8014571948998178
1647 个刻度 = 1320 秒。
现在,如果我们将此因子应用于时间戳,则偏移量几乎保持不变(秒除外,但由于您不知道它们的值,我刚刚在源数据中使用了 0)
>>> for s, dt in values:
... print dt - datetime.fromtimestamp(s * factor)
...
7567 days, 17:16:08.233151
7567 days, 17:16:08.233151
7567 days, 17:16:15.336976
7567 days, 17:16:22.440802
考虑到这一点,您可以使用此偏移量和因子来转换原始值:
>>> offset = values[0][1] - datetime.fromtimestamp(values[0][0]*factor)
>>> offset
datetime.timedelta(7567, 62168, 233151)
def hex_to_datetime(s):
return datetime.fromtimestamp(s*factor) + offset
>>> for s, dt in values:
... print hex_to_datetime(s), dt
...
2013-01-28 21:52:00 2013-01-28 21:52:00
2013-01-28 22:14:00 2013-01-28 22:14:00
2013-01-28 22:14:52.896175 2013-01-28 22:15:00
2013-01-28 22:15:45.792349 2013-01-28 22:16:00
这对我来说看起来很有希望。
因此,十六进制整数显然是小端序。要转换为整数:
880336163 > 2013-01-28 21:52:XX
880337810 > 2013-01-28 22:14:XX
880337876 > 2013-01-28 22:15:XX
880337942 > 2013-01-28 22:16:XX
考虑到最后三个之间的增量,该值以秒为单位。
使用提供的日期并回溯相应的秒数,您将获得以下纪元日期:
1985-03-07 20:02:37
1985-03-07 19:57:10
1985-03-07 19:57:04
1985-03-07 19:56:58
即使考虑到时间偏差和采样误差,这个纪元看起来也很奇怪。GPS时代是1980年1月6日,我想...您将需要更多样本来确定准确的纪元。
无论如何,这是我用来推导纪元的脚本:
import datetime as dt
data = [
('23 DD 78 34',(2013,01,28,21,52)),
('92 e3 78 34',(2013,01,28,22,14)),
('d4 e3 78 34',(2013,01,28,22,15)),
('16 e4 78 34',(2013,01,28,22,16)),
]
def hex_to_int(string):
string = ''.join(reversed(string.split()))
return int(string,16)
for (string,date) in data:
secs = hex_to_int(string)
date = dt.datetime(*date)
delta = dt.timedelta(seconds=secs)
print date - delta
获得纪元后,将十六进制字符串转换为上述整数,并将其添加到纪元以获取相应的日期时间:
import datetime as dt
epoch = dt.datetime(YYYY,MM,DD,HH,MM,SS)
def hex_to_datetime(string):
delta = dt.timedelta(seconds=hex_to_int(string))
return epoch + delta
希望这有帮助。