Fix calendar timezone handling and related TypeError

This commit is contained in:
ChuckBuilds
2025-04-21 14:19:00 -05:00
parent f7f93f6564
commit bbf855b60a

View File

@@ -11,6 +11,7 @@ from PIL import Image, ImageDraw, ImageFont
import numpy as np import numpy as np
from rgbmatrix import graphics from rgbmatrix import graphics
import pytz import pytz
from src.config_manager import ConfigManager
class CalendarManager: class CalendarManager:
def __init__(self, matrix, canvas, config): def __init__(self, matrix, canvas, config):
@@ -30,6 +31,15 @@ class CalendarManager:
from src.display_manager import DisplayManager from src.display_manager import DisplayManager
self.display_manager = DisplayManager._instance self.display_manager = DisplayManager._instance
# Get timezone from config
self.config_manager = ConfigManager()
timezone_str = self.config_manager.get_timezone()
try:
self.timezone = pytz.timezone(timezone_str)
except pytz.UnknownTimeZoneError:
logging.warning(f"Unknown timezone '{timezone_str}' in config, defaulting to UTC.")
self.timezone = pytz.utc
if self.enabled: if self.enabled:
self.authenticate() self.authenticate()
@@ -197,11 +207,13 @@ class CalendarManager:
dt = datetime.fromisoformat(start.replace('Z', '+00:00')) dt = datetime.fromisoformat(start.replace('Z', '+00:00'))
else: else:
dt = datetime.strptime(start, '%Y-%m-%d') dt = datetime.strptime(start, '%Y-%m-%d')
# Make date object timezone-aware (assume UTC if no tz info)
dt = pytz.utc.localize(dt)
local_dt = dt.astimezone(pytz.local) local_dt = dt.astimezone(self.timezone) # Use configured timezone
return local_dt.strftime("%a %-m/%-d") # e.g., "Mon 4/21" return local_dt.strftime("%a %-m/%-d") # e.g., "Mon 4/21"
except ValueError: except ValueError as e:
logging.error(f"Could not parse date string: {start}") logging.error(f"Could not parse date string: {start} - {e}")
return "" return ""
def _format_event_time(self, event): def _format_event_time(self, event):
@@ -210,9 +222,13 @@ class CalendarManager:
if not start or 'T' not in start: # Only show time for dateTime events if not start or 'T' not in start: # Only show time for dateTime events
return "All Day" return "All Day"
dt = datetime.fromisoformat(start.replace('Z', '+00:00')) try:
local_dt = dt.astimezone(pytz.local) dt = datetime.fromisoformat(start.replace('Z', '+00:00'))
return local_dt.strftime("%I:%M %p") local_dt = dt.astimezone(self.timezone) # Use configured timezone
return local_dt.strftime("%I:%M %p")
except ValueError as e:
logging.error(f"Could not parse time string: {start} - {e}")
return "Invalid Time"
def display(self): def display(self):
"""Display calendar events on the matrix""" """Display calendar events on the matrix"""