From 3abe726bbc152fadb826a0bebfc9e531ba50a3ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Fern=C3=A1ndez=20Rojas?= Date: Sun, 18 Feb 2024 20:26:52 +0100 Subject: [PATCH] Provide datetimes in UTC and Local timezones MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Álvaro Fernández Rojas --- aemet_opendata/__init__.py | 1 + aemet_opendata/const.py | 3 ++- aemet_opendata/exceptions.py | 1 + aemet_opendata/forecast.py | 27 +++++++++++++++------- aemet_opendata/interface.py | 4 ++-- aemet_opendata/station.py | 8 +++---- aemet_opendata/town.py | 31 +++++++++++++++++++------- examples/basic.py | 1 + examples/get-town-by-coords-precise.py | 1 + examples/get-town-by-coords.py | 1 + examples/get-town-by-id.py | 1 + examples/get-town-forecast-daily.py | 1 + examples/get-town-forecast-hourly.py | 1 + 13 files changed, 58 insertions(+), 23 deletions(-) diff --git a/aemet_opendata/__init__.py b/aemet_opendata/__init__.py index ded9b31..796b535 100644 --- a/aemet_opendata/__init__.py +++ b/aemet_opendata/__init__.py @@ -1,4 +1,5 @@ """AEMET OpenData API.""" + from .interface import AEMET __all__ = ["AEMET"] diff --git a/aemet_opendata/const.py b/aemet_opendata/const.py index 49df5fc..6b9ac4a 100644 --- a/aemet_opendata/const.py +++ b/aemet_opendata/const.py @@ -103,7 +103,8 @@ AOD_TEMP_FEELING: Final[str] = "temperature-feeling" AOD_TEMP_MAX: Final[str] = "temperature-max" AOD_TEMP_MIN: Final[str] = "temperature-min" -AOD_TIMESTAMP: Final[str] = "timestamp" +AOD_TIMESTAMP_LOCAL: Final[str] = "timestamp-local" +AOD_TIMESTAMP_UTC: Final[str] = "timestamp-utc" AOD_TIMEZONE: Final[str] = "timezone" AOD_TOWN: Final[str] = "town" AOD_UV_INDEX: Final[str] = "uv-index" diff --git a/aemet_opendata/exceptions.py b/aemet_opendata/exceptions.py index 0230cc1..70888e5 100644 --- a/aemet_opendata/exceptions.py +++ b/aemet_opendata/exceptions.py @@ -1,4 +1,5 @@ """AEMET OpenData API exceptions.""" + from __future__ import annotations diff --git a/aemet_opendata/forecast.py b/aemet_opendata/forecast.py index 0368056..3d59ca6 100644 --- a/aemet_opendata/forecast.py +++ b/aemet_opendata/forecast.py @@ -1,6 +1,6 @@ """AEMET OpenData Forecast.""" -from datetime import datetime +from datetime import datetime, timezone from typing import Any, Final from .const import ( @@ -53,7 +53,8 @@ AOD_TEMP, AOD_TEMP_MAX, AOD_TEMP_MIN, - AOD_TIMESTAMP, + AOD_TIMESTAMP_LOCAL, + AOD_TIMESTAMP_UTC, AOD_UV_INDEX, AOD_WIND_DIRECTION, AOD_WIND_SPEED, @@ -311,10 +312,14 @@ def get_temp_min(self) -> int: """Return Town daily forecast minimum temperature.""" return self.temp_min - def get_timestamp(self) -> str: - """Return Town daily forecast timestamp.""" + def get_timestamp_local(self) -> str: + """Return Town daily forecast local timestamp.""" return self._datetime.isoformat() + def get_timestamp_utc(self) -> str: + """Return Town daily forecast UTC timestamp.""" + return self._datetime.astimezone(timezone.utc).isoformat() + def get_uv_index(self) -> int | None: """Return Town daily forecast UV index.""" return self.uv_index @@ -338,7 +343,8 @@ def data(self) -> dict[str, Any]: AOD_PRECIPITATION_PROBABILITY: self.get_precipitation_prob(), AOD_TEMP_MAX: self.get_temp_max(), AOD_TEMP_MIN: self.get_temp_min(), - AOD_TIMESTAMP: self.get_timestamp(), + AOD_TIMESTAMP_LOCAL: self.get_timestamp_local(), + AOD_TIMESTAMP_UTC: self.get_timestamp_utc(), AOD_UV_INDEX: self.get_uv_index(), AOD_WIND_DIRECTION: self.get_wind_direction(), AOD_WIND_SPEED: self.get_wind_speed(), @@ -549,10 +555,14 @@ def get_temp(self) -> int | None: """Return Town hourly forecast temperature.""" return self.temp - def get_timestamp(self) -> str: - """Return Town hourly forecast timestamp.""" + def get_timestamp_local(self) -> str: + """Return Town hourly forecast local timestamp.""" return self._datetime.isoformat() + def get_timestamp_utc(self) -> str: + """Return Town hourly forecast UTC timestamp.""" + return self._datetime.astimezone(timezone.utc).isoformat() + def get_wind_direction(self) -> float | None: """Return Town hourly forecast wind direction.""" return self.wind_direction @@ -581,7 +591,8 @@ def data(self) -> dict[str, Any]: AOD_SUNRISE: self.get_sunrise(), AOD_SUNSET: self.get_sunset(), AOD_TEMP: self.get_temp(), - AOD_TIMESTAMP: self.get_timestamp(), + AOD_TIMESTAMP_LOCAL: self.get_timestamp_local(), + AOD_TIMESTAMP_UTC: self.get_timestamp_utc(), AOD_WIND_DIRECTION: self.get_wind_direction(), AOD_WIND_SPEED: self.get_wind_speed(), AOD_WIND_SPEED_MAX: self.get_wind_speed_max(), diff --git a/aemet_opendata/interface.py b/aemet_opendata/interface.py index 6ed5d4e..985fb3b 100644 --- a/aemet_opendata/interface.py +++ b/aemet_opendata/interface.py @@ -33,7 +33,7 @@ AOD_STATION, AOD_STORM_PROBABILITY, AOD_TEMP, - AOD_TIMESTAMP, + AOD_TIMESTAMP_UTC, AOD_TOWN, AOD_UV_INDEX, AOD_WEATHER, @@ -233,7 +233,7 @@ def data(self) -> dict[str, Any]: if weather is not None: data[AOD_WEATHER] = weather - data[AOD_TIMESTAMP] = get_current_datetime().isoformat() + data[AOD_TIMESTAMP_UTC] = get_current_datetime().isoformat() return data diff --git a/aemet_opendata/station.py b/aemet_opendata/station.py index 3f838fc..6c02b9c 100644 --- a/aemet_opendata/station.py +++ b/aemet_opendata/station.py @@ -36,7 +36,7 @@ AOD_TEMP, AOD_TEMP_MAX, AOD_TEMP_MIN, - AOD_TIMESTAMP, + AOD_TIMESTAMP_UTC, AOD_TIMEZONE, AOD_WIND_DIRECTION, AOD_WIND_SPEED, @@ -146,8 +146,8 @@ def get_temp_min(self) -> float | None: """Return Station minimum temperature.""" return self.temp_min - def get_timestamp(self) -> str: - """Return Station timestamp.""" + def get_timestamp_utc(self) -> str: + """Return Station UTC timestamp.""" return self._datetime.isoformat() def get_timezone(self) -> ZoneInfo: @@ -229,7 +229,7 @@ def data(self) -> dict[str, Any]: AOD_ID: self.get_id(), AOD_NAME: self.get_name(), AOD_OUTDATED: self.get_outdated(), - AOD_TIMESTAMP: self.get_timestamp(), + AOD_TIMESTAMP_UTC: self.get_timestamp_utc(), AOD_TIMEZONE: self.get_timezone(), } diff --git a/aemet_opendata/town.py b/aemet_opendata/town.py index cd89d93..5692c6c 100644 --- a/aemet_opendata/town.py +++ b/aemet_opendata/town.py @@ -1,6 +1,6 @@ """AEMET OpenData Town.""" -from datetime import datetime +from datetime import datetime, timezone import logging from typing import Any from zoneinfo import ZoneInfo @@ -37,7 +37,8 @@ AOD_SNOW_PROBABILITY, AOD_STORM_PROBABILITY, AOD_TEMP, - AOD_TIMESTAMP, + AOD_TIMESTAMP_LOCAL, + AOD_TIMESTAMP_UTC, AOD_TIMEZONE, AOD_UV_INDEX, AOD_WIND_DIRECTION, @@ -96,10 +97,14 @@ def get_current_forecast(self) -> DailyForecastValue | None: return leg_forecast return None - def get_timestamp(self) -> str: + def get_timestamp_local(self) -> str: """Return Town daily forecast timestamp.""" return self._datetime.isoformat() + def get_timestamp_utc(self) -> str: + """Return Town daily forecast timestamp.""" + return self._datetime.astimezone(timezone.utc).isoformat() + def get_timezone(self) -> ZoneInfo: """Return Town daily forecast timezone.""" return self.zoneinfo @@ -108,7 +113,8 @@ def data(self) -> dict[str, Any]: """Return Town daily forecast data.""" data: dict[str, Any] = { AOD_FORECAST: [], - AOD_TIMESTAMP: self.get_timestamp(), + AOD_TIMESTAMP_LOCAL: self.get_timestamp_local(), + AOD_TIMESTAMP_UTC: self.get_timestamp_utc(), AOD_TIMEZONE: self.get_timezone(), } @@ -189,6 +195,14 @@ def get_timestamp(self) -> str: """Return Town hourly forecast timestamp.""" return self._datetime.isoformat() + def get_timestamp_local(self) -> str: + """Return Town daily forecast timestamp.""" + return self._datetime.isoformat() + + def get_timestamp_utc(self) -> str: + """Return Town daily forecast timestamp.""" + return self._datetime.astimezone(timezone.utc).isoformat() + def get_timezone(self) -> ZoneInfo: """Return Town hourly forecast timezone.""" return self.zoneinfo @@ -197,7 +211,8 @@ def data(self) -> dict[str, Any]: """Return Town hourly forecast data.""" data: dict[str, Any] = { AOD_FORECAST: [], - AOD_TIMESTAMP: self.get_timestamp(), + AOD_TIMESTAMP_LOCAL: self.get_timestamp_local(), + AOD_TIMESTAMP_UTC: self.get_timestamp_utc(), AOD_TIMEZONE: self.get_timezone(), } @@ -223,9 +238,9 @@ def weather(self) -> dict[str, Any]: weather[AOD_CONDITION] = forecast.get_condition() weather[AOD_HUMIDITY] = forecast.get_humidity() weather[AOD_PRECIPITATION] = forecast.get_precipitation() - weather[ - AOD_PRECIPITATION_PROBABILITY - ] = forecast.get_precipitation_probability() + weather[AOD_PRECIPITATION_PROBABILITY] = ( + forecast.get_precipitation_probability() + ) weather[AOD_TEMP] = forecast.get_temp() weather[AOD_RAIN] = forecast.get_rain() weather[AOD_RAIN_PROBABILITY] = forecast.get_rain_probability() diff --git a/examples/basic.py b/examples/basic.py index a7a37fb..40115a9 100644 --- a/examples/basic.py +++ b/examples/basic.py @@ -1,4 +1,5 @@ """Basic AEMET OpenData client example.""" + import asyncio import timeit diff --git a/examples/get-town-by-coords-precise.py b/examples/get-town-by-coords-precise.py index 6f4486d..32ada35 100644 --- a/examples/get-town-by-coords-precise.py +++ b/examples/get-town-by-coords-precise.py @@ -1,4 +1,5 @@ """Get AEMET OpenData town data by coords example.""" + import asyncio import timeit diff --git a/examples/get-town-by-coords.py b/examples/get-town-by-coords.py index e14c24c..5fed067 100644 --- a/examples/get-town-by-coords.py +++ b/examples/get-town-by-coords.py @@ -1,4 +1,5 @@ """Get AEMET OpenData town data by coords example.""" + import asyncio import timeit diff --git a/examples/get-town-by-id.py b/examples/get-town-by-id.py index 5d72e7c..8ddad50 100644 --- a/examples/get-town-by-id.py +++ b/examples/get-town-by-id.py @@ -1,4 +1,5 @@ """Get AEMET OpenData town data by ID example.""" + import asyncio import timeit diff --git a/examples/get-town-forecast-daily.py b/examples/get-town-forecast-daily.py index e38943b..7e08e06 100644 --- a/examples/get-town-forecast-daily.py +++ b/examples/get-town-forecast-daily.py @@ -1,4 +1,5 @@ """Get AEMET OpenData town daily forecast example.""" + import asyncio import timeit diff --git a/examples/get-town-forecast-hourly.py b/examples/get-town-forecast-hourly.py index 816de74..f2dc68c 100644 --- a/examples/get-town-forecast-hourly.py +++ b/examples/get-town-forecast-hourly.py @@ -1,4 +1,5 @@ """Get AEMET OpenData town hourly forecast example.""" + import asyncio import timeit