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 Date
答案 0 :(得分:1)
十六进制值看起来像是小端编码,但它不会使用秒作为刻度值:
>>> 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 ticks = 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
这看起来很有希望。
答案 1 :(得分:1)
所以,十六进制整数显然是小端的。要转换为整数:
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
希望这有帮助。