将 Epoch DateTime 转换为 Python 中的字节数组

Converting Epoch DateTime to Byte Array in Python

我正在尝试将 python 中的纪元日期时间转换为字节数组 但它是 10 字节,它应该是 4 字节。

from time import time
curTime = int(time.time())
b = bytearray(str(curTime))
len(b)                 #comming as 10

任何人都可以帮助我的错误

您正在转换时间戳的字符串表示形式,而不是整数。

你需要的是这个功能:

struct.pack_into(fmt, 缓冲区, 偏移量, v1, v2, ...) 它记录在靠近顶部的 http://docs.python.org/library/struct.html 中。

import struct
from time import time
curTime = int(time())
b = struct.pack(">i", curTime)
len(b)    # 4

从这里偷来的: