Python OverflowError从32位系统中的时间戳创建日期 [英] Python OverflowError creating date from timestamp in 32bit system
问题描述
因此,我尝试使用内置的python 运行Debian Buster的RaspberryPi4中的datetime 模块.
So I am trying to do a simple conversion of a timestamp to date using python builtin datetime module in RaspberryPi4 running Debian Buster.
该转换在我的笔记本电脑(64位Debian)上运行良好,但在Debian中引发OverflowError.以下是两个示例.
The conversion works fine in my laptop (64bit Debian) but trows a OverflowError in Debian. The 2 examples follow.
有人知道这个问题有一个简单的解决方法吗?谢谢!
Anyone knows of a simple workaround this issue? Thank you!
在64位Debian系统中:
In 64bit Debian system:
$ python3 -c "from datetime import datetime; d=datetime.fromtimestamp(int("-11486707200")); print(d.year)"
1606
在RasbberryPi(32位)Raspbian系统中:
In RasbberryPi (32bit) Raspbian system:
$ python3 -c "from datetime import datetime; d=datetime.fromtimestamp(int("-11486707200")); print(d.year)"
Traceback (most recent call last):
File "<string>", line 1, in <module>
OverflowError: timestamp out of range for platform time_t
推荐答案
假设 -11486707200
是从纪元(Unix时间)起的秒数,您可以尝试将其添加为 timedelta
进入时代;
assuming -11486707200
is seconds since the epoch (Unix time), you could try to add it as a timedelta
to the epoch;
from datetime import datetime, timedelta, timezone
d = datetime.fromtimestamp(0, tz=timezone.utc) + timedelta(seconds=-11486707200)
print(d.year)
>>> 1606
当我在32位Python 3.7.9上进行测试时,此方法有效.注意,Raspberry Pi4 CPU具有64位架构,因此它不是"32位系统".从这个意义上讲.不确定这是否是Pi的问题,但我无法测试以确保.因此,如果仍然存在,请在此处.
This works when I test on Python 3.7.9 32 bit. Note that the Raspberry Pi4 CPU has a 64bit architecture, so it's not a "32bit system" in that sense. Not sure if this is an issue of the Pi but I can't test to make sure. So if it persists, maybe ask here.
这篇关于Python OverflowError从32位系统中的时间戳创建日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!