fromtimestamp() 的反义词是什么?

What is the opposite of fromtimestamp()?

我正在尝试弄清楚如何将二进制文件导入 python,但需要大量的猜测和反复试验。 在这种情况下,我想做的是找到特定日期在字节流中出现的位置,即:

import re
import datetime
import binascii
import time

with open('inputfile.nda', 'rb') as f:
    data = f.read()

# This line needs fixing
i = int.oppositeof_fromtimestamp(2019, 10, 10, 14, 53, 57.000) # '2019/10/10 14:53:57.000' 


time_stamp = i.to_bytes(4, byteorder='little')

df = [m.start() for m in re.finditer(time_stamp, data)]

print(df)

是否有一个函数(或一组函数)可以将日期转换为字节流?

您正在寻找 datetime.timestamp():

import datetime

ts = datetime.datetime(2019, 10, 10, 14, 53, 57)
unix_time = int(ts.timestamp())
unix_time_bytes = unix_time.to_bytes(4, byteorder='little')
print(ts, unix_time, unix_time_bytes)

产出

2019-10-10 14:53:57 1570708437 b'\xd5\x1b\x9f]'

那么您可能希望使用 data.index(unix_time_bytes) 来查找字符串,而不是正则表达式。