Timestamp out of range for platform localtime()/gmtime() function(平台 localtime()/gmtime() 函数的时间戳超出范围)
问题描述
我试试:
ts = -216345600000
datetime.datetime.fromtimestamp(ts/1000)
ValueError:平台 localtime()/gmtime() 函数的时间戳超出范围
我检查 epochconverter 值:-216345600 其返回 GMT:星期六,1963 年 2 月 23 日 00:00:格林威治标准时间 00
I check on epochconverter value : -216345600 its return GMT: Sat, 23 Feb 1963 00:00:00 GMT
如何得到正确的结果?
推荐答案
对于许多值,比如过去或未来太远,只需将时间戳提供给 fromtimestamp()
就会报错超出范围错误.但是,您可以使用 timedelta()
相对于时代.
For many values, like too far in the past or the future, just feeding the timestamp to fromtimestamp()
will complain with an out of range error. However, you can calculate the date using timedelta()
relative from the epoch.
>>> from datetime import datetime, timedelta
>>> date = datetime(1970, 1, 1) + timedelta(seconds=-216345600)
>>> date
datetime.datetime(1963, 2, 23, 0, 0)
>>> date.strftime('%a, %d %b %Y %H:%M:%S GMT')
'Sat, 23 Feb 1963 00:00:00 GMT'
但是,请注意,您不能使用它来回到恐龙时代,因为 datetime()
仍然有它可以支持的最小值和最大值.
However, do note that you can't use this to go back to the dinosaur era, since datetime()
still has a min and max value it can support.
>>> datetime(1970, 1, 1) + timedelta(seconds=-62135596800)
datetime.datetime(1, 1, 1, 0, 0)
>>> datetime(1970, 1, 1) + timedelta(seconds=253402300799)
datetime.datetime(9999, 12, 31, 23, 59, 59)
>>> datetime(1970, 1, 1) + timedelta(seconds=253402300800)
Traceback (most recent call last):
File "<pyshell#157>", line 1, in <module>
datetime(1970, 1, 1) + timedelta(seconds=253402300800)
OverflowError: date value out of range
timedelta()
也有其局限性,但以时代为参考点,我们甚至还没有达到.
timedelta()
has its limits as well, but with the epoch as a reference point, we haven't come even near reaching them.
>>> timedelta(microseconds=1000000000*86400*10000-1)
datetime.timedelta(9999999, 86399, 999999)
这篇关于平台 localtime()/gmtime() 函数的时间戳超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:平台 localtime()/gmtime() 函数的时间戳超出范围


基础教程推荐
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 筛选NumPy数组 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01