Skip to content

Commit 66ecef7

Browse files
Use explicit strptime for old python
1 parent 6a3a963 commit 66ecef7

File tree

1 file changed

+49
-9
lines changed

1 file changed

+49
-9
lines changed

src/datapoint/Forecast.py

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime
2+
from platform import python_version
23

34
from datapoint.exceptions import APIException
45
from datapoint.weather_codes import WEATHER_CODES
@@ -109,9 +110,21 @@ def __init__(self, frequency, api_data):
109110
:type api_data: dict
110111
"""
111112
self.frequency = frequency
112-
self.data_date = datetime.datetime.fromisoformat(
113-
api_data["features"][0]["properties"]["modelRunDate"]
114-
) #: The date the provided forecast was generated.
113+
if python_version() < "3.11":
114+
# Need to parse format like 2024-02-17T15:00Z. This can only be
115+
# done with datetime.datetime.fromisoformat from python 3.11
116+
# onwards. Using this if statement to remember to remove the
117+
# explicit strptime in the future, and it annoyed me that
118+
# fromisoformat couldn't handle all iso-formatted datetimes.
119+
data_date = datetime.datetime.strptime(
120+
api_data["features"][0]["properties"]["modelRunDate"],
121+
"%Y-%m-%dT%H:%M%z",
122+
)
123+
else:
124+
data_date = datetime.datetime.fromisoformat(
125+
api_data["features"][0]["properties"]["modelRunDate"]
126+
)
127+
self.data_date = data_date #: The date the provided forecast was generated.
115128

116129
self.forecast_longitude = api_data["features"][0]["geometry"]["coordinates"][
117130
0
@@ -160,11 +173,28 @@ def _build_timesteps_from_daily(self, forecasts, parameters):
160173

161174
timesteps = []
162175
for forecast in forecasts:
163-
night_step = {"time": datetime.datetime.fromisoformat(forecast["time"])}
164-
day_step = {
165-
"time": datetime.datetime.fromisoformat(forecast["time"])
166-
+ datetime.timedelta(hours=12)
167-
}
176+
if python_version() < "3.11":
177+
# Need to parse format like 2024-02-17T15:00Z. This can only be
178+
# done with datetime.datetime.fromisoformat from python 3.11
179+
# onwards. Using this if statement to remember to remove the
180+
# explicit strptime in the future, and it annoyed me that
181+
# fromisoformat couldn't handle all iso-formatted datetimes.
182+
night_step = datetime.datetime.strptime(
183+
forecast["time"], "%Y-%m-%dT%H:%M%z"
184+
)
185+
day_step = {
186+
"time": datetime.datetime.strptime(
187+
forecast["time"], "%Y-%m-%dT%H:%M%z"
188+
)
189+
+ datetime.timedelta(hours=12)
190+
}
191+
192+
else:
193+
night_step = {"time": datetime.datetime.fromisoformat(forecast["time"])}
194+
day_step = {
195+
"time": datetime.datetime.fromisoformat(forecast["time"])
196+
+ datetime.timedelta(hours=12)
197+
}
168198

169199
for element, value in forecast.items():
170200
if element.startswith("midday"):
@@ -228,7 +258,17 @@ def _build_timestep(self, forecast, parameters):
228258
timestep = {}
229259
for element, value in forecast.items():
230260
if element == "time":
231-
timestep["time"] = datetime.datetime.fromisoformat(forecast["time"])
261+
if python_version() < "3.11":
262+
# Need to parse format like 2024-02-17T15:00Z. This can only be
263+
# done with datetime.datetime.fromisoformat from python 3.11
264+
# onwards. Using this if statement to remember to remove the
265+
# explicit strptime in the future.
266+
timestep = datetime.datetime.strptime(
267+
forecast["time"], "%Y-%m-%dT%H:%M%z"
268+
)
269+
else:
270+
timestep["time"] = datetime.datetime.fromisoformat(forecast["time"])
271+
232272
elif element == "significantWeatherCode":
233273
timestep[element] = {
234274
"value": WEATHER_CODES[str(value)],

0 commit comments

Comments
 (0)