python - 忙/闲查询

标签 python timezone google-calendar-api

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

{
  "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

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

我说得对吗?

将日期时间转换为字符串时如何保留 tzinfo?

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

最佳答案

试试这个

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()

关于python - 忙/闲查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43407096/

相关文章:

python - Pandas 中产品的连续日期

python - 从 Kafka 轮询几条消息

timezone - UNIX 时间戳会随时区变化吗?

python - '属性错误 : 'module' object has no attribute 'file' ' when using oauth2client with Google Calendar

python - Python 中的命名信号量?

python - 尝试将模型从应用迁移到应用时出现 Django 导入错误

python - 一天总是 86,400 纪元秒长吗?

java - 在 Java 8 中加载自定义 TimeZoneNameProvider

ssl - 谷歌 API 日历

calendar - Google 日历事件创建/更新/删除 Webhook?