def calculate_effective_schedule_end_dt(schedule): """ Calculation of the last end 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 runtime = datetime.timedelta(minutes=schedule.programme._runtime) # If there are no rrules if not schedule.recurrences: if not schedule.effective_start_dt: # 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 return None
def calculate_effective_schedule_end_dt(schedule): """ Calculation of the last end 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 runtime = datetime.timedelta(minutes=schedule.programme._runtime) # If there are no rrules if not schedule.recurrences: if not schedule.effective_start_dt: # 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 return None
def calculate_effective_schedule_end_dt(schedule): """ Calculation of the last end date to improve performance """ programme_start_dt = schedule.programme.start_dt programme_end_dt = schedule.programme.end_dt # If there are no rrules if not schedule.has_recurrences(): if not schedule.effective_start_dt: # WARNING: this depends on effective_start_dt return None # returning None if there is no effective_start_dt return schedule.start_dt + schedule.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 + schedule.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) + schedule.runtime return None
def calculate_effective_schedule_start_dt(schedule): """ Calculation of the first start date to improve performance """ programme_start_dt = schedule.programme.start_dt programme_end_dt = schedule.programme.end_dt # If there are no rrules if not schedule.has_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))) if first_start_dt: if programme_end_dt and programme_end_dt < first_start_dt: return None return first_start_dt return None
def calculate_effective_schedule_start_dt(schedule): """ Calculation of the first start date to improve performance """ programme_start_dt = schedule.programme.start_dt programme_end_dt = schedule.programme.end_dt # If there are no rrules if not schedule.has_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))) if first_start_dt: if programme_end_dt and programme_end_dt < first_start_dt: return None return first_start_dt return None
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))) if first_start_dt: if programme_end_dt and programme_end_dt < first_start_dt: return None return first_start_dt return None
def date_after(self, after): after_date = self._merge_after(after) if not after_date: return after_date = transform_dt_to_default_tz(after_date) start_dt = transform_dt_to_default_tz(self.start_dt) date = recurrence_after(self.recurrences, after_date, start_dt) return fix_recurrence_dst(date)
def date_after(self, after): after_date = self._merge_after(after) if not after_date: return after_date = transform_dt_to_default_tz(after_date) start_dt = transform_dt_to_default_tz(self.start_dt) date = recurrence_after(self.recurrences, after_date, start_dt) return fix_recurrence_dst(date)
def calculate_effective_schedule_end_dt(schedule): """ Calculation of the last end date to improve performance """ programme_start_dt = schedule.programme.start_dt programme_end_dt = schedule.programme.end_dt # If there are no rrules if not schedule.has_recurrences(): if not schedule.effective_start_dt: # WARNING: this depends on effective_start_dt return None # returning None if there is no effective_start_dt return schedule.start_dt + schedule.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 + schedule.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) + schedule.runtime return None
def dates_between(self, after, before): """ Return a sorted list of dates between after and before """ after_date = self._merge_after(after) if not after_date: return after_date = transform_dt_to_default_tz(after_date) before_date = transform_dt_to_default_tz(self._merge_before(before)) start_dt = transform_dt_to_default_tz(self.start_dt) # We need to send the dates in the default timezone recurrence_dates_between = self.recurrences.between(after_date, before_date, inc=True, dtstart=start_dt) # Special case to include started episodes date_before = self.date_before(after_date) if date_before and date_before < after_date < date_before + self.runtime: yield date_before # Date was already fixed for date in recurrence_dates_between: yield fix_recurrence_dst(date) # Fixing date
def dates_between(self, after, before): """ Return a sorted list of dates between after and before """ after_date = self._merge_after(after) if not after_date: return after_date = transform_dt_to_default_tz(after_date) before_date = transform_dt_to_default_tz(self._merge_before(before)) start_dt = transform_dt_to_default_tz(self.start_dt) # We need to send the dates in the default timezone recurrence_dates_between = self.recurrences.between(after_date, before_date, inc=True, dtstart=start_dt) # Special case to include started episodes date_before = self.date_before(after_date) if date_before and date_before < after_date < date_before + self.runtime: yield date_before # Date was already fixed for date in recurrence_dates_between: yield fix_recurrence_dst(date) # Fixing date
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))) if first_start_dt: if programme_end_dt and programme_end_dt < first_start_dt: return None return first_start_dt return None
def date_before(self, before): before_date = transform_dt_to_default_tz(self._merge_before(before)) start_dt = transform_dt_to_default_tz(self.start_dt) date = recurrence_before(self.recurrences, before_date, start_dt) return fix_recurrence_dst(date)
def date_before(self, before): before_date = transform_dt_to_default_tz(self._merge_before(before)) start_dt = transform_dt_to_default_tz(self.start_dt) date = recurrence_before(self.recurrences, before_date, start_dt) return fix_recurrence_dst(date)