diff --git a/ckanext/fairdatapoint/profiles.py b/ckanext/fairdatapoint/profiles.py index 61f57db..f232c7a 100644 --- a/ckanext/fairdatapoint/profiles.py +++ b/ckanext/fairdatapoint/profiles.py @@ -3,7 +3,7 @@ # check for multiple-text fields in the schema # All changes are © Stichting Health-RI and are licensed under the AGPLv3 license -from datetime import datetime +from datetime import datetime, timezone import re import json import logging @@ -85,9 +85,11 @@ def convert_datetime_string(date_value: str) -> datetime: Converts datestrings (e.g. '2023-10-06T10:12:55.614000+00:00') to datetime class instance """ try: - date_value = dateparser.parse(date_value) + date_value = dateparser.parse(date_value, yearfirst=True) + if date_value.tzinfo is not None: + date_value = date_value.astimezone(timezone.utc) except ParserError: - log.error('A date field string value can not be parsed to a date') + log.error(f'A date field string value {date_value} can not be parsed to a date') return date_value diff --git a/ckanext/fairdatapoint/tests/test_profiles.py b/ckanext/fairdatapoint/tests/test_profiles.py index 96fecbb..c0b1931 100644 --- a/ckanext/fairdatapoint/tests/test_profiles.py +++ b/ckanext/fairdatapoint/tests/test_profiles.py @@ -18,7 +18,7 @@ import pytest from datetime import datetime -from dateutil.tz import tzutc, tzoffset +from dateutil.tz import tzutc from pathlib import Path from rdflib import Graph, URIRef from ckanext.fairdatapoint.profiles import validate_tags, convert_datetime_string @@ -91,8 +91,15 @@ def test_parse_dataset(): ("2023-10-06T10:12:55.614000+00:00", datetime(2023, 10, 6, 10, 12, 55, 614000, tzinfo=tzutc())), ("2024-02-15 11:16:37+03:00", - datetime(2024, 2, 15, 11, 16, 37, tzinfo=tzoffset(None, 10800))), + datetime(2024, 2, 15, 8, 16, 37, tzinfo=tzutc())), + ("2014-09-12T19:34:29Z", + datetime(2014, 9, 12, 19, 34, 29, tzinfo=tzutc())), + ("2007-04-05T12:30.512000-02:00", + datetime(2007, 4, 5, 14, 30, 30, tzinfo=tzutc())), + ("2007-04-05T12:30-02:00", + datetime(2007, 4, 5, 14, 30, tzinfo=tzutc())), ("November 9, 1999", datetime(1999, 11, 9, 0, 0, 0)), + ("25-06-2023", datetime(2023, 6, 25)), ("2006-09", datetime(2006, 9, datetime.today().day))]) def test_convert_datetime_string(input_timestring, expected_output): actual = convert_datetime_string(input_timestring)