relativedelta doesn't change year when adding or subtracting months

Bug #1406004 reported by David Lehrian
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
dateutil
Invalid
Undecided
Unassigned

Bug Description

Adding one month to a date in December doesn't change the date to the same date in January the *following* year, but changes it to January of the *same* year. The same bug appears when subtracting 12 months from a date; I end up with the same date, today. I'm using python 3.4 and downloaded dateutil a few days ago via pip install python-dateutil.

print(timezone.now())
2014-12-27 20:47:22.730468+00:000
print(timezone.now()+relativedelta(month=1))
2014-01-27 20:47:46.242187+00:00
print(timezone.now()-relativedelta(month=12))
2014-12-27 20:48:14.533203+00:00

Revision history for this message
David Lehrian (f-david-0) wrote :

After stepping through the code I see there are 2 options when initializing relativedelta, there is month and there is months.

        >>> print(datetime(2014,12,25,12,15,30)+relativedelta(months=1))
        2015-01-25 12:15:30

behaves as I would expect.

        >>> print(datetime(2014,12,25,12,15,30)+relativedelta(month=1))
       2014-01-25 12:15:30

fails to change the year. But on further debugging I see what it is doing. It isn't adding the month at all, it is replacing the month of the existing date with the month of the relativedelta. It only seemed like it was a bug because it is December. It wasn't adding 1 to 12 to get 1, it was simply changing the 12 to a one like it will change the 5 to a 1 in the following example.

        >>> print(datetime(2014,5,25,12,15,30)+relativedelta(month=1))
       2014-01-25 12:15:30

So this is not a bug, but rather is designed this way, I just didn't understand.

Revision history for this message
jarondl (jarondl) wrote :

OK. If you feel this should be better documented, open an issue / pull request on https://github.com/dateutil/dateutil

Changed in dateutil:
status: New → Invalid
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.