Beispiel #1
0
 def _download_zip_files(self, date):
     zip_files = []
     print('Downloading zip files for ', date)
     months_added = 0
     iter_date = date
     while months_added != 3:
         zip_name = self._generate_filename(iter_date)
         for file_name in self.ftp.nlst():
             if zip_name in file_name:
                 file = BytesIO()
                 print('Downloading file ', file_name, '...')
                 self.ftp.retrbinary('RETR ' + file_name, file.write)
                 zip_files.append(file)
         iter_date = date_utils.add_months(iter_date, 1)
         months_added += 1
     print('All files downloaded!')
     return zip_files
Beispiel #2
0
 def test_subtract_months_to_prev_year(self):
     original = datetime(2020,
                         7,
                         10,
                         15,
                         30,
                         59,
                         123456,
                         tzinfo=timezone.utc)
     added = date_utils.add_months(original, -10)
     expected = datetime(2019,
                         9,
                         10,
                         15,
                         30,
                         59,
                         123456,
                         tzinfo=timezone.utc)
     self.assertEqual(expected, added)
Beispiel #3
0
 def test_subtract_one_month(self):
     original = datetime(2020,
                         7,
                         10,
                         15,
                         30,
                         59,
                         123456,
                         tzinfo=timezone.utc)
     added = date_utils.add_months(original, -1)
     expected = datetime(2020,
                         6,
                         10,
                         15,
                         30,
                         59,
                         123456,
                         tzinfo=timezone.utc)
     self.assertEqual(expected, added)
Beispiel #4
0
 def test_add_months_to_last_day_when_next_leap_february(self):
     original = datetime(2019,
                         7,
                         30,
                         15,
                         30,
                         59,
                         123456,
                         tzinfo=timezone.utc)
     added = date_utils.add_months(original, 7)
     expected = datetime(2020,
                         2,
                         29,
                         15,
                         30,
                         59,
                         123456,
                         tzinfo=timezone.utc)
     self.assertEqual(expected, added)
Beispiel #5
0
 def test_add_months_to_last_day_when_next_longer(self):
     original = datetime(2020,
                         6,
                         30,
                         15,
                         30,
                         59,
                         123456,
                         tzinfo=timezone.utc)
     added = date_utils.add_months(original, 2)
     expected = datetime(2020,
                         8,
                         30,
                         15,
                         30,
                         59,
                         123456,
                         tzinfo=timezone.utc)
     self.assertEqual(expected, added)
Beispiel #6
0
 def test_add_months_to_roll_multiple_years(self):
     original = datetime(2020,
                         7,
                         10,
                         15,
                         30,
                         59,
                         123456,
                         tzinfo=timezone.utc)
     added = date_utils.add_months(original, 33)
     expected = datetime(2023,
                         4,
                         10,
                         15,
                         30,
                         59,
                         123456,
                         tzinfo=timezone.utc)
     self.assertEqual(expected, added)
Beispiel #7
0
 def test_add_months_to_roll_next_year(self):
     original = datetime(2020,
                         7,
                         10,
                         15,
                         30,
                         59,
                         123456,
                         tzinfo=timezone.utc)
     added = date_utils.add_months(original, 6)
     expected = datetime(2021,
                         1,
                         10,
                         15,
                         30,
                         59,
                         123456,
                         tzinfo=timezone.utc)
     self.assertEqual(expected, added)
Beispiel #8
0
 def test_add_4_months(self):
     original = datetime(2020,
                         7,
                         10,
                         15,
                         30,
                         59,
                         123456,
                         tzinfo=timezone.utc)
     added = date_utils.add_months(original, 4)
     expected = datetime(2020,
                         11,
                         10,
                         15,
                         30,
                         59,
                         123456,
                         tzinfo=timezone.utc)
     self.assertEqual(expected, added)
Beispiel #9
0
    def get_next_time(self):
        if not self.repeatable:
            return self.start_datetime

        if self.repeat_unit == 'minutes':
            next_time_func = lambda start, iteration_index: \
                start + timedelta(minutes=self.repeat_period * iteration_index)
            get_initial_multiplier = lambda start: \
                ((now - start).seconds // 60 + (now - start).days * 1440) \
                // self.repeat_period
        elif self.repeat_unit == 'hours':
            next_time_func = lambda start, iteration_index: start + timedelta(
                hours=self.repeat_period * iteration_index)

            get_initial_multiplier = lambda start: \
                ((now - start).seconds // 3600 + (now - start).days * 24) \
                // self.repeat_period
        elif self.repeat_unit == 'days':
            next_time_func = lambda start, iteration_index: start + timedelta(days=self.repeat_period * iteration_index)
            get_initial_multiplier = lambda start: (now - start).days // self.repeat_period
        elif self.repeat_unit == 'months':
            next_time_func = lambda start, iteration_index: date_utils.add_months(start,
                                                                                  self.repeat_period * iteration_index)
            get_initial_multiplier = lambda start: (now - start).days // 28 // self.repeat_period
        elif self.repeat_unit == 'weeks':
            start_weekday = self.start_datetime.weekday()
            offset = 0
            for weekday in self.weekdays:
                index = ALLOWED_WEEKDAYS.index(weekday)
                if index < start_weekday:
                    offset += 1

            def next_weekday(start: datetime, iteration_index):
                weeks_multiplier = (iteration_index + offset) // len(self.weekdays)
                next_weekday_index = (iteration_index + offset) % len(self.weekdays)
                next_weekday_name = self.weekdays[next_weekday_index]
                next_weekday = ALLOWED_WEEKDAYS.index(next_weekday_name)

                return start \
                       + timedelta(weeks=self.repeat_period * weeks_multiplier) \
                       + timedelta(days=(next_weekday - start.weekday()))

            next_time_func = next_weekday

            get_initial_multiplier = lambda start: (now - start).days // 7 // self.repeat_period * len(
                self.weekdays) - 1
        else:
            raise Exception('Unknown unit: ' + repr(self.repeat_unit))

        now = date_utils.now(tz=timezone.utc)
        max_iterations = 10000
        initial_multiplier = max(0, get_initial_multiplier(self.start_datetime))
        i = 0
        while True:
            resolved_time = next_time_func(self.start_datetime, i + initial_multiplier)
            if resolved_time >= now:
                return resolved_time

            i += 1
            if i > max_iterations:
                raise Exception('Endless loop in calc next time')