Created
February 27, 2015 15:18
-
-
Save pganssle/74a7b5dfae366211956e to your computer and use it in GitHub Desktop.
RRULE2 Profile: Daily BYMONTH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime, timedelta | |
try: | |
from itertools import islice, filterfalse | |
except ImportError: | |
# Python 2.x | |
from itertools import islice, ifilterfalse as filterfalse | |
def _daily(start_date=datetime.now(), interval=1): | |
td = timedelta(days=interval) | |
while True: | |
yield start_date | |
start_date += td | |
def _daily_skip(start_date=datetime.now(), interval=1, include_months=(1,)): | |
td = timedelta(days=interval) | |
include_months = sorted(include_months) | |
include_month = include_months[0] | |
month_index = 0 | |
while True: | |
if start_date.month in include_months: | |
yield start_date | |
start_date += td | |
else: | |
if start_date.month > include_months[-1]: | |
advance_date = start_date.replace(year=start_date.year + 1, | |
month=include_months[0], | |
day=1) | |
month_index = 0 | |
else: | |
for include_month in include_months[month_index:]: | |
month_index += 1 | |
if start_date.month < include_month: | |
advance_date = start_date.replace(month=include_month, | |
day=1) | |
break | |
advance_days = (advance_date - start_date).days | |
advance_days = -(-advance_days // interval) | |
start_date += timedelta(days=advance_days) | |
def test_filter(count=10000, start_date=datetime.now(), | |
interval=1, months=(1,)): | |
return list(islice(filterfalse(lambda x: x.month not in months, | |
_daily(start_date=start_date, | |
interval=interval)), | |
count)) | |
def test_skip(count=10000, start_date=datetime.now(), interval=1, months=(1,)): | |
return list(islice(_daily_skip(start_date=start_date, | |
interval=interval, include_months=months), | |
count)) | |
@profile | |
def run_tests(): | |
sd = datetime(1997, 1, 1) | |
test_filter(100, start_date=sd, months=(1,)) | |
test_skip(100, start_date=sd, months=(1,)) | |
test_filter(100, start_date=sd, months=(2, 4, 7)) | |
test_skip(100, start_date=sd, months=(2, 4, 7)) | |
test_filter(10000, start_date=sd, months=(1,)) | |
test_skip(10000, start_date=sd, months=(1,)) | |
test_filter(10000, start_date=sd, months=(2, 4, 7)) | |
test_skip(10000, start_date=sd, months=(2, 4, 7)) | |
if __name__ == "__main__": | |
run_tests() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment