Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rrule bug or wrong use case? #405

Closed
drunkard opened this issue Jun 30, 2017 · 1 comment
Closed

rrule bug or wrong use case? #405

drunkard opened this issue Jun 30, 2017 · 1 comment

Comments

@drunkard
Copy link

drunkard commented Jun 30, 2017

I'm using dateutil.rrule to count amount of month between two dates, but I got some wrong results, this makes me doute about whether I'm using dateutil.rrule in right case, just see my test code:

In [48]: import datetime
    ...: from dateutil.rrule import rrule, MONTHLY
    ...: strt_dt, end_dt = datetime.date(2017,1,1), datetime.date(2017,12,31)
    ...: day1 = datetime.timedelta(days=1)
    ...: while strt_dt < datetime.date(2019,1,1):
    ...:     strt_dt += day1
    ...:     end_dt += day1
    ...:     mc = rrule(MONTHLY, dtstart=strt_dt, until=end_dt).count()
    ...:     if mc != 12:
    ...:         print('date range is:', strt_dt, '->', end_dt, end='')
    ...:         print('\tmonth count is', mc)
    ...:
date range is: 2017-01-29 - 2018-01-28  month count is 11
date range is: 2017-01-30 - 2018-01-29  month count is 11
date range is: 2017-01-31 - 2018-01-30  month count is 7
date range is: 2017-03-29 - 2018-03-28  month count is 11
date range is: 2017-03-30 - 2018-03-29  month count is 11
date range is: 2017-03-31 - 2018-03-30  month count is 7
date range is: 2017-04-29 - 2018-04-28  month count is 11
date range is: 2017-04-30 - 2018-04-29  month count is 11
date range is: 2017-05-29 - 2018-05-28  month count is 11
date range is: 2017-05-30 - 2018-05-29  month count is 11
date range is: 2017-05-31 - 2018-05-30  month count is 7
[snipped]

Obviously, month count should be 12, but it won't work in some cases. Is it a bug? or wrong use case?

@drunkard drunkard changed the title rrule use case rrule bug or wrong use case? Jun 30, 2017
@pganssle
Copy link
Member

@drunkard This is most definitely the wrong way to get this particular piece of information. The reason sometimes you get 11 and sometimes you get 12 and sometimes you get 7 is that a MONTHLY rrule that starts on the 31st does not fall back to the end of the month, so if it starts Jan 31, the next matching rule is Mar 31, then May 31, etc. (see #149)

I'm not exactly sure what matters to you about the number of months between the two dates, but I think relativedelta is probably a closer fit:

import datetime
from dateutil.rrule import rrule, MONTHLY
strt_dt, end_dt = datetime.date(2017,1,1), datetime.date(2017,12,31)
day1 = datetime.timedelta(days=1)
from dateutil.relativedelta import relativedelta

while strt_dt < datetime.date(2019,1,1):
    strt_dt += day1
    end_dt += day1

    rd = relativedelta(end_dt, strt_dt)
    mc = rd.years * 12 + rd.months
    print('date range is:', strt_dt, '->', end_dt, end='')
    print('\tmonth count is', mc)

Note that this returns 11 months every time, because the way you've set it up it's usually 11 months and 27-30 days, depending on the months. Presumably you can use the richer relativedelta object to define whatever rounding scheme you want.

That said, if you really want to do it the way you are doing it, I think you'll be able to do so after #285 is implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants