# Path: happenings/utils/common.py
# def check_weekday(year, month, day, reverse=False):
#     """
#     Make sure any event day we send back for weekday repeating
#     events is not a weekend.
#     """
#     d = date(year, month, day)
#     while d.weekday() in (5, 6):
#         if reverse:
#             d -= timedelta(days=1)
#         else:
#             d += timedelta(days=1)
#     return d.year, d.month, d.day
# 
# def inc_month(month, year):
#     """
#     Increment the month and, if neccessary, the year.
#     Both month and year should be ints.
#     """
#     month += 1
#     if month > 12:
#         month = 1
#         year += 1
#     return month, year
# 
# Path: happenings/utils/handlers.py
# class CountHandler(object):
#     def __init__(self, year, month, events):
#         self.year = year
#         self.month = month
#         self.events = events
#         self.count = defaultdict(list)
# 
#     def _handle_single_chunk(self, event):
#         """
#         This handles either a non-repeating event chunk, or the first
#         month of a repeating event chunk.
#         """
#         if not event.starts_same_month_as(self.month) and not \
#                 event.repeats('NEVER'):
#             # no repeating chunk events if we're not in it's start month
#             return
# 
#         # add the events into an empty defaultdict. This is better than passing
#         # in self.count, which we don't want to make another copy of because it
#         # could be very large.
#         mycount = defaultdict(list)
#         r = Repeater(
#             mycount, self.year, self.month, day=event.l_start_date.day,
#             end_repeat=event.end_repeat, event=event, count_first=True,
#             end_on=event.l_end_date.day, num=1
#         )
# 
#         if event.starts_same_month_as(self.month):
#             if not event.ends_same_month_as(self.month):
#                 # The chunk event starts this month,
#                 # but does NOT end this month
#                 r.end_on = None
#         else:
#             # event chunks can be maximum of 7 days, so if an event chunk
#             # didn't start this month, we know it will end this month.
#             r.day = 1
#         r.repeat()
#         # now we add in the events we generated to self.count
#         for k, v in r.count.items():
#             self.count[k].extend(v)
# 
#     def _handle_month_event(self, event):
#         if event.is_chunk():
#             self._handle_single_chunk(event)
#         elif event.repeats('WEEKDAY'):
#             if event.l_start_date.weekday() not in (5, 6):
#                 self.count[event.l_start_date.day].append(
#                     (event.title, event.pk)
#                 )
#         else:
#             self.count[event.l_start_date.day].append((event.title, event.pk))
# 
#     def get_count(self):
#         kwargs = {'year': self.year, 'month': self.month, 'count': self.count}
#         for event in self.events:
#             kwargs['event'] = event
#             if event.starts_ends_yr_mo(self.year, self.month):
#                 self._handle_month_event(event)
#             if event.repeats('WEEKLY') or event.repeats('BIWEEKLY'):
#                 r = WeeklyRepeater(**kwargs)
#             elif event.repeats('MONTHLY'):
#                 r = MonthlyRepeater(**kwargs)
#             elif event.repeats('DAILY') or event.repeats('WEEKDAY'):
#                 r = DailyRepeater(**kwargs)
#             else:
#                 r = YearlyRepeater(**kwargs)
#             self.count = r.repeat_it()
#         return self.count


from datetime import date
from .common import check_weekday, inc_month
from .handlers import CountHandler
    Returns the next occurrence of a given event, relative to 'now'.
    The 'event' arg should be an iterable containing one element,
    namely the event we'd like to find the occurrence of.
    The reason for this is b/c the get_count() function of CountHandler,
    which this func makes use of, expects an iterable.
    CHANGED: The 'now' arg must be an instance of datetime.datetime()
    to allow time comparison (used to accept datetime.date() as well)
    """
    year = now.year
    month = now.month
    day = now.day
    e_day = event[0].l_start_date.day
    e_end_day = event[0].l_end_date.day
    good_today = True if event[0].l_start_date.time() >= now.time() else False
    if event[0].starts_same_year_month_as(year, month) and \
            e_day <= now.day <= e_end_day:
        occurrences = CountHandler(year, month, event).get_count()
        future_dates = (x for x in occurrences if x >= now.day)
        day = min(future_dates, key=lambda x: abs(x - now.day))
    else:
        e_year = event[0].l_start_date.year
        e_month = event[0].l_start_date.month
        # convert to datetime.date() to be sure we can make a comparison
        if date(e_year, e_month, e_day) > date(now.year, now.month, now.day):
            # if the event hasn't started yet, then its next occurrence will
            # be on its start date, so return that.
            year = e_year
            month = e_month
            day = e_day
        else:

# Path: happenings/utils/common.py
# def check_weekday(year, month, day, reverse=False):
#     """
#     Make sure any event day we send back for weekday repeating
#     events is not a weekend.
#     """
#     d = date(year, month, day)
#     while d.weekday() in (5, 6):
#         if reverse:
#             d -= timedelta(days=1)
#         else:
#             d += timedelta(days=1)
#     return d.year, d.month, d.day
# 
# def inc_month(month, year):
#     """
#     Increment the month and, if neccessary, the year.
#     Both month and year should be ints.
#     """
#     month += 1
#     if month > 12:
#         month = 1
#         year += 1
#     return month, year
# 
# Path: happenings/utils/handlers.py
# class CountHandler(object):
#     def __init__(self, year, month, events):
#         self.year = year
#         self.month = month
#         self.events = events
#         self.count = defaultdict(list)
# 
#     def _handle_single_chunk(self, event):
#         """
#         This handles either a non-repeating event chunk, or the first
#         month of a repeating event chunk.
#         """
#         if not event.starts_same_month_as(self.month) and not \
#                 event.repeats('NEVER'):
#             # no repeating chunk events if we're not in it's start month
#             return
# 
#         # add the events into an empty defaultdict. This is better than passing
#         # in self.count, which we don't want to make another copy of because it
#         # could be very large.
#         mycount = defaultdict(list)
#         r = Repeater(
#             mycount, self.year, self.month, day=event.l_start_date.day,
#             end_repeat=event.end_repeat, event=event, count_first=True,
#             end_on=event.l_end_date.day, num=1
#         )
# 
#         if event.starts_same_month_as(self.month):
#             if not event.ends_same_month_as(self.month):
#                 # The chunk event starts this month,
#                 # but does NOT end this month
#                 r.end_on = None
#         else:
#             # event chunks can be maximum of 7 days, so if an event chunk
#             # didn't start this month, we know it will end this month.
#             r.day = 1
#         r.repeat()
#         # now we add in the events we generated to self.count
#         for k, v in r.count.items():
#             self.count[k].extend(v)
# 
#     def _handle_month_event(self, event):
#         if event.is_chunk():
#             self._handle_single_chunk(event)
#         elif event.repeats('WEEKDAY'):
#             if event.l_start_date.weekday() not in (5, 6):
#                 self.count[event.l_start_date.day].append(
#                     (event.title, event.pk)
#                 )
#         else:
#             self.count[event.l_start_date.day].append((event.title, event.pk))
# 
#     def get_count(self):
#         kwargs = {'year': self.year, 'month': self.month, 'count': self.count}
#         for event in self.events:
#             kwargs['event'] = event
#             if event.starts_ends_yr_mo(self.year, self.month):
#                 self._handle_month_event(event)
#             if event.repeats('WEEKLY') or event.repeats('BIWEEKLY'):
#                 r = WeeklyRepeater(**kwargs)
#             elif event.repeats('MONTHLY'):
#                 r = MonthlyRepeater(**kwargs)
#             elif event.repeats('DAILY') or event.repeats('WEEKDAY'):
#                 r = DailyRepeater(**kwargs)
#             else:
#                 r = YearlyRepeater(**kwargs)
#             self.count = r.repeat_it()
#         return self.count


# Path: happenings/utils/next_event.py
from datetime import date
from .common import check_weekday, inc_month
from .handlers import CountHandler

    Returns the next occurrence of a given event, relative to 'now'.
    The 'event' arg should be an iterable containing one element,
    namely the event we'd like to find the occurrence of.
    The reason for this is b/c the get_count() function of CountHandler,
    which this func makes use of, expects an iterable.
    CHANGED: The 'now' arg must be an instance of datetime.datetime()
    to allow time comparison (used to accept datetime.date() as well)
    """
    year = now.year
    month = now.month
    day = now.day
    e_day = event[0].l_start_date.day
    e_end_day = event[0].l_end_date.day
    good_today = True if event[0].l_start_date.time() >= now.time() else False
    if event[0].starts_same_year_month_as(year, month) and \
            e_day <= now.day <= e_end_day:
        occurrences = CountHandler(year, month, event).get_count()
        future_dates = (x for x in occurrences if x >= now.day)
        day = min(future_dates, key=lambda x: abs(x - now.day))
    else:
        e_year = event[0].l_start_date.year
        e_month = event[0].l_start_date.month
        # convert to datetime.date() to be sure we can make a comparison
        if date(e_year, e_month, e_day) > date(now.year, now.month, now.day):
            # if the event hasn't started yet, then its next occurrence will
            # be on its start date, so return that.
            year = e_year
            month = e_month
            day = e_day
        else:

            occurrences = CountHandler(year, month, event).get_count()