简体   繁体   English

如何在谷歌日历中返回特定日期的所有事件 API

[英]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添加到请求中,如下所示?

From:从:

events_result = api_service.events().list(calendarId='primary', timeMin=event_date,timeMax=end).execute()

To:至:

events_result = api_service.events().list(calendarId='primary', timeMin=event_date,timeMax=end, timeZone="UTC").execute()

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Calendar API使用Python查询最近1个月的所有事件 - Google Calendar API querying all events for the last 1 months in Python 通过 API 将整个 Google 日历(其所有事件)导出到文件 - Exporting an entire Google Calendar (all of its events) TO A FILE via API 如何从 Django 中的 Google Calendar API 获取事件? - How to fetch events from Google Calendar API in Django? Google Calendar API Python-导入多个事件 - Google Calendar API Python - Import Multiple Events google-calendar-api的service.events()。list的最早数据是哪个日期? - Which date is the earliest data for service.events().list of google-calendar-api? 过滤来自 json 响应的结果,以仅显示没有结束日期的重复事件。 Python 和谷歌日历 API - Filter results from json response to only show recurring events with no end date. Python and Google Calendar API QML 日历和 Google 日历 API 在 Python 事件集成 - QML Calendar and Google Calendar API in Python events integration Google Calendars API - 新日历事件或现有事件更新的侦听器? - Google Calendars API - Listener for new calendar events or updates to existing events? 如何在Google日历中为返回的事件设置日期限制并按顺序排列-Python - How to set a date restriction for returned events in Google Calendar and put them in order - Python 谷歌日历 api 返回不同的日期格式 - Google calendar api returns different date formats
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM