简体   繁体   中英

Microsoft Graph API CalendarView request not working

Here's my piece of code that handles and processes a specific date:

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

So the date being received is: 2018-10-18T12:00:00-06:00 , the idea is to remove the time from this date (because it's ambiguous), as I have no access to the API and therefore I have to deal with it the way it's being sent. After that, the idea is to parse that date into an ISO 8601 format so that the request made to graph API is done in the correct datetime format asked (ISO 8601).

The resulting datetime is 2018-10-18 00:00:00 . When the request is made, the resulting URL is:

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

Which brings back 1 event between the 18th and 19th. Seems to be fine, except there's actually 2 events between the 18th 00:00:00 and the 19th 00:00:00 .

One of the events is on the 18th at 12pm and the other one is at 8:30pm. Can anyone tell me why the date range is not working? I'm trying to figure out why, but nothing occurs to me.

You're not specifying a timezone so 2018-10-18 00:00:00 is treated as UTC 0 .

The event scheduled for midnight most likely not scheduled for midnight UTC but rather some other timezone. If the event was scheduled for US Eastern, for example, the timezone would be UTC - 5 . When 10/18/2018 @ 12:00 AM EST is translated to UTC 0 it is 5 hours earlier, or 10/17/2018 @ 8:00 PM UTC.

To get the event in a specific timezone, you need to add the offset. For example:

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

After much testing, I was able to parse the incoming date, reset the time it came with to 0 hours and set the timezone to UTC with the following generic function:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM