Skip to content

Commit

Permalink
Handle timezone-aware datetime objects when writing SPDX.
Browse files Browse the repository at this point in the history
Fixes #766.

Signed-off-by: Jeff Licquia <jeff@licquia.org>
  • Loading branch information
Jeff Licquia authored and armintaenzertng committed Oct 12, 2023
1 parent 248394c commit 195e244
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies = ["click", "pyyaml", "xmltodict", "rdflib", "beartype", "uritools"
dynamic = ["version"]

[project.optional-dependencies]
test = ["pytest", "pyshacl"]
test = ["pytest", "pyshacl", "tzdata"]
code_style = ["isort", "black", "flake8"]
graph_generation = ["pygraphviz", "networkx"]
development = ["black", "flake8", "isort", "networkx", "pytest"]
Expand Down
6 changes: 5 additions & 1 deletion src/spdx_tools/spdx/datetime_conversions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-FileCopyrightText: 2022 spdx contributors
#
# SPDX-License-Identifier: Apache-2.0
from datetime import datetime
from datetime import datetime, timezone


def datetime_from_str(date_str: str) -> datetime:
Expand All @@ -16,6 +16,10 @@ def datetime_to_iso_string(date: datetime) -> str:
"""
Return an ISO-8601 representation of a datetime object.
"""
if date.tzinfo is not None:
date = date.astimezone(timezone.utc) # Convert aware datetimes to UTC
date = date.replace(tzinfo=None) # Convert to naive datetime

if date.microsecond != 0:
date = date.replace(microsecond=0) # SPDX does not support microseconds

Expand Down
27 changes: 26 additions & 1 deletion tests/spdx/test_datetime_conversions.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
# SPDX-FileCopyrightText: 2022 spdx contributors
#
# SPDX-License-Identifier: Apache-2.0
from datetime import datetime
from datetime import datetime, timezone

import pytest

from spdx_tools.spdx.datetime_conversions import datetime_from_str, datetime_to_iso_string

# The following is required as long as we support Python 3.8.x or
# older. Once Python 3.9 is the oldest version we support, we can
# rely solely on the section which imports and uses zoneinfo.

try:
# Python 3.9 and later
from zoneinfo import ZoneInfo

tz_nyc = ZoneInfo("America/New_York")
except ImportError:
# Python 3.8 and earlier
from datetime import timedelta

tz_nyc = timezone(timedelta(hours=-4))


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


def test_utc_datetime_to_iso_string():
dt = datetime(2023, 10, 4, 1, 2, 3, tzinfo=timezone.utc)
assert datetime_to_iso_string(dt) == "2023-10-04T01:02:03Z"


def test_local_datetime_to_iso_string():
dt = datetime(2023, 10, 4, 1, 2, 3, tzinfo=tz_nyc)
assert datetime_to_iso_string(dt) == "2023-10-04T05:02:03Z"


def test_datetime_from_str():
date_str = "2010-03-04T05:45:11Z"

Expand Down

0 comments on commit 195e244

Please sign in to comment.