import re


def parse_ancestor_offsets(date_arg):
    """Parse any ancestor offsets in date runtime argument.

    The date argument passed in doesn't *need* to have any of these
    offsets.

    Args:
        date_arg: A string containing a date argument to be
            parsed.

    Returns:
        A two-tuple containing the passed in date argument with the
        offest portion removed and an integer N specifying that the date
        argument's offsets have said to use the Nth ancestor of the
        passed in date.
    """
    tilde_pattern = r".*~(-?\d*)$"
    offset = 0

    while True:
        # Test for caret
        if date_arg.endswith("^"):
            offset += 1

            date_arg = date_arg[:-1]

            continue

        # Test for tilde offsetting
        regex_match = re.match(tilde_pattern, date_arg)

        if regex_match and regex_match.group(1):
            offset_str = regex_match.group(1)

            offset += int(offset_str)

            date_arg = date_arg[: -(len(offset_str) + 1)]

            continue

        # No offsets left to parse
        return (date_arg, offset)