From 9ba2c185951c14aab549b46776a086996c080483 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 5 Jan 2022 13:31:07 +0100 Subject: [PATCH] Better logic for timezone handling/conversion in calendar plugin --- platypush/plugins/calendar/ical/__init__.py | 30 ++++++++++++++------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/platypush/plugins/calendar/ical/__init__.py b/platypush/plugins/calendar/ical/__init__.py index c13c0a2d2..f2db31a9e 100644 --- a/platypush/plugins/calendar/ical/__init__.py +++ b/platypush/plugins/calendar/ical/__init__.py @@ -5,6 +5,7 @@ import datetime import dateutil.parser import requests +from typing import Union, Optional from platypush.plugins import Plugin, action from platypush.plugins.calendar import CalendarInterface @@ -29,7 +30,19 @@ class CalendarIcalPlugin(Plugin, CalendarInterface): self.url = url @staticmethod - def _translate_event(event): + def _convert_timestamp(event, attribute: str) -> datetime.datetime: + import pytz + t = event.get(attribute) + if not t: + return + + return ( + dateutil.parser.isoparse(t.dt.isoformat()) + .replace(tzinfo=pytz.timezone('UTC')) + ) + + @classmethod + def _translate_event(cls, event): return { 'id': str(event.get('uid')) if event.get('uid') else None, 'kind': 'calendar#event', @@ -44,15 +57,15 @@ class CalendarIcalPlugin(Plugin, CalendarInterface): 'displayName': event.get('organizer').params['cn'] } if event.get('organizer') else None, - 'created': event.get('created').dt.isoformat() if event.get('created') else None, - 'updated': event.get('last-modified').dt.isoformat() if event.get('last-modified') else None, + 'created': cls._convert_timestamp(event, 'created'), + 'updated': cls._convert_timestamp(event, 'last-modified'), 'start': { - 'dateTime': event.get('dtstart').dt.isoformat() if event.get('dtstart') else None, - 'timeZone': 'UTC', # TODO Support multiple timezone IDs + 'dateTime': cls._convert_timestamp(event, 'dtstart'), + 'timeZone': 'UTC', }, 'end': { - 'dateTime': event.get('dtend').dt.isoformat() if event.get('dtend') else None, + 'dateTime': cls._convert_timestamp(event, 'dtend'), 'timeZone': 'UTC', }, } @@ -81,9 +94,8 @@ class CalendarIcalPlugin(Plugin, CalendarInterface): if ( event['status'] != 'cancelled' - and event['end']['dateTime'] - and dateutil.parser.parse(event['end']['dateTime']).replace(tzinfo=pytz.timezone('UTC')) >= - datetime.datetime.now(pytz.timezone('UTC')) + and event['end'].get('dateTime') + and event['end']['dateTime'] >= datetime.datetime.now(pytz.timezone('UTC')) and ( (only_participating and event.get('responseStatus') in [None, 'accepted', 'tentative'])