Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of datetime values less than datetime.min?

What is happening here?

This expected:

>>> datetime.min - timedelta(days=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: date value out of range

Unexpected:

>>> datetime.min - timedelta(days=2)
datetime.datetime(1, 0, 255, 0, 0)

>>> datetime.min > (datetime.min - timedelta(days=2))
True

In python, what do these values mean when you subtract from datetime.min? What dates do they represent? Why do some cases not trigger an OverflowError?

like image 543
poundifdef Avatar asked Nov 04 '22 12:11

poundifdef


1 Answers

Because you need to upgrade to Python 2.6 or later, which fixed this bug.

$ python2.5 -c 'import datetime; print(datetime.datetime.min - datetime.timedelta(days=2))'
0001-00-255 00:00:00
$ python2.6 -c 'import datetime; print(datetime.datetime.min - datetime.timedelta(days=2))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
OverflowError: date value out of range
$ python2.7 -c 'import datetime; print(datetime.datetime.min - datetime.timedelta(days=2))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
OverflowError: date value out of range
$ python3.3 -c 'import datetime; print(datetime.datetime.min - datetime.timedelta(days=2))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
OverflowError: date value out of range

Do you need someone to track down the bug number, patch, and python-dev discussion, or is that enough information for you?

like image 119
abarnert Avatar answered Nov 08 '22 11:11

abarnert