Time of occurence of a Google calendar recurring event (Python API)

寵の児 提交于 2019-12-23 04:33:24

问题


In my program I use google-api-python-client to connect to the Calendar API. I make a query for the events for the next 3 days with:

service = build(serviceName='calendar', version='v3', http=http)
events = service.events().list(
      calendarId=calendarId,
      timeMin='2013-01-04T00:00:00+01:00',
      timeMax='2013-01-06T00:00:00+01:00').execute()

The result is a list of events, modelled in a Python dictionary, roughly looking like this (I omitted some unnecessary data):

{
     u'created': u'2012-11-26T00:54:43.000Z',
     u'description': u'abc abc',
     u'start': {u'dateTime': u'2012-11-26T08:00:00+01:00',
                u'timeZone': u'Europe/Copenhagen'},
     u'end': {u'dateTime': u'2012-11-26T10:00:00+01:00',
              u'timeZone': u'Europe/Copenhagen'},
     u'kind': u'calendar#event',
     u'recurrence': [u'RRULE:FREQ=DAILY'],
     u'sequence': 0
}

The above event is a recurring event, created several months ago, and the 'start' and 'end' fields reflect the date of the creation. There will be several copies of this event in the resulting list, since it occurs several times between the 'timeMin' and 'timeMax' range.

Now, the problem is I don't have the dateTime of the exact occurrence of the event (should be 2013-01-04T00:08:00+01:00 in this case). Anyone knows if I can get that time from the Google Calendar API (I'm using version 3 via their python package)?

Or would suggest how to implement it on the client side?


回答1:


Reading the docs again at https://developers.google.com/google-apps/calendar/v3/reference/events/list

I've found I've overlooked this:

singleEvents boolean

Whether to expand recurring events into instances and only return single one-off events and instances of recurring events, but not the underlying recurring events themselves. Optional. The default is False.




回答2:


Look at the start field and the recurrence field. I would use the Python datetime API to calculate at which days it occurs.

If it is daily, then every day until the recurrence ends. Note that it the recurrence field can contain the number of times the event will recur too, so you would have to calculate this. If you want details on how to calculate these things, I can hack together a code sample. Note that the datetime API can handle adding days to a date etc., so it should be easy to calculate. And it can calculate the difference between two dates, so calculating the number of times it has already occurred too.

Edit: Check out the links below:

iCal recurrence format
Datetime documentation



来源:https://stackoverflow.com/questions/14145977/time-of-occurence-of-a-google-calendar-recurring-event-python-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!