def __str__(self): dstart = 'DTSTART:{}'.format(to_ical(self._dtstart)) rules = [] if self._freq not in range(len(rrule.FREQNAMES)): raise Exception('Invalid freq "{}"'.format(self._freq)) rules.append('FREQ=' + rrule.FREQNAMES[self._freq]) for name, value in [('INTERVAL', self._interval), ('WKST', self._wkst), ('COUNT', self._count)]: if value is not None: if name == 'WKST': value = ['MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU'][value] rules.append('{}={}'.format(name, value)) if self._until: rules.append('UNTIL={}'.format(to_ical(self._until))) for name, value in [('BYSETPOS', self._bysetpos), ('BYMONTH', self._bymonth), ('BYMONTHDAY', self._bymonthday), ('BYYEARDAY', self._byyearday), ('BYWEEKNO', self._byweekno), ('BYWEEKDAY', self._byweekday), ('BYHOUR', self._byhour), ('BYMINUTE', self._byminute), ('BYSECOND', self._bysecond), ]: if name == "BYWEEKDAY" and value: value = (rrule.weekdays[num] for num in value) if value: rules.append(name + '=' + ','.join(str(v) for v in value)) return '{0} RRULE:{1}'.format(dstart, ';'.join(rules))
def test_until(): dtstart = datetime.utcnow() until = dtstart + relativedelta(years=100) rule = RRule(freq=rrule.YEARLY, dtstart=dtstart, until=until) expected_rrule = ( 'RRULE:FREQ=YEARLY;INTERVAL=1;WKST=MO;UNTIL={1};BYMONTH={0.month};' 'BYMONTHDAY={0.day};BYHOUR={0.hour};BYMINUTE={0.minute};BYSECOND={0.second}' ) assert str(rule) == 'DTSTART:{0} {1}'.format( to_ical(dtstart), expected_rrule.format(dtstart, to_ical(until)))
def test_count(): dtstart = datetime.utcnow() rule = RRule(freq=rrule.YEARLY, dtstart=dtstart, count=10) expected_rrule = ( 'RRULE:FREQ=YEARLY;INTERVAL=1;WKST=MO;COUNT=10;BYMONTH={0.month};' 'BYMONTHDAY={0.day};BYHOUR={0.hour};BYMINUTE={0.minute};BYSECOND={0.second}' ) assert str(rule) == 'DTSTART:{0} {1}'.format( to_ical(dtstart), expected_rrule.format(dtstart))
def test_to_ical(): now = datetime.utcnow() ical_datetime = utils.to_ical(now) date = str(now.date()).replace('-', '') time = str(now.time()).split('.')[0].replace(':', '') assert ical_datetime == '{}T{}Z'.format(date, time)
def test_int_frequency(frequency, expected_rrule): dtstart = datetime.utcnow() rule = RRule(freq=frequency, dtstart=dtstart) weekday_str = ['MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU'][dtstart.weekday()] assert str(rule) == 'DTSTART:{0} {1}'.format( to_ical(dtstart), expected_rrule.format(dtstart, weekday_str))