Python: six to six days

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? This is not a UNIX Date

+4
source share
2 answers

The hexadecimal value looks like it is slightly endian encoded, but does not use seams to use seconds as check 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 

As the bias decreases over time, I calculated the 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 coefficient to timestamps, the bias remains pretty constant (with the exception of seconds, but since you did not know their value, which I used only 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 

Given this, you can use this offset and coefficient 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 pretty promising to me.

+1
source

So, hexadecimal integers are obviously little-endian. 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 given the delta between the last three, the value is in seconds.

Using the provided date and having worked the appropriate number of seconds, you will receive the following dates of the era:

 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 taking into account time bias errors and sampling errors, the era looks strange. GPS era - January 6, 1980. I think ... You will need more samples to determine the exact era.

Anyway, here is the script I used to get the eras:

 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 an epoch, convert the hexadecimal string to an integer as above, and add it to the epoch to get the corresponding time of the date:

 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.

+1
source

All Articles