Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Convert UnixTimeStamp (with Minus) to Date

How i can convert UnixTimeStamp like -266086800 (with Minus) to date

>>> datetime.fromtimestamp('-266086800').strftime("%A, %B %d, %Y %I:%M:%S")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required (got type str)

UPDATE

After fixing bug where timestamp was passed as a string

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

datetime.datetime.fromtimestamp(-266086800)

another error is presented (on Windows)

OSError: [Errno 22] Invalid argument

>Solution :

After changing the parameter from a string to an integer, success is platform dependent. fromtimestamp uses the C localtime function and from classmethod datetime.fromtimestamp(timestamp, tz=None):
It’s common for this to be restricted to years in 1970 through 2038.

Negative Unix timestamps mean time before Jan 1, 1970. You could get the time a timestamp 0 and then add a python timedelta object for the time. Now the calculation is done in python, removing that restriction.

>>> import datetime
>>> datetime.datetime.fromtimestamp(0) + datetime.timedelta(seconds=-266086800)
datetime.datetime(1961, 7, 27, 0, 0)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading