mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-04-10 21:03:01 +00:00
Update clock.py
Error handling for timezone finder
This commit is contained in:
46
src/clock.py
46
src/clock.py
@@ -12,29 +12,47 @@ class Clock:
|
|||||||
self.display_manager = DisplayManager(self.config.get('display', {}))
|
self.display_manager = DisplayManager(self.config.get('display', {}))
|
||||||
self.location = self.config.get('location', {})
|
self.location = self.config.get('location', {})
|
||||||
self.clock_config = self.config.get('clock', {})
|
self.clock_config = self.config.get('clock', {})
|
||||||
|
# Use configured timezone if available, otherwise try to determine it
|
||||||
self.timezone = self._get_timezone()
|
self.timezone = self._get_timezone()
|
||||||
|
|
||||||
def _get_timezone(self) -> str:
|
def _get_timezone(self) -> pytz.timezone:
|
||||||
"""Get timezone based on location."""
|
"""Get timezone based on location or config."""
|
||||||
from timezonefinder import TimezoneFinder
|
# First try to use timezone from config if it exists
|
||||||
from geopy.geocoders import Nominatim
|
if 'timezone' in self.config:
|
||||||
|
try:
|
||||||
|
return pytz.timezone(self.config['timezone'])
|
||||||
|
except pytz.exceptions.UnknownTimeZoneError:
|
||||||
|
print(f"Warning: Invalid timezone in config: {self.config['timezone']}")
|
||||||
|
|
||||||
|
# If no timezone in config or it's invalid, try to determine from location
|
||||||
try:
|
try:
|
||||||
|
from timezonefinder import TimezoneFinder
|
||||||
|
from geopy.geocoders import Nominatim
|
||||||
|
from geopy.exc import GeocoderTimedOut
|
||||||
|
|
||||||
# Get coordinates for the location
|
# Get coordinates for the location
|
||||||
geolocator = Nominatim(user_agent="led_matrix_clock")
|
geolocator = Nominatim(user_agent="led_matrix_clock")
|
||||||
location_str = f"{self.location['city']}, {self.location['state']}, {self.location['country']}"
|
location_str = f"{self.location['city']}, {self.location['state']}, {self.location['country']}"
|
||||||
location = geolocator.geocode(location_str)
|
|
||||||
|
|
||||||
if location:
|
try:
|
||||||
# Find timezone from coordinates
|
location = geolocator.geocode(location_str, timeout=5) # 5 second timeout
|
||||||
tf = TimezoneFinder()
|
if location:
|
||||||
timezone_str = tf.timezone_at(lng=location.longitude, lat=location.latitude)
|
# Find timezone from coordinates
|
||||||
return pytz.timezone(timezone_str)
|
tf = TimezoneFinder()
|
||||||
except Exception as e:
|
timezone_str = tf.timezone_at(lng=location.longitude, lat=location.latitude)
|
||||||
print(f"Error finding timezone: {e}")
|
if timezone_str:
|
||||||
|
return pytz.timezone(timezone_str)
|
||||||
|
except GeocoderTimedOut:
|
||||||
|
print("Warning: Timeout while looking up location coordinates")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Warning: Error finding timezone from location: {e}")
|
||||||
|
|
||||||
# Fallback to UTC
|
except Exception as e:
|
||||||
return pytz.UTC
|
print(f"Warning: Error importing geolocation libraries: {e}")
|
||||||
|
|
||||||
|
# Fallback to US/Central for Dallas
|
||||||
|
print("Using fallback timezone: US/Central")
|
||||||
|
return pytz.timezone('US/Central')
|
||||||
|
|
||||||
def get_current_time(self) -> str:
|
def get_current_time(self) -> str:
|
||||||
"""Get the current time in the configured timezone."""
|
"""Get the current time in the configured timezone."""
|
||||||
|
|||||||
Reference in New Issue
Block a user