classification
Title: datetime operations spanning MINYEAR give bad results
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: mark.leander, pythonhacker (2)
Priority: Keywords

Created on 2009-10-16 09:45 by mark.leander, last changed 2009-10-25 09:29 by pythonhacker.

Files
File name Uploaded Description Edit Remove
datetimemodule.c.svndiff pythonhacker, 2009-10-25 09:29
Messages (3)
msg94132 - (view) Author: (mark.leander) Date: 2009-10-16 09:45
The datetime module documentation would imply that operations that cause
dates to fall outside the MINYEAR--MAXYEAR range should raise
OverflowError. The interpreter session below shows that this is not
always the case, and that such operations may cause bogus and
inconsistent results.

Python 2.6.3 (r263rc1:75186, Oct  2 2009, 20:40:30) [MSC v.1500 32 bit
(Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> t0=datetime.datetime(1,1,1)
>>> d1, d2, d3 = map(datetime.timedelta, range(1,4))
# The following is expected and accoring to the docs:
>>> t0-d1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: date value out of range

# The following is completely bogus:
>>> t0-d2
datetime.datetime(1, 0, 255, 0, 0)

# The two following behaving differently may be very confusing,
# the second one is correct
>>> t0-d2+d3
datetime.datetime(1, 8, 15, 0, 0)
>>> t0+d3-d2
datetime.datetime(1, 1, 2, 0, 0)
>>>
msg94384 - (view) Author: Anand B Pillai (pythonhacker) Date: 2009-10-23 13:06
The issue is present in Python 3.0 and 2.5 as well.

Python 2.5.1 (r251:54863, Jul 17 2008, 13:21:31)
[GCC 4.3.1 20080708 (Red Hat 4.3.1-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> t0=datetime.datetime(1,1,1)
>>> d1,d2,d3=map(datetime.timedelta, range(1,4))
>>> t0-d1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: date value out of range
>>> t0-d2
datetime.datetime(1, 0, 255, 0, 0)

I think this is bug in datetime for all Python versions
msg94449 - (view) Author: Anand B Pillai (pythonhacker) Date: 2009-10-25 09:29
The problem seems to be in the "normalize_date" function in
datetimemodule.c. It is checking for a valid year range, but not
checking for a valid month or day range. 

I have a patch which fixes this problem. It checks for month range
(1<=m<=12) and day range(1<=d<=31). Here is Python with the patch.

anand@anand-laptop:~/projects/python/py3k$ ./python
Python 3.2a0 (py3k:75627, Oct 25 2009, 14:28:21) 
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Traceback (most recent call last):
  File "/home/anand/.pythonrc", line 2, in <module>
    import readline
ImportError: No module named readline
>>> import datetime
>>> t0=datetime.datetime(1,1,1)
>>> d1,d2,d3=map(datetime.timedelta, range(1,4))
>>> t0-d1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: date value out of range
>>> t0-d2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: date value out of range
>>> t0-d3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: date value out of range
>>> d0=datetime.timedelta(0)
>>> t0-d0
datetime.datetime(1, 1, 1, 0, 0)
>>> 

Svn diff is attached.
History
Date User Action Args
2009-10-25 09:29:51pythonhackersetfiles: + datetimemodule.c.svndiff

messages: + msg94449
2009-10-23 13:06:01pythonhackersetnosy: + pythonhacker
messages: + msg94384
2009-10-16 09:45:28mark.leandercreate