relativedelta doesn't change year when adding or subtracting months
Bug #1406004 reported by
David Lehrian
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.
2014-12-27 20:47:22.
print(timezone.
2014-01-27 20:47:46.
print(timezone.
2014-12-27 20:48:14.
To post a comment you must log in.
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)+relativedel ta(months= 1))
2015-01-25 12:15:30
behaves as I would expect.
>>> print(datetime( 2014,12, 25,12,15, 30)+relativedel ta(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)+relativedel ta(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.