Python 以人类可读的格式转换历元时间

Python 以人类可读的格式转换历元时间,python,python-3.x,epoch,Python,Python 3.x,Epoch,我想把历元时间转换成人类可读的格式,当我给出随机历元时间时,例如下面的那个 epoch1 = datetime.datetime.fromtimestamp(1347517370).strftime('%c') print(epoch1) 那么这就行了。 但是,如果我使用的是通过API检索的纪元时间。这不起作用,给了我错误 epoch1 = datetime.datetime.fromtimestamp(1563924640001).strftime('%c') print(epoch1)

我想把历元时间转换成人类可读的格式,当我给出随机历元时间时,例如下面的那个

 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

从那个API中可以得到毫秒级的时间

除以1000得到fromtimestamp接受的秒数:

>>>datetime.datetime.fromtimestamp1563924640001/1000.strftime“%c” “2019年7月24日星期三02:30:40”
从那个API中可以得到毫秒级的时间

除以1000得到fromtimestamp接受的秒数:

>>>datetime.datetime.fromtimestamp1563924640001/1000.strftime“%c” “2019年7月24日星期三02:30:40” 试试下面这样

>>> import datetime
>>> datetime.datetime.fromtimestamp(1347517370).strftime('%Y-%m-%d %H:%M:%S')
'2012-09-13 14:22:50' # Local time
在您的情况下,1563924640001到156392464000

试试下面这样

>>> import datetime
>>> datetime.datetime.fromtimestamp(1347517370).strftime('%Y-%m-%d %H:%M:%S')
'2012-09-13 14:22:50' # Local time
在您的情况下,1563924640001到156392464000


您的代码epoch1=datetime.datetime.fromtimestamp1563924640001.strftime“%c”使我获得值错误:51528年超出范围。您的时间戳可能是毫秒而不是s?无论如何,我不明白你为什么会出错。试着将1563924640001更改为156392464000。如果它是正确的,那么你应该排除最后一个digit@AnjaneyuluBatta除非他处理的是相对较远的预测,例如6925年,否则我倾向于相信你的方法可能不是OP想要的。你的代码epoch1=datetime.datetime.fromTimestal1563924640001。strftime“%c”让我得到ValueError:51528年超出范围。您的时间戳可能是毫秒而不是s?无论如何,我不明白你为什么会出错。试着将1563924640001更改为156392464000。如果它是正确的,那么你应该排除最后一个digit@AnjaneyuluBatta除非他处理的是相对较远的预测,例如6925年?,否则我倾向于相信你的方法可能不是OP想要的。这是一个有效的假设,但为什么1563924640001会抛出错误,而不仅仅是给出遥远未来的日期?在我的系统中,抛出一个ValueError直到9999年。@norok2 1563924640001在51528年。我知道了,但为什么OSError?OP在Windows上;基本的日期API的工作原理有点不同。这是一个有效的假设,但是为什么1563924640001会抛出OSError,而不仅仅是给出一个遥远的未来的日期呢?在我的系统中,抛出一个ValueError直到9999年。@norok2 1563924640001在51528年。我知道了,但为什么OSError?OP在Windows上;基础日期API的工作方式略有不同。