datetime值小于datetime.min的含义是什么?

时间:2012-11-01 22:27:26

标签: python datetime timedelta

这里发生了什么?

预期:

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

意外的:

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

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

在python中,当你从datetime.min中减去这些值意味着什么?他们代表什么日期?为什么有些情况不会触发OverflowError?

1 个答案:

答案 0 :(得分:2)

因为您需要升级到修复此错误的Python 2.6或更高版本。

$ 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

您是否需要有人追踪错误编号,补丁和python-dev讨论,或者是否有足够的信息供您使用?