def rrule_to_schedule(rrule, dtstart=None):
"""
Convert an rrule data structure to schedule
"""
if not hasattr(rrule, '_freq'):
raise Exception("Missing _freq")
if rrule._freq + 1 > len(FREQUENCY_STRUCT):
raise Exception("Can't do that frequency.")
schedule = FREQUENCY_STRUCT[rrule._freq](rrule)
past = rrule._until and tz.now().date() > rrule._until.date()
upcoming = dtstart is not None and tz.now().date() < dtstart
current = not upcoming and not past
schedule_type = ''
if past:
schedule_type = 'past'
elif upcoming:
schedule_type = 'upcoming'
elif current:
schedule_type = 'current'
schedule = schedule._replace(schedule_type=schedule_type)
if rrule._until:
if dtstart is not None:
schedule = schedule._replace(date_range=" - ".join([datetime.datetime.strftime(dtstart, DATE_FORMAT), rrule._until.strftime(DATE_FORMAT)]))
else:
schedule = schedule._replace(date_range=" ".join([_('until'), rrule._until.strftime(DATE_FORMAT)]))
schedule = schedule._replace(end_date=rrule._until.date())
# elif rrule._count:
# text.extend([' ', _('for'), ' ', str(rrule._count), ])
# text.extend([' ', gettext.ngettext('time', 'times', rrule._count)])
return schedule
评论列表
文章目录