简体   繁体   中英

Unable to convert Epoch value to Human readable date format python

I am trying to convert Epoch value -2208988800000 using Python version 3.

I have tried online tool to convert https://www.epochconverter.com/ so there I am getting output as 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 behaviour is dependant on platform. I'm on Windows 10 and negative values are unsupported.

Exerpt from 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

I cannot reproduce your issue; I am using Python 3.6 on Ubuntu 18.04 and get this output (might be slightly different because of my timezone):

>>> import datetime
>>> datetime.datetime.fromtimestamp(-2208988800000 / 1000).strftime('%c')
'Sun Dec 31 20:09:20 1899'
>>> datetime.datetime.fromtimestamp(-2208988800000 / 1000)
datetime.datetime(1899, 12, 31, 20, 9, 20)

And using the converter you mentioned in the question, this seems correct:

截图


The fromtimestamp() docs say that any OSError comes from time.localtime() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM