def _encodeRelativeValidityPeriod(validityPeriod):
    """ Encodes the specified relative validity period timedelta into an integer for use in an SMS PDU
    (based on the table in section 9.2.3.12 of GSM 03.40)

    :param validityPeriod: The validity period to encode
    :type validityPeriod: datetime.timedelta
    :rtype: int
    """
    seconds = validityPeriod.seconds + validityPeriod.days * 24 * 3600
    if seconds <= 43200:
        tpVp = int(seconds / 300) - 1
    elif seconds <= 86400:
        tpVp = int((seconds - 43200) / 1800) + 143
    elif validityPeriod.days <= 30:
        tpVp = validityPeriod.days + 166
    elif validityPeriod.days <= 441:
        tpVp = int(validityPeriod.days / 7) + 192
    else:
        raise ValueError(
            'Validity period too long; tpVp limited to 1 octet (max value: 255)'
            )
    return tpVp
