def get_default_booking_interval(duration=90, precision=15, force_today=False): """Get the default booking interval for a room. Returns the default booking interval for a room as a tuple containing the start and end times as `datetime` objects. The start time is the default working start time or the current time (if the working start time is in the past); rounded up to the given precision in minutes (15 by default). The end time corresponds to the start time plus the given duration in minutes. If the booking ends after the end of work time, it is automatically moved to the next day. :param duration: int -- The duration of a booking in minutes (must be greater than 1) :param precision: int -- The number of minutes by which to round up the current time for the start time of a booking. Negative values are allowed but will round the time down and create a booking starting in the past. :param force_today: Forces a booking to be for today, even if it past the end of work time. This is ignored if the current time is either after 23:50 or within the amount of minutes of the precision from midnight. For example with a precision of 30 minutes, if the current time is 23:42 then the meeting will be the following day. :returns: (datetime, datetime, bool) -- A tuple with the start and end times of the booking and a boolean which is `True` if the date was changed from today and `False` otherwise. :raises: ValueError if the duration is less than 1 minute """ if duration < 1: raise ValueError( "The duration must be strictly positive (got {} min)".format( duration)) date_changed = False work_start = datetime.combine(date.today(), Location.working_time_periods[0][0]) work_end = datetime.combine(date.today(), Location.working_time_periods[-1][1]) start_dt = max(work_start, round_up_to_minutes(datetime.now(), precision=precision)) end_dt = start_dt + timedelta(minutes=duration) if end_dt.date() > start_dt.date(): end_dt = get_day_end(start_dt.date()) if ((not force_today and start_dt > work_end) or start_dt.date() > date.today() or end_dt - start_dt < timedelta(minutes=10)): date_changed = True start_dt = work_start + timedelta(days=1) end_dt = start_dt + timedelta(minutes=duration) return start_dt, end_dt, date_changed
def get_default_booking_interval(duration=90, precision=15, force_today=False): """Get the default booking interval for a room. Returns the default booking interval for a room as a tuple containing the start and end times as `datetime` objects. The start time is the default working start time or the current time (if the working start time is in the past); rounded up to the given precision in minutes (15 by default). The end time corresponds to the start time plus the given duration in minutes. If the booking ends after the end of work time, it is automatically moved to the next day. :param duration: int -- The duration of a booking in minutes (must be greater than 1) :param precision: int -- The number of minutes by which to round up the current time for the start time of a booking. Negative values are allowed but will round the time down and create a booking starting in the past. :param force_today: Forces a booking to be for today, even if it past the end of work time. This is ignored if the current time is either after 23:50 or within the amount of minutes of the precision from midnight. For example with a precision of 30 minutes, if the current time is 23:42 then the meeting will be the following day. :returns: (datetime, datetime, bool) -- A tuple with the start and end times of the booking and a boolean which is `True` if the date was changed from today and `False` otherwise. :raises: ValueError if the duration is less than 1 minute """ if duration < 1: raise ValueError("The duration must be strictly positive (got {} min)".format(duration)) date_changed = False work_start = datetime.combine(date.today(), Location.working_time_periods[0][0]) work_end = datetime.combine(date.today(), Location.working_time_periods[-1][1]) start_dt = max(work_start, round_up_to_minutes(datetime.now(), precision=precision)) end_dt = start_dt + timedelta(minutes=duration) if end_dt.date() > start_dt.date(): end_dt = get_day_end(start_dt.date()) if ((not force_today and start_dt > work_end) or start_dt.date() > date.today() or end_dt - start_dt < timedelta(minutes=10)): date_changed = True start_dt = work_start + timedelta(days=1) end_dt = start_dt + timedelta(minutes=duration) return start_dt, end_dt, date_changed
def _update_datetime(self): """Make necessary changes to reservation's start and end datetime. Move the start date to the current day, adjust the end date accordingly and if the start datetime is still in the past, change the start and end time to the current (rounded up) time. :return: A dict with the new start and end datetime """ reservation_duration = self._reservation.end_dt - self._reservation.start_dt date_delta = date.today() - self._reservation.start_dt.date() start_dt = self._reservation.start_dt + date_delta end_dt = start_dt + reservation_duration if start_dt < datetime.now(): new_start_dt = round_up_to_minutes(datetime.now(), 15) + timedelta(minutes=15) time_delta = new_start_dt - start_dt start_dt = new_start_dt end_dt = end_dt + time_delta return {'start_dt': start_dt, 'end_dt': end_dt}