忙/闲查询

时间:2017-04-14 07:24:06

标签: python timezone google-calendar-api

根据https://developers.google.com/google-apps/calendar/v3/reference/freebusy/query处的文档执行freebusy查询,您必须在正文中提供此参数:

{
  "timeMin": datetime,
  "timeMax": datetime,
  "timeZone": string,
  "groupExpansionMax": integer,
  "calendarExpansionMax": integer,
  "items": [
    {
      "id": string
    }
  ]
}
构建查询时

# calendarlist = a list with all the ids of the calendars I do query
# calendar is the access token to google calendar API
start = datetime.now()
end = start.replace(hour=23)
freebusy_query = {
        "timeMin": start,
        "timeMax": end,
        "timeZone": 'Europe/Madrid',
        "items": calendarlist
    }

availability = calendar.freebusy().query(body=freebusy_query).execute()

我收到的回复告诉我:

TypeError: datetime.datetime(2017, 4, 14, 23, 00, 59, 999999)) is not JSON serializable

所以我想我对文档的阅读是错误的,我必须提供的是日期时间字符串

我是对的吗?

将datetime转换为字符串时,如何保留tzinfo?

date_format = '%Y-%m-%dT%H:%M:%S.%f%Z'
start_string = start.strftime(date_format)
...

1 个答案:

答案 0 :(得分:1)

试试这个

start = datetime.now().replace(microsecond=0).isoformat() + "-04:00"
end = datetime.now().replace(hour=23, microsecond=0).isoformat() + "-04:00"



freebusy = service.freebusy().query(body=
    {"timeMin": start,
      "timeMax": end,
      "timeZone": 'Europe/Madrid',
      "items": calendarList
    }).execute()
相关问题