1

I have a date time in this format.

1999-12-31 09:00:00

Which came from the hex value:

F0C46C38

How do you make the datetime value of the above format into a 4 byte hex? The values I posted above are complements to each other. The hex in the second code block is reversed.

Thank you!

user1150764
  • 103
  • 1
  • 4
  • 10

2 Answers2

3

386CC4F0(hex) == 946652400(dec)
946652400 is Unix timestamp for 1999-12-31 15:00:00 GMT.

import time
print hex(int(time.mktime(time.strptime('1999-12-31 15:00:00', '%Y-%m-%d %H:%M:%S'))) - time.timezone)
manuskc
  • 784
  • 4
  • 14
  • `time.timezone` shouldn't be used here. `calendar.timegm()` should be used instead of `time.mktime()` if input time is in UTC. – jfs Mar 09 '13 at 03:03
1
#!/usr/bin/env python3
import binascii
import struct
from datetime import datetime

# convert time string into datetime object
dt = datetime.strptime('1999-12-31 09:00:00', '%Y-%m-%d %H:%M:%S')

# get seconds since Epoch
timestamp = dt.timestamp() # assume dt is a local time

# print the timestamp as 4 byte hex (little-endian order)
print(binascii.hexlify(struct.pack('<I', round(timestamp))))
# -> b'f0c46c38'
jfs
  • 399,953
  • 195
  • 994
  • 1,670