Fixed timezone related bug and minor improvements

This commit is contained in:
Maximilian Giller 2019-04-28 15:24:15 +02:00
parent 76495bd16c
commit 69d1041f98
4 changed files with 45 additions and 20 deletions

View file

@ -28,11 +28,13 @@ class CalendarInterface (DataSourceInterface):
def __get_events__ (self):
raise NotImplementedError("Functions needs to be implemented")
def get_upcoming_events (self, timespan = None):
def get_upcoming_events (self, timespan = None, start_time = None):
if timespan is None:
timespan = timedelta(31)
local_tzinfo = datetime.now(timezone.utc).astimezone().tzinfo
return self.__get_events_in_range__(datetime.now(local_tzinfo), timespan)
if start_time == None:
local_tzinfo = datetime.now(timezone.utc).astimezone().tzinfo
start_time = datetime.now(local_tzinfo)
return self.__get_events_in_range__(start_time, timespan)
def get_today_events (self):
return self.get_day_events(datetime.today())

View file

@ -2,7 +2,7 @@ from DesignEntity import DesignEntity
from PIL import ImageDraw
from TextDesign import TextDesign
from WeatherColumnDesign import WeatherColumnDesign
from datetime import date, timedelta, datetime
from datetime import date, timedelta, datetime, timezone
from SingelDayEventListDesign import SingelDayEventListDesign
from Assets import fonts, colors, defaultfontsize
from settings import general_settings
@ -45,9 +45,10 @@ class DayHeaderDesign (DesignEntity):
self.draw_design(design)
def add_calendar (self, calendar):
now = datetime.now()
time_until_tomorrow = (datetime(now.year, now.month, now.day) + timedelta(1)) - now
self.__draw_event_list__(calendar.get_upcoming_events(time_until_tomorrow))
local_tzinfo = datetime.now(timezone.utc).astimezone().tzinfo
now = datetime.now(local_tzinfo)
time_until_tomorrow = (datetime(now.year, now.month, now.day, 0, 0, 0, 0, local_tzinfo) + timedelta(1)) - now
self.__draw_event_list__(calendar.get_upcoming_events(time_until_tomorrow, now))
def add_events (self, events):
self.__draw_event_list__(events)

View file

@ -15,7 +15,14 @@ class IcalEvents(CalendarInterface):
def is_available(self):
try:
urlopen("https://google.com", timeout=2)
testurl = ""
if self.urls:
testurl = self.urls[0]
elif self.highlighted_urls:
testurl = self.highlighted_urls[0]
else:
return False
urlopen(testurl)
return True
except:
return False
@ -35,11 +42,12 @@ class IcalEvents(CalendarInterface):
def __get_events_from_urls__(self, urls):
loaded_events = []
try:
if urls is None:
return loaded_events
for calendar in urls:
if urls is None:
return loaded_events
for calendar in urls:
try:
decode = str(urlopen(calendar).read().decode())
decode = self.__remove_alarms__(decode)
@ -61,17 +69,26 @@ class IcalEvents(CalendarInterface):
cal_event.end_datetime = cal_event.end_datetime.astimezone(None)
if cal_event.allday:
cal_event.end_datetime = cal_event.end_datetime - timedelta(1)
cal_event.duration = cal_event.duration - timedelta(1)
cal_event = self.__fix_allday__(cal_event)
cal_event.multiday = self.__is_multiday__(cal_event)
loaded_events.append(cal_event)
return loaded_events
except BaseException as ex:
print("ICal-Error [" + calendar + "]")
print(ex)
return loaded_events
except BaseException as ex:
print("ICal-Error [" + calendar + "]")
print(ex)
return loaded_events
def __fix_allday__(self, event):
local_tzinfo = datetime.now(timezone.utc).astimezone().tzinfo
begin_utc = event.begin_datetime.astimezone(timezone.utc)
end_utc = event.end_datetime.astimezone(timezone.utc)
event.begin_datetime = datetime(begin_utc.year, begin_utc.month, begin_utc.day, 0, 0, 0, 0, local_tzinfo)
event.end_datetime = datetime(end_utc.year, end_utc.month, end_utc.day, 0, 0, 0, 0, local_tzinfo) - timedelta(1)
event.duration = event.duration - timedelta(1)
return event
def __remove_alarms__(self, decode):
alarm_begin = 'BEGIN:VALARM'

View file

@ -14,7 +14,12 @@ class RssParserPosts (RssInterface):
def is_available(self):
try:
urlopen("https://google.com", timeout=2)
testurl = ""
if self.urls:
testurl = self.urls[0]
else:
return False
urlopen(testurl)
return True
except:
return False