Skip to content
Open
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
11 changes: 9 additions & 2 deletions src/spdx_tools/spdx/datetime_conversions.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
# SPDX-FileCopyrightText: 2022 spdx contributors
#
# SPDX-License-Identifier: Apache-2.0
import re
from datetime import datetime, timezone


def datetime_from_str(date_str: str) -> datetime:
if not isinstance(date_str, str):
raise TypeError(f"Could not convert str to datetime, invalid type: {type(date_str).__name__}")

date = datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%SZ") # raises ValueError if format does not match
return date
if "." not in date_str:
return datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%SZ") # raises ValueError if format does not match

# Based on the https://www.w3.org/TR/xmlschema11-2/#dateTimeStamp
# The secondFrag allows fractional second notation as well.
# normalize to micro seconds so that we can use %f with strptime
date_str = re.sub(r"\.(\d{1,6})\d*Z$", r".\1Z", date_str)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
date_str = re.sub(r"\.(\d{1,6})\d*Z$", r".\1Z", date_str)
date_str = re.sub(r"\.(\d{1,6})\d*Z$", r".\1Z", date_str)
warnings.warn(
"Invalid date format. Expected YYYY-MM-DDThh:mm:ssZ "
"Sub-second fractions have been discarded",
category=UserWarning,
stacklevel=2
)

Line 18 is fine. It increases robustness, as a tool.

But it should not just go silently. It should raise a warning.
Users should be notified that this is wrong.

return datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%S.%fZ") # raises ValueError if format does not match
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%S.%fZ") # raises ValueError if format does not match
return datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%SZ") # raises ValueError if format does not match

Line 19 is not fine. It returns a data format not supported by the spec.

Fractions should be dropped.



def datetime_to_iso_string(date: datetime) -> str:
Expand Down
16 changes: 14 additions & 2 deletions tests/spdx/test_datetime_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,22 @@ def test_local_datetime_to_iso_string():

def test_datetime_from_str():
date_str = "2010-03-04T05:45:11Z"
assert datetime_from_str(date_str) == datetime(2010, 3, 4, 5, 45, 11)

date = datetime_from_str(date_str)
date_str = "2010-03-04T05:45:11.0Z"
assert datetime_from_str(date_str) == datetime(2010, 3, 4, 5, 45, 11)

assert date == datetime(2010, 3, 4, 5, 45, 11)
# implicit notation
date_str = "2010-03-04T05:45:11.1Z"
assert datetime_from_str(date_str) == datetime(2010, 3, 4, 5, 45, 11, 100000)

# explicity notation
date_str = "2010-03-04T05:45:11.123456Z"
assert datetime_from_str(date_str) == datetime(2010, 3, 4, 5, 45, 11, 123456)

# truncation of nano seconds
date_str = "2010-03-04T05:45:11.1234567Z"
assert datetime_from_str(date_str) == datetime(2010, 3, 4, 5, 45, 11, 123456)


@pytest.mark.parametrize(
Expand Down
5 changes: 1 addition & 4 deletions tests/spdx3/writer/tag_value/test_write_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ def test_render_creation_info():
output_str = io.StringIO()
write_spdx_document(spdx_document, text_output=output_str)

assert (
output_str.getvalue()
== """\
assert output_str.getvalue() == """\
## SPDX Document
SPDXID: SPDXRef-FOO
name: BAR
Expand All @@ -42,4 +40,3 @@ def test_render_creation_info():
data license: CC0-1.0
elements:
""" # noqa: W291 # elements: are printed with a space
)