如何在太平洋时区使用 Google Calendar API 提取今天的所有活动

问题描述 投票:0回答:2

我正在尝试使用谷歌日历 API 提取从一天开始到一天结束的所有日常事件。我使用 API 遵循了 google 的快速入门程序,但它为我提供了接下来的 10 个事件,并且在 UTC 时区中具有 0 偏移量。我只想抓取当天和“美国/洛杉矶”时区的活动。

    now = datetime.datetime.utcnow().isoformat() + 'Z'
    print('Getting todays events')
    events_results = service.events().list(calendarId='primary', timeMin=now,
                                        maxResults=10, singleEvents=True,
                                        orderBy='startTime').execute()
python-3.x google-calendar-api
2个回答
1
投票

您可以对脚本进行以下更改:

today = datetime.datetime.today();
start = (datetime.datetime(today.year, today.month, today.day, 00, 00)).isoformat() + 'Z'
tomorrow = today + datetime.timedelta(days=1)
end =  (datetime.datetime(tomorrow.year, tomorrow.month, tomorrow.day, 00, 00)).isoformat() + 'Z'
print('Getting todays events')
events_results = service.events().list(calendarId='primary', timeMin=start, timeMax=end, singleEvents=True, orderBy='startTime').execute()

如果您想检索今天的所有事件,则必须使用

timeMin
timeMax
参数。
timeMin
将设置为今天的日期时间
00:00
,至于
timeMax
这将代表这一天的结束 - 因此已选择使用
00:00
设置的明天日期。

此外,这里不需要

maxResults
参数,因为您想要检索所有事件而不是有限数量。

请记住,为

timeMin
timeMax
提供的日期都是 独占 范围,并且它们必须是带有强制时区偏移的 RFC3339 时间戳。

对于未来的请求,您还可以使用Calendar API Reference来模拟它们并测试所需的参数。

参考


0
投票

我正想问如何在这里调整时区,因为我使用上面的代码得到了错误的结果,但我想通了 - 所以我想我会说我是如何做到的,因为有同样问题的人也可能会在这里结束:)

请求今天的活动列表时调整时区:

today = dt.datetime.today()
start = (dt.datetime(today.year, today.month, today.day, 00, 00, 00)
        ).astimezone(tz_UTC).isoformat().removesuffix("+00:00")+'Z'
end = (dt.datetime(today.year, today.month, today.day, 23, 59, 59)
        ).astimezone(tz_UTC).isoformat().removesuffix("+00:00")+'Z'
© www.soinside.com 2019 - 2024. All rights reserved.