“溢出错误:mktime 参数超出范围”在 python:3 docker 镜像上

发布于 2025-01-19 18:19:10 字数 772 浏览 2 评论 0原文

我正在使用time.mktime()以某种格式获得使用第三方API的日期。

我可以在Python上重现一个溢出错误:3座图像,我在Ubuntu Lts上本地运行时看不到这一点(20.04)

>>> import pytz
>>> import datetime
>>> import time
>>> time.mktime(pytz.timezone('Europe/London').localize(datetime.datetime(2022, 3, 29)).timetuple())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: mktime argument out of range

2022年3月27日以上的日期似乎在欧洲/伦敦时区似乎都在给我,但是我不要以为这是遇到任何32位整数限制的限制,因为这将在2038年预期。

首先转换为UTC TighZone可以通过一个简单的解决方法。

>>> time.mktime(pytz.timezone('Europe/London').localize(datetime.datetime(2022, 3, 29)).astimezone(pytz.UTC).timetuple())
1648508400.0

为什么要溢出?如果是一个错误,那么在哪里报告它是什么?

I'm using time.mktime() to get dates in a certain format to use a 3rd-party API.

I can reproduce an overflow error on the python:3 docker images, which I don't see when running locally on ubuntu LTS (20.04)

>>> import pytz
>>> import datetime
>>> import time
>>> time.mktime(pytz.timezone('Europe/London').localize(datetime.datetime(2022, 3, 29)).timetuple())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: mktime argument out of range

Any dates greater than 27 March 2022 seem to give this year for the Europe/London timezone, but I don't think this is hitting any 32-bit integer limit yet, as that would be expected in the year 2038.

There's a simple workaround by first converting to the UTC timezone.

>>> time.mktime(pytz.timezone('Europe/London').localize(datetime.datetime(2022, 3, 29)).astimezone(pytz.UTC).timetuple())
1648508400.0

Why does this overflow? If it's a bug, where's the best place to report it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文