[英]How to return all events on a specific date in Google Calendar API
i am using Python and Google Calendar API to build a calendar.我正在使用 Python 和 Google 日历 API 来构建日历。 I would like to get all the events that exist on a given date.我想获取给定日期存在的所有事件。 The date will be given as a string 'yyyy-mm-dd'.日期将以字符串 'yyyy-mm-dd' 的形式给出。 I have created a function that does that but it is still returning the all-day events on the following day.我创建了一个 function 来执行此操作,但它仍在第二天返回全天事件。
from datetime import *
import pytz
def get_events_by_date(api_service, event_date):
split_date = event_date.split('-')
event_date = datetime(int(split_date[0]), int(split_date[1]), int(split_date[2]), 00, 00, 00, 0)
event_date = pytz.UTC.localize(event_date).isoformat()
end = datetime(int(split_date[0]), int(split_date[1]), int(split_date[2]), 23, 59, 59, 999999)
end = pytz.UTC.localize(end).isoformat()
events_result = api_service.events().list(calendarId='primary', timeMin=event_date,timeMax=end).execute()
return events_result.get('items', [])
>>all_events = get_events_by_date(api, '2022-09-24')
>>for event in all_events:
print(event['summary'])
using this example, it prints all events on the '2022-09-24' AND any all-day events on the '2022-09-25'.使用此示例,它会打印“2022-09-24”上的所有事件以及“2022-09-25”上的所有全天事件。 Any ideas why?任何想法为什么? (Or a better way to implement this feature)? (或者实现此功能的更好方法)?
NOTE: i got the idea for the time zone conversion from this post Generate RFC 3339 timestamp in Python注意:我从这篇文章中得到了时区转换的想法Generate RFC 3339 timestamp in Python
Although I'm not sure whether this is the direct solution to your issue, from your showing script, how about adding timeZone
to the request as follows?虽然我不确定这是否是您问题的直接解决方案,但从您的显示脚本来看,如何将timeZone
添加到请求中,如下所示?
events_result = api_service.events().list(calendarId='primary', timeMin=event_date,timeMax=end).execute()
events_result = api_service.events().list(calendarId='primary', timeMin=event_date,timeMax=end, timeZone="UTC").execute()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.