def test_end_dst_double_occurrence(self): cron_expression = "30 1 * * * *" timezone = pytz.timezone("Europe/London") start = dt.datetime.strptime('2015-10-25T00:00:00', "%Y-%m-%dT%H:%M:%S") start = timezone.localize(start, is_dst=True) testee = tzcron.Schedule(cron_expression, timezone, start) self.assertRaises(pytz.AmbiguousTimeError, lambda: next(testee))
def test_start_dst_invalid_occurrence(self): """Test that tzcron updates the offset when moving to DST""" cron_expression = "30 1 * * * *" timezone = pytz.timezone("Europe/London") start = dt.datetime.strptime('2015-03-29T00:00:00', "%Y-%m-%dT%H:%M:%S") start = timezone.localize(start, is_dst=False) testee = tzcron.Schedule(cron_expression, timezone, start) self.assertRaises(pytz.NonExistentTimeError, lambda: next(testee))
def test_next_minute(self): cron_expression = "* * * * * *" timezone = pytz.utc start = self.now testee = tzcron.Schedule(cron_expression, timezone, start) next_it = next(testee) expected_time = self.now.replace(second=0, microsecond=0) expected_time = expected_time + dt.timedelta(0, 60) self.assertEqual(str(expected_time), str(next_it))
def cron_next(param): # Get current date as set by the source time_now = pytz.utc.localize(datetime.datetime.utcfromtimestamp(utcnow())) # Get next trigger time next_dt = next(tzcron.Schedule(param, pytz.utc, time_now)) # Return when the next trigger will happen in seconds return (next_dt - datetime.datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()
def update_computed_fields(self): future_rs = tzcron.Schedule(self.schedule.crontab, pytz.utc) next_run_actual = next(future_rs) if next_run_actual is not None: if not datetime_exists(next_run_actual): # skip imaginary dates, like 2:30 on DST boundaries next_run_actual = next(future_rs) next_run_actual = next_run_actual.astimezone(pytz.utc) self.next_run = next_run_actual emit_channel_notification('schedules-changed', dict(id=self.id, group_name='schedules'))
def test_end_dst(self): cron_expression = "30 5 * * * *" timezone = pytz.timezone("Europe/London") start = dt.datetime.strptime('2015-10-25T00:00:00', "%Y-%m-%dT%H:%M:%S") start = timezone.localize(start, is_dst=True) testee = tzcron.Schedule(cron_expression, timezone, start) next_it = next(testee) expected_time = dt.datetime.strptime('2015-10-25T05:30:00', "%Y-%m-%dT%H:%M:%S") expected_time = timezone.localize(expected_time, is_dst=False) self.assertEqual(str(expected_time), str(next_it))
def test_friday_at_5(self): cron_expression = "0 5 * * 5 *" timezone = pytz.utc start = dt.datetime.strptime('1989-04-24T05:01:00', "%Y-%m-%dT%H:%M:%S") start = start.replace(tzinfo=timezone) testee = tzcron.Schedule(cron_expression, timezone, start) next_it = next(testee) expected_time = dt.datetime.strptime('1989-04-28T05:00:00', "%Y-%m-%dT%H:%M:%S") expected_time = expected_time.replace(tzinfo=timezone) self.assertEqual(str(expected_time), str(next_it))
def list(self, request, *args, **kwargs): obj = self.get_object() now = datetime.datetime.now(pytz.utc) start_month = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0) year = now.year if start_month.month == 12: year += 1 relative_month = dateutil.relativedelta.relativedelta(months=1) end_month = datetime.datetime(year, (start_month + relative_month).month, 1) - datetime.timedelta(days=1) end_month = end_month.replace(hour=23, minute=59, second=50, tzinfo=pytz.utc) schedule = tzcron.Schedule(obj.schedule.crontab, pytz.utc, start_month, end_month) return Response([s.isoformat() for s in schedule])
def test_api_v1_get_policy_calendar_1(self): url = reverse('api:policy_calendar', kwargs={'version': 'v1', 'pk': 1}) self.client.login(username=self.user_login, password=self.user_pass) response = self.client.get(url, format='json') import datetime import tzcron import dateutil import pytz now = datetime.datetime.now(pytz.utc) start_month = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0) year = now.year if start_month.month == 12: year += 1 relative_month = dateutil.relativedelta.relativedelta(months=1) end_month = datetime.datetime(year, (start_month + relative_month).month, 1) - datetime.timedelta(days=1) end_month = end_month.replace(hour=23, minute=59, second=50, tzinfo=pytz.utc) schedule = tzcron.Schedule("0 5 * * MON *", pytz.utc, start_month, end_month) expectedCalendar = [s.isoformat() for s in schedule] self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, expectedCalendar)
def test_2016_06_01(self, expression): results = tzcron.Schedule(expression, self.timezone, self.start) expected_date = dt.datetime(2016, 6, 1).replace(tzinfo=pytz.utc) self.assertEqual(next(results), expected_date)