user1150764
user1150764

Reputation: 103

Getting a datetime in this format and converting to 4 byte hex

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!

Upvotes: 1

Views: 10484

Answers (2)

jfs
jfs

Reputation: 414585

#!/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'

Upvotes: 1

manuskc
manuskc

Reputation: 794

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)

Upvotes: 3

Related Questions