Skip to content

Handle timezone-aware datetime objects when writing SPDX. #768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/install_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
python -m pip install --upgrade ./dist/*.whl
python -m pip install pytest
python -m pip install pyshacl
python -m pip install tzdata
python -m pip install networkx
shell: bash
- name: Run tests
Expand Down
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