def test_impossible_recurrence_before(self): """ Testing error calling before and function wrapper to solve it (recurrence_before) """ start_dt = datetime.datetime(2014, 1, 20, 14, 0, 0) until_dt = datetime.datetime(2014, 1, 19, 14, 0, 0) daily_recurrence = recurrence.Recurrence( rrules=[recurrence.Rule(recurrence.MONTHLY, until=until_dt)]) dt = daily_recurrence.before(start_dt + datetime.timedelta(seconds=1), dtstart=start_dt) self.assertEqual(start_dt, dt) # wrong! self.assertIsNone(recurrence_before(daily_recurrence, start_dt + datetime.timedelta(seconds=1), start_dt))
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 test_impossible_recurrence_before(self): """ Testing error calling before and function wrapper to solve it (recurrence_before) """ start_dt = datetime.datetime(2014, 1, 20, 14, 0, 0) until_dt = datetime.datetime(2014, 1, 19, 14, 0, 0) daily_recurrence = recurrence.Recurrence( rrules=[recurrence.Rule(recurrence.MONTHLY, until=until_dt)]) dt = daily_recurrence.before(start_dt + datetime.timedelta(seconds=1), dtstart=start_dt) self.assertEqual(start_dt, dt) # wrong! self.assertIsNone( recurrence_before(daily_recurrence, start_dt + datetime.timedelta(seconds=1), start_dt))
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 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)