Create all day event using google calendar api in python

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

there were similar questions addressed here in C # and JAVA languages But none of them were Python

The documentation for creating google apis events is here . In Python, you basically send a simple json object to create an event with specific event parameters.

Here is my code trying to complete all day:

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 clear difference between the event I create with this one, which I create on Google Calendar, and mark it as an all day event. Here's a simple image that shows the difference:enter image description here

Things I've tried:

  • defining only start time without end time (broken)

  • send date only as string '2015-07-13' for start and '2015-7-14' for end (broken)

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

I came here hoping that someone has solved this in the past, I cannot be the only one using python who wants to be able to do this kind of thing, can I?

+3


source to share


1 answer


I'm guessing when you say -

send date only as string '2015-07-13' for start and '2015-7-14' for end (broken)

You tried to post date

to the field datetime

, as per the google api documentation -



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

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

Try sending the date in the format yyyy-mm-dd

to the field date

. 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,
    },
}

      

+5


source







All Articles