def ensure_tz(tz):
    """
    Attempts to convert an object to a time zone.

    @rtype
      `datetime.tzinfo`
    """
    if isinstance(tz, datetime.tzinfo):
        return tz

    if tz is None:
        return UTC

    if isinstance(tz, str):
        try:
            return pytz.timezone(tz)
        except pytz.exceptions.UnknownTimeZoneError:
            raise ValueError("not a time zone: {}".format(tz))

    raise TypeError("not a time zone: {!r}".format(tz))
dataset/ETHPy150Open twosigma/ngrid/ngrid/datetime.py/ensure_tz/original