Skip to content

Commit

Permalink
convert sunset,sunrise time to local
Browse files Browse the repository at this point in the history
  • Loading branch information
amit-newstok committed Jul 5, 2023
1 parent 955301b commit b102a03
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
7 changes: 7 additions & 0 deletions src/css.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@
}
.f-xsm {
font-size: 13px;
}
.f-sm {
font-size: 14px;
}
.f-msm {
font-size: 15px;
}
.f-m {
font-size: 16px;
}
Expand Down Expand Up @@ -159,3 +165,4 @@
"""

# Thanks to https://github.com/SalaniLeo for this beautiful gradients collection

23 changes: 10 additions & 13 deletions src/ui_current_w.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import gi
import datetime
from datetime import datetime
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
from gi.repository import Gtk,Gio,GLib

from .constants import icons,bg_css
from .units import measurements,get_measurement_type
from .utils import convert_to_local_time

def current_weather(main_window,upper_row,data):
global g_main_window,selected_city,settings,added_cities,cities,use_gradient
Expand All @@ -14,7 +15,7 @@ def current_weather(main_window,upper_row,data):
selected_city = int(str(settings.get_value('selected-city')))
added_cities = list(settings.get_value('added-cities'))
cities = [f"{x.split(',')[0]},{x.split(',')[1]}" for x in added_cities]
settings.set_value("updated-at",GLib.Variant("s",str(datetime.datetime.now())))
settings.set_value("updated-at",GLib.Variant("s",str(datetime.now())))
measurement_type = get_measurement_type()

g_main_window = main_window
Expand Down Expand Up @@ -77,7 +78,7 @@ def current_weather(main_window,upper_row,data):
feels_like_label = Gtk.Label(label=_(f"Feels like {data['main']['feels_like']:.1f}{measurements[measurement_type]['temp_unit']}"))
feels_like_label.set_halign(Gtk.Align.START)
feels_like_label.set_margin_start(5)
feels_like_label.set_css_classes(['secondary-lighter','f-sm'])
feels_like_label.set_css_classes(['secondary-light','f-msm'])
temp_box_l.append(feels_like_label)

temp_box_r = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
Expand All @@ -86,10 +87,11 @@ def current_weather(main_window,upper_row,data):

temp_max_label = Gtk.Label(label = f"↑ {data['main']['temp_max']:.1f}°")
temp_min_label = Gtk.Label(label = f"↓ {data['main']['temp_min']:.1f}°")
temp_max_label.set_css_classes(['secondary-light'])
temp_min_label.set_css_classes(['secondary-light'])
temp_max_label.set_css_classes(['secondary-light','bold'])
temp_min_label.set_css_classes(['secondary-light','f-xsm'])
temp_min_label.set_halign(Gtk.Align.START)
temp_max_label.set_margin_top(10)
temp_max_label.set_margin_bottom(30)
temp_max_label.set_margin_bottom(4)
temp_box_r.append(temp_max_label)
temp_box_r.append(temp_min_label)

Expand Down Expand Up @@ -125,11 +127,8 @@ def current_weather(main_window,upper_row,data):

weather_data = []

sunrise_time = datetime.datetime.fromtimestamp(data['sys']['sunrise'])
sunset_time = datetime.datetime.fromtimestamp(data['sys']['sunset'])
print(sunrise_time)
print(sunset_time)
print(data)
sunrise_time = convert_to_local_time(data['sys']['sunrise'],data['timezone'] )
sunset_time = convert_to_local_time(data['sys']['sunset'],data['timezone'] )

sunrise_label = Gtk.Label(label=f"{sunrise_time.hour}:{sunrise_time.minute} AM")
sunrise_label.set_margin_end(20)
Expand Down Expand Up @@ -158,8 +157,6 @@ def current_weather(main_window,upper_row,data):
weather_data.append([_("Pressure"), _("{0} hPa").format(data['main']['pressure'])])
weather_data.append([_("Wind speed"), _("{0:.1f} {1} {2}").format(data['wind']['speed']*measurements[measurement_type]['speed_mul'],measurements[measurement_type]['speed_unit'],wind_dir(data['wind']['deg']))])
weather_data.append([_("Visibility"), f"{data['visibility']*measurements[measurement_type]['dist_mul']:.1f} {measurements[measurement_type]['dist_unit']}"])
# weather_data.append(["Sunrise", f"{sunrise_time.hour}:{sunrise_time.minute} AM"])
# weather_data.append(["Sunset", f"{sunset_time.hour-12}:{sunset_time.minute} PM"])

label_grid = Gtk.Grid()
label_grid.set_row_spacing(2)
Expand Down
11 changes: 8 additions & 3 deletions src/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests
from datetime import datetime, timedelta, timezone
from gi.repository import Adw,Gio

current_weather_data = None
Expand Down Expand Up @@ -43,6 +44,10 @@ def get_selected_city_cord():
return latitude,longitude

def create_toast(text,priority=0):
toast = Adw.Toast.new(text)
toast.set_priority(Adw.ToastPriority(priority))
return toast
toast = Adw.Toast.new(text)
toast.set_priority(Adw.ToastPriority(priority))
return toast

def convert_to_local_time(timestamp, timezone_stamp):
hour_offset_from_utc = (timezone_stamp)/3600
return datetime.fromtimestamp(timestamp,tz=timezone.utc) + timedelta(hours=hour_offset_from_utc)
6 changes: 3 additions & 3 deletions src/weather.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import gi
import datetime
from datetime import datetime
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
from gi.repository import Gtk,Adw,Gio,GLib
Expand Down Expand Up @@ -130,11 +130,11 @@ def refresh_weather(self,widget,ignore=True):
t_arr = tm.split(":")
t_sec = float(t_arr[2])

if ignore and abs(datetime.datetime.now().second - t_sec) < 5:
if ignore and abs(datetime.now().second - t_sec) < 5:
self.toast_overlay.add_toast(create_toast(_("Refresh within 5 seconds is ignored!"),1))
return

date_time = datetime.datetime.now()
date_time = datetime.now()
updated_at = str(date_time)
settings.set_value("updated-at",GLib.Variant("s",updated_at))
upper_child = self.upper_row.get_first_child()
Expand Down
2 changes: 1 addition & 1 deletion src/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,4 @@ def change_unit(self,widget,value):
def save_api_key(self,widget,target):
settings.set_value("personal-api-key",GLib.Variant("s",target.get_text()))
settings.set_value("using-personal-api-key",GLib.Variant("b",True))
self._dialog.add_toast(create_toast(_("Saved Successfully")))
self.add_toast(create_toast(_("Saved Successfully"),1))

0 comments on commit b102a03

Please sign in to comment.