Skip to content

Commit

Permalink
Update timezone handling and dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Clon1998 committed Feb 25, 2024
1 parent dac140a commit badac35
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
4 changes: 2 additions & 2 deletions mobileraker/data/dtos/moonraker/printer_snapshot.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions mobileraker/util/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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:

Expand Down Expand Up @@ -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(
Expand Down
1 change: 0 additions & 1 deletion mobileraker/util/notification_placeholders.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions scripts/mobileraker-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
coloredlogs==15.0.1
requests==2.26.0
websockets==10.1
pytz==2022.7.1
pillow
pillow
dateutil

0 comments on commit badac35

Please sign in to comment.