diff --git a/src/logic/Helpers.py b/src/logic/Helpers.py index 2eee764531f5c68c4f1ca1177c75c1e6312f0120..3d7a6e8dfdfe2865d8c504c47c36dacb0fe9de8b 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 3fd7910701b66d43c134965e8f20231eb7923028..1375d48f3b496fbb670f7cda9120e3183c553b07 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 92caa7885b5e15f096c9e31b005db011d220afcd..1d79fd8fd3cc2da6557b35778ee10943156840d3 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 7b76e78f7def541f4214e31c2927c249a9848b98..0849ab6f830b74f6a7b92457186897095a83dfb1 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)