문제

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