Microsoft Graph API CalendarView 请求无效

Microsoft Graph API CalendarView request not working

这是我处理和处理特定日期的一段代码:

def o365_calendar_parse_url(self):
    if validate_params(self.parameters):
        data_url = self.o365_base_url + self.SUPPORTED_O365_INTENTS_URL['O365_CALENDAR_PARAMS']
        start_date = ''
        end_date = ''
        if self.parameters["date"]:
            split_date = self.parameters["date"].split("T")[0]
            start_date = split_date
            start_date = date_utilities.parse(start_date)
            print(start_date)
            end_date = start_date + datetime.timedelta(days=1)
            final_url = data_url.format(start_date, end_date)
            return final_url
        else:
            #other stuff

所以收到的日期是:2018-10-18T12:00:00-06:00,我的想法是从这个日期中删除时间(因为它不明确),因为我无法访问 API,因此我有按照发送的方式处理它。之后,想法是将该日期解析为 ISO 8601 格式,以便以要求的正确日期时间格式(ISO 8601)完成对图 API 的请求。

生成的日期时间是 2018-10-18 00:00:00。发出请求后,结果 URL 为:

https://graph.microsoft.com/v1.0/me/calendarView?startDateTime=2018-10-18%2000:00:00&endDateTime=2018-10-19%2000:00:00&$select=subject,bodyPreview,start,end,location,organizer,webLink

其中带回了 18 号和 19 号之间的 1 个事件。似乎没问题,除了 18th 00:00:0019th 00:00:00 之间实际上有 2 个事件。

其中一项活动是在 18 日中午 12 点,另一项活动是在 8:30pm。谁能告诉我为什么日期范围不起作用?我试图找出原因,但我什么也没想到。

您没有指定时区,因此 2018-10-18 00:00:00 被视为 UTC 0

安排在午夜的事件很可能不是安排在 UTC 午夜,而是安排在其他时区。例如,如果活动安排在美国东部时间,则时区将为 UTC - 5。当 10/18/2018 @ 12:00 AM EST 转换为 UTC 0 时,它提前 5 小时,或者 10/17/2018 @ 8:00 PM UTC。

要获取特定时区的事件,您需要添加偏移量。例如:

2018-10-18T00:00:00-05:00

经过多次测试,我能够解析传入日期,将它附带的时间重置为 0 小时,并使用以下通用函数将时区设置为 UTC:

def to_utc_iso(date_string):
    the_date = parser.parse(date_string)
    string_date = the_date.replace(hour=0, tzinfo=timezone.utc).timestamp()
    return datetime.fromtimestamp(string_date).isoformat()