from datetime import datetime, date, time, timedelta def get_prize_for(start, end, pph=0, hh_start=None, hh_end=None, pphh=0): """ calculate prize of billard table rental :param start: start datetime of rental :param end: end datetime of rental :param pph: prize per hour of rental :param hh_start: start of happy hour :param hh_end: end of happy hour :param pphh: prize per hour in happy hour :return: the calculated prize of rental """ if end <= start: raise ValueError('end date must be after start date') prize = 0 prize_normal = 0 prize_hh = 0 if hh_start and hh_end and pphh: d = start.date() t = start.time() end_date = end.date() end_time = end.time() while True: if t < hh_start: if end_time < hh_start and d == end_date: p = calculate_prize_for(start=t, end=end_time, pph=pph) prize += p prize_normal += p break else: p = calculate_prize_for(start=t, end=hh_start, pph=pph) prize += p prize_normal += p t = hh_start continue elif hh_start <= t < hh_end: if end_time < hh_end and d == end_date: p = calculate_prize_for(start=t, end=end_time, pph=pphh) prize += p prize_hh += p break else: p = calculate_prize_for(start=t, end=hh_end, pph=pphh) prize += p prize_hh += p t = hh_end continue else: if d == end_date: p = calculate_prize_for(start=t, end=end_time, pph=pph) prize += p prize_normal += p break else: p = calculate_prize_for(start=t, end=datetime.strptime('23:59:59', '%H:%M:%S').time(), pph=pph) prize += p prize_normal += p t = datetime.strptime('00:00:00', '%H:%M:%S').time() d = (datetime.combine(d, t) + timedelta(days=1)).date() continue else: prize = calculate_prize_for(start=start, end=end, pph=pph) prize_normal = prize return prize, prize_normal, prize_hh def calculate_prize_for(start, end, pph=0): pps = pph / 3600 d = date.today() if isinstance(start, datetime): seconds = (end - start).seconds else: seconds = (datetime.combine(d, end) - datetime.combine(d, start)).seconds return round((pps * seconds), 1)