【发布时间】:2019-12-03 15:33:43
【问题描述】:
我想将纪元时间转换为人类可读的格式,当我给出随机纪元时间时,例如下面的一个
epoch1 = datetime.datetime.fromtimestamp(1347517370).strftime('%c')
print(epoch1)
那么这工作正常。 但是,如果我使用的是我通过 API 检索的纪元时间。这不起作用并给我错误
epoch1 = datetime.datetime.fromtimestamp(1563924640001).strftime('%c')
print(epoch1)
这有什么问题?即使我传递变量它也不起作用。
epoch1 = datetime.datetime.fromtimestamp(epoch).strftime('%c')
我想从 API 获取所有纪元时间并将其转换为人类可读的格式。
非常感谢任何帮助
错误:
Traceback (most recent call last):
File "C:/Users/kiran.tanweer/Documents/Python Scripts/siem/01_GetOffenses.py", line 1042, in <module>
main()
File "C:/Users/kiran.tanweer/Documents/Python Scripts/siem/01_GetOffenses.py", line 953, in main
epoch1 = datetime.datetime.fromtimestamp(1563924640001).strftime('%c')
OSError: [Errno 22] Invalid argument
【问题讨论】:
-
您的代码
epoch1 = datetime.datetime.fromtimestamp(1563924640001).strftime('%c')让我访问ValueError: year 51528 is out of range。可能是您的时间戳在ms而不是s?不管怎样,我不明白你为什么会收到OSError。 -
尝试将
1563924640001更改为156392464000。如果正确,则应排除最后一位 -
@AnjaneyuluBatta 除非他正在处理相对较远的预测(例如 6925 年?),否则我倾向于相信您的方法可能不是 OP 想要的。
标签: python python-3.x epoch