python - dateutils rrule returns dates that 2 months apart -


i new python , dateutil module. passing following arguments:

disclosure_start_date = resultsdict['fd_disclosure_start_date'] disclosure_end_date = datetime.datetime.now() disclosure_dates = [dt dt in rrule(monthly, dtstart=disclosure_start_date, until=disclosure_end_date)] 

here disclosure_start_date = 2012-10-31 00:00:00 converted datetime datetime.datetime(2012, 10, 31, 0, 0)

end date of now.

when use:

disclosure_dates = [dt dt in rrule(monthly, dtstart=disclosure_start_date, until=disclosure_end_date)] 

i dates every other month or 2 months apart. result is:

>>> list(disclosure_dates) [datetime.datetime(2012, 10, 31, 0, 0),   datetime.datetime(2012, 12, 31, 0, 0),   datetime.datetime(2013, 1, 31, 0, 0),   datetime.datetime(2013, 3, 31, 0, 0),   datetime.datetime(2013, 5, 31, 0, 0),   datetime.datetime(2013, 7, 31, 0, 0),   datetime.datetime(2013, 8, 31, 0, 0),   datetime.datetime(2013, 10, 31, 0, 0),   datetime.datetime(2013, 12, 31, 0, 0),   datetime.datetime(2014, 1, 31, 0, 0),   datetime.datetime(2014, 3, 31, 0, 0),   datetime.datetime(2014, 5, 31, 0, 0),   datetime.datetime(2014, 7, 31, 0, 0),   datetime.datetime(2014, 8, 31, 0, 0),   datetime.datetime(2014, 10, 31, 0, 0),   datetime.datetime(2014, 12, 31, 0, 0),   datetime.datetime(2015, 1, 31, 0, 0),   datetime.datetime(2015, 3, 31, 0, 0),   datetime.datetime(2015, 5, 31, 0, 0),   datetime.datetime(2015, 7, 31, 0, 0),   datetime.datetime(2015, 8, 31, 0, 0),   datetime.datetime(2015, 10, 31, 0, 0),   datetime.datetime(2015, 12, 31, 0, 0),   datetime.datetime(2016, 1, 31, 0, 0),   datetime.datetime(2016, 3, 31, 0, 0),   datetime.datetime(2016, 5, 31, 0, 0)] 

i not sure doing wrong. can please point out mistake here?

the issue coming against comes fact datetime.datetime(2012, 10, 31, 0, 0) 31st of month, , not months have 31st. since rrule module implementation of rfc 2445. per rfc 3.3.10:

recurrence rules may generate recurrence instances invalid date (e.g., february 30) or nonexistent local time (e.g., 1:30 on day local time moved forward hour @ 1:00 am). such recurrence instances must ignored , must not counted part of recurrence set.

since have monthly rule generates 31st of month, skip months 30 or fewer days. can see this bug report in dateutil issue.

if want last day of month, should use bymonthday=-1 argument:

from dateutil.rrule import rrule, monthly datetime import datetime  disclosure_start_date = datetime(2012, 10, 31, 0, 0)  rr = rrule(freq=monthly, dtstart=disclosure_start_date, bymonthday=-1) # >>>rr.between(datetime(2013, 1, 1), datetime(2013, 5, 1)) # [datetime.datetime(2013, 1, 31, 0, 0), #  datetime.datetime(2013, 2, 28, 0, 0), #  datetime.datetime(2013, 3, 31, 0, 0), #  datetime.datetime(2013, 4, 30, 0, 0)] 

unfortunately, don't think there's rfc-compliant way generate simple rrule falls end of month if-and-only-if it's necessary (e.g. do january 30th - need fallback february, don't want use bymonthday=-2 because give feb. 27th, etc).

alternatively, simple monthly rule this, better option use relativedelta, does fall end of month:

from dateutil.relativedelta import relativedelta datetime import datetime  def disclosure_dates(dtstart, rd, dtend=none):     ii = 0     while true:         cdate = dtstart + ii*rd         ii += 1          yield cdate         if dtend not none , cdate >= dtend:             break   dtstart = datetime(2013, 1, 31, 0, 0) rd = relativedelta(months=1) rr = disclosure_dates(dtstart, rd, dtend=datetime(2013, 5, 1))  # >>> list(rr) # [datetime.datetime(2013, 1, 31, 0, 0), #  datetime.datetime(2013, 2, 28, 0, 0), #  datetime.datetime(2013, 3, 31, 0, 0), #  datetime.datetime(2013, 4, 30, 0, 0), #  datetime.datetime(2013, 5, 31, 0, 0)] 

note used cdate = dtstart + ii * rd, not want keep "running tally", pin shortest month tally has seen:

dt_base = datetime(2013, 1, 31) dt = dt_base ii in range(5):     cdt = dt_base + ii*rd     print('{} | {}'.format(dt, cdt))     dt += rd 

result:

2013-01-31 00:00:00 | 2013-01-31 00:00:00 2013-02-28 00:00:00 | 2013-02-28 00:00:00 2013-03-28 00:00:00 | 2013-03-31 00:00:00 2013-04-28 00:00:00 | 2013-04-30 00:00:00 2013-05-28 00:00:00 | 2013-05-31 00:00:00 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -