Skip to content

Commit

Permalink
offset timezone to utc if timezone info present in source
Browse files Browse the repository at this point in the history
  • Loading branch information
anastasianayden committed May 13, 2024
1 parent 37ce28c commit 0199e71
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
8 changes: 5 additions & 3 deletions ckanext/fairdatapoint/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down
11 changes: 9 additions & 2 deletions ckanext/fairdatapoint/tests/test_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 0199e71

Please sign in to comment.