From b102a03ee9b05028c617be5170ff7290a04a3a67 Mon Sep 17 00:00:00 2001 From: Amit Chaudhary Date: Wed, 5 Jul 2023 19:49:09 +0530 Subject: [PATCH] convert sunset,sunrise time to local --- src/css.py | 7 +++++++ src/ui_current_w.py | 23 ++++++++++------------- src/utils.py | 11 ++++++++--- src/weather.py | 6 +++--- src/windows.py | 2 +- 5 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/css.py b/src/css.py index 5af590e..1ebff64 100644 --- a/src/css.py +++ b/src/css.py @@ -27,9 +27,15 @@ } +.f-xsm { + font-size: 13px; + } .f-sm { font-size: 14px; } +.f-msm { + font-size: 15px; + } .f-m { font-size: 16px; } @@ -159,3 +165,4 @@ """ # Thanks to https://github.com/SalaniLeo for this beautiful gradients collection + diff --git a/src/ui_current_w.py b/src/ui_current_w.py index 7df51c7..cd8b828 100644 --- a/src/ui_current_w.py +++ b/src/ui_current_w.py @@ -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 @@ -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 @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/src/utils.py b/src/utils.py index 2738909..d9a869a 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,4 +1,5 @@ import requests +from datetime import datetime, timedelta, timezone from gi.repository import Adw,Gio current_weather_data = None @@ -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) diff --git a/src/weather.py b/src/weather.py index 3f1a381..4010e38 100644 --- a/src/weather.py +++ b/src/weather.py @@ -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 @@ -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() diff --git a/src/windows.py b/src/windows.py index ca18e02..ff9c6eb 100644 --- a/src/windows.py +++ b/src/windows.py @@ -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))