Skip to content

Commit 6c46722

Browse files
committed
Fix parsing datetime containing timezone information for python 3.6
Required method for parsing datetime 'fromisoformat' was added in python version 3.7, thus backport of that method was added to the requirements list. Also '%z' directive was updated in python 3.7 to support timezone information in the "+xx:xx" or "Z" format. Hence, changes were made to allow compatibility of these notations in python 3.6 and lower. https://docs.python.org/3/library/datetime.html
1 parent 1f9cce9 commit 6c46722

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2727
unknown type/member - Martin Miksik
2828
- Race condition in `test_types_repository_separation` - Martin Miksik
2929
- Import error while using python version prior to 3.7 - Martin Miksik
30+
- Parsing datetime containing timezone information for python 3.6 and lower - Martin Miksik
3031

3132
## [1.3.0]
3233

pyodata/v4/type_traits.py

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
""" Type traits for types specific to the ODATA V4"""
22

3+
import sys
34
import datetime
45

56
# In case you want to use geojson types. You have to install pip package 'geojson'
@@ -14,6 +15,10 @@
1415
from pyodata.exceptions import PyODataModelError, PyODataException
1516
from pyodata.model.type_traits import TypTraits
1617

18+
if sys.version_info < (3, 7):
19+
from backports.datetime_fromisoformat import MonkeyPatch
20+
MonkeyPatch.patch_fromisoformat()
21+
1722

1823
class EdmDoubleQuotesEncapsulatedTypTraits(TypTraits):
1924
"""Good for all types which are encapsulated in double quotes"""
@@ -218,6 +223,13 @@ def from_literal(self, value: str):
218223

219224
value = super().from_literal(value)
220225

226+
if sys.version_info < (3, 7):
227+
if value[len(value) - 3] == ':':
228+
value = value[:len(value) - 3] + value[-2:]
229+
230+
if value[len(value) - 1] == 'Z':
231+
value = value[:len(value) - 1] + "+0000"
232+
221233
try:
222234
value = datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S.%f%z')
223235
except ValueError:

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
lxml>=3.7.3
2+
backports-datetime-fromisoformat>=1.0

0 commit comments

Comments
 (0)