Reputation: 360
I am trying to use the Google Calendar API v3 in Python to get back all events for a particular date.
So far, I've declared my dates as follows:
today_beginning = datetime.datetime.combine(date.today(), time())
today_end = today_beginning + datetime.timedelta(1, 0) - datetime.timedelta(0, 1)
today_beginning = today_beginning.isoformat() + 'Z'
today_end = today_end.isoformat() + 'Z'
I then make the API call using these dates
eventsResult = service.events().list(
calendarId=cal_id.calendarId, timeMin=today_beginning, timeMax=today_end).execute()
The issue is that events from before my timeMin date are returned too.
Maybe I have a poor understanding of the API docs?
For reference, if today was January 10th, 2016, start and end time would appear as follows:
2016-01-10T00:00:00Z
2016-01-10T23:59:59Z
Upvotes: 1
Views: 3447
Reputation: 166
from the docs:
timeMin: Lower bound (exclusive) for an event's end time to filter by. Optional. The default is not to filter by end time. Must be an RFC3339 timestamp with mandatory time zone offset, for example, 2011-06-03T10:00:00-07:00, 2011-06-03T10:00:00Z. Milliseconds may be provided but are ignored. If timeMax is set, timeMin must be smaller than timeMax
so events that start before timeMin will be included if they end after timeMin
Upvotes: 0
Reputation: 1
Not sure what is wrong with your code but here is my fix to correctly subtract time keeping with the UTC format.
Note: I began with their sample code from the same page you reference as the API docs. And only timeMin is used in this example.
Beginning by declaring dates:
tmin = datetime.datetime.utcnow() - datetime.timedelta(hours=24)
time = tmin.isoformat() + 'Z'
Then using the same format for calling the API
events_result = service.events().list(
calendarId='primary', timeMin=time, maxResults=10, singleEvents=True, orderBy='startTime').execute()
This just subtracts 24 hours from whatever time datetime.datetime.utcnow() has found for your calendar.
I would imagine for timeMax you could do the exact same but add datetime.timedelta() instead. It could be added to your current time of utcnow() or added to the timeMin time of tmin.
Upvotes: 0
Reputation: 7832
Pay attention that you've set the dates in UTC timezone
2016-01-10T00:00:00Z
2016-01-10T23:59:59Z
Z
stands for Zulu = UTC timezone
So if your calendar's timezone isn't UTC - the period you've set will not match 2016-01-10
(it will be a shift according to your calendar's timezone)
Upvotes: 1