def timezone_from_str(tz_str):
    """
    Convert a timezone string to a timezone object.

    :param tz_str: string with format 'Asia/Shanghai' or 'UTC±[hh]:[mm]'
    :return: a timezone object (tzinfo)
    """
    m = re.match(r'UTC([+|-]\d{1,2}):(\d{2})', tz_str)
    if m:
        # in format 'UTC±[hh]:[mm]'
        delta_h = int(m.group(1))
        delta_m = int(m.group(2)) if delta_h >= 0 else -int(m.group(2))
        return timezone(timedelta(hours=delta_h, minutes=delta_m))

    # in format 'Asia/Shanghai'
    try:
        return pytz.timezone(tz_str)
    except pytz.exceptions.UnknownTimeZoneError:
        return None
def timezone_from_str ( tz_str ) : m = re . match ( r'UTC([+|-]\d{1,2}):(\d{2})' , tz_str ) if m : delta_h = int ( m . group ( 1 ) ) delta_m = int ( m . group ( 2 ) ) if delta_h >= 0 else - int ( m . group ( 2 ) ) return timezone ( timedelta ( hours = delta_h , minutes = delta_m ) ) try : return pytz . timezone ( tz_str ) except pytz . exceptions . UnknownTimeZoneError : return None
Convert a timezone string to a timezone object.

    :param tz_str: string with format 'Asia/Shanghai' or 'UTC±[hh]:[mm]'
    :return: a timezone object (tzinfo)