# Path: radioco/apps/radioco/tz_utils.py
# def transform_dt_to_default_tz(dt):
#     """
#     Transform a datetime in other timezone to the current one
#     """
#     tz = timezone.get_default_timezone()
#     return tz.normalize(dt.astimezone(tz))
# 
# def fix_recurrence_dst(dt):
#     """
#     Fix for django-recurrence 1.3
#     Function to fix a datetime tz aware with an incorrect offset
# 
#     Returns: A datetime in the same timezone but with the offset fixed
#     """
#     if dt:
#         tz = dt.tzinfo
#         return tz.localize(datetime.datetime.combine(dt.date(), dt.time()))
#     return None


import datetime
import django.db.models.deletion
import pytz
from collections import namedtuple
from itertools import chain
from django.db import migrations, models
from django.utils import timezone
from radioco.apps.radioco.tz_utils import transform_dt_to_default_tz, fix_recurrence_dst
        return _fix_invalid_dt(recurrence, dt)
    return dt


def calculate_effective_schedule_start_dt(schedule):
    """
    Calculation of the first start date to improve performance
    """
    tz = timezone.get_default_timezone()
    programme_start_dt = tz.localize(
        datetime.datetime.combine(schedule.programme.start_date, datetime.time())
    ).astimezone(pytz.utc) if schedule.programme.start_date else None

    programme_end_dt = tz.localize(
        datetime.datetime.combine(schedule.programme.end_date, datetime.time(23, 59, 59))
    ).astimezone(pytz.utc) if schedule.programme.end_date else None

    # If there are no rrules
    if not schedule.recurrences:
        if programme_start_dt and programme_start_dt > schedule.start_dt:
            return None
        if programme_end_dt and schedule.start_dt > programme_end_dt:
            return None
        return schedule.start_dt

    # Get first date
    after_dt = schedule.start_dt
    if programme_start_dt:
        after_dt = max(schedule.start_dt, programme_start_dt)
    first_start_dt = fix_recurrence_dst(recurrence_after(

# Path: radioco/apps/radioco/tz_utils.py
# def transform_dt_to_default_tz(dt):
#     """
#     Transform a datetime in other timezone to the current one
#     """
#     tz = timezone.get_default_timezone()
#     return tz.normalize(dt.astimezone(tz))
# 
# def fix_recurrence_dst(dt):
#     """
#     Fix for django-recurrence 1.3
#     Function to fix a datetime tz aware with an incorrect offset
# 
#     Returns: A datetime in the same timezone but with the offset fixed
#     """
#     if dt:
#         tz = dt.tzinfo
#         return tz.localize(datetime.datetime.combine(dt.date(), dt.time()))
#     return None


# Path: radioco/apps/schedules/migrations/0005__v3_0__migrating_schedules_to_unique_calendar.py
import datetime
import django.db.models.deletion
import pytz
from collections import namedtuple
from itertools import chain
from django.db import migrations, models
from django.utils import timezone
from radioco.apps.radioco.tz_utils import transform_dt_to_default_tz, fix_recurrence_dst

        return _fix_invalid_dt(recurrence, dt)
    return dt


def calculate_effective_schedule_start_dt(schedule):
    """
    Calculation of the first start date to improve performance
    """
    tz = timezone.get_default_timezone()
    programme_start_dt = tz.localize(
        datetime.datetime.combine(schedule.programme.start_date, datetime.time())
    ).astimezone(pytz.utc) if schedule.programme.start_date else None

    programme_end_dt = tz.localize(
        datetime.datetime.combine(schedule.programme.end_date, datetime.time(23, 59, 59))
    ).astimezone(pytz.utc) if schedule.programme.end_date else None

    # If there are no rrules
    if not schedule.recurrences:
        if programme_start_dt and programme_start_dt > schedule.start_dt:
            return None
        if programme_end_dt and schedule.start_dt > programme_end_dt:
            return None
        return schedule.start_dt

    # Get first date
    after_dt = schedule.start_dt
    if programme_start_dt:
        after_dt = max(schedule.start_dt, programme_start_dt)
    first_start_dt = fix_recurrence_dst(recurrence_after(

        schedule.recurrences, transform_dt_to_default_tz(after_dt), transform_dt_to_default_tz(schedule.start_dt)))