import time
from datetime import datetime, timedelta


def date_to_timestamp(dt):
    return time.mktime(dt.timetuple())


def get_expiration_time(timestamp, duration):
    #
    now_date = datetime.fromtimestamp(timestamp)
    exp_date = now_date.replace(second=0, microsecond=0)
    if (int(date_to_timestamp(exp_date + timedelta(minutes=1))) - timestamp) > 30:
        exp_date = exp_date + timedelta(minutes=1)

    else:
        exp_date = exp_date + timedelta(minutes=2)
    exp = []
    for _ in range(5):
        exp.append(date_to_timestamp(exp_date))
        exp_date = exp_date + timedelta(minutes=1)

    idx = 50
    index = 0
    now_date = datetime.fromtimestamp(timestamp)
    exp_date = now_date.replace(second=0, microsecond=0)
    while index < idx:
        if int(exp_date.strftime("%M")) % 15 == 0 and (int(date_to_timestamp(exp_date)) - int(timestamp)) > 60 * 5:
            exp.append(date_to_timestamp(exp_date))
            index = index + 1
        exp_date = exp_date + timedelta(minutes=1)

    remaning = []

    for t in exp:
        remaning.append(int(t) - int(time.time()))

    close = [abs(x - 60 * duration) for x in remaning]

    return int(exp[close.index(min(close))]), int(close.index(min(close)))
