diff --git a/mobileraker/data/dtos/moonraker/printer_snapshot.py b/mobileraker/data/dtos/moonraker/printer_snapshot.py index e59907c..4e90ec7 100644 --- a/mobileraker/data/dtos/moonraker/printer_snapshot.py +++ b/mobileraker/data/dtos/moonraker/printer_snapshot.py @@ -1,7 +1,7 @@ from datetime import datetime, timedelta from typing import Optional import math -import pytz +from dateutil import tz from mobileraker.data.dtos.moonraker.printer_objects import GCodeFile, GCodeMove, PrintStats, Toolhead, VirtualSDCard @@ -173,7 +173,7 @@ def eta(self) -> Optional[datetime]: @property def eta_seconds_utc(self) -> Optional[int]: return int(self.eta.astimezone( - pytz.UTC).timestamp()) if self.eta else None + tz.UTC).timestamp()) if self.eta else None @property diff --git a/mobileraker/util/configs.py b/mobileraker/util/configs.py index 2b84b12..4e1d9c1 100644 --- a/mobileraker/util/configs.py +++ b/mobileraker/util/configs.py @@ -3,10 +3,8 @@ import logging import os import pathlib -import time from typing import Any, Dict, Optional, Union - -import pytz +from dateutil import tz home_dir = os.path.expanduser("~/") companion_dir = pathlib.Path(__file__).parent.parent.parent.resolve() @@ -24,14 +22,14 @@ def get_local_timezone() -> str: """ Returns the local timezone. - Thanks to https://www.reddit.com/user/destinal/ for this snippet. - return: The local timezone. """ - local_tz_offset = -time.timezone if (time.localtime().tm_isdst == 0) else -time.altzone - local_tz = pytz.timezone(f'Etc/GMT{local_tz_offset//3600:+d}') + # Get the system's current local timezone + local_timezone = tz.tzlocal() + # Convert the timezone to a string representation + timezone_abbr = local_timezone.tzname(datetime.datetime.now()) - return local_tz.zone if local_tz.zone is not None else 'Etc/UTC' + return timezone_abbr if timezone_abbr is not None else 'UTC' class CompanionRemoteConfig: @@ -87,7 +85,9 @@ def __init__(self, passed_config_file: str) -> None: 'general', 'language', fallback='en') self.timezone_str: str = self.config.get( 'general', 'timezone', fallback=get_local_timezone()) # fallback to system timezone (Hopefully) - self.timezone: datetime.tzinfo = pytz.timezone(self.timezone_str if self.timezone_str is not None else "Greenwich") + + parsed_tz = tz.gettz(self.timezone_str) + self.timezone: datetime.tzinfo = parsed_tz if parsed_tz is not None else tz.UTC self.eta_format: str = self.config.get( 'general', 'eta_format', fallback='%d.%m.%Y, %H:%M:%S') self.include_snapshot: bool = self.config.getboolean( diff --git a/mobileraker/util/notification_placeholders.py b/mobileraker/util/notification_placeholders.py index 3d22aa8..0873302 100644 --- a/mobileraker/util/notification_placeholders.py +++ b/mobileraker/util/notification_placeholders.py @@ -1,6 +1,5 @@ from datetime import datetime, timedelta from typing import Optional -import pytz from mobileraker.data.dtos.mobileraker.notification_config_dto import DeviceNotificationEntry from mobileraker.data.dtos.moonraker.printer_snapshot import PrinterSnapshot from mobileraker.util.configs import CompanionLocalConfig diff --git a/scripts/mobileraker-requirements.txt b/scripts/mobileraker-requirements.txt index 198fabd..8e5f5a2 100644 --- a/scripts/mobileraker-requirements.txt +++ b/scripts/mobileraker-requirements.txt @@ -1,5 +1,5 @@ coloredlogs==15.0.1 requests==2.26.0 websockets==10.1 -pytz==2022.7.1 -pillow \ No newline at end of file +pillow +dateutil \ No newline at end of file