Skip to content

Commit

Permalink
improve app loading time
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit committed Jun 8, 2024
1 parent 89ebe4a commit 311b655
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
8 changes: 1 addition & 7 deletions src/frontendCardDayNight.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand All @@ -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:
Expand Down
21 changes: 10 additions & 11 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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

0 comments on commit 311b655

Please sign in to comment.