'Utility method that sorts and removes overlapping inside datetime
intervals. The intervals are sorted based on increasing starting datetime.
Overlapping intervals are merged into a single one.
:param list intervals: list of intervals; each interval is a tuple
(datetime_from, datetime_to)
:return list cleaned: list of sorted intervals without overlap'

def interval_clean(self, intervals):

    intervals = sorted(intervals, key=itemgetter(0))
    cleaned = []
    working_interval = None
    while intervals:
        current_interval = intervals.pop(0)
        if (not working_interval):
            working_interval = [current_interval[0], current_interval[1]]
        elif (working_interval[1] < current_interval[0]):
            cleaned.append(tuple(working_interval))
            working_interval = [current_interval[0], current_interval[1]]
        elif (working_interval[1] < current_interval[1]):
            working_interval[1] = current_interval[1]
    if working_interval:
        cleaned.append(tuple(working_interval))
    return cleaned
