diff --git a/README.md b/README.md index de19d5f..6172e59 100644 --- a/README.md +++ b/README.md @@ -170,7 +170,7 @@ Doc-Write, see: https://github.com/boxine/bx_py_utils/blob/master/bx_py_utils/do #### bx_py_utils.test_utils.datetime -* [`parse_dt()`](https://github.com/boxine/bx_py_utils/blob/master/bx_py_utils/test_utils/datetime.py#L4-L15) - Helper for easy generate a `datetime` instance via string. +* [`parse_dt()`](https://github.com/boxine/bx_py_utils/blob/master/bx_py_utils/test_utils/datetime.py#L5-L25) - Helper for easy generate a `datetime` instance via string. #### bx_py_utils.test_utils.deny_requests diff --git a/bx_py_utils/__init__.py b/bx_py_utils/__init__.py index 9f7684a..bf07c37 100644 --- a/bx_py_utils/__init__.py +++ b/bx_py_utils/__init__.py @@ -1 +1 @@ -__version__ = '104' +__version__ = '105' diff --git a/bx_py_utils/test_utils/datetime.py b/bx_py_utils/test_utils/datetime.py index e1338ef..b3f8a41 100644 --- a/bx_py_utils/test_utils/datetime.py +++ b/bx_py_utils/test_utils/datetime.py @@ -1,4 +1,5 @@ import datetime +import sys def parse_dt(dtstr): @@ -9,7 +10,16 @@ def parse_dt(dtstr): True >>> parse_dt('2020-01-01T00:00:00+0000') datetime.datetime(2020, 1, 1, 0, 0, tzinfo=datetime.timezone.utc) + >>> parse_dt('2020-01-02T03:04:05.678901+00:00') + datetime.datetime(2020, 1, 2, 3, 4, 5, 678901, tzinfo=datetime.timezone.utc) """ if dtstr is None: return None - return datetime.datetime.strptime(dtstr, '%Y-%m-%dT%H:%M:%S%z') + + try: + return datetime.datetime.fromisoformat(dtstr) + except ValueError: + if sys.version_info[:2] == (3, 10): + # Python 3.10 supports only formats emitted by date.isoformat() or datetime.isoformat() :( + return datetime.datetime.strptime(dtstr, '%Y-%m-%dT%H:%M:%S%z') + raise