سؤال

I want to edit an existing rrule that was parsed from a string to set an UNTIL date. How can I do that? Theoretically, I could modify the rule string and re-parse it, but then it gets complicated. I want to make it simple: Whatever the rule says about how many occurrences or until what date it goes to, I want to override it with a new UNTIL date.

Thanks.

هل كانت مفيدة؟

المحلول

I am not aware of public interface for this, but if you really need to then setting _until property directly seems to work. I should warn you that it is a bad practice to use it and this code could be broken by future releases of dateutil.

>>> r = rrule(DAILY,dtstart=datetime(2013,7,15,0,0,0), until=datetime.now())
>>> list(r)
[datetime.datetime(2013, 7, 15, 0, 0),
 datetime.datetime(2013, 7, 16, 0, 0),
 datetime.datetime(2013, 7, 17, 0, 0),
 datetime.datetime(2013, 7, 18, 0, 0),
 datetime.datetime(2013, 7, 19, 0, 0),
 datetime.datetime(2013, 7, 20, 0, 0),
 datetime.datetime(2013, 7, 21, 0, 0),
 datetime.datetime(2013, 7, 22, 0, 0),
 datetime.datetime(2013, 7, 23, 0, 0)]

>>> r._until = datetime(2013, 7, 20, 0, 0)
>>> list(r)
[datetime.datetime(2013, 7, 15, 0, 0),
 datetime.datetime(2013, 7, 16, 0, 0),
 datetime.datetime(2013, 7, 17, 0, 0),
 datetime.datetime(2013, 7, 18, 0, 0),
 datetime.datetime(2013, 7, 19, 0, 0),
 datetime.datetime(2013, 7, 20, 0, 0)]

نصائح أخرى

As of 2019, I'd prefer the rrule.replace() method, e.g.:

r2 = r1.replace(until=datetime(2013, 7, 20, 0, 0))

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top