Reputation: 61
I want to convert epoch time into human readable format, when I am giving random epoch time for example the below one
epoch1 = datetime.datetime.fromtimestamp(1347517370).strftime('%c')
print(epoch1)
then this is working fine. But, If i am using my epoch time which i am retrieving through an API. this doesn't work and gives me error
epoch1 = datetime.datetime.fromtimestamp(1563924640001).strftime('%c')
print(epoch1)
what is the issue with this? even if I am passing the variable it doesn't work.
epoch1 = datetime.datetime.fromtimestamp(epoch).strftime('%c')
I want to get all the epoch time from the API and convert it in human readable format.
Any help highly appreciated
error:
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
Upvotes: 1
Views: 1955
Reputation: 11695
Try like below
>>> import datetime
>>> datetime.datetime.fromtimestamp(1347517370).strftime('%Y-%m-%d %H:%M:%S')
'2012-09-13 14:22:50' # Local time
In your case 1563924640001
to 156392464000
epoch1 = datetime.datetime.fromtimestamp(156392464000).strftime('%c')
print(epoch1)
# output: Sun Nov 18 04:36:40 6925
Upvotes: 2
Reputation: 169267
You're getting millisecond epoch time from that API.
Divide by 1000 to get seconds which fromtimestamp
accepts:
>>> datetime.datetime.fromtimestamp(1563924640001 / 1000).strftime('%c')
'Wed Jul 24 02:30:40 2019'
Upvotes: 4