Skip to content

Commit

Permalink
Make datetime parsing independent of locale
Browse files Browse the repository at this point in the history
  • Loading branch information
FL550 committed Oct 31, 2023
1 parent 3d2cdbe commit bbf1ea6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 23 deletions.
3 changes: 2 additions & 1 deletion requires.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
lxml
requests
Pillow
Pillow
arrow
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="simple_dwd_weatherforecast",
version="2.0.21",
version="2.0.22",
author="Max Fermor",
description="A simple tool to retrieve a weather forecast from DWD OpenData",
long_description=long_description,
Expand Down
29 changes: 8 additions & 21 deletions simple_dwd_weatherforecast/dwdforecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from enum import Enum
from lxml import etree
from datetime import datetime, timedelta, timezone
import time
import arrow
import math
import json
import csv
Expand Down Expand Up @@ -478,15 +478,9 @@ def get_timeframe_values(self, timestamp: datetime, timeframe: int):
def get_day_values(self, timestamp: datetime):
"timestamp has to be checked prior to be in timerange"
result = []
first_entry_date = datetime(
*(
time.strptime(next(iter(self.forecast_data)), "%Y-%m-%dT%H:%M:%S.%fZ")[
0:6
]
),
0,
timezone.utc,
)
first_entry_date = arrow.get(
next(iter(self.forecast_data)), "YYYY-MM-DDTHH:mm:ss.SSSZ"
).datetime
if timestamp.day != first_entry_date.day:
time_step = self.strip_to_day(timestamp)
for _ in range(24):
Expand Down Expand Up @@ -633,17 +627,10 @@ def parse_placemark(self, stream):
placemark.clear()

def parse_issue_time(self, tree):
issue_time_new = datetime(
*(
time.strptime(
tree.xpath("//dwd:IssueTime", namespaces=self.namespaces)[0].text,
"%Y-%m-%dT%H:%M:%S.%fZ",
)[0:6]
),
0,
timezone.utc,
)

issue_time_new = arrow.get(
tree.xpath("//dwd:IssueTime", namespaces=self.namespaces)[0].text,
"YYYY-MM-DDTHH:mm:ss.SSSZ",
).datetime
return issue_time_new

def parse_station_name(self, tree):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_parsekml.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime, timezone
import unittest

from simple_dwd_weatherforecast import dwdforecast
Expand All @@ -15,6 +16,7 @@ def test_parse_kml(self):
with open(self.FILE_NAME, "rb") as kml:
self.dwd_weather.parse_kml(kml)
self.assertEqual(self.dwd_weather.forecast_data, dummy_data.parsed_data)
self.assertEqual(self.dwd_weather.issue_time, datetime(2020, 11, 6, 3, 0, tzinfo=timezone.utc))


class KMLParseFullTestCase(unittest.TestCase):
Expand Down

0 comments on commit bbf1ea6

Please sign in to comment.