Skip to content

Commit 195e244

Browse files
Jeff Licquiaarmintaenzertng
authored andcommitted
Handle timezone-aware datetime objects when writing SPDX.
Fixes #766. Signed-off-by: Jeff Licquia <jeff@licquia.org>
1 parent 248394c commit 195e244

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ dependencies = ["click", "pyyaml", "xmltodict", "rdflib", "beartype", "uritools"
2828
dynamic = ["version"]
2929

3030
[project.optional-dependencies]
31-
test = ["pytest", "pyshacl"]
31+
test = ["pytest", "pyshacl", "tzdata"]
3232
code_style = ["isort", "black", "flake8"]
3333
graph_generation = ["pygraphviz", "networkx"]
3434
development = ["black", "flake8", "isort", "networkx", "pytest"]

src/spdx_tools/spdx/datetime_conversions.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SPDX-FileCopyrightText: 2022 spdx contributors
22
#
33
# SPDX-License-Identifier: Apache-2.0
4-
from datetime import datetime
4+
from datetime import datetime, timezone
55

66

77
def datetime_from_str(date_str: str) -> datetime:
@@ -16,6 +16,10 @@ def datetime_to_iso_string(date: datetime) -> str:
1616
"""
1717
Return an ISO-8601 representation of a datetime object.
1818
"""
19+
if date.tzinfo is not None:
20+
date = date.astimezone(timezone.utc) # Convert aware datetimes to UTC
21+
date = date.replace(tzinfo=None) # Convert to naive datetime
22+
1923
if date.microsecond != 0:
2024
date = date.replace(microsecond=0) # SPDX does not support microseconds
2125

tests/spdx/test_datetime_conversions.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
# SPDX-FileCopyrightText: 2022 spdx contributors
22
#
33
# SPDX-License-Identifier: Apache-2.0
4-
from datetime import datetime
4+
from datetime import datetime, timezone
55

66
import pytest
77

88
from spdx_tools.spdx.datetime_conversions import datetime_from_str, datetime_to_iso_string
99

10+
# The following is required as long as we support Python 3.8.x or
11+
# older. Once Python 3.9 is the oldest version we support, we can
12+
# rely solely on the section which imports and uses zoneinfo.
13+
14+
try:
15+
# Python 3.9 and later
16+
from zoneinfo import ZoneInfo
17+
18+
tz_nyc = ZoneInfo("America/New_York")
19+
except ImportError:
20+
# Python 3.8 and earlier
21+
from datetime import timedelta
22+
23+
tz_nyc = timezone(timedelta(hours=-4))
24+
1025

1126
def test_datetime_to_iso_string():
1227
assert datetime_to_iso_string(datetime(2022, 12, 13, 1, 2, 3)) == "2022-12-13T01:02:03Z"
@@ -16,6 +31,16 @@ def test_datetime_to_iso_string_with_microseconds():
1631
assert datetime_to_iso_string(datetime(2022, 12, 13, 1, 2, 3, 666666)) == "2022-12-13T01:02:03Z"
1732

1833

34+
def test_utc_datetime_to_iso_string():
35+
dt = datetime(2023, 10, 4, 1, 2, 3, tzinfo=timezone.utc)
36+
assert datetime_to_iso_string(dt) == "2023-10-04T01:02:03Z"
37+
38+
39+
def test_local_datetime_to_iso_string():
40+
dt = datetime(2023, 10, 4, 1, 2, 3, tzinfo=tz_nyc)
41+
assert datetime_to_iso_string(dt) == "2023-10-04T05:02:03Z"
42+
43+
1944
def test_datetime_from_str():
2045
date_str = "2010-03-04T05:45:11Z"
2146

0 commit comments

Comments
 (0)