Skip to content

Commit 249ecf0

Browse files
committed
use standard lib datetime instead of third party library
Signed-off-by: Kunz, Immanuel <immanuel.kunz@aisec.fraunhofer.de>
1 parent 64aecfb commit 249ecf0

4 files changed

Lines changed: 13 additions & 8 deletions

File tree

requirements.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ packaging==21.0
33
pyparsing==2.4.7
44
semantic-version==2.8.5
55
semver==2.13.0
6-
isort==5.10.1
7-
python-dateutil==2.9.0.post0
6+
isort==5.10.1

setup.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ install_requires =
6363
packaging
6464
semantic-version
6565
semver
66-
python-dateutil
6766

6867

6968
[options.packages.find]

src/univers/datetime.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,31 @@
55

66
import re
77

8-
from dateutil.parser import isoparse
8+
from datetime import datetime, timezone
99

1010

1111
class DatetimeVersion:
1212
"""
1313
datetime version.
1414
15-
The timestamp must be RFC3339-compliant, i.e., a subset of ISO8601, where the date AND time are always specified. Therefore, we can use dateutil's ISO-parser but have to check for compliance with the RFC format first via a regex.
15+
The timestamp must be RFC3339-compliant, i.e., a subset of ISO8601, where the date AND time are always specified. Therefore, we cannot use an ISO-parser directly, but have to check for compliance with the RFC format via a regex.
1616
"""
1717

1818
VERSION_PATTERN = re.compile(
1919
r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$"
2020
)
2121

2222
def __init__(self, version):
23+
version = str(version).strip()
2324
if not self.is_valid(version):
2425
raise InvalidVersionError(version)
2526

26-
version = str(version).strip()
27+
# fromisoformat doesn't accept the "Z" suffix prior to 3.11, so we normalize it:
28+
if version.endswith("Z"):
29+
version = version[:-1] + "+00:00"
30+
2731
self.original = version
28-
self.parsed_stamp = isoparse(version)
32+
self.parsed_stamp = datetime.fromisoformat(version).astimezone(timezone.utc)
2933

3034
def __eq__(self, other):
3135
return self.parsed_stamp == other.parsed_stamp
@@ -44,7 +48,7 @@ def __ge__(self, other):
4448

4549
@classmethod
4650
def is_valid(cls, string):
47-
return cls.VERSION_PATTERN.match(string)
51+
return bool(cls.VERSION_PATTERN.fullmatch(string))
4852

4953

5054
class InvalidVersionError(ValueError):

tests/test_version_range.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@ def test_version_range_datetime():
383383
assert DatetimeVersion("2021-05-05T01:02:03.1234Z") >= DatetimeVersion(
384384
"2020-05-05T01:02:03.1234Z"
385385
)
386+
assert DatetimeVersion("2021-05-05T01:02:03.1234Z") > DatetimeVersion(
387+
"2020-05-05T01:02:03.1234+01:00"
388+
)
386389
assert DatetimeVersion("2000-01-01T01:02:03.1234Z") in DatetimeVersionRange.from_string(
387390
"vers:datetime/*"
388391
)

0 commit comments

Comments
 (0)