5

这里发生了什么?

这预期:

>>> 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?

4

1 回答 1

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 讨论,或者这些信息对您来说是否足够?

于 2012-11-01T23:04:33.530 回答