Search code examples
pythonpython-3.xepoch

converting epoch time in human readable format


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

Solution

  • 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'