Python dateutil.rrule.MONTHLY Examples

The following are 30 code examples of dateutil.rrule.MONTHLY(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module dateutil.rrule , or try the search function .
Example #1
Source File: helpers.py    From figures with MIT License 7 votes vote down vote up
def previous_months_iterator(month_for, months_back):
    '''Iterator returns a year,month tuple for n months including the month_for

    month_for is either a date, datetime, or tuple with year and month
    months back is the number of months to iterate

    includes the month_for
    '''

    if isinstance(month_for, tuple):
        # TODO make sure we've got just two values in the tuple
        month_for = datetime.date(year=month_for[0], month=month_for[1], day=1)
    if isinstance(month_for, (datetime.datetime, datetime.date)):
        start_month = month_for - relativedelta(months=months_back)

    for dt in rrule(freq=MONTHLY, dtstart=start_month, until=month_for):
        last_day_of_month = days_in_month(month_for=dt)
        yield (dt.year, dt.month, last_day_of_month) 
Example #2
Source File: test_imports.py    From plugin.video.emby with GNU General Public License v3.0 7 votes vote down vote up
def testRRuleAll(self):
        from dateutil.rrule import rrule
        from dateutil.rrule import rruleset
        from dateutil.rrule import rrulestr
        from dateutil.rrule import YEARLY, MONTHLY, WEEKLY, DAILY
        from dateutil.rrule import HOURLY, MINUTELY, SECONDLY
        from dateutil.rrule import MO, TU, WE, TH, FR, SA, SU

        rr_all = (rrule, rruleset, rrulestr,
                  YEARLY, MONTHLY, WEEKLY, DAILY,
                  HOURLY, MINUTELY, SECONDLY,
                  MO, TU, WE, TH, FR, SA, SU)

        for var in rr_all:
            self.assertIsNot(var, None)

        # In the public interface but not in all
        from dateutil.rrule import weekday
        self.assertIsNot(weekday, None) 
Example #3
Source File: backfill.py    From figures with MIT License 6 votes vote down vote up
def backfill_monthly_metrics_for_site(site, overwrite=False):
    """Backfill all historical site metrics for the specified site
    """
    site_sm = get_student_modules_for_site(site)
    if not site_sm:
        return None

    first_created = site_sm.order_by('created').first().created

    start_month = datetime(year=first_created.year,
                           month=first_created.month,
                           day=1,
                           tzinfo=utc)
    last_month = datetime.utcnow().replace(tzinfo=utc) - relativedelta(months=1)
    backfilled = []
    for dt in rrule(freq=MONTHLY, dtstart=start_month, until=last_month):
        obj, created = fill_month(site=site,
                                  month_for=dt,
                                  student_modules=site_sm,
                                  overwrite=overwrite)
        backfilled.append(dict(obj=obj, created=created, dt=dt))

    return backfilled 
Example #4
Source File: recurrence.py    From ls.joyous with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _getWhen(self, offset, numDays=1):
        retval = ""
        if self.freq == DAILY:
            retval = self.__getDailyWhen()

        elif self.freq == WEEKLY:
            retval = self.__getWeeklyWhen(offset)

        elif self.freq == MONTHLY:
            retval = self.__getMonthlyWhen(offset)

        elif self.freq == YEARLY:
            retval = self.__getYearlyWhen(offset)

        if numDays >= 2:
            retval += " "+_("for {n} days").format(n=numDays)
        if self.until:
            until = self.until + dt.timedelta(days=offset)
            retval += " "+_("(until {when})").format(when=dateShortFormat(until))
        return retval 
Example #5
Source File: test_imports.py    From bazarr with GNU General Public License v3.0 6 votes vote down vote up
def testRRuleAll(self):
        from dateutil.rrule import rrule
        from dateutil.rrule import rruleset
        from dateutil.rrule import rrulestr
        from dateutil.rrule import YEARLY, MONTHLY, WEEKLY, DAILY
        from dateutil.rrule import HOURLY, MINUTELY, SECONDLY
        from dateutil.rrule import MO, TU, WE, TH, FR, SA, SU

        rr_all = (rrule, rruleset, rrulestr,
                  YEARLY, MONTHLY, WEEKLY, DAILY,
                  HOURLY, MINUTELY, SECONDLY,
                  MO, TU, WE, TH, FR, SA, SU)

        for var in rr_all:
            self.assertIsNot(var, None)

        # In the public interface but not in all
        from dateutil.rrule import weekday
        self.assertIsNot(weekday, None) 
Example #6
Source File: misc.py    From entsoe-py with MIT License 6 votes vote down vote up
def month_blocks(start, end):
    """
    Create pairs of start and end with max a month in between, to deal with usage restrictions on the API

    Parameters
    ----------
    start : dt.datetime | pd.Timestamp
    end : dt.datetime | pd.Timestamp

    Returns
    -------
    ((pd.Timestamp, pd.Timestamp))
    """
    rule = rrule.MONTHLY

    res = []
    for day in rrule.rrule(rule, dtstart=start, until=end):
        res.append(pd.Timestamp(day))
    res.append(end)
    res = sorted(set(res))
    res = pairwise(res)
    return res 
Example #7
Source File: test_util.py    From palladium with Apache License 2.0 5 votes vote down vote up
def test_last_execution(self, RruleThread):
        func = Mock()
        thread = RruleThread(func, rrule.rrule(
            rrule.MONTHLY,
            bymonthday=1,
            dtstart=datetime(2014, 10, 30, 13, 21, 18)))
        thread.last_execution = datetime(2014, 10, 30, 13, 21, 18)
        thread.start()
        sleep(0.02)
        assert func.call_count == 1 
Example #8
Source File: test_recurrence.py    From django-ical with MIT License 5 votes vote down vote up
def test_ever_month_3rd_tu(self):
        """Repeat every month on the 3rd Tuesday."""
        vrecurr = utils.build_rrule_from_text("FREQ=MONTHLY;BYDAY=+3TU")
        assert vrecurr["FREQ"] == ["MONTHLY"]
        assert vrecurr["BYDAY"] == ["+3TU"]
        vRecur(vrecurr).to_ical().decode() == "FREQ=MONTHLY;BYDAY=+3TU"
        assert len(vrecurr.keys()) == 2 
Example #9
Source File: test_recurrence.py    From django-ical with MIT License 5 votes vote down vote up
def test_ever_month_3rd_last_tu(self):
        """Repeat every month on the 3rd last Tuesday."""
        vrecurr = utils.build_rrule_from_text("FREQ=MONTHLY;BYDAY=-3TU")
        assert vrecurr["FREQ"] == ["MONTHLY"]
        assert vrecurr["BYDAY"] == ["-3TU"]
        vRecur(vrecurr).to_ical().decode() == "FREQ=MONTHLY;BYDAY=-3TU"
        assert len(vrecurr.keys()) == 2 
Example #10
Source File: test_recurrence.py    From django-ical with MIT License 5 votes vote down vote up
def test_every_month_last_working_day(self):
        """Repeat the last working day of each month."""
        vrecurr = utils.build_rrule_from_text(
            "FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1;"
        )
        assert vrecurr["FREQ"] == ["MONTHLY"]
        assert vrecurr["BYDAY"] == ["MO", "TU", "WE", "TH", "FR"]
        assert vrecurr["BYSETPOS"] == [-1]
        vRecur(
            vrecurr
        ).to_ical().decode() == "FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1"
        assert len(vrecurr.keys()) == 3 
Example #11
Source File: test_recurrence.py    From django-ical with MIT License 5 votes vote down vote up
def test_ever_month_last_day(self):
        """Repeat the last day of each month."""
        vrecurr = utils.build_rrule_from_text("FREQ=MONTHLY;BYMONTHDAY=-1")
        assert vrecurr["FREQ"] == ["MONTHLY"]
        assert vrecurr["BYMONTHDAY"] == [-1]
        vRecur(vrecurr).to_ical().decode() == "FREQ=MONTHLY;BYMONTHDAY=-1"
        assert len(vrecurr.keys()) == 2 
Example #12
Source File: test_recurrence.py    From django-ical with MIT License 5 votes vote down vote up
def test_every_2nd_15th_of_month(self):
        """Repeat monthly on the 2nd and 15th of the month."""
        vrecurr = utils.build_rrule_from_text("FREQ=MONTHLY;BYMONTHDAY=4,15")
        assert vrecurr["FREQ"] == ["MONTHLY"]
        assert vrecurr["BYMONTHDAY"] == [4, 15]
        vRecur(vrecurr).to_ical().decode() == "FREQ=MONTHLY;BYMONTHDAY=4,15"
        assert len(vrecurr.keys()) == 2 
Example #13
Source File: test_recurrence.py    From django-ical with MIT License 5 votes vote down vote up
def test_monthly_month_monthday(self):
        rule = rrule(
            MONTHLY,
            count=3,
            bymonth=(1, 3),
            bymonthday=(5, 7),
            dtstart=datetime.datetime(1997, 9, 2, 9, 0),
        )
        vrecurr = utils.build_rrule_from_dateutil_rrule(rule)
        vRecur(
            vrecurr
        ).to_ical().decode() == "FREQ=MONTHLY;COUNT=3;BYMONTHDAY=5,7;BYMONTH=1,3" 
Example #14
Source File: test_recurrence.py    From django-ical with MIT License 5 votes vote down vote up
def test_monthly_nweekday(self):
        rule = rrule(
            MONTHLY,
            count=3,
            byweekday=(TU(1), TH(-1)),
            dtstart=datetime.datetime(1997, 9, 2, 9, 0),
        )
        vrecurr = utils.build_rrule_from_dateutil_rrule(rule)
        vRecur(vrecurr).to_ical().decode() == "FREQ=MONTHLY;COUNT=3;BYDAY=+1TU,-1TH" 
Example #15
Source File: rules.py    From ctparse with MIT License 5 votes vote down vote up
def ruleDOWDOM(ts: datetime, dow: Time, dom: Time) -> Time:
    # Monday 5th
    # Find next date at this day of week and day of month
    dm = rrule(MONTHLY, dtstart=ts, byweekday=dow.DOW, bymonthday=dom.day, count=1)[0]
    return Time(year=dm.year, month=dm.month, day=dm.day) 
Example #16
Source File: tradingcalendar_tse.py    From zipline-chinese with Apache License 2.0 5 votes vote down vote up
def get_early_closes(start, end):
    # TSX closed at 1:00 PM on december 24th.

    start = canonicalize_datetime(start)
    end = canonicalize_datetime(end)

    start = max(start, datetime(1993, 1, 1, tzinfo=pytz.utc))
    end = max(end, datetime(1993, 1, 1, tzinfo=pytz.utc))

    # Not included here are early closes prior to 1993
    # or unplanned early closes

    early_close_rules = []

    christmas_eve = rrule.rrule(
        rrule.MONTHLY,
        bymonth=12,
        bymonthday=24,
        byweekday=(rrule.MO, rrule.TU, rrule.WE, rrule.TH, rrule.FR),
        cache=True,
        dtstart=start,
        until=end
    )
    early_close_rules.append(christmas_eve)

    early_close_ruleset = rrule.rruleset()

    for rule in early_close_rules:
        early_close_ruleset.rrule(rule)
    early_closes = early_close_ruleset.between(start, end, inc=True)

    early_closes.sort()
    return pd.DatetimeIndex(early_closes) 
Example #17
Source File: Date.py    From AIL-framework with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_full_month_str(date_from, date_to):
    # add one day (if last day of the month)
    date_to = date_to + relativedelta(days=+1)
    full_month = [dt for dt in rrule(MONTHLY, bymonthday=1,dtstart=date_from, until=date_to)]
    # remove last_month (incomplete)
    if len(full_month):
        full_month = full_month[:-1]
    return full_month 
Example #18
Source File: test_recurrence.py    From django-ical with MIT License 5 votes vote down vote up
def test_ever_month_last_mo(self):
        """Repeat every month on the last Monday."""
        vrecurr = utils.build_rrule_from_text("FREQ=MONTHLY;BYDAY=-1MO")
        assert vrecurr["FREQ"] == ["MONTHLY"]
        assert vrecurr["BYDAY"] == ["-1MO"]
        assert len(vrecurr.keys()) == 2 
Example #19
Source File: test_util.py    From palladium with Apache License 2.0 5 votes vote down vote up
def test_func_raises(self, RruleThread):
        func = Mock(__name__='buggy')
        func.side_effect = KeyError
        thread = RruleThread(func, rrule.rrule(
            rrule.MONTHLY,
            bymonthday=1,
            dtstart=datetime(2014, 10, 30, 13, 21, 18)))
        thread.last_execution = datetime(2014, 10, 30, 13, 21, 18)

        with patch('palladium.util.logger') as logger:
            thread.start()
            sleep(0.02)
            assert func.call_count == 1
            assert logger.exception.call_count == 1 
Example #20
Source File: recurrence.py    From ls.joyous with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def frequency(self):
        """
        How often the recurrence repeats.
        ("YEARLY", "MONTHLY", "WEEKLY", "DAILY")
        """
        freqOptions = ("YEARLY", "MONTHLY", "WEEKLY", "DAILY")
        if self.rule._freq < len(freqOptions):
            return freqOptions[self.rule._freq]
        else:
            return "unsupported_frequency_{}".format(self.rule._freq) 
Example #21
Source File: recurrence.py    From ls.joyous with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __getWhenByMonthday(self, offset, of):
        daysOffset = ""
        d = self.bymonthday[0]
        if d == 1 and offset < 0:
            # bump first day to previous month
            d = offset
            if self.freq != MONTHLY:
                months = [WRAPPED_MONTH_NAMES[m-1] for m in self.bymonth]
                of = " "+_("of {months}").format(months=hrJoin(months))

        elif d == -1 and offset > 0:
            # bump last day to next month
            d = offset
            if self.freq != MONTHLY:
                months = [WRAPPED_MONTH_NAMES[m+1] for m in self.bymonth]
                of = " "+_("of {months}").format(months=hrJoin(months))

        elif 0 < d + offset <= 28:
            # adjust within the month
            d += offset

        else:
            # too complicated don't adjust for any offset
            daysOffset = toDaysOffsetStr(offset)

        theOrdinal = toTheOrdinal(d, inTitleCase=False)
        if daysOffset:
            retval = _("{DaysOffset} {theOrdinal} day")  \
                     .format(DaysOffset=daysOffset, theOrdinal=theOrdinal)
        else:
            TheOrdinal = theOrdinal[0].upper() + theOrdinal[1:]
            retval = _("{TheOrdinal} day").format(TheOrdinal=TheOrdinal)
        retval += of
        return retval 
Example #22
Source File: test_recurrence.py    From django-ical with MIT License 5 votes vote down vote up
def test_every_month_on_the_4th(self):
        """Repeat every month on the 4th."""
        vrecurr = utils.build_rrule_from_text("FREQ=MONTHLY;BYMONTHDAY=4")
        assert vrecurr["FREQ"] == ["MONTHLY"]
        assert vrecurr["BYMONTHDAY"] == [4]
        assert len(vrecurr.keys()) == 2
        vrecurr = utils.build_rrule_from_text("FREQ=MONTHLY;BYMONTHDAY=+4")
        assert vrecurr["FREQ"] == ["MONTHLY"]
        assert vrecurr["BYMONTHDAY"] == [4]
        vRecur(vrecurr).to_ical().decode() == "FREQ=MONTHLY;BYMONTHDAY=4"
        assert len(vrecurr.keys()) == 2 
Example #23
Source File: test_recurrence.py    From django-ical with MIT License 5 votes vote down vote up
def test_every_month_on_the_4th_last(self):
        """Repeat every month on the 4th last."""
        vrecurr = utils.build_rrule(freq="MONTHLY", bymonthday=-4)
        assert vrecurr["FREQ"] == "MONTHLY"
        assert vrecurr["BYMONTHDAY"] == -4
        vRecur(vrecurr).to_ical().decode() == "FREQ=MONTHLY;BYMONTHDAY=-4"
        assert len(vrecurr.keys()) == 2 
Example #24
Source File: test_recurrence.py    From django-ical with MIT License 5 votes vote down vote up
def test_ever_month_3rd_tu(self):
        """Repeat every month on the 3rd Tuesday."""
        vrecurr = utils.build_rrule(freq="MONTHLY", byday="+3TU")
        assert vrecurr["FREQ"] == "MONTHLY"
        assert vrecurr["BYDAY"] == "+3TU"
        vRecur(vrecurr).to_ical().decode() == "FREQ=MONTHLY;BYDAY=+3TU"
        assert len(vrecurr.keys()) == 2 
Example #25
Source File: test_recurrence.py    From django-ical with MIT License 5 votes vote down vote up
def test_every_month_on_the_4th(self):
        """Repeat every month on the 4th."""
        vrecurr = utils.build_rrule(freq="MONTHLY", bymonthday=4)
        assert vrecurr["FREQ"] == "MONTHLY"
        assert vrecurr["BYMONTHDAY"] == 4
        vRecur(vrecurr).to_ical().decode() == "FREQ=MONTHLY;BYMONTHDAY=4"
        assert len(vrecurr.keys()) == 2 
Example #26
Source File: test_recurrence.py    From django-ical with MIT License 5 votes vote down vote up
def test_every_6_months(self):
        """Repeat very 6 months."""
        vrecurr = utils.build_rrule(interval=6, freq="MONTHLY")
        assert vrecurr["FREQ"] == "MONTHLY"
        assert vrecurr["INTERVAL"] == 6
        vRecur(vrecurr).to_ical().decode() == "FREQ=MONTHLY;INTERVAL=6"
        assert len(vrecurr.keys()) == 2 
Example #27
Source File: test_recurrence.py    From django-ical with MIT License 5 votes vote down vote up
def test_every_month(self):
        """Repeat every month."""
        vrecurr = utils.build_rrule(freq="MONTHLY")
        assert vrecurr["FREQ"] == "MONTHLY"
        vRecur(vrecurr).to_ical().decode() == "FREQ=MONTHLY"
        assert len(vrecurr.keys()) == 1 
Example #28
Source File: ocp_report_db_accessor.py    From koku with GNU Affero General Public License v3.0 5 votes vote down vote up
def populate_monthly_cost(self, cost_type, rate_type, rate, start_date, end_date, cluster_id, cluster_alias):
        """
        Populate the monthly cost of a customer.

        Right now this is just the node/month cost. Calculated from
        node_cost * number_unique_nodes.

        args:
            node_cost (Decimal): The node cost per month
            start_date (datetime, str): The start_date to calculate monthly_cost.
            end_date (datetime, str): The end_date to calculate monthly_cost.

        """
        if isinstance(start_date, str):
            start_date = parse(start_date).date()
        if isinstance(end_date, str):
            end_date = parse(end_date).date()

        # usage_start, usage_end are date types
        first_month = datetime.datetime(*start_date.replace(day=1).timetuple()[:3]).replace(tzinfo=pytz.UTC)
        end_date = datetime.datetime(*end_date.timetuple()[:3]).replace(hour=23, minute=59, second=59, tzinfo=pytz.UTC)

        # Calculate monthly cost for each month from start date to end date
        for curr_month in rrule(freq=MONTHLY, until=end_date, dtstart=first_month):
            first_curr_month, first_next_month = month_date_range_tuple(curr_month)
            LOG.info("Populating monthly cost from %s to %s.", first_curr_month, first_next_month)
            if cost_type == "Node":
                if rate is None:
                    self.remove_monthly_cost(first_curr_month, first_next_month, cluster_id, cost_type)
                else:
                    self.upsert_monthly_node_cost_line_item(
                        first_curr_month, first_next_month, cluster_id, cluster_alias, rate_type, rate
                    )
            elif cost_type == "Cluster":
                if rate is None:
                    self.remove_monthly_cost(first_curr_month, first_next_month, cluster_id, cost_type)
                else:
                    self.upsert_monthly_cluster_cost_line_item(
                        first_curr_month, first_next_month, cluster_id, cluster_alias, rate_type, rate
                    ) 
Example #29
Source File: tests_syncer.py    From koku with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_sync_file_fail_no_file(self, mock_boto3):
        """Test syncing a file from one S3 bucket to another fails due to no matching files."""
        source_bucket_name = fake.slug()
        destination_bucket_name = fake.slug()
        schema_name = self.schema

        start_date = date(2019, 1, 1)
        end_date = date(2019, 3, 1)
        date_range = (start_date, end_date)

        end_date = end_date - timedelta(days=1)
        days = rrule(DAILY, dtstart=start_date, until=end_date)
        months = rrule(MONTHLY, dtstart=start_date, until=end_date)

        self.assertNotEqual(source_bucket_name, destination_bucket_name)

        mock_resource = mock_boto3.resource
        mock_buckets = mock_resource.return_value.Bucket
        mock_filter = mock_buckets.return_value.objects.filter
        mock_filter.return_value = ()
        mock_destination_object = mock_buckets.return_value.Object
        mock_copy_from = mock_destination_object.return_value.copy_from

        syncer = AwsS3Syncer(source_bucket_name)
        syncer.sync_bucket(schema_name, destination_bucket_name, date_range)

        mock_resource.assert_called_with("s3", settings.S3_REGION)
        mock_buckets.assert_any_call(source_bucket_name)
        mock_buckets.assert_any_call(destination_bucket_name)

        expected_filter_calls = self.get_expected_filter_calls(schema_name, days, months)
        mock_filter.assert_has_calls(expected_filter_calls, any_order=True)
        self.assertEqual(len(mock_filter.call_args_list), len(expected_filter_calls))

        mock_destination_object.assert_not_called()
        mock_copy_from.assert_not_called() 
Example #30
Source File: test_recurrence.py    From django-ical with MIT License 5 votes vote down vote up
def test_ever_month_last_mo(self):
        """Repeat every month on the last Monday."""
        vrecurr = utils.build_rrule(freq="MONTHLY", byday="-1MO")
        assert vrecurr["FREQ"] == "MONTHLY"
        assert vrecurr["BYDAY"] == "-1MO"
        vRecur(vrecurr).to_ical().decode() == "FREQ=MONTHLY;BYDAY=-1MO"
        assert len(vrecurr.keys()) == 2