def parse_runtime_limit(value, now=None):
def extract_time(value):<EOL><INDENT>value = _RE_RUNTIMELIMIT_HOUR.search(value).groupdict()<EOL>return timedelta(hours=int(value['<STR_LIT>']),<EOL>minutes=int(value['<STR_LIT>']))<EOL><DEDENT>def extract_weekday(value):<EOL><INDENT>key = value[:<NUM_LIT:3>].lower()<EOL>try:<EOL><INDENT>return {<EOL>'<STR_LIT>': <NUM_LIT:0>,<EOL>'<STR_LIT>': <NUM_LIT:1>,<EOL>'<STR_LIT>': <NUM_LIT:2>,<EOL>'<STR_LIT>': <NUM_LIT:3>,<EOL>'<STR_LIT>': <NUM_LIT:4>,<EOL>'<STR_LIT>': <NUM_LIT:5>,<EOL>'<STR_LIT>': <NUM_LIT:6>,<EOL>}[key]<EOL><DEDENT>except KeyError:<EOL><INDENT>raise ValueError("<STR_LIT>" % value)<EOL><DEDENT><DEDENT>if now is None:<EOL><INDENT>now = datetime.now()<EOL><DEDENT>today = now.date()<EOL>g = _RE_RUNTIMELIMIT_FULL.search(value)<EOL>if not g:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % value)<EOL><DEDENT>pieces = g.groupdict()<EOL>if pieces['<STR_LIT>'] is None:<EOL><INDENT>first_occasion_day = timedelta(days=<NUM_LIT:0>)<EOL>next_occasion_delta = timedelta(days=<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>if pieces['<STR_LIT>'] is None:<EOL><INDENT>pieces['<STR_LIT>'] = pieces['<STR_LIT>']<EOL><DEDENT>weekday_begin = extract_weekday(pieces['<STR_LIT>'])<EOL>weekday_end = extract_weekday(pieces['<STR_LIT>'])<EOL>if weekday_begin <= today.weekday() <= weekday_end:<EOL><INDENT>first_occasion_day = timedelta(days=<NUM_LIT:0>)<EOL><DEDENT>else:<EOL><INDENT>days = (weekday_begin - today.weekday()) % <NUM_LIT:7><EOL>first_occasion_day = timedelta(days=days)<EOL><DEDENT>weekday = (now + first_occasion_day).weekday()<EOL>if weekday < weekday_end:<EOL><INDENT>next_occasion_delta = timedelta(days=<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>days = weekday_begin - weekday + <NUM_LIT:7><EOL>next_occasion_delta = timedelta(days=days)<EOL><DEDENT><DEDENT>if pieces['<STR_LIT>'] is None:<EOL><INDENT>pieces['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>if pieces['<STR_LIT>'] is None:<EOL><INDENT>pieces['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>beginning_time = extract_time(pieces['<STR_LIT>'])<EOL>ending_time = extract_time(pieces['<STR_LIT>'])<EOL>if not ending_time:<EOL><INDENT>ending_time = beginning_time + timedelta(days=<NUM_LIT:1>)<EOL><DEDENT>elif beginning_time and ending_time and beginning_time > ending_time:<EOL><INDENT>ending_time += timedelta(days=<NUM_LIT:1>)<EOL><DEDENT>start_time = real_datetime.combine(today, real_time(hour=<NUM_LIT:0>, minute=<NUM_LIT:0>))<EOL>current_range = (<EOL>start_time + first_occasion_day + beginning_time,<EOL>start_time + first_occasion_day + ending_time<EOL>)<EOL>if now > current_range[<NUM_LIT:1>]:<EOL><INDENT>current_range = tuple(t + next_occasion_delta for t in current_range)<EOL><DEDENT>future_range = (<EOL>current_range[<NUM_LIT:0>] + next_occasion_delta,<EOL>current_range[<NUM_LIT:1>] + next_occasion_delta<EOL>)<EOL>return current_range, future_range<EOL>
Parsing CLI option for runtime limit, supplied as VALUE.

    Value could be something like: Sunday 23:00-05:00, the format being
    [Wee[kday]] [hh[:mm][-hh[:mm]]].
    The function will return two valid time ranges. The first could be in the
    past, containing the present or in the future. The second is always in the
    future.