Python dateutil.rrule.rruleset() Examples

The following are 12 code examples of dateutil.rrule.rruleset(). 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: 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 #2
Source File: tradingcalendar_bmf.py    From zipline-chinese with Apache License 2.0 6 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)

    early_close_rules = []

    early_close_rules.append(quarta_cinzas)

    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 #3
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 #4
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 #5
Source File: ical.py    From autosuspend with GNU General Public License v2.0 5 votes vote down vote up
def _expand_rrule_all_day(
    rrule: str, start: date, exclusions: Iterable, start_at: datetime, end_at: datetime
) -> Iterable[date]:
    """Expand an rrule for all-day events.

    To my mind, these events cannot have changes, just exclusions, because
    changes only affect the time, which doesn't exist for all-day events.
    """

    rules = rruleset()
    rules.rrule(rrulestr(rrule, dtstart=start, ignoretz=True))

    # add exclusions
    if exclusions:
        for xdate in exclusions:
            rules.exdate(datetime.combine(xdate.dts[0].dt, datetime.min.time()))

    dates = []
    # reduce start and end to datetimes without timezone that just represent a
    # date at midnight.
    for candidate in rules.between(
        datetime.combine(start_at.date(), datetime.min.time()),
        datetime.combine(end_at.date(), datetime.min.time()),
        inc=True,
    ):
        dates.append(candidate.date())
    return dates 
Example #6
Source File: speed_comparison.py    From py-business-calendar with MIT License 5 votes vote down vote up
def init_rruleset():
    rr = rruleset()
    rr.rrule(rrule(DAILY,
                   byweekday=(MO,TU,WE,TH,FR),
                   dtstart=datetime.datetime(2010,1,1)))
    for h in holidays:
        rr.exdate(h)
    return rr 
Example #7
Source File: test_business_calendar.py    From py-business-calendar with MIT License 5 votes vote down vote up
def __init__(self):
        BaseCalendarTest.__init__(self)
        self.cal = Calendar()
        rr = rruleset()
        rr.rrule(rrule(DAILY,
                       byweekday=(MO,TU,WE,TH,FR),
                       dtstart=datetime.datetime(2010,1,1)))
        self.rr = rr
        self.dates = rr.between(datetime.datetime(2010,1,1),
                                datetime.datetime(2013,12,31),
                                inc=True) 
Example #8
Source File: test_business_calendar.py    From py-business-calendar with MIT License 5 votes vote down vote up
def __init__(self):
        BaseCalendarTest.__init__(self)
        self.cal = Calendar(workdays=[0,1,4,6])
        rr = rruleset()
        rr.rrule(rrule(DAILY,
                       byweekday=(MO,TU,FR,SU),
                       dtstart=datetime.datetime(2010,1,1)))
        self.rr = rr
        self.dates = rr.between(datetime.datetime(2010,1,1),
                                datetime.datetime(2013,12,31),
                                inc=True) 
Example #9
Source File: test_business_calendar.py    From py-business-calendar with MIT License 5 votes vote down vote up
def __init__(self):
        BaseCalendarTest.__init__(self)
        self.holidays = [parse(x) for x in global_holidays.split('\n')]
        self.cal = Calendar(holidays=self.holidays)
        self.cal.warn_on_holiday_exhaustion = False
        rr = rruleset()
        rr.rrule(rrule(DAILY,
                       byweekday=(MO,TU,WE,TH,FR),
                       dtstart=datetime.datetime(2010,1,1)))
        for h in self.holidays:
            rr.exdate(h)
        self.rr = rr
        self.dates = rr.between(datetime.datetime(2010,1,1),
                                datetime.datetime(2013,12,31),
                                inc=True) 
Example #10
Source File: test_business_calendar.py    From py-business-calendar with MIT License 5 votes vote down vote up
def __init__(self):
        BaseCalendarTest.__init__(self)
        self.holidays = [parse(x) for x in global_holidays.split('\n')]
        self.cal = Calendar(workdays=[0,1,4,6], holidays=self.holidays)
        rr = rruleset()
        rr.rrule(rrule(DAILY,
                       byweekday=(MO,TU,FR,SU),
                       dtstart=datetime.datetime(2010,1,1)))
        for h in self.holidays:
            rr.exdate(h)
        self.rr = rr
        self.dates = rr.between(datetime.datetime(2010,1,1),
                                datetime.datetime(2013,12,31),
                                inc=True) 
Example #11
Source File: ical.py    From autosuspend with GNU General Public License v2.0 4 votes vote down vote up
def _expand_rrule(
    rrule: str,
    start: datetime,
    instance_duration: timedelta,
    exclusions: Iterable,
    changes: Iterable[icalendar.cal.Event],
    start_at: datetime,
    end_at: datetime,
) -> Sequence[datetime]:

    # unify everything to a single timezone and then strip it to handle DST
    # changes correctly
    orig_tz = start.tzinfo
    start = start.replace(tzinfo=None)
    start_at = start_at.astimezone(orig_tz).replace(tzinfo=None)
    end_at = end_at.astimezone(orig_tz).replace(tzinfo=None)

    rules = rruleset()
    first_rule = rrulestr(rrule, dtstart=start, ignoretz=True)

    # apply the same timezone logic for the until part of the rule after
    # parsing it.
    if first_rule._until:
        first_rule._until = (
            pytz.utc.localize(first_rule._until)
            .astimezone(orig_tz)
            .replace(tzinfo=None)
        )

    rules.rrule(first_rule)

    # add exclusions
    if exclusions:
        for xdate in exclusions:
            try:
                # also in this case, unify and strip the timezone
                rules.exdate(xdate.dts[0].dt.astimezone(orig_tz).replace(tzinfo=None))
            except AttributeError:
                pass

    # add events that were changed
    for change in changes:
        # same timezone mangling applies here
        rules.exdate(
            change.get("recurrence-id").dt.astimezone(orig_tz).replace(tzinfo=None)
        )

    # expand the rrule
    dates = []
    for candidate in rules.between(start_at - instance_duration, end_at, inc=True):
        localized = orig_tz.localize(candidate)  # type: ignore
        dates.append(localized)
    return dates 
Example #12
Source File: icalparser.py    From icalevents with MIT License 4 votes vote down vote up
def parse_rrule(component, tz=UTC):
    """
    Extract a dateutil.rrule object from an icalendar component. Also includes
    the component's dtstart and exdate properties. The rdate and exrule
    properties are not yet supported.

    :param component: icalendar component
    :param tz: timezone for DST handling
    :return: extracted rrule or rruleset
    """
    if component.get('rrule'):
        # component['rrule'] can be both a scalar and a list
        rrules = component['rrule']
        if not isinstance(rrules, list):
            rrules = [rrules]

        # If dtstart is a datetime, make sure it's in a timezone.
        rdtstart = component['dtstart'].dt
        if type(rdtstart) is datetime:
            rdtstart = normalize(rdtstart, tz=tz)

        # Parse the rrules, might return a rruleset instance, instead of rrule
        rule = rrulestr('\n'.join(x.to_ical().decode() for x in rrules),
                        dtstart=rdtstart)

        if component.get('exdate'):
            # Make sure, to work with a rruleset
            if isinstance(rule, rrule):
                rules = rruleset()
                rules.rrule(rule)
                rule = rules

            # Add exdates to the rruleset
            for exd in extract_exdates(component):
                rule.exdate(exd)

        # TODO: What about rdates and exrules?

    # You really want an rrule for a component without rrule? Here you are.
    else:
        rule = rruleset()
        rule.rdate(normalize(component['dtstart'].dt, tz=tz))

    return rule