Timestamp out of range for platform localtime()/gmtime() function

16,700

Solution 1

For many values, like too far in the past or the future, just feeding the timestamp to fromtimestamp() will complain with an out of range error. However, you can calculate the date using timedelta() relative from the epoch.

>>> 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, do note that you can't use this to go back to the dinosaur era, since datetime() still has a min and max value 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() has its limits as well, but with the epoch as a reference point, we haven't come even near reaching them.

>>> timedelta(microseconds=1000000000*86400*10000-1)
datetime.timedelta(9999999, 86399, 999999)

Solution 2

Maybe slightly less related to the problem from the question but may be applicable to those who want to represent full date-time range without any very specific workarounds to limitations of the default datetime implementation.

I have checked a few libraries and I suggest using:

dateparser - for parsing date/time stated naturally for a human being and in multitude of languages.

arrow - drop-in replacement for datetime without its limitations (e.g. possibility to represent dates before and close to the epoch of 1. year AD).

Share:
16,700
user7172
Author by

user7172

Updated on June 15, 2022

Comments

  • user7172
    user7172 almost 2 years

    I try:

    ts = -216345600000
    datetime.datetime.fromtimestamp(ts/1000)
    

    ValueError: timestamp out of range for platform localtime()/gmtime() function

    I check on epochconverter value : -216345600 its return GMT: Sat, 23 Feb 1963 00:00:00 GMT

    How to get the correct result?

  • D. O.
    D. O. about 4 years
    I don't understand why dividing by 1000.
  • D. O.
    D. O. about 4 years
    Thank you for the arrow module!
  • D. O.
    D. O. about 4 years
    I am in Montreal. I used utcnow() function, it give me 16:20. But now it is 12:20 in Montreal. Why this difference?
  • sophros
    sophros about 4 years
    UTC = Universal Time Coordinated or GMT. Montreal's timezone is (GMT-4) hence the readings. GMT is for London.
  • D. O.
    D. O. about 4 years
    Understand now! Thank you! I use arrow.now() function instead.
  • Reti43
    Reti43 about 4 years
    @D.O. I don't divide anything by 1000. Maybe you're referring to what the OP did? In that case he was converting milliseconds to seconds.