首页 > 解决方案 > 谷歌日历在日历的时区中包含 timeMin 的事件列表

问题描述

要求

我可以在此参考之后检索 google-calendar 事件列表:https ://developers.google.com/calendar/v3/reference/events/list

如何根据日历的 timeZone2017-03-30T14:30:00获取所有事件的列表?2017-03-30T15:30:00

我的调查

有可选参数timeMin, timeMax。问题是它们应该具有以下格式之一(RFC3339 时间戳):

  1. 2017-03-30T14:30:00Z, 这里 Z代表 Zulu = UTC timeZone
  2. 2017-03-30T14:30:00+03, 这里+03代表与 UTC 相比的时区偏移

在这两种情况下,我都必须获取日历的时区(如何以我需要的偏移格式获取它,例如,、、?-04),然后执行一些额外的计算/操作。+00+03

显然,在我的情况下,我宁愿只传递值而不提及时区,并使 API 使用日历的时区。

还有另一个可选参数timeZone,但这是将在响应中使用的 timeZone,而不是在过滤器中。默认是日历的时区,这对我有好处。

标签: timezonegoogle-calendar-api

解决方案


最后我找到了在日期时间应用 GC (Google Calendar) TZ 的方法:

from dateutil import tz
from datetime import datetime
tzinfo = tz.gettz(default_calendar['timeZone'])

# Option1: create date without TZ and then apply GC TZ using astimezone:
dt = datetime(2018,7,3,14,30,0)
dt_in_gc_tz = dt.astimezone(tzinfo) # 2018-07-03 14:30:00+03:00

# Option2: just create dateTime with GC TZ:
dt_in_gc_tz = datetime(2018, 7,3,14,30,0, tzinfo=tzinfo) # 2018-07-03 14:30:00+03:00

# Convert to RFC3339 timestamp string:
dt_in_gc_tz.isoformat() # '2018-07-03T14:30:00+03:00'

推荐阅读