/python/meetup/meetup.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import calendar
from datetime import date

day_names = list(calendar.day_name)


def is_teen(n):
    return 13 <= n <= 19


def meetup_day(year, month, weekday, nth):
    # Look up the day name
    weekday_num = day_names.index(weekday)

    # Find out information about the specified month
    first_weekday, days_in_month = calendar.monthrange(year, month)

    # Build a list of matching weekdays
    offset = (weekday_num - first_weekday) % 7
    weekdays = range(offset+1, days_in_month+1, 7)

    if nth == 'last':
        day_of_month = weekdays[-1]
    elif nth == 'teenth':
        day_of_month = filter(is_teen, weekdays)[0]
    else:
        # At this point, assume an ordinal. It'll only ever be 1-5
        day_of_month = weekdays[int(nth[0]) - 1]

    return date(year, month, day_of_month)

Comments


You're not logged in right now. Please login via GitHub to comment