fix get_system_tz to return a proper timezone key and UTC if nothing was found

This commit is contained in:
mrbwburns 2024-01-17 21:27:40 +00:00
parent 4a60019056
commit 2da11c4e9d

View File

@ -3,6 +3,7 @@ Inkycal custom-functions for ease-of-use
Copyright by aceinnolab Copyright by aceinnolab
""" """
import arrow
import logging import logging
import os import os
import time import time
@ -10,7 +11,7 @@ import traceback
import PIL import PIL
import requests import requests
from PIL import ImageFont, ImageDraw, Image import tzlocal
logs = logging.getLogger(__name__) logs = logging.getLogger(__name__)
logs.setLevel(level=logging.INFO) logs.setLevel(level=logging.INFO)
@ -60,14 +61,14 @@ def get_fonts():
print(fonts) print(fonts)
def get_system_tz(): def get_system_tz()->str:
"""Gets the system-timezone """Gets the system-timezone
Gets the timezone set by the system. Gets the timezone set by the system.
Returns: Returns:
- A timezone if a system timezone was found. - A timezone if a system timezone was found.
- None if no timezone was found. - UTC if no timezone was found.
The extracted timezone can be used to show the local time instead of UTC. e.g. The extracted timezone can be used to show the local time instead of UTC. e.g.
@ -76,11 +77,13 @@ def get_system_tz():
>>> print(arrow.now(tz=get_system_tz()) # prints timezone aware time. >>> print(arrow.now(tz=get_system_tz()) # prints timezone aware time.
""" """
try: try:
local_tz = time.tzname[1] local_tz = tzlocal.get_localzone().key
logs.debug(f"Local system timezone is {local_tz}.")
except: except:
print('System timezone could not be parsed!') logs.error("System timezone could not be parsed!")
print('Please set timezone manually!. Setting timezone to None...') logs.error("Please set timezone manually!. Falling back to UTC...")
local_tz = None local_tz = "UTC"
logs.debug(f"The time is {arrow.now(tz=local_tz).format('YYYY-MM-DD HH:mm:ss ZZ')}.")
return local_tz return local_tz