def extract_weeks_offset(tokens, end=None, key_tokens=(
        'next', 'previous', 'last', 'upcoming', 'past', 'prev')):
    """Extract the number of week offsets needed.

    >>> extract_weeks_offset(['next', 'next', 'week'])
    (0, ['week'], 2)
    >>> extract_weeks_offset(['upcoming', 'Monday'])
    (0, ['Monday'], 0)
    >>> extract_weeks_offset(['last', 'Monday'])
    (0, ['Monday'], -1)
    >>> extract_weeks_offset(['past', 'Tuesday'])
    (0, ['Tuesday'], -1)
    >>> extract_weeks_offset(['past', 'Wed', 'next', 'week'], end=1)
    (0, ['Wed', 'next', 'week'], -1)
    """
    offset = 0
    end = len(tokens) - 1 if end is None else end
    start = end - 1
    if start < 0 or start >= len(tokens):
        return 0, tokens, 0

    while len(tokens) > start >= 0 and \
            tokens[start] in key_tokens:
        candidate = tokens[start]
        if candidate == 'upcoming':
            return start, tokens[:end-1] + tokens[end:], 0
        if candidate == 'next':
            offset += 1
        elif candidate in ('previous', 'prev', 'last', 'past'):
            offset -= 1
        start -= 1
    return start + 1, tokens[:start + 1] + tokens[end:], offset