首页 > 解决方案 > 有没有办法计算秒的 32 位小数?

问题描述

我正在尝试获取 64 位(8 字节)的 python 十六进制时间戳(当前时间/一天中的时间)。

new0 = hex(int(time.time()))[2:]
new0
'60cc8697'   <--- (only 32 bits of MSB bits)

预期格式(秒的分数):(32 位 LSB 位)60cc839600000000

试过:使用python版本:python3.8

new0 = hex(long(time.time())

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'long' is not defined

标签: pythonpython-3.x

解决方案


您不需要使用字符串,只需一个位掩码。

>>> hex(int(time.time()) & 0xffffffff000000000)
'0x0'

但是,2038 年之前的时间戳不会设置任何高 32 位。也许您想要高 48 位(即实际使用的 32 位的高 16 位):

>>> hex(int(time.time()) & 0xffffffffffff0000)
'0x60cc0000'

或者(出于某种原因)您可能只想用 32 位 0 填充时间戳。你可以用左移来做到这一点。

>>> hex(int(time.time()) << 32)
'0x60cc8e4200000000'

推荐阅读