Skip to content

Commit 8b39ec7

Browse files
Just use strptime for all python versions
1 parent f237fcb commit 8b39ec7

File tree

1 file changed

+23
-51
lines changed

1 file changed

+23
-51
lines changed

src/datapoint/Forecast.py

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

43
from datapoint.exceptions import APIException
54
from datapoint.weather_codes import WEATHER_CODES
@@ -110,21 +109,13 @@ def __init__(self, frequency, api_data):
110109
:type api_data: dict
111110
"""
112111
self.frequency = frequency
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.
112+
# Need to parse format like 2024-02-17T15:00Z. This can only be
113+
# done with datetime.datetime.fromisoformat from python 3.11
114+
# onwards.
115+
self.data_date = datetime.datetime.strptime(
116+
api_data["features"][0]["properties"]["modelRunDate"],
117+
"%Y-%m-%dT%H:%M%z",
118+
) #: The date the provided forecast was generated.
128119

129120
self.forecast_longitude = api_data["features"][0]["geometry"]["coordinates"][
130121
0
@@ -173,30 +164,16 @@ def _build_timesteps_from_daily(self, forecasts, parameters):
173164

174165
timesteps = []
175166
for forecast in forecasts:
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 = {
183-
"time": datetime.datetime.strptime(
184-
forecast["time"], "%Y-%m-%dT%H:%M%z"
185-
)
186-
}
187-
day_step = {
188-
"time": datetime.datetime.strptime(
189-
forecast["time"], "%Y-%m-%dT%H:%M%z"
190-
)
191-
+ datetime.timedelta(hours=12)
192-
}
193-
194-
else:
195-
night_step = {"time": datetime.datetime.fromisoformat(forecast["time"])}
196-
day_step = {
197-
"time": datetime.datetime.fromisoformat(forecast["time"])
198-
+ datetime.timedelta(hours=12)
199-
}
167+
# Need to parse format like 2024-02-17T15:00Z. This can only be
168+
# done with datetime.datetime.fromisoformat from python 3.11
169+
# onwards.
170+
night_step = {
171+
"time": datetime.datetime.strptime(forecast["time"], "%Y-%m-%dT%H:%M%z")
172+
}
173+
day_step = {
174+
"time": datetime.datetime.strptime(forecast["time"], "%Y-%m-%dT%H:%M%z")
175+
+ datetime.timedelta(hours=12)
176+
}
200177

201178
for element, value in forecast.items():
202179
if element.startswith("midday"):
@@ -260,17 +237,12 @@ def _build_timestep(self, forecast, parameters):
260237
timestep = {}
261238
for element, value in forecast.items():
262239
if element == "time":
263-
if python_version() < "3.11":
264-
# Need to parse format like 2024-02-17T15:00Z. This can only be
265-
# done with datetime.datetime.fromisoformat from python 3.11
266-
# onwards. Using this if statement to remember to remove the
267-
# explicit strptime in the future, and it annoyed me that
268-
# fromisoformat couldn't handle all iso-formatted datetimes.
269-
timestep["time"] = datetime.datetime.strptime(
270-
forecast["time"], "%Y-%m-%dT%H:%M%z"
271-
)
272-
else:
273-
timestep["time"] = datetime.datetime.fromisoformat(forecast["time"])
240+
# Need to parse format like 2024-02-17T15:00Z. This can only be
241+
# done with datetime.datetime.fromisoformat from python 3.11
242+
# onwards.
243+
timestep["time"] = datetime.datetime.strptime(
244+
forecast["time"], "%Y-%m-%dT%H:%M%z"
245+
)
274246

275247
elif element == "significantWeatherCode":
276248
timestep[element] = {

0 commit comments

Comments
 (0)