Skip to content

Instantly share code, notes, and snippets.

@tebriel
Forked from pthrasher/fridays_of.py
Last active August 29, 2015 14:11
Show Gist options
  • Save tebriel/7748baf3d40bc202675f to your computer and use it in GitHub Desktop.
Save tebriel/7748baf3d40bc202675f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Find 5 friday (4) the 12th's
# python3 fridays_of.py 12 4 5
import datetime
import sys
def make_get_specified_date(start, day_of_month, day_of_week):
"""
>>> maker = make_get_specified_date(datetime.date.today(), 12, 4)
>>> next(maker)
datetime.date(2014, 12, 12)
"""
one_day = datetime.timedelta(days=1)
while True:
if start.day == day_of_month and start.weekday() == day_of_week:
yield start
start -= one_day
def find_dates():
today = datetime.date.today()
month_day = int(sys.argv[1])
week_day = int(sys.argv[2])
max_items = 5
if len(sys.argv) > 3:
max_items = int(sys.argv[3])
get_specified_date = make_get_specified_date(today, month_day, week_day)
for i in range(0, max_items):
print(next(get_specified_date))
if __name__ == '__main__':
if len(sys.argv) == 1:
import doctest
sys.exit(doctest.testmod()[0])
else:
find_dates()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment