# 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
            # WARNING: this depends on effective_start_dt
            return None  # returning None if there is no effective_start_dt
        return schedule.start_dt + runtime

    # If we have a programme restriction
    if programme_end_dt:
        last_effective_start_date = fix_recurrence_dst(recurrence_before(
            schedule.recurrences, transform_dt_to_default_tz(programme_end_dt), transform_dt_to_default_tz(schedule.start_dt)))
        if last_effective_start_date:
            if programme_start_dt and programme_start_dt > last_effective_start_date:
                return None
            return last_effective_start_date + runtime

    rrules_until_dates = [_rrule.until for _rrule in schedule.recurrences.rrules]

    # If we have a rrule without a until date we don't know the last date
    if any([x is None for x in rrules_until_dates]):
        return None

    possible_limit_dates = schedule.recurrences.rdates + rrules_until_dates
    if not possible_limit_dates:
        return None

    # Get the biggest possible start_date. It could be that the biggest date is excluded
    biggest_date = max(possible_limit_dates)
    last_effective_start_date = schedule.recurrences.before(
        transform_dt_to_default_tz(biggest_date), True, dtstart=transform_dt_to_default_tz(schedule.start_dt))
    if last_effective_start_date:
        if programme_start_dt and programme_start_dt > last_effective_start_date:
            return None

# 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

            # WARNING: this depends on effective_start_dt
            return None  # returning None if there is no effective_start_dt
        return schedule.start_dt + runtime

    # If we have a programme restriction
    if programme_end_dt:
        last_effective_start_date = fix_recurrence_dst(recurrence_before(
            schedule.recurrences, transform_dt_to_default_tz(programme_end_dt), transform_dt_to_default_tz(schedule.start_dt)))
        if last_effective_start_date:
            if programme_start_dt and programme_start_dt > last_effective_start_date:
                return None
            return last_effective_start_date + runtime

    rrules_until_dates = [_rrule.until for _rrule in schedule.recurrences.rrules]

    # If we have a rrule without a until date we don't know the last date
    if any([x is None for x in rrules_until_dates]):
        return None

    possible_limit_dates = schedule.recurrences.rdates + rrules_until_dates
    if not possible_limit_dates:
        return None

    # Get the biggest possible start_date. It could be that the biggest date is excluded
    biggest_date = max(possible_limit_dates)
    last_effective_start_date = schedule.recurrences.before(
        transform_dt_to_default_tz(biggest_date), True, dtstart=transform_dt_to_default_tz(schedule.start_dt))
    if last_effective_start_date:
        if programme_start_dt and programme_start_dt > last_effective_start_date:
            return None

        return fix_recurrence_dst(last_effective_start_date) + runtime