Skip to content

Consistent datetime parsing across all py-versions #372

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 1 commit into from
Feb 24, 2025
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
5 changes: 4 additions & 1 deletion linkml_runtime/utils/metamodelcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,11 @@ def __new__(cls, value: Union[str, datetime.datetime, Literal]) -> str:
else:
if "T" in str(value):
value = isodate.parse_datetime(value)
else:
elif " " in value.strip():
value = isodate.parse_datetime("T".join(value.strip().split(' ', 1)))
else:
# As datetime.fromisoformat allows dates to be parsed as datetime we do the same.
value = isodate.parse_datetime(f"{value.strip()}T00:00:00")
return value.isoformat()
except (TypeError, ValueError) as e:
if is_strict():
Expand Down
2 changes: 2 additions & 0 deletions tests/test_utils/test_metamodelcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,10 @@ def test_datetime(self):
self.assertEqual('2019-07-06 17:22:39.007300', vstr) # Note that this has no 'T'
self.assertEqual('2019-07-06T17:22:39.007300', XSDDateTime(vstr))
self.assertEqual('2019-07-06T17:22:39+00:00', XSDDateTime("2019-07-06T17:22:39Z"))
self.assertEqual('2019-07-06T00:00:00', XSDDateTime("2019-07-06")) # Date as datetime
with self.assertRaises(ValueError):
XSDDateTime('Jan 12, 2019')

lax()
self.assertEqual('penguins', XSDDateTime('penguins'))
XSDDateTime(datetime.datetime.now())
Expand Down