import pytz
from datetime import datetime


def UTCSecondToLocalDatetime(utcsec, timezone="Europe/Berlin"):
    """
    Convert utc second to local datetime (specify correct local timezone with string).

    Parameters
    ----------
    utcsec: int
      Time in utc seconds (unix time).

    timezone: string
      Time zone string compatible with pytz format

    Returns
    -------
      dt: Datetime object corresponding to local time.

    Notes
    -----
    Adapted from a stackoverflow answer.

    To list all available time zones:
    >> import pytz
    >> pytz.all_timezones

    To print the returned datetime object in a certain format:
    >> from pyik.time_conversion import UTCSecondToLocalDatetime
    >> dt = UTCSecondToLocalDatetime(1484314559)
    >> dt.strftime("%d/%m/%Y, %H:%M:%S")
    """


    local_tz = pytz.timezone(timezone)
    utc_dt = datetime.utcfromtimestamp(utcsec)
    local_dt = utc_dt.replace(tzinfo=pytz.utc).astimezone(local_tz)

    return local_tz.normalize(local_dt)