Skip to content

Commit

Permalink
Update date parsing (partially use isodate pkg)
Browse files Browse the repository at this point in the history
isodate is already installed since it is a rdflib dependency
  • Loading branch information
dalito committed Jan 9, 2025
1 parent df6ff94 commit c03b4a3
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions linkml_runtime/utils/metamodelcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Union, Optional, Tuple
from urllib.parse import urlparse

import isodate
from rdflib import Literal, BNode, URIRef
from rdflib.namespace import is_ncname
from rdflib.term import Identifier as rdflib_Identifier
Expand Down Expand Up @@ -230,13 +231,10 @@ def __new__(cls, value: Union[str, datetime.time, datetime.datetime, Literal]) -
if not isinstance(value, datetime.time):
value = datetime.time.fromisoformat(value)
return datetime.time.fromisoformat(str(value)).isoformat()
except TypeError as e:
pass
except ValueError as e:
pass
if not is_strict():
return str(value)
raise e
except (TypeError, ValueError) as e:
if is_strict():
raise e
return str(value)

@classmethod
def is_valid(cls, value: Union[str, datetime.time, datetime.datetime, Literal]) -> bool:
Expand All @@ -260,15 +258,13 @@ def __new__(cls, value: Union[str, datetime.date, Literal]) -> str:
value = value.value
try:
if not isinstance(value, datetime.date):
value = datetime.date.fromisoformat(str(value))
# value = datetime.date.fromisoformat(str(value))
value = isodate.parse_date(value)
return value.isoformat()
except TypeError as e:
pass
except ValueError as e:
pass
if not is_strict():
return str(value)
raise e
except (TypeError, ValueError) as e:
if is_strict():
raise e
return str(value)

@classmethod
def is_valid(cls, value: Union[str, datetime.date, Literal]) -> bool:
Expand All @@ -279,7 +275,8 @@ def is_valid(cls, value: Union[str, datetime.date, Literal]) -> bool:
if not re.match(r'^\d{4}-\d{2}-\d{2}$', value):
return False
try:
datetime.date.fromisoformat(str(value))
# datetime.date.fromisoformat(str(value))
value = isodate.parse_date(value)
except ValueError:
return False
return True
Expand All @@ -294,15 +291,16 @@ def __new__(cls, value: Union[str, datetime.datetime, Literal]) -> str:
value = value.value
try:
if not isinstance(value, datetime.datetime):
value = datetime.datetime.fromisoformat(value) # Note that this handles non 'T' format as well
# value = datetime.datetime.fromisoformat(value) # Note that this handles non 'T' format as well
if "T" in str(value):
value = isodate.parse_datetime(value)
else:
value = isodate.parse_datetime("T".join(value.strip().split(' ', 1)))
return value.isoformat()
except TypeError as e:
pass
except ValueError as e:
pass
if not is_strict():
return str(value)
raise e
except (TypeError, ValueError) as e:
if is_strict():
raise e
return str(value)

@classmethod
def is_valid(cls, value: Union[str, datetime.datetime, Literal]) -> bool:
Expand All @@ -311,8 +309,14 @@ def is_valid(cls, value: Union[str, datetime.datetime, Literal]) -> bool:
if isinstance(value, datetime.datetime):
value = value.isoformat()
try:
datetime.datetime.fromisoformat(value)
except ValueError:
# datetime.datetime.fromisoformat(value)
if "T" in str(value):
isodate.parse_datetime(value)
elif " " in value.strip():
isodate.parse_datetime("T".join(value.strip().split(' ', 1)))
else:
datetime.datetime.fromisoformat(value)
except (ValueError, TypeError):
return False
return True

Expand Down

0 comments on commit c03b4a3

Please sign in to comment.