sakurashinken sakurashinken - 1 year ago 121
Python Question

How to get rrule.between() to only include start timestamp

How would I create an rrule that excludes only the end timestamp using dateutil? Would I have to create a custom function or is there a way to do it natively?

here is an example

rule = rrule.rrule(rule.HOURLY,tsstart=somedate,until=somedate_four_hours_later)

I want the output to EXCLUDE
and only generate 4 timestamps, somedate, somedate+1 hour, etc.

Answer Source

One way to do this is to use an rruleset, which allows you to combine recurrence rules and specific dates as required. In this case, what you'd do is set the until date as an exdate (excluded date):

from dateutil import rrule
from datetime import datetime, timedelta

dtstart = datetime(2015, 1, 3, 12)
dtuntil = datetime(2015, 1, 3, 16)

rr = rrule.rrule(freq=rrule.HOURLY, dtstart=dtstart, until=dtuntil)

# Add your rrule to the ruleset, then exclude the until date from the rule set
rrset = rrule.rruleset()

l1 = list(rrset)

l2 = list(rrset)

print(l1[-1])   # 2015-01-03 16:00:00
print(l2[-1])   # 2015-01-03 15:00:00

The rrule itself will include the until date, but the exdate will exclude it from the rruleset.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download