'Extend this time frame with events
@param events: iterable of events
@todo: integrate in constructor
@todo: handle self.rule == None'

def extend(self, events):

    if (not events):
        return
    empty = self.empty
    events = sorted(events)
    rule = self.rule
    periods = self.periods
    start = events[0].start
    if ((start is None) or (start <= self.start)):
        first = rule[0]
    else:
        first = rule.before(start, inc=True)
    current_events = {}
    previous_events = {}
    for start in rule.between(first, self.end, inc=True):
        end = rule.after(start)
        if (not end):
            if (start < self.end):
                end = self.end
            else:
                break
        for (index, event) in enumerate(events):
            if (event.end and (event.end < start)):
                previous_events[event.event_id] = event
            elif ((event.start is None) or (event.start < end)):
                current_events[event.event_id] = event
            else:
                break
        period = periods.get(start)
        if (period is None):
            period = periods[start] = S3TimeSeriesPeriod(start, end=end)
        for event in current_events.values():
            period.add_current(event)
        for event in previous_events.values():
            period.add_previous(event)
        empty = False
        events = events[index:]
        if (not events):
            break
        remaining = {}
        for (event_id, event) in current_events.items():
            if ((not event.end) or (event.end > end)):
                remaining[event_id] = event
            else:
                previous_events[event_id] = event
        current_events = remaining
    self.empty = empty
    return
