PatternMatching
PatternMatching

Reputation: 466

Inconsistent behavior of python-dateutil's relativedelta

Perhaps I don't understand the intent behind relativedelta, but the inconsistency in behavior whereby smaller subintervals are collapsed into larger ones so that the smallest set of subintervals are represented seems undesirable. Specifically, months seem to collapse into years, but days and weeks remain ambiguous (i.e. days = # of weeks * 7 + remainder of days).

from dateutil.parsers import parse as dparse
from dateutil.relativedelta import relativedelta as rdelta

start = dparse('12/3/15')
end = dparse('1/28/17')

rd = rdelta(end, start)

Here rd.years = 1, rd.months = 1, rd.weeks = 3, and rd.days = 25.

Why is that? I would expect subintervals to be exclusive of each other.

Upvotes: 1

Views: 93

Answers (1)

warvariuc
warvariuc

Reputation: 59594

From the source code:

@property
def weeks(self):
    return self.days // 7
@weeks.setter
def weeks(self, value):
    self.days = self.days - (self.weeks * 7) + value * 7

So weeks is a convenience method to represent days as weeks.

Upvotes: 3

Related Questions