python - 无法将 Epoch 值转换为人类可读的日期格式 python

标签 python python-3.x

我正在尝试使用 Python 版本 3 转换纪元值 -2208988800000

我试过在线工具来转换https://www.epochconverter.com/所以我得到的输出是 1/Jan/1900。

import datetime
datetime.datetime.fromtimestamp(-2208988800000 / 1000).strftime('%c')

---------------------------------------------------------------------------
OSError
Traceback (most recent call last)
  <ipython-input-15-abaf34dc5003> in <module>()
----> 1 datetime.datetime.fromtimestamp(-2208988800000 / 1000).strftime('%c')

OSError: [Errno 22] Invalid argument

最佳答案

fromtimestamp 行为取决于平台。我使用的是 Windows 10,不支持负值。

摘自 the documentation :

fromtimestamp() may raise ValueError, if the timestamp is out of the range of values supported by the platform C localtime() or gmtime() functions. It’s common for this to be restricted to years in 1970 through 2038.

>>> from datetime import datetime as dt
>>> dt.fromtimestamp(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument

关于python - 无法将 Epoch 值转换为人类可读的日期格式 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57728111/

相关文章:

python - 转义正则表达式字符串

python - Django : RunTimeWarning : DateTimeField received a naive datetime while time zone support is active

python - 具体情况不会通过第 8 行 elif :

python-3.x - 如何在 ipywidget out 小部件中显示 matplotlib 饼图?我正在使用 Jupyter Notebook,目前没有任何显示

python - 须藤 : pip: command not found in CentOS

python - 使用 HTTPS 的 charles 代理检查 Python 请求

python - 值错误: Sample larger than population or is negative

php - Python 相当于 PHP 的 compact() 和 extract()

python - 如何为 Python 的 'input' 函数生成键盘输入?

python - 在我的 scrapy 蜘蛛中,我的解析模块无法工作,无法打印