python - 在 Python 中将 Epoch DateTime 转换为字节数组

标签 python epoch

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

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

谁能帮我解决一下

最佳答案

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

你需要的是这个函数:

struct.pack_into(fmt, buffer, offset, 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

从这里偷来的: https://stackoverflow.com/a/7921876/2442434

关于python - 在 Python 中将 Epoch DateTime 转换为字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38183676/

相关文章:

Python 溢出错误 : cannot fit 'long' into an index=sized integer

python - 记住方法调用之间的状态

python - 如何使用多索引为现有系列赋值?

python - 如何从 NLTK 树中获取标记?

datetime - DB2 timestampdiff 函数返回意外结果

java - 如何更改 1970-01-01T00 :00:00Z for LocalDateTime 的默认纪元

c - Arduino UDPNTP Epoch,确定一天中的分钟

Python 异步调试示例

java - 为什么以 0 long 值作为参数的 Date 构造函数将在 Java 中输出 1970 年之前的日期?

python - 当设备的纪元时间不可靠时使用 Python 测量时间