diff --git a/src/frontendCardDayNight.py b/src/frontendCardDayNight.py index 17a745a..f4faf50 100644 --- a/src/frontendCardDayNight.py +++ b/src/frontendCardDayNight.py @@ -7,9 +7,7 @@ from .frontendUiDrawDayNight import * from .config import settings from .utils import ( - get_tz_offset_by_cord, get_cords, - get_my_tz_offset_from_utc, get_time_difference, ) @@ -31,15 +29,11 @@ def get_sunset_sunrise_degree(self): time_diff = t_data.get("epoch_diff") target_time = t_data.get("target_time") - my_tz_offset = get_my_tz_offset_from_utc() - - tz_offset_from_curr_tz = get_tz_offset_by_cord(*get_cords()) - sunrise_ts, sunset_ts = 0, 0 for i, data in enumerate(daily_data.time.get("data")): date_ = int( datetime.fromtimestamp( - data + my_tz_offset + tz_offset_from_curr_tz + data + time_diff ).strftime(r"%d") ) if date_ == datetime.today().date().day: diff --git a/src/utils.py b/src/utils.py index 3965c3b..7fdb8c5 100644 --- a/src/utils.py +++ b/src/utils.py @@ -11,6 +11,9 @@ forecast_weather_data = None epoch_offset = None +global local_time_data +local_time_data = dict() + TIMEOUT = 5 domains = { "google": "http://www.google.com", @@ -77,17 +80,6 @@ def get_cords(): return [float(x) for x in selected_city_.split(",")] -def get_my_tz_offset_from_utc(): - try: - offset = datetime.utcnow() - datetime.now() - # Convert the offset to seconds - offset_seconds = int(offset.total_seconds()) - - return offset_seconds - except Exception as e: - return f"Error: {str(e)}" - - def get_tz_offset_by_cord(lat, lon): global epoch_offset @@ -118,6 +110,12 @@ def get_tz_offset_by_cord(lat, lon): def get_time_difference(target_latitude, target_longitude): + global local_time_data + + cord_str = f"{target_latitude}_{target_longitude}" + if local_time_data.get(cord_str) is not None: + return local_time_data[cord_str] + # Get current time in the target location using timeapi.io url = f"https://timeapi.io/api/Time/current/coordinate?latitude={target_latitude}&longitude={target_longitude}" target_time_response = requests.get(url) @@ -127,4 +125,5 @@ def get_time_difference(target_latitude, target_longitude): epoch_diff = time.time() - target_time.timestamp() data = {"epoch_diff": epoch_diff, "target_time": target_time.timestamp()} + local_time_data[cord_str] = data return data