From f37c967d98ee1f92e7d163324df90199037f4c5d Mon Sep 17 00:00:00 2001
From: Robert Goldmann <deadlocker@gmx.de>
Date: Mon, 2 Nov 2020 20:51:32 +0100
Subject: [PATCH] Fixed #31 - weather icons and color not correct

---
 src/logic/Helpers.py                       | 12 ++---
 src/logic/tile/tiles/CurrentWeatherTile.py |  6 +--
 src/logic/tile/tiles/HourlyForecastTile.py |  6 +--
 test/logic/HelpersTest.py                  | 51 ++++++++++++++++++----
 4 files changed, 55 insertions(+), 20 deletions(-)

diff --git a/src/logic/Helpers.py b/src/logic/Helpers.py
index 2eee764..3d7a6e8 100644
--- a/src/logic/Helpers.py
+++ b/src/logic/Helpers.py
@@ -1,5 +1,7 @@
 from datetime import datetime
 
+import pytz
+
 from logic import Constants
 
 
@@ -50,12 +52,12 @@ def determine_color_for_weather_icon(iconId: int, isDayTime: bool):
         return Constants.WHITE.to_rgba()
 
 
-def is_dayTime(sunrise: int, sunset: int, currentTimestamp: int = None) -> bool:
+def is_dayTime(sunrise: datetime, sunset: datetime, currentTimestamp: datetime) -> bool:
     if not currentTimestamp:
-        currentTimestamp = int(datetime.now().timestamp())
+        currentTimestamp = datetime.now()
     return sunrise < currentTimestamp < sunset
 
 
-def timestamp_to_timezone(timestamp: int, timeZone: datetime.tzinfo):
-    timestamp = datetime.utcfromtimestamp(timestamp)
-    return timeZone.fromutc(timestamp)
+def timestamp_to_timezone(timestamp: int, timeZone: datetime.tzinfo) -> datetime:
+    timestamp = datetime.fromtimestamp(timestamp, pytz.timezone('UTC'))
+    return timestamp.astimezone(timeZone)
diff --git a/src/logic/tile/tiles/CurrentWeatherTile.py b/src/logic/tile/tiles/CurrentWeatherTile.py
index 3fd7910..1375d48 100644
--- a/src/logic/tile/tiles/CurrentWeatherTile.py
+++ b/src/logic/tile/tiles/CurrentWeatherTile.py
@@ -35,9 +35,9 @@ class CurrentWeatherTile(Tile):
         feelsLike = currentWeather['feels_like']
         windSpeed = currentWeather['wind_speed'] * 3.6
         icon = currentWeather['weather'][0]['id']
-        sunrise = Helpers.timestamp_to_timezone(currentWeather['sunrise'], timeZone).timestamp()
-        sunset = Helpers.timestamp_to_timezone(currentWeather['sunset'], timeZone).timestamp()
-        isDayTime = Helpers.is_dayTime(sunrise, sunset)
+        sunrise = Helpers.timestamp_to_timezone(currentWeather['sunrise'], timeZone)
+        sunset = Helpers.timestamp_to_timezone(currentWeather['sunset'], timeZone)
+        isDayTime = Helpers.is_dayTime(sunrise, sunset, datetime.now(tz=timeZone))
 
         return {
             'temperature': Helpers.round_to_decimals(currentTemperature, 1),
diff --git a/src/logic/tile/tiles/HourlyForecastTile.py b/src/logic/tile/tiles/HourlyForecastTile.py
index 92caa78..1d79fd8 100644
--- a/src/logic/tile/tiles/HourlyForecastTile.py
+++ b/src/logic/tile/tiles/HourlyForecastTile.py
@@ -31,8 +31,8 @@ class HourlyForecastTile(Tile):
         # cache key will be determined in service
         weatherData = weatherService.get_data('', fetchIntervalInSeconds, self._settings)
 
-        sunrise = Helpers.timestamp_to_timezone(weatherData['current']['sunrise'], timeZone).timestamp()
-        sunset = Helpers.timestamp_to_timezone(weatherData['current']['sunset'], timeZone).timestamp()
+        sunrise = Helpers.timestamp_to_timezone(weatherData['current']['sunrise'], timeZone)
+        sunset = Helpers.timestamp_to_timezone(weatherData['current']['sunset'], timeZone)
 
         hourData = []
         hourlyForecast = weatherData['hourly']
@@ -44,7 +44,7 @@ class HourlyForecastTile(Tile):
             rainProbability = round(entry['pop'] * 100, -1)  # -1 rounds to the next ten
             windSpeed = entry['wind_speed'] * 3.6
 
-            isDayTime = Helpers.is_dayTime(sunrise, sunset, currentTimestamp=timestamp.timestamp())
+            isDayTime = Helpers.is_dayTime(sunrise, sunset, currentTimestamp=timestamp)
 
             hourData.append({
                 'hour': timestamp.strftime('%H'),
diff --git a/test/logic/HelpersTest.py b/test/logic/HelpersTest.py
index 7b76e78..0849ab6 100644
--- a/test/logic/HelpersTest.py
+++ b/test/logic/HelpersTest.py
@@ -1,6 +1,8 @@
 import unittest
 from datetime import datetime
 
+import pytz
+
 from logic import Helpers
 
 
@@ -15,25 +17,56 @@ class HelpersTest(unittest.TestCase):
         self.assertEqual('0', Helpers.round_to_decimals(0.428, 0))
 
     def test_is_dayTime_true(self):
-        sunrise = datetime(year=2020, month=11, day=1, hour=8, minute=0, second=0).timestamp()
-        sunset = datetime(year=2020, month=11, day=1, hour=17, minute=0, second=0).timestamp()
+        sunrise = datetime(year=2020, month=11, day=1, hour=8, minute=0, second=0)
+        sunset = datetime(year=2020, month=11, day=1, hour=17, minute=0, second=0)
 
-        currentTimestamp = datetime(year=2020, month=11, day=1, hour=12, minute=0, second=0).timestamp()
+        currentTimestamp = datetime(year=2020, month=11, day=1, hour=12, minute=0, second=0)
 
         self.assertTrue(Helpers.is_dayTime(sunrise, sunset, currentTimestamp))
 
     def test_is_dayTime_false_before(self):
-        sunrise = datetime(year=2020, month=11, day=1, hour=8, minute=0, second=0).timestamp()
-        sunset = datetime(year=2020, month=11, day=1, hour=17, minute=0, second=0).timestamp()
+        sunrise = datetime(year=2020, month=11, day=1, hour=8, minute=0, second=0)
+        sunset = datetime(year=2020, month=11, day=1, hour=17, minute=0, second=0)
 
-        currentTimestamp = datetime(year=2020, month=11, day=1, hour=4, minute=0, second=0).timestamp()
+        currentTimestamp = datetime(year=2020, month=11, day=1, hour=4, minute=0, second=0)
 
         self.assertFalse(Helpers.is_dayTime(sunrise, sunset, currentTimestamp))
 
     def test_is_dayTime_false_after(self):
-        sunrise = datetime(year=2020, month=11, day=1, hour=8, minute=0, second=0).timestamp()
-        sunset = datetime(year=2020, month=11, day=1, hour=17, minute=0, second=0).timestamp()
+        sunrise = datetime(year=2020, month=11, day=1, hour=8, minute=0, second=0)
+        sunset = datetime(year=2020, month=11, day=1, hour=17, minute=0, second=0)
 
-        currentTimestamp = datetime(year=2020, month=11, day=1, hour=18, minute=0, second=0).timestamp()
+        currentTimestamp = datetime(year=2020, month=11, day=1, hour=18, minute=0, second=0)
 
         self.assertFalse(Helpers.is_dayTime(sunrise, sunset, currentTimestamp))
+
+    def test_is_dayTime_true_complex(self):
+        timeZone = pytz.timezone('Europe/Berlin')
+        sunrise = Helpers.timestamp_to_timezone(1604296782, timeZone)
+        sunset = Helpers.timestamp_to_timezone(1604331478, timeZone)
+
+        now = timeZone.localize(datetime(year=2020, month=11, day=2, hour=15, minute=0, second=0))
+
+        self.assertTrue(Helpers.is_dayTime(sunrise, sunset, now))
+
+    def test_timestamp_to_timezone_berlin(self):
+        timestamp = 1604331478
+        timeZone = pytz.timezone('Europe/Berlin')
+
+        expected = datetime(year=2020, month=11, day=2, hour=16, minute=37, second=58)
+        self.__compareDates(expected, Helpers.timestamp_to_timezone(timestamp, timeZone))
+
+    def test_timestamp_to_timezone_london(self):
+        timestamp = 1604331478
+        timeZone = pytz.timezone('Europe/London')
+        expected = datetime(year=2020, month=11, day=2, hour=15, minute=37, second=58, tzinfo=timeZone)
+        self.__compareDates(expected, Helpers.timestamp_to_timezone(timestamp, timeZone))
+
+    def __compareDates(self, a: datetime, b: datetime):
+        self.assertEqual(a.year, b.year)
+        self.assertEqual(a.month, b.month)
+        self.assertEqual(a.day, b.day)
+        self.assertEqual(a.hour, b.hour)
+        self.assertEqual(a.minute, b.minute)
+        self.assertEqual(a.second, b.second)
+        self.assertEqual(a.microsecond, b.microsecond)
-- 
GitLab