You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
The text was updated successfully, but these errors were encountered:
drunkard
changed the title
rrule use case
rrule bug or wrong use case?
Jun 30, 2017
@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 MONTHLYrrule 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:
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.
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:
Obviously, month count should be 12, but it won't work in some cases. Is it a bug? or wrong use case?
The text was updated successfully, but these errors were encountered: