How to get dates from these numbers

Viewed 257

I have problems to convert some timestamps to readable dates. Unfortunately I don't know how they are encoded. I was just told, that these dates were created with delphi as a System.TDate object.
I know which date they represent, but I can not figure out how to convert them.

What I have -> date they represent
131006721 -> 1999-01-01
126550273 -> 1931-01-01
130875649 -> 1997-01-01
2 Answers

With provided dates, The representation seems like,

(year * (2^16)) + (month * (2^8)) + (day * (2^0))

For example, if we take first one,

(1999 * (2^16)) + (1 * (2^8)) + (1 * (2^0)) = 131006721

Hence you can revert this like below to get corresponding dates from number representations.

reps=[131006721,126550273,130875649]
for a in reps:
    print(a//(2**16),(a%(2**16))//(2**8),((a%(2**16))%(2**8))//(2**0))

Output

1999 1 1
1931 1 1
1997 1 1

as @BrakNicku commented, most likely you have 2 bytes representing the year and 1 byte each representing day / month:

import struct

ints = (131006721, 126550273, 130875649)

for i in ints:
    # to bytes
    b = struct.pack('>i', i) 
    # bytes 1 & 2 to year (as 16 bit int), byte 3 to month, byte 4 to day
    year, month, day = struct.unpack('>h', b[:2])[0], b[2], b[3]
    print(i, '->', year, month, day)
    
# 131006721 -> 1999 1 1
# 126550273 -> 1931 1 1
# 130875649 -> 1997 1 1
Related