From 9243e40b541cc1b37d4a857939198599ce997b5d Mon Sep 17 00:00:00 2001 From: Bas van den Heuvel Date: Wed, 21 Aug 2019 22:23:01 +0200 Subject: [PATCH] Order sunrise and sunset according to current time Before sunrise, first sunrise is shown and then sunset. After sunrise, before sunset, first sunet is shown, and then tomorrow's sunrise. After sunset, first sunrise is shown and then sunset, both for tomorrow. --- bumblebee/modules/sun.py | 41 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/bumblebee/modules/sun.py b/bumblebee/modules/sun.py index a614120..e27d750 100644 --- a/bumblebee/modules/sun.py +++ b/bumblebee/modules/sun.py @@ -15,6 +15,12 @@ try: import requests except ImportError: pass +try: + from dateutil.tz import tzlocal +except ImportError: + pass + +import datetime import bumblebee.input import bumblebee.output @@ -43,11 +49,16 @@ class Module(bumblebee.engine.Module): def suntimes(self, _): if self._sunset and self._sunrise: + if self._isup: + return u"\u21A7{} \u21A5{}".format( + self._sunset.strftime('%H:%M'), + self._sunrise.strftime('%H:%M')) return u"\u21A5{} \u21A7{}".format(self._sunrise.strftime('%H:%M'), self._sunset.strftime('%H:%M')) return "?" def _calculate_times(self): + self._isup = False try: sun = Sun(self._lat, self._lon) except Exception: @@ -55,15 +66,41 @@ class Module(bumblebee.engine.Module): self._sunset = None return + order_matters = True + try: self._sunrise = sun.get_local_sunrise_time() except SunTimeException: - self._sunrise = 'no sunrise' + self._sunrise = "no sunrise" + order_matters = False try: self._sunset = sun.get_local_sunset_time() except SunTimeException: - self._sunset = 'no sunset' + self._sunset = "no sunset" + order_matters = False + + if not order_matters: + return + + now = datetime.datetime.now(tz=tzlocal()) + if now > self._sunset: + tomorrow = (now + datetime.timedelta(days=1)).date() + try: + self._sunrise = sun.get_local_sunrise_time(tomorrow) + self._sunset = sun.get_local_sunset_time(tomorrow) + except SunTimeException: + self._sunrise = "no sunrise" + self._sunset = "no sunset" + + elif now > self._sunrise: + tomorrow = (now + datetime.timedelta(days=1)).date() + try: + self._sunrise = sun.get_local_sunrise_time(tomorrow) + except SunTimeException: + self._sunrise = "no sunrise" + return + self._isup = True def update(self, widgets): if not self._lat or not self._lon: