For many values, for example, too far in the past or future, just giving a timestamp on fromtimestamp()
will complain of an error out of range. However, you can calculate the date using timedelta()
relative to an era.
>>> from datetime import datetime, timedelta >>> date = datetime(1970, 1, 1) + timedelta(seconds=-216345600) >>> date datetime.datetime(1963, 2, 23, 0, 0) >>> date.strftime('%a, %d %b %Y %H:%M:%S GMT') 'Sat, 23 Feb 1963 00:00:00 GMT'
However, note that you cannot use this to go back to the dinosaur era, as datetime()
still has the minimum and maximum value that it can support.
>>> datetime(1970, 1, 1) + timedelta(seconds=-62135596800) datetime.datetime(1, 1, 1, 0, 0) >>> datetime(1970, 1, 1) + timedelta(seconds=253402300799) datetime.datetime(9999, 12, 31, 23, 59, 59) >>> datetime(1970, 1, 1) + timedelta(seconds=253402300800) Traceback (most recent call last): File "<pyshell#157>", line 1, in <module> datetime(1970, 1, 1) + timedelta(seconds=253402300800) OverflowError: date value out of range
timedelta()
also has its limits, but with the era as a control point, we have not even reached approximation to them.
>>> timedelta(microseconds=1000000000*86400*10000-1) datetime.timedelta(9999999, 86399, 999999)