Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an 'all day' event using google calendar api in python

I am using google calendars api to add events to a google calendar from another website. I am running into a problem though when I attempt to create an all-day type of event. I can easily add a datetime.timedelta(days=1) to a datetime object that starts at 12am, but that creates an event that begins at 00:00 and ends on 23:59 not an all-day event (there is a difference)

there have been similar questions addressed here in the languages of C# and JAVA But none in python

google apis event creation documentation is here. In Python, basically you submit a simple json object to create an event with the event parameters defined.

Here is my code attempting an all-day event:

creds = customer.get_calendar_creds()
http = creds.authorize(httplib2.Http())
service = discovery.build('calendar', 'v3', http=http)
calendar = service.calendars().get(calendarId=customer.calendar_id or 'primary').execute()
tz_string = calendar['timeZone']
gc_tz = pytz.timezone(tz_string)
start_time = gc_tz.localize(parser.parse(calendar_event_form.cleaned_data['date']))
end_time = start_time + datetime.timedelta(days=1)
start_string = start_time.strftime("%Y-%m-%dT%H:%M:%S.%f%z")
start_string = start_string[:-8] + start_string[-5:]  # removing miliseconds
end_string = end_time.strftime("%Y-%m-%dT%H:%M:%S.%f%z")
end_string = end_string[:-8] + end_string[-5:]
event = {
    'summary': calendar_event_form.cleaned_data['name'],
    'location': calendar_event_form.cleaned_data['location'],
    'description': calendar_event_form.cleaned_data['description'],
    'start': {
        'dateTime': start_string,
        'timeZone': tz_str,
    },
    'end': {
        'dateTime': end_string,
        'timeZone': tz_str,
    },
}
event = service.events().insert(calendarId=customer.calendar_id or 'primary', body=event).execute()

This code 'works' but there is a distinct difference between an event that I create using this an one that I create on google calendars and mark as an all day event. Here is a simple image that shows the difference:enter image description here

Things I have tried:

  • defining only a start time with no end time (broke)

  • sending date only as a string '2015-07-13' for start and '2015-7-14' for end (broke)

  • sending '2015-07-13T00:00:00-0700' as start and both '2015-07-14T00:00:00-0700' and '2015-07-13T23:59:59-0700' as end (one finished on same day, the other went 1 second into the next)

I came here with the hopes that someone had tackled this in the past, I cant be the only one using python who wants to be able to do this sort of thing can I?

like image 486
krayzk Avatar asked Mar 16 '23 10:03

krayzk


1 Answers

I am guessing when you say -

sending date only as a string '2015-07-13' for start and '2015-7-14' for end (broke)

You tried sending the date in the datetime field , According to google api documentation -

start.date - date - The date, in the format "yyyy-mm-dd", if this is an all-day event.

end.date - date - The date, in the format "yyyy-mm-dd", if this is an all-day event.

Try sending the date in the yyyy-mm-dd format in date field. Example -

event = {
    'summary': calendar_event_form.cleaned_data['name'],
    'location': calendar_event_form.cleaned_data['location'],
    'description': calendar_event_form.cleaned_data['description'],
    'start': {
        'date': start_string, #date here
        'timeZone': tz_str,
    },
    'end': {
        'date': end_string, #date here
        'timeZone': tz_str,
    },
}
like image 121
Anand S Kumar Avatar answered Mar 18 '23 15:03

Anand S Kumar