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 if hh_start is not None and hh_end is not None and pphh is not None: 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: prize += calculate_prize_for(start=t, end=end_time, pph=pph) break else: prize += calculate_prize_for(start=t, end=hh_start, pph=pph) t = hh_start elif hh_start <= t < hh_end: if end_time < hh_end and d == end_date: prize += calculate_prize_for(start=t, end=end_time, pph=pphh) break else: prize += calculate_prize_for(start=t, end=hh_end, pph=pphh) t = hh_end else: if d == end_date: prize += calculate_prize_for(start=t, end=end_time, pph=pph) break else: prize += calculate_prize_for(start=t, end=datetime.strptime('23:59:59', '%H:%M:%S').time(), pph=pph) t = datetime.strptime('00:00:00', '%H:%M:%S').time() d = (datetime.combine(d, t) + timedelta(days=1)).date() else: prize = calculate_prize_for(start=start, end=end, pph=pph) return prize def calculate_prize_for(start, end, pph=0): pps = pph / 3600 d = date.today() seconds = 0 if isinstance(start, datetime): seconds = (end - start).seconds else: seconds = (datetime.combine(d, end) - datetime.combine(d, start)).seconds return round((pps * seconds), 1)