Home > Enterprise >  find an open slot in google calendar api between time
find an open slot in google calendar api between time

Time:05-12

i am struggling to iterate over a time interval in google calendar api and check for an open slot, for example: i have a time interval from 12:10 pm to 6:30 pm and there are 3 events from 12:10 to 3 pm and 3:10 to 4:25 and 5: 00 to 6:20 pm, on that day and the event i want to check has time duration of 25 minutes and i want to iterate over that time period, and possibly place it between 4:25pm to 5:50pm. i have tried this but its not correct way:

def set_window_filter(title, windowx1, windowx2, event_duration, calendar, next_day):
    schedule_date = None
    if (windowx1 is not None) and (windowx2 is not None):

        x = datetime.strptime(
            windowx1, '%I:%M %p')

        y = datetime.strptime(
            windowx2, '%I:%M %p')

        start_date = datetime.combine(
            next_day.date(), x.time())
        
        event_between_date = start_date
        
        event_end_date = event_between_date   timedelta(minutes=event_duration)
        
        end_date = datetime.combine(
            next_day.date(), y.time())
        
        while event_end_date <= end_date:
            if event_end_date >= end_date - timedelta(minutes=(event_duration)):
                return None, False
            
            if event_between_date < event_end_date:
                event_value, events = calendar.calendar_event_func(
                    event_between_date, event_end_date)
                print('event_value, events: ', event_value, events)
            
            if event_value is None:
                print('event_value: ', event_value)
                schedule_date = event_between_date.strftime(
                    '%A, %d, %B, %Y       %I:%M %p')
                return schedule_date, False

            else:
                event_between_date  = timedelta(minutes=10)
                event_end_date  = timedelta(minutes=10)

basically windowx1 and windowx2 are input of time period like 1 PM TO 5 PM

event_value, events = calendar.calendar_event_func(
                    event_between_date, event_end_date)

this function will return event value(i.e True) and events list if finds any events between the given time period and None and [] if none found, what could be a better way than this.

CodePudding user response:

This function below will find the earliest available time when a meeting of a desired duration can be scheduled. For example, if the desired window is from 5 to 10, the desired duration is 1 hour and there is an existing meeting from 5:30 to 6:45, the function will return the 6:45 time.

def findFirstOpenSlot(events,startTime,endTime,duration):

    def parseDate(rawDate):
        #Transform the datetime given by the API to a python datetime object.
        return datetime.datetime.strptime(rawDate[:-6]  rawDate[-6:].replace(":",""), '%Y-%m-%dT%H:%M:%S%z')

    eventStarts = [parseDate(e['start'].get('dateTime', e['start'].get('date'))) for e in events]
    eventEnds = [parseDate(e['end'].get('dateTime', e['end'].get('date'))) for e in events]

    gaps = [start-end for (start,end) in zip(eventStarts[1:], eventEnds[:-1])]
    
    if startTime   duration < eventStarts[0]:
        #A slot is open at the start of the desired window.
        return startTime

    for i, gap in enumerate(gaps):
        if gap > duration:
            #This means that a gap is bigger than the desired slot duration, and we can "squeeze" a meeting.
            #Just after that meeting ends.
            return eventEnds[i]

    #If no suitable gaps are found, return none.
    return None


The function parameters are as follows:

  • events: a list of raw event objects as obtained via events.get.
  • startTime , endTime: The start and end of the desired window where the new event should be placed, as a python datetime.
  • duration: the duration of the new event, as a python timedelta.

The function will return None if it is impossible to schedule the meeting. With that, you can proceed to create the new event with event.insert.

  • Related