def localize_datetime(dt, tz):
    """
    Localize a timezone naive datetime. Handles pytz timezones correctly.

    """
    try:
        return tz.localize(dt)
    except AttributeError:
        return dt.replace(tzinfo=tz)
