def _localize(dt, tzinfo):
    """
    :return: A localized datetime
    :paramd dt: Naive datetime
    :param tzinfo: A pytz or python time zone object

    Attempts to localize the datetime using pytz if possible. Falls back to the builtin
    timezone handling otherwise.
    """
    try:
        return tzinfo.localize(dt)
    except AttributeError:
        return dt.replace(tzinfo=tzinfo)