|
1 | 1 | import datetime |
| 2 | +from platform import python_version |
2 | 3 |
|
3 | 4 | from datapoint.exceptions import APIException |
4 | 5 | from datapoint.weather_codes import WEATHER_CODES |
@@ -109,9 +110,21 @@ def __init__(self, frequency, api_data): |
109 | 110 | :type api_data: dict |
110 | 111 | """ |
111 | 112 | 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. |
115 | 128 |
|
116 | 129 | self.forecast_longitude = api_data["features"][0]["geometry"]["coordinates"][ |
117 | 130 | 0 |
@@ -160,11 +173,28 @@ def _build_timesteps_from_daily(self, forecasts, parameters): |
160 | 173 |
|
161 | 174 | timesteps = [] |
162 | 175 | 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 | + } |
168 | 198 |
|
169 | 199 | for element, value in forecast.items(): |
170 | 200 | if element.startswith("midday"): |
@@ -228,7 +258,17 @@ def _build_timestep(self, forecast, parameters): |
228 | 258 | timestep = {} |
229 | 259 | for element, value in forecast.items(): |
230 | 260 | 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 | + |
232 | 272 | elif element == "significantWeatherCode": |
233 | 273 | timestep[element] = { |
234 | 274 | "value": WEATHER_CODES[str(value)], |
|
0 commit comments