user1972227
user1972227

Reputation:

Python : hex to date time

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 

how to convert hex to date time?it is not UNIX Date

Upvotes: 4

Views: 8549

Answers (2)

isedev
isedev

Reputation: 19631

So, the hex integers are obviously little-endian. To converting to integers:

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

And considering the delta between the last three, the value is in seconds.

Using the date provided and working back the corresponding numbers of seconds, you get the following epoch dates:

1985-03-07 20:02:37
1985-03-07 19:57:10
1985-03-07 19:57:04
1985-03-07 19:56:58

Even allowing for time skewing and sampling errors, the epoch looks odd. GPS epoch is January 6, 1980 I think... You'll need more samples to determine an accurate epoch.

Anyway, here's the script I used to derive the epochs:

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

Once you have the epoch, convert the hex string to an integer as above and add it to the epoch to get the corresponding date time:

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

Hope this helps.

Upvotes: 1

bikeshedder
bikeshedder

Reputation: 7487

The hex value looks like it is little endian encoded, but it does not seam to use seconds as tick values:

>>> 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

Since the offset is shrinking over time I calculated a correction factor:

>>> 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 seconds.

Now if we apply this factor to the timestamps the offset stays pretty much constant (except for the seconds but since you didn't know their value I had just used 0 in the source data)

>>> 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

Taking this into account you can use this offset and factor to convert the original values:

>>> 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

This looks quite promising to me.

Upvotes: 1

Related Questions